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