-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMethodGraphNode.java
More file actions
180 lines (147 loc) · 4.92 KB
/
Copy pathMethodGraphNode.java
File metadata and controls
180 lines (147 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package builder;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.TypePath;
import org.objectweb.asm.tree.MethodNode;
import java.util.HashSet;
import java.util.Set;
import static org.objectweb.asm.Opcodes.ASM9;
/**
* A class representing a node for each method inside a ClassGraphNode.
* Can act as a method node and a method node visitor.
*/
public class MethodGraphNode extends MethodNode {
String owner;
private Set<MethodGraphNode> methodCalls = new HashSet<>();
private Set<MethodGraphNode> callingMethods = new HashSet<>();
private DependencyCollector collector;
private boolean used;
private boolean visited;
private boolean calledVisited;
public MethodGraphNode(int access, String owner, String name, String desc, String signature, String[] exceptions) {
super(ASM9, access, name, desc, signature, exceptions);
this.owner = owner;
used = false;
visited = false;
calledVisited = false;
}
public void setCollector(DependencyCollector collector) {
this.collector = collector;
}
public Set<String> getDependentClassNames() {
return collector.getDependencies();
}
public void addMethodCall(MethodGraphNode calledMethod) {
methodCalls.add(calledMethod);
}
public void addCallingMethod(MethodGraphNode callingMethod) {
callingMethods.add(callingMethod);
}
public void markAsUsed() {
used = true;
}
public void markAsVisited() {
visited = true;
}
/**
* Mark the method when every method call made inside the current method node is visited
*/
public void markAsCalledVisited() {
calledVisited = true;
}
public boolean isUsed() {
return used;
}
public boolean isVisited() {
return visited;
}
public boolean isCalledVisited() {
return calledVisited;
}
@Override
public AnnotationVisitor visitAnnotationDefault() {
return null;
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
collector.addDesc(desc);
return null;
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
collector.addDesc(desc);
return null;
}
@Override
public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
collector.addDesc(desc);
return null;
}
@Override
public void visitTypeInsn(int opcode, String type) {
collector.addInternalName(owner);
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
collector.addInternalName(owner);
}
/**
* Visit INVOKESTATIC, INVOKESPECIAL, INVOKEINTERFACE, INVOKEVIRTUAL method calls
*/
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
collector.addInternalName(owner);
collector.addMethodDesc(desc);
super.visitMethodInsn(opcode, owner, name, desc, itf);
}
/**
* Visit INVOKEDYNAMIC method calls
*/
@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
super.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
}
@Override
public void visitLdcInsn(Object constant) {
collector.addConstant(constant);
}
@Override
public void visitMultiANewArrayInsn(String desc, int dims) {
}
@Override
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MethodGraphNode) {
MethodGraphNode mn = (MethodGraphNode) obj;
return owner.equals(mn.owner) && name.equals(mn.name) && desc.equals(mn.desc);
}
return false;
}
public MethodGraphNode getCopy() {
return new MethodGraphNode(access, owner, name, desc, null, null);
}
}