src/share/classes/com/sun/tools/classfile/ReferenceFinder.java

Wed, 10 Jun 2015 14:22:04 -0700

author
mfang
date
Wed, 10 Jun 2015 14:22:04 -0700
changeset 2815
d4051d4f5daf
parent 0
959103a6100f
permissions
-rw-r--r--

8083601: jdk8u60 l10n resource file translation update 2
Reviewed-by: ksrini, yhuang

     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.classfile;
    28 import java.util.ArrayList;
    29 import java.util.HashSet;
    30 import java.util.List;
    31 import java.util.Objects;
    32 import java.util.Set;
    33 import com.sun.tools.classfile.Instruction.TypeKind;
    34 import static com.sun.tools.classfile.ConstantPool.*;
    36 /**
    37  * A utility class to find where in a ClassFile references
    38  * a {@link CONSTANT_Methodref_info method},
    39  * a {@link CONSTANT_InterfaceMethodref_info interface method,
    40  * or a {@link CONSTANT_Fieldref_info field}.
    41  */
    42 public final class ReferenceFinder {
    43     /**
    44      * Filter for ReferenceFinder of what constant pool entries for reference lookup.
    45      */
    46     public interface Filter {
    47         /**
    48          * Decides if the given CPRefInfo entry should be accepted or filtered.
    49          *
    50          * @param cpool  ConstantPool of the ClassFile being parsed
    51          * @param cpref  constant pool entry representing a reference to
    52          *               a fields method, and interface method.
    53          * @return {@code true} if accepted; otherwise {@code false}
    54          */
    55         boolean accept(ConstantPool cpool, CPRefInfo cpref);
    56     }
    58     /**
    59      * Visitor of individual method of a ClassFile that references the
    60      * accepted field, method, or interface method references.
    61      */
    62     public interface Visitor {
    63         /**
    64          * Invoked for a method containing one or more accepted CPRefInfo entries
    65          *
    66          * @param cf      ClassFile
    67          * @param method  Method that does the references the accepted references
    68          * @param refs    Accepted constant pool method/field reference
    69          */
    70         void visit(ClassFile cf, Method method, List<CPRefInfo> refConstantPool);
    71     }
    73     private final Filter filter;
    74     private final Visitor visitor;
    76     /**
    77      * Constructor.
    78      */
    79     public ReferenceFinder(Filter filter, Visitor visitor) {
    80         this.filter = Objects.requireNonNull(filter);
    81         this.visitor = Objects.requireNonNull(visitor);
    82     }
    84     /**
    85      * Parses a given ClassFile and invoke the visitor if there is any reference
    86      * to the constant pool entries referencing field, method, or
    87      * interface method that are accepted. This method will return
    88      * {@code true} if there is one or more accepted constant pool entries
    89      * to lookup; otherwise, it will return {@code false}.
    90      *
    91      * @param  cf  ClassFile
    92      * @return {@code true} if the given class file is processed to lookup
    93      *         references
    94      * @throws ConstantPoolException if an error of the constant pool
    95      */
    96     public boolean parse(ClassFile cf) throws ConstantPoolException {
    97         List<Integer> cprefs = new ArrayList<Integer>();
    98         int index = 1;
    99         for (ConstantPool.CPInfo cpInfo : cf.constant_pool.entries()) {
   100             if (cpInfo.accept(cpVisitor, cf.constant_pool)) {
   101                 cprefs.add(index);
   102             }
   103             index += cpInfo.size();
   104         }
   106         if (cprefs.isEmpty()) {
   107             return false;
   108         }
   110         for (Method m : cf.methods) {
   111             Set<Integer> ids = new HashSet<Integer>();
   112             Code_attribute c_attr = (Code_attribute) m.attributes.get(Attribute.Code);
   113             if (c_attr != null) {
   114                 for (Instruction instr : c_attr.getInstructions()) {
   115                     int idx = instr.accept(codeVisitor, cprefs);
   116                     if (idx > 0) {
   117                         ids.add(idx);
   118                     }
   119                 }
   120             }
   121             if (ids.size() > 0) {
   122                 List<CPRefInfo> refInfos = new ArrayList<CPRefInfo>(ids.size());
   123                 for (int id : ids) {
   124                     refInfos.add(CPRefInfo.class.cast(cf.constant_pool.get(id)));
   125                 }
   126                 visitor.visit(cf, m, refInfos);
   127             }
   128         }
   129         return true;
   130     }
   132     private ConstantPool.Visitor<Boolean,ConstantPool> cpVisitor =
   133             new ConstantPool.Visitor<Boolean,ConstantPool>()
   134     {
   135         public Boolean visitClass(CONSTANT_Class_info info, ConstantPool cpool) {
   136             return false;
   137         }
   139         public Boolean visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, ConstantPool cpool) {
   140             return filter.accept(cpool, info);
   141         }
   143         public Boolean visitMethodref(CONSTANT_Methodref_info info, ConstantPool cpool) {
   144             return filter.accept(cpool, info);
   145         }
   147         public Boolean visitFieldref(CONSTANT_Fieldref_info info, ConstantPool cpool) {
   148             return filter.accept(cpool, info);
   149         }
   151         public Boolean visitDouble(CONSTANT_Double_info info, ConstantPool cpool) {
   152             return false;
   153         }
   155         public Boolean visitFloat(CONSTANT_Float_info info, ConstantPool cpool) {
   156             return false;
   157         }
   159         public Boolean visitInteger(CONSTANT_Integer_info info, ConstantPool cpool) {
   160             return false;
   161         }
   163         public Boolean visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, ConstantPool cpool) {
   164             return false;
   165         }
   167         public Boolean visitLong(CONSTANT_Long_info info, ConstantPool cpool) {
   168             return false;
   169         }
   171         public Boolean visitNameAndType(CONSTANT_NameAndType_info info, ConstantPool cpool) {
   172             return false;
   173         }
   175         public Boolean visitMethodHandle(CONSTANT_MethodHandle_info info, ConstantPool cpool) {
   176             return false;
   177         }
   179         public Boolean visitMethodType(CONSTANT_MethodType_info info, ConstantPool cpool) {
   180             return false;
   181         }
   183         public Boolean visitString(CONSTANT_String_info info, ConstantPool cpool) {
   184             return false;
   185         }
   187         public Boolean visitUtf8(CONSTANT_Utf8_info info, ConstantPool cpool) {
   188             return false;
   189         }
   190     };
   192     private Instruction.KindVisitor<Integer, List<Integer>> codeVisitor =
   193             new Instruction.KindVisitor<Integer, List<Integer>>()
   194     {
   195         public Integer visitNoOperands(Instruction instr, List<Integer> p) {
   196             return 0;
   197         }
   199         public Integer visitArrayType(Instruction instr, TypeKind kind, List<Integer> p) {
   200             return 0;
   201         }
   203         public Integer visitBranch(Instruction instr, int offset, List<Integer> p) {
   204             return 0;
   205         }
   207         public Integer visitConstantPoolRef(Instruction instr, int index, List<Integer> p) {
   208             return p.contains(index) ? index : 0;
   209         }
   211         public Integer visitConstantPoolRefAndValue(Instruction instr, int index, int value, List<Integer> p) {
   212             return p.contains(index) ? index : 0;
   213         }
   215         public Integer visitLocal(Instruction instr, int index, List<Integer> p) {
   216             return 0;
   217         }
   219         public Integer visitLocalAndValue(Instruction instr, int index, int value, List<Integer> p) {
   220             return 0;
   221         }
   223         public Integer visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, List<Integer> p) {
   224             return 0;
   225         }
   227         public Integer visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, List<Integer> p) {
   228             return 0;
   229         }
   231         public Integer visitValue(Instruction instr, int value, List<Integer> p) {
   232             return 0;
   233         }
   235         public Integer visitUnknown(Instruction instr, List<Integer> p) {
   236             return 0;
   237         }
   238     };
   239 }

mercurial