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  */
  25 
  26 package com.sun.tools.classfile;
  27 
  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.*;
  35 
  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     }
  57 
  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     }
  72 
  73     private final Filter filter;
  74     private final Visitor visitor;
  75 
  76     /**
  77      * Constructor.
  78      */
  79     public ReferenceFinder(Filter filter, Visitor visitor) {
  80         this.filter = Objects.requireNonNull(filter);
  81         this.visitor = Objects.requireNonNull(visitor);
  82     }
  83 
  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<>();
  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         }
 105 
 106         if (cprefs.isEmpty()) {
 107             return false;
 108         }
 109 
 110         for (Method m : cf.methods) {
 111             Set<Integer> ids = new HashSet<>();
 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<>(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     }
 131 
 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         }
 138 
 139         public Boolean visitFieldref(CONSTANT_Fieldref_info info, ConstantPool cpool) {
 140             return filter.accept(cpool, info);
 141         }
 142 
 143         public Boolean visitDouble(CONSTANT_Double_info info, ConstantPool cpool) {
 144             return false;
 145         }
 146 
 147         public Boolean visitFloat(CONSTANT_Float_info info, ConstantPool cpool) {
 148             return false;
 149         }
 150 
 151         public Boolean visitInteger(CONSTANT_Integer_info info, ConstantPool cpool) {
 152             return false;
 153         }
 154 
 155         public Boolean visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, ConstantPool cpool) {
 156             return filter.accept(cpool, info);
 157         }
 158 
 159         public Boolean visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, ConstantPool cpool) {
 160             return false;
 161         }
 162 
 163         public Boolean visitLong(CONSTANT_Long_info info, ConstantPool cpool) {
 164             return false;
 165         }
 166 
 167         public Boolean visitMethodHandle(CONSTANT_MethodHandle_info info, ConstantPool cpool) {
 168             return false;
 169         }
 170 
 171         public Boolean visitMethodref(CONSTANT_Methodref_info info, ConstantPool cpool) {
 172             return filter.accept(cpool, info);
 173         }
 174 
 175         public Boolean visitMethodType(CONSTANT_MethodType_info info, ConstantPool cpool) {
 176             return false;
 177         }
 178 
 179         public Boolean visitModule(CONSTANT_Module_info info, ConstantPool cpool) {
 180             return false;
 181         }
 182 
 183         public Boolean visitNameAndType(CONSTANT_NameAndType_info info, ConstantPool cpool) {
 184             return false;
 185         }
 186 
 187         public Boolean visitPackage(CONSTANT_Package_info info, ConstantPool cpool) {
 188             return false;
 189         }
 190 
 191         public Boolean visitString(CONSTANT_String_info info, ConstantPool cpool) {
 192             return false;
 193         }
 194 
 195         public Boolean visitUtf8(CONSTANT_Utf8_info info, ConstantPool cpool) {
 196             return false;
 197         }
 198     };
 199 
 200     private Instruction.KindVisitor<Integer, List<Integer>> codeVisitor =
 201             new Instruction.KindVisitor<Integer, List<Integer>>()
 202     {
 203         public Integer visitNoOperands(Instruction instr, List<Integer> p) {
 204             return 0;
 205         }
 206 
 207         public Integer visitArrayType(Instruction instr, TypeKind kind, List<Integer> p) {
 208             return 0;
 209         }
 210 
 211         public Integer visitBranch(Instruction instr, int offset, List<Integer> p) {
 212             return 0;
 213         }
 214 
 215         public Integer visitConstantPoolRef(Instruction instr, int index, List<Integer> p) {
 216             return p.contains(index) ? index : 0;
 217         }
 218 
 219         public Integer visitConstantPoolRefAndValue(Instruction instr, int index, int value, List<Integer> p) {
 220             return p.contains(index) ? index : 0;
 221         }
 222 
 223         public Integer visitLocal(Instruction instr, int index, List<Integer> p) {
 224             return 0;
 225         }
 226 
 227         public Integer visitLocalAndValue(Instruction instr, int index, int value, List<Integer> p) {
 228             return 0;
 229         }
 230 
 231         public Integer visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, List<Integer> p) {
 232             return 0;
 233         }
 234 
 235         public Integer visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, List<Integer> p) {
 236             return 0;
 237         }
 238 
 239         public Integer visitValue(Instruction instr, int value, List<Integer> p) {
 240             return 0;
 241         }
 242 
 243         public Integer visitUnknown(Instruction instr, List<Integer> p) {
 244             return 0;
 245         }
 246     };
 247 }
 248