6338N/A/*
6338N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6338N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6338N/A *
6338N/A * This code is free software; you can redistribute it and/or modify it
6338N/A * under the terms of the GNU General Public License version 2 only, as
6338N/A * published by the Free Software Foundation.
6338N/A *
6338N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6338N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6338N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6338N/A * version 2 for more details (a copy is included in the LICENSE file that
6338N/A * accompanied this code).
6338N/A *
6338N/A * You should have received a copy of the GNU General Public License version
6338N/A * 2 along with this work; if not, write to the Free Software Foundation,
6338N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6338N/A *
6338N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6338N/A * or visit www.oracle.com if you need additional information or have any
6338N/A * questions.
6338N/A */
6338N/A
6338N/Aimport java.util.*;
6338N/Aimport com.sun.tools.classfile.*;
6338N/Aimport static com.sun.tools.classfile.ConstantPool.*;
6338N/Aimport com.sun.tools.classfile.Instruction.TypeKind;
6338N/A
6338N/A/**
6338N/A * MethodFinder utility class to find references to the given methods.
6338N/A */
6338N/Apublic abstract class MethodFinder {
6338N/A final List<String> methods;
6338N/A public MethodFinder(String... methods) {
6338N/A this.methods = Arrays.asList(methods);
6338N/A }
6338N/A
6338N/A /**
6338N/A * A callback method will be invoked when a method referencing
6338N/A * any of the lookup methods.
6338N/A *
6338N/A * @param cf ClassFile
6338N/A * @param m Method
6338N/A * @param refs Set of constant pool indices that reference the methods
6338N/A * matching the given lookup method names
6338N/A */
6338N/A public abstract void referenceFound(ClassFile cf, Method m, Set<Integer> refs)
6338N/A throws ConstantPoolException;
6338N/A
6338N/A public String parse(ClassFile cf) throws ConstantPoolException {
6338N/A List<Integer> cprefs = new ArrayList<Integer>();
6338N/A int index = 1;
6338N/A for (ConstantPool.CPInfo cpInfo : cf.constant_pool.entries()) {
6338N/A if (cpInfo.accept(cpVisitor, null)) {
6338N/A cprefs.add(index);
6338N/A }
6338N/A index += cpInfo.size();
6338N/A }
6338N/A
6338N/A if (!cprefs.isEmpty()) {
6338N/A for (Method m : cf.methods) {
6338N/A Set<Integer> refs = new HashSet<Integer>();
6338N/A Code_attribute c_attr = (Code_attribute) m.attributes.get(Attribute.Code);
6338N/A if (c_attr != null) {
6338N/A for (Instruction instr : c_attr.getInstructions()) {
6338N/A int idx = instr.accept(codeVisitor, cprefs);
6338N/A if (idx > 0) {
6338N/A refs.add(idx);
6338N/A }
6338N/A }
6338N/A }
6338N/A if (refs.size() > 0) {
6338N/A referenceFound(cf, m, refs);
6338N/A }
6338N/A }
6338N/A }
6338N/A return cprefs.isEmpty() ? "" : cf.getName();
6338N/A }
6338N/A
6338N/A private ConstantPool.Visitor<Boolean,Void> cpVisitor =
6338N/A new ConstantPool.Visitor<Boolean,Void>()
6338N/A {
6338N/A private boolean matches(CPRefInfo info) {
6338N/A try {
6338N/A CONSTANT_NameAndType_info nat = info.getNameAndTypeInfo();
6338N/A return matches(info.getClassName(), nat.getName(), nat.getType());
6338N/A } catch (ConstantPoolException ex) {
6338N/A return false;
6338N/A }
6338N/A }
6338N/A
6338N/A private boolean matches(String cn, String name, String type) {
6338N/A return methods.contains(cn + "." + name);
6338N/A }
6338N/A
6338N/A public Boolean visitClass(CONSTANT_Class_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
6338N/A return matches(info);
6338N/A }
6338N/A
6338N/A public Boolean visitMethodref(CONSTANT_Methodref_info info, Void p) {
6338N/A return matches(info);
6338N/A }
6338N/A
6338N/A public Boolean visitDouble(CONSTANT_Double_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitFieldref(CONSTANT_Fieldref_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitFloat(CONSTANT_Float_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitInteger(CONSTANT_Integer_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitLong(CONSTANT_Long_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitMethodHandle(CONSTANT_MethodHandle_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitMethodType(CONSTANT_MethodType_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitString(CONSTANT_String_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A
6338N/A public Boolean visitUtf8(CONSTANT_Utf8_info info, Void p) {
6338N/A return false;
6338N/A }
6338N/A };
6338N/A
6338N/A private Instruction.KindVisitor<Integer, List<Integer>> codeVisitor =
6338N/A new Instruction.KindVisitor<Integer, List<Integer>>()
6338N/A {
6338N/A public Integer visitNoOperands(Instruction instr, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A
6338N/A public Integer visitArrayType(Instruction instr, TypeKind kind, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A
6338N/A public Integer visitBranch(Instruction instr, int offset, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A
6338N/A public Integer visitConstantPoolRef(Instruction instr, int index, List<Integer> p) {
6338N/A return p.contains(index) ? index : 0;
6338N/A }
6338N/A
6338N/A public Integer visitConstantPoolRefAndValue(Instruction instr, int index, int value, List<Integer> p) {
6338N/A return p.contains(index) ? index : 0;
6338N/A }
6338N/A
6338N/A public Integer visitLocal(Instruction instr, int index, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A
6338N/A public Integer visitLocalAndValue(Instruction instr, int index, int value, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A
6338N/A public Integer visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A
6338N/A public Integer visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A
6338N/A public Integer visitValue(Instruction instr, int value, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A
6338N/A public Integer visitUnknown(Instruction instr, List<Integer> p) {
6338N/A return 0;
6338N/A }
6338N/A };
6338N/A}
6338N/A