jdk/test/sun/reflect/CallerSensitive/MethodFinder.java
changeset 17553 4ea383c26563
parent 17552 241d13ccb08b
parent 17289 4dec41b3c5e3
child 17554 eca763ef7c5e
equal deleted inserted replaced
17552:241d13ccb08b 17553:4ea383c26563
     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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.util.*;
       
    25 import com.sun.tools.classfile.*;
       
    26 import static com.sun.tools.classfile.ConstantPool.*;
       
    27 import com.sun.tools.classfile.Instruction.TypeKind;
       
    28 
       
    29 /**
       
    30  * MethodFinder utility class to find references to the given methods.
       
    31  */
       
    32 public abstract class MethodFinder {
       
    33     final List<String> methods;
       
    34     public MethodFinder(String... methods) {
       
    35         this.methods = Arrays.asList(methods);
       
    36     }
       
    37 
       
    38     /**
       
    39      * A callback method will be invoked when a method referencing
       
    40      * any of the lookup methods.
       
    41      *
       
    42      * @param cf ClassFile
       
    43      * @param m Method
       
    44      * @param refs Set of constant pool indices that reference the methods
       
    45      *             matching the given lookup method names
       
    46      */
       
    47     public abstract void referenceFound(ClassFile cf, Method m, Set<Integer> refs)
       
    48             throws ConstantPoolException;
       
    49 
       
    50     public String parse(ClassFile cf) throws ConstantPoolException {
       
    51         List<Integer> cprefs = new ArrayList<Integer>();
       
    52         int index = 1;
       
    53         for (ConstantPool.CPInfo cpInfo : cf.constant_pool.entries()) {
       
    54             if (cpInfo.accept(cpVisitor, null)) {
       
    55                 cprefs.add(index);
       
    56             }
       
    57             index += cpInfo.size();
       
    58         }
       
    59 
       
    60         if (!cprefs.isEmpty()) {
       
    61             for (Method m : cf.methods) {
       
    62                 Set<Integer> refs = new HashSet<Integer>();
       
    63                 Code_attribute c_attr = (Code_attribute) m.attributes.get(Attribute.Code);
       
    64                 if (c_attr != null) {
       
    65                     for (Instruction instr : c_attr.getInstructions()) {
       
    66                         int idx = instr.accept(codeVisitor, cprefs);
       
    67                         if (idx > 0) {
       
    68                             refs.add(idx);
       
    69                         }
       
    70                     }
       
    71                 }
       
    72                 if (refs.size() > 0) {
       
    73                     referenceFound(cf, m, refs);
       
    74                 }
       
    75             }
       
    76         }
       
    77         return cprefs.isEmpty() ? "" : cf.getName();
       
    78     }
       
    79 
       
    80     private ConstantPool.Visitor<Boolean,Void> cpVisitor =
       
    81             new ConstantPool.Visitor<Boolean,Void>()
       
    82     {
       
    83         private boolean matches(CPRefInfo info) {
       
    84             try {
       
    85                 CONSTANT_NameAndType_info nat = info.getNameAndTypeInfo();
       
    86                 return matches(info.getClassName(), nat.getName(), nat.getType());
       
    87             } catch (ConstantPoolException ex) {
       
    88                 return false;
       
    89             }
       
    90         }
       
    91 
       
    92         private boolean matches(String cn, String name, String type) {
       
    93             return methods.contains(cn + "." + name);
       
    94         }
       
    95 
       
    96         public Boolean visitClass(CONSTANT_Class_info info, Void p) {
       
    97             return false;
       
    98         }
       
    99 
       
   100         public Boolean visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
       
   101             return matches(info);
       
   102         }
       
   103 
       
   104         public Boolean visitMethodref(CONSTANT_Methodref_info info, Void p) {
       
   105             return matches(info);
       
   106         }
       
   107 
       
   108         public Boolean visitDouble(CONSTANT_Double_info info, Void p) {
       
   109             return false;
       
   110         }
       
   111 
       
   112         public Boolean visitFieldref(CONSTANT_Fieldref_info info, Void p) {
       
   113             return false;
       
   114         }
       
   115 
       
   116         public Boolean visitFloat(CONSTANT_Float_info info, Void p) {
       
   117             return false;
       
   118         }
       
   119 
       
   120         public Boolean visitInteger(CONSTANT_Integer_info info, Void p) {
       
   121             return false;
       
   122         }
       
   123 
       
   124         public Boolean visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, Void p) {
       
   125             return false;
       
   126         }
       
   127 
       
   128         public Boolean visitLong(CONSTANT_Long_info info, Void p) {
       
   129             return false;
       
   130         }
       
   131 
       
   132         public Boolean visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
       
   133             return false;
       
   134         }
       
   135 
       
   136         public Boolean visitMethodHandle(CONSTANT_MethodHandle_info info, Void p) {
       
   137             return false;
       
   138         }
       
   139 
       
   140         public Boolean visitMethodType(CONSTANT_MethodType_info info, Void p) {
       
   141             return false;
       
   142         }
       
   143 
       
   144         public Boolean visitString(CONSTANT_String_info info, Void p) {
       
   145             return false;
       
   146         }
       
   147 
       
   148         public Boolean visitUtf8(CONSTANT_Utf8_info info, Void p) {
       
   149             return false;
       
   150         }
       
   151     };
       
   152 
       
   153     private Instruction.KindVisitor<Integer, List<Integer>> codeVisitor =
       
   154             new Instruction.KindVisitor<Integer, List<Integer>>()
       
   155     {
       
   156         public Integer visitNoOperands(Instruction instr, List<Integer> p) {
       
   157             return 0;
       
   158         }
       
   159 
       
   160         public Integer visitArrayType(Instruction instr, TypeKind kind, List<Integer> p) {
       
   161             return 0;
       
   162         }
       
   163 
       
   164         public Integer visitBranch(Instruction instr, int offset, List<Integer> p) {
       
   165             return 0;
       
   166         }
       
   167 
       
   168         public Integer visitConstantPoolRef(Instruction instr, int index, List<Integer> p) {
       
   169             return p.contains(index) ? index : 0;
       
   170         }
       
   171 
       
   172         public Integer visitConstantPoolRefAndValue(Instruction instr, int index, int value, List<Integer> p) {
       
   173             return p.contains(index) ? index : 0;
       
   174         }
       
   175 
       
   176         public Integer visitLocal(Instruction instr, int index, List<Integer> p) {
       
   177             return 0;
       
   178         }
       
   179 
       
   180         public Integer visitLocalAndValue(Instruction instr, int index, int value, List<Integer> p) {
       
   181             return 0;
       
   182         }
       
   183 
       
   184         public Integer visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, List<Integer> p) {
       
   185             return 0;
       
   186         }
       
   187 
       
   188         public Integer visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, List<Integer> p) {
       
   189             return 0;
       
   190         }
       
   191 
       
   192         public Integer visitValue(Instruction instr, int value, List<Integer> p) {
       
   193             return 0;
       
   194         }
       
   195 
       
   196         public Integer visitUnknown(Instruction instr, List<Integer> p) {
       
   197             return 0;
       
   198         }
       
   199     };
       
   200 }
       
   201