/
VisitorState.java
266 lines (239 loc) · 9.23 KB
/
VisitorState.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright 2011 Google Inc. All Rights Reserved.
*
* Licensed 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 com.google.errorprone;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ArrayType;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;
import java.lang.reflect.Method;
/**
* @author alexeagle@google.com (Alex Eagle)
*/
public class VisitorState {
private final DescriptionListener descriptionListener;
private final MatchListener matchListener;
public final Context context;
private final TreePath path;
private VisitorState(Context context, TreePath path,
DescriptionListener descriptionListener, MatchListener matchListener) {
this.context = context;
this.path = path;
this.descriptionListener = descriptionListener;
this.matchListener = matchListener;
}
public VisitorState(Context context, DescriptionListener listener) {
this(context, null, listener, new MatchListener() {
@Override
public void onMatch(Tree tree) {
}
});
}
public VisitorState(Context context, MatchListener listener) {
this(context, null, new DescriptionListener() {
@Override
public void onDescribed(Description description) {}
}, listener);
}
public VisitorState withPath(TreePath path) {
return new VisitorState(context, path, descriptionListener, matchListener);
}
public TreePath getPath() {
return path;
}
public TreeMaker getTreeMaker() {
return TreeMaker.instance(context);
}
public Types getTypes() {
return Types.instance(context);
}
public Symtab getSymtab() {
return Symtab.instance(context);
}
public JavacElements getElements() {
return JavacElements.instance(context);
}
public Name.Table getNames() {
return Name.Table.instance(context);
}
public DescriptionListener getDescriptionListener() {
return descriptionListener;
}
public MatchListener getMatchListener() {
return matchListener;
}
// Cache the name lookup strategy since it requires expensive reflection, and is used a lot
private static final NameLookupStrategy NAME_LOOKUP_STRATEGY = createNameLookup();
private static NameLookupStrategy createNameLookup() {
ClassLoader classLoader = VisitorState.class.getClassLoader();
// OpenJDK 7
try {
Class<?> namesClass = classLoader.loadClass("com.sun.tools.javac.util.Names");
final Method instanceMethod = namesClass.getDeclaredMethod("instance", Context.class);
final Method fromStringMethod = namesClass.getDeclaredMethod("fromString", String.class);
return new NameLookupStrategy() {
@Override public Name fromString(Context context, String nameStr) {
try {
Object names = instanceMethod.invoke(null, context);
return (Name) fromStringMethod.invoke(names, nameStr);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
} catch (ClassNotFoundException e) {
// OpenJDK 6
try {
Class<?> nameTableClass = classLoader.loadClass("com.sun.tools.javac.util.Name$Table");
final Method instanceMethod = nameTableClass.getMethod("instance", Context.class);
final Method fromStringMethod = Name.class.getMethod("fromString", nameTableClass, String.class);
return new NameLookupStrategy() {
@Override public Name fromString(Context context, String nameStr) {
try {
Object nameTable = instanceMethod.invoke(null, context);
return (Name) fromStringMethod.invoke(null, nameTable, nameStr);
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
};
} catch (Exception e1) {
throw new RuntimeException("Unexpected error loading com.sun.tools.javac.util.Names", e1);
}
} catch (Exception e) {
throw new RuntimeException("Unexpected error loading com.sun.tools.javac.util.Names", e);
}
}
public Name getName(String nameStr) {
return NAME_LOOKUP_STRATEGY.fromString(context, nameStr);
}
/**
* Given the string representation of a simple (non-array, non-generic) type, return the
* matching Type.
*
* <p>If this method returns null, the compiler has not seen this type yet, which means that if
* you are comparing other types to this for equality or the subtype relation, your result would
* always be false even if it could create the type. Thus it might be best to bail out early in
* your matcher if this method returns null on your type of interest.
*
* @param typeStr The canonical string representation of a simple type (e.g., "java.lang.Object")
* @return The Type that corresponds to the string, or null if it cannot be found
*/
public Type getTypeFromString(String typeStr) {
validateTypeStr(typeStr);
if (isPrimitiveType(typeStr)) {
return getPrimitiveType(typeStr);
}
Name typeName = getName(typeStr);
ClassSymbol typeSymbol = getSymtab().classes.get(typeName);
if (typeSymbol != null) {
return typeSymbol.asType();
}
return null;
}
/**
* Build an instance of a Type.
*/
public Type getType(Type baseType, boolean isArray, List<Type> typeParams) {
boolean isGeneric = typeParams != null && !typeParams.equals(List.nil());
if (!isArray && !isGeneric) {
// Simple type.
return baseType;
} else if (isArray && !isGeneric) {
// Array type, not generic.
ClassSymbol arraySymbol = getSymtab().arrayClass;
return new ArrayType(baseType, arraySymbol);
} else if (!isArray && isGeneric) {
return new ClassType(Type.noType, typeParams, baseType.tsym);
} else {
throw new IllegalArgumentException("Unsupported arguments to getType");
}
}
/**
* Build an instance of a Type.
*/
public Type getType(Type baseType, boolean isArray, java.util.List<Type> typeParams) {
boolean isGeneric = typeParams != null && !typeParams.equals(List.nil());
if (!isArray && !isGeneric) {
// Simple type.
return baseType;
} else if (isArray && !isGeneric) {
// Array type, not generic.
ClassSymbol arraySymbol = getSymtab().arrayClass;
return new ArrayType(baseType, arraySymbol);
} else if (!isArray && isGeneric) {
// Generic type, not array.
List<Type> typeParamsCopy = List.from(typeParams.toArray(new Type[typeParams.size()]));
return new ClassType(Type.noType, typeParamsCopy, baseType.tsym);
} else {
throw new IllegalArgumentException("Unsupported arguments to getType");
}
}
/**
* Validates a type string, ensuring it is not generic and not an array type.
*/
private static void validateTypeStr(String typeStr) {
if (typeStr.contains("[") || typeStr.contains("]")) {
throw new IllegalArgumentException("Cannot convert array types, please build them using "
+ "getType()");
}
if (typeStr.contains("<") || typeStr.contains(">")) {
throw new IllegalArgumentException("Cannot covnert generic types, please build them using "
+ "getType()");
}
}
/**
* Given a string that represents a primitive type (e.g., "int"), return the corresponding Type.
*/
private Type getPrimitiveType(String typeStr) {
if (typeStr.equals("byte")) {
return getSymtab().byteType;
} else if (typeStr.equals("short")) {
return getSymtab().shortType;
} else if (typeStr.equals("int")) {
return getSymtab().intType;
} else if (typeStr.equals("long")) {
return getSymtab().longType;
} else if (typeStr.equals("float")) {
return getSymtab().floatType;
} else if (typeStr.equals("double")) {
return getSymtab().doubleType;
} else if (typeStr.equals("boolean")) {
return getSymtab().booleanType;
} else if (typeStr.equals("char")) {
return getSymtab().charType;
} else {
throw new IllegalStateException("Type string " + typeStr + " expected to be primitive");
}
}
private static boolean isPrimitiveType(String typeStr) {
return typeStr.equals("byte") || typeStr.equals("short") || typeStr.equals("int") ||
typeStr.equals("long") || typeStr.equals("float") || typeStr.equals("double") ||
typeStr.equals("boolean") || typeStr.equals("char");
}
private interface NameLookupStrategy {
Name fromString(Context context, String nameStr);
}
}