aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.classfile; aoqi@0: aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.HashSet; aoqi@0: import java.util.List; aoqi@0: import java.util.Objects; aoqi@0: import java.util.Set; aoqi@0: import com.sun.tools.classfile.Instruction.TypeKind; aoqi@0: import static com.sun.tools.classfile.ConstantPool.*; aoqi@0: aoqi@0: /** aoqi@0: * A utility class to find where in a ClassFile references aoqi@0: * a {@link CONSTANT_Methodref_info method}, aoqi@0: * a {@link CONSTANT_InterfaceMethodref_info interface method, aoqi@0: * or a {@link CONSTANT_Fieldref_info field}. aoqi@0: */ aoqi@0: public final class ReferenceFinder { aoqi@0: /** aoqi@0: * Filter for ReferenceFinder of what constant pool entries for reference lookup. aoqi@0: */ aoqi@0: public interface Filter { aoqi@0: /** aoqi@0: * Decides if the given CPRefInfo entry should be accepted or filtered. aoqi@0: * aoqi@0: * @param cpool ConstantPool of the ClassFile being parsed aoqi@0: * @param cpref constant pool entry representing a reference to aoqi@0: * a fields method, and interface method. aoqi@0: * @return {@code true} if accepted; otherwise {@code false} aoqi@0: */ aoqi@0: boolean accept(ConstantPool cpool, CPRefInfo cpref); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Visitor of individual method of a ClassFile that references the aoqi@0: * accepted field, method, or interface method references. aoqi@0: */ aoqi@0: public interface Visitor { aoqi@0: /** aoqi@0: * Invoked for a method containing one or more accepted CPRefInfo entries aoqi@0: * aoqi@0: * @param cf ClassFile aoqi@0: * @param method Method that does the references the accepted references aoqi@0: * @param refs Accepted constant pool method/field reference aoqi@0: */ aoqi@0: void visit(ClassFile cf, Method method, List refConstantPool); aoqi@0: } aoqi@0: aoqi@0: private final Filter filter; aoqi@0: private final Visitor visitor; aoqi@0: aoqi@0: /** aoqi@0: * Constructor. aoqi@0: */ aoqi@0: public ReferenceFinder(Filter filter, Visitor visitor) { aoqi@0: this.filter = Objects.requireNonNull(filter); aoqi@0: this.visitor = Objects.requireNonNull(visitor); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Parses a given ClassFile and invoke the visitor if there is any reference aoqi@0: * to the constant pool entries referencing field, method, or aoqi@0: * interface method that are accepted. This method will return aoqi@0: * {@code true} if there is one or more accepted constant pool entries aoqi@0: * to lookup; otherwise, it will return {@code false}. aoqi@0: * aoqi@0: * @param cf ClassFile aoqi@0: * @return {@code true} if the given class file is processed to lookup aoqi@0: * references aoqi@0: * @throws ConstantPoolException if an error of the constant pool aoqi@0: */ aoqi@0: public boolean parse(ClassFile cf) throws ConstantPoolException { aoqi@0: List cprefs = new ArrayList(); aoqi@0: int index = 1; aoqi@0: for (ConstantPool.CPInfo cpInfo : cf.constant_pool.entries()) { aoqi@0: if (cpInfo.accept(cpVisitor, cf.constant_pool)) { aoqi@0: cprefs.add(index); aoqi@0: } aoqi@0: index += cpInfo.size(); aoqi@0: } aoqi@0: aoqi@0: if (cprefs.isEmpty()) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: for (Method m : cf.methods) { aoqi@0: Set ids = new HashSet(); aoqi@0: Code_attribute c_attr = (Code_attribute) m.attributes.get(Attribute.Code); aoqi@0: if (c_attr != null) { aoqi@0: for (Instruction instr : c_attr.getInstructions()) { aoqi@0: int idx = instr.accept(codeVisitor, cprefs); aoqi@0: if (idx > 0) { aoqi@0: ids.add(idx); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (ids.size() > 0) { aoqi@0: List refInfos = new ArrayList(ids.size()); aoqi@0: for (int id : ids) { aoqi@0: refInfos.add(CPRefInfo.class.cast(cf.constant_pool.get(id))); aoqi@0: } aoqi@0: visitor.visit(cf, m, refInfos); aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: private ConstantPool.Visitor cpVisitor = aoqi@0: new ConstantPool.Visitor() aoqi@0: { aoqi@0: public Boolean visitClass(CONSTANT_Class_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, ConstantPool cpool) { aoqi@0: return filter.accept(cpool, info); aoqi@0: } aoqi@0: aoqi@0: public Boolean visitMethodref(CONSTANT_Methodref_info info, ConstantPool cpool) { aoqi@0: return filter.accept(cpool, info); aoqi@0: } aoqi@0: aoqi@0: public Boolean visitFieldref(CONSTANT_Fieldref_info info, ConstantPool cpool) { aoqi@0: return filter.accept(cpool, info); aoqi@0: } aoqi@0: aoqi@0: public Boolean visitDouble(CONSTANT_Double_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitFloat(CONSTANT_Float_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitInteger(CONSTANT_Integer_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitLong(CONSTANT_Long_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitNameAndType(CONSTANT_NameAndType_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitMethodHandle(CONSTANT_MethodHandle_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitMethodType(CONSTANT_MethodType_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitString(CONSTANT_String_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public Boolean visitUtf8(CONSTANT_Utf8_info info, ConstantPool cpool) { aoqi@0: return false; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: private Instruction.KindVisitor> codeVisitor = aoqi@0: new Instruction.KindVisitor>() aoqi@0: { aoqi@0: public Integer visitNoOperands(Instruction instr, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitArrayType(Instruction instr, TypeKind kind, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitBranch(Instruction instr, int offset, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitConstantPoolRef(Instruction instr, int index, List p) { aoqi@0: return p.contains(index) ? index : 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitConstantPoolRefAndValue(Instruction instr, int index, int value, List p) { aoqi@0: return p.contains(index) ? index : 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitLocal(Instruction instr, int index, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitLocalAndValue(Instruction instr, int index, int value, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitValue(Instruction instr, int value, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: public Integer visitUnknown(Instruction instr, List p) { aoqi@0: return 0; aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: