-
Notifications
You must be signed in to change notification settings - Fork 9
/
Reaper.java
213 lines (185 loc) · 7.85 KB
/
Reaper.java
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package org.congocc.codegen.java;
import java.util.*;
import java.util.logging.Logger;
import org.congocc.parser.*;
import org.congocc.parser.tree.*;
import static org.congocc.parser.Token.TokenType.*;
/**
* A visitor that eliminates unused code.
* It is not absolutely correct, in the sense of catching all
* unused methods or fields, but works for our purposes.
* For example, it does not take account of overloaded methods, so
* if the method name is referenced somewhere, it is assumed to be used.
* However, it might be a reference to a method with the same name
* with different arguments.
* Also variable names can be in a sense overloaded by being defined
* in inner classes, but we don't bother about that either.
*/
class Reaper extends Node.Visitor {
private static Logger logger = Logger.getLogger("reaper");
private final Set<String> usedMethodNames = new HashSet<>();
private final Set<String> usedTypeNames = new HashSet<>();
private final Set<String> usedVarNames = new HashSet<>();
private final Set<String> usedImportDeclarations = new HashSet<>();
private final CompilationUnit jcu;
private boolean onSecondPass;
Reaper(CompilationUnit jcu) {
this.jcu = jcu;
}
void stripUnused() {
// Visit the tree over and over until
// nothing is added. Then we can stop.
int prevNameCount, nameCount;
do {
prevNameCount = usedMethodNames.size() + usedTypeNames.size() + usedVarNames.size();
visit(jcu);
logger.fine(String.format("used methods: %d", usedMethodNames.size()));
logger.fine(String.format("used types: %d", usedTypeNames.size()));
logger.fine(String.format("used vars: %d", usedVarNames.size()));
nameCount = usedMethodNames.size() + usedTypeNames.size() + usedVarNames.size();
} while (nameCount > prevNameCount);
// If the name of the method is not in usedMethodNames, we delete it.
for (MethodDeclaration md : jcu.descendants(MethodDeclaration.class, md->!usedMethodNames.contains(md.getName()))) {
logger.fine(String.format("removing method: %s", md.getName()));
md.getParent().remove(md);
}
// We go through all the private FieldDeclarations and get rid of any variables that
// are not in usedVarNames
for (FieldDeclaration fd : jcu.descendants(FieldDeclaration.class, this::isPrivate)) {
stripUnusedVars(fd);
}
for (TypeDeclaration td : jcu.descendants(TypeDeclaration.class, this::isPrivate)) {
if (!usedTypeNames.contains(td.getName())) {
logger.fine(String.format("removing type: %s", td.getName()));
td.getParent().remove(td);
}
}
usedMethodNames.clear();
usedTypeNames.clear();
usedVarNames.clear();
onSecondPass = true;
visit(jcu);
// Now get rid of unused and repeated imports.
for (ImportDeclaration imp : jcu.childrenOfType(ImportDeclaration.class)) {
if (!usedImportDeclarations.add(getKey(imp))) {
logger.fine(String.format("removing import: %s", imp));
jcu.remove(imp);
continue;
}
if (imp.firstChildOfType(STAR) == null) {
List<Identifier> names = imp.descendants(Identifier.class);
String name = names.get(names.size()-1).toString();
// Note that a static import can import methods.
if (imp.firstChildOfType(STATIC) != null && usedMethodNames.contains(name)) continue;
if (!usedTypeNames.contains(name)) {
logger.fine(String.format("removing import: %s", imp));
jcu.remove(imp);
}
}
}
}
private String getKey(ImportDeclaration decl) {
StringBuilder result = new StringBuilder();
for (Node child : decl.descendants(Token.class)) result.append(child);
return result.toString();
}
private boolean isPrivate(Node node) {
// a bit kludgy, when we are on the second pass before
// getting rid of imports, we just say nothing is private, since we
// presumably got rid of everything to be got rid of, on the previous pass
// except for unused imports.
if (onSecondPass) return false;
if (node.firstChildOfType(PRIVATE) != null) return true;
Modifiers mods = node.firstChildOfType(Modifiers.class);
return mods != null && mods.firstChildOfType(PRIVATE) != null;
}
void visit(MethodDeclaration md) {
if (!isPrivate(md)) {
usedMethodNames.add(md.getName());
}
if (usedMethodNames.contains(md.getName())) {
recurse(md);
}
}
void visit(ObjectType ot) {
Identifier firstID = ot.firstChildOfType(Identifier.class);
usedTypeNames.add(firstID.toString());
recurse(ot);
}
void visit(Annotation ann) {
String firstID = ann.firstDescendantOfType(Identifier.class).toString();
usedTypeNames.add(firstID);
recurse(ann);
}
void visit(MethodCall mc) {
String lhs = mc.firstChildOfType(InvocationArguments.class).previousSibling().getImage();
usedMethodNames.add(lhs.substring(lhs.lastIndexOf('.')+1));
recurse(mc);
}
void visit(MethodReference mr) {
usedMethodNames.add(mr.firstChildOfType(Identifier.class).toString());
recurse(mr);
}
void visit(Name name) {
if (name.size() > 1 || !(name.getParent() instanceof MethodCall)) {
usedVarNames.add(name.get(0).toString());
usedTypeNames.add(name.get(0).toString());
}
}
void visit(DotName dotName) {
usedVarNames.add(dotName.getLastChild().toString());
recurse(dotName);
}
void visit(VariableDeclarator vd) {
if (!isPrivate(vd.getParent()) || usedVarNames.contains(vd.getName())) {
recurse(vd);
}
}
void visit(TypeDeclaration td) {
if (!isPrivate(td) || usedTypeNames.contains(td.getName())) {
recurse(td);
}
}
void visit(ImportDeclaration decl) {}
void visit(PackageDeclaration decl) {}
void visit(FieldDeclaration fd) {
if (isUsed(fd)) {
recurse(fd);
}
}
private boolean isUsed(FieldDeclaration fd) {
if (!isPrivate(fd)) return true;
for (VariableDeclarator vd : fd.childrenOfType(VariableDeclarator.class)) {
if (usedVarNames.contains(vd.getName())) return true;
}
return false;
}
// Get rid of any variable declarations where the variable name
// is not in usedVarNames. The only complicated case is if the field
// has more than one variable declaration comma-separated
private void stripUnusedVars(FieldDeclaration fd) {
Set<Node> toBeRemoved = new HashSet<>();
for (VariableDeclarator vd : fd.childrenOfType(VariableDeclarator.class)) {
if (!usedVarNames.contains(vd.getName())) {
toBeRemoved.add(vd);
Node prev = vd.previousSibling();
Node next = vd.nextSibling();
if (prev.getType()==COMMA) {
toBeRemoved.add(prev);
}
else if (next.getType() == COMMA) {
toBeRemoved.add(next);
}
}
}
for (Node n : toBeRemoved) {
logger.fine(String.format("removing field %s from declaration at %s", n.firstChildOfType(Identifier.class),
n.getLocation()));
fd.remove(n);
}
if (fd.firstChildOfType(VariableDeclarator.class) == null) {
logger.fine(String.format("removing field declaration at %s", fd.getLocation()));
fd.getParent().remove(fd);
}
}
}