Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ReferenceFinder.java
41161 views
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
@Override
164
public Boolean visitDynamicConstant(CONSTANT_Dynamic_info info, ConstantPool constantPool) {
165
return false;
166
}
167
168
public Boolean visitLong(CONSTANT_Long_info info, ConstantPool cpool) {
169
return false;
170
}
171
172
public Boolean visitMethodHandle(CONSTANT_MethodHandle_info info, ConstantPool cpool) {
173
return false;
174
}
175
176
public Boolean visitMethodref(CONSTANT_Methodref_info info, ConstantPool cpool) {
177
return filter.accept(cpool, info);
178
}
179
180
public Boolean visitMethodType(CONSTANT_MethodType_info info, ConstantPool cpool) {
181
return false;
182
}
183
184
public Boolean visitModule(CONSTANT_Module_info info, ConstantPool cpool) {
185
return false;
186
}
187
188
public Boolean visitNameAndType(CONSTANT_NameAndType_info info, ConstantPool cpool) {
189
return false;
190
}
191
192
public Boolean visitPackage(CONSTANT_Package_info info, ConstantPool cpool) {
193
return false;
194
}
195
196
public Boolean visitString(CONSTANT_String_info info, ConstantPool cpool) {
197
return false;
198
}
199
200
public Boolean visitUtf8(CONSTANT_Utf8_info info, ConstantPool cpool) {
201
return false;
202
}
203
};
204
205
private Instruction.KindVisitor<Integer, List<Integer>> codeVisitor =
206
new Instruction.KindVisitor<Integer, List<Integer>>()
207
{
208
public Integer visitNoOperands(Instruction instr, List<Integer> p) {
209
return 0;
210
}
211
212
public Integer visitArrayType(Instruction instr, TypeKind kind, List<Integer> p) {
213
return 0;
214
}
215
216
public Integer visitBranch(Instruction instr, int offset, List<Integer> p) {
217
return 0;
218
}
219
220
public Integer visitConstantPoolRef(Instruction instr, int index, List<Integer> p) {
221
return p.contains(index) ? index : 0;
222
}
223
224
public Integer visitConstantPoolRefAndValue(Instruction instr, int index, int value, List<Integer> p) {
225
return p.contains(index) ? index : 0;
226
}
227
228
public Integer visitLocal(Instruction instr, int index, List<Integer> p) {
229
return 0;
230
}
231
232
public Integer visitLocalAndValue(Instruction instr, int index, int value, List<Integer> p) {
233
return 0;
234
}
235
236
public Integer visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, List<Integer> p) {
237
return 0;
238
}
239
240
public Integer visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, List<Integer> p) {
241
return 0;
242
}
243
244
public Integer visitValue(Instruction instr, int value, List<Integer> p) {
245
return 0;
246
}
247
248
public Integer visitUnknown(Instruction instr, List<Integer> p) {
249
return 0;
250
}
251
};
252
}
253
254
255