-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathCheckOverrides.java
More file actions
377 lines (363 loc) · 13.6 KB
/
CheckOverrides.java
File metadata and controls
377 lines (363 loc) · 13.6 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.laytonsmith.PureUtilities.Common.Annotations;
import com.laytonsmith.PureUtilities.ClassLoading.ClassDiscovery;
import com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.ClassMirror;
import com.laytonsmith.PureUtilities.Common.ArrayUtils;
import com.laytonsmith.PureUtilities.Common.ClassUtils;
import com.laytonsmith.PureUtilities.Common.StreamUtils;
import com.laytonsmith.PureUtilities.Common.StringUtils;
import com.laytonsmith.annotations.MustUseOverride;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
/**
*
*/
@SupportedAnnotationTypes({"java.lang.Override", "com.laytonsmith.annotations.MustUseOverride"})
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class CheckOverrides extends AbstractProcessor {
private static final boolean ENABLED = true;
private static Map<Class, Set<Method>> methods = null;
private static final Set<Class> INTERFACES_WITH_MUST_USE_OVERRIDE = new HashSet<>();
private static final Pattern METHOD_SIGNATURE = Pattern.compile("[a-zA-Z0-9_]+\\((.*)\\)");
private static final Pattern CLASS_TEMPLATES = Pattern.compile("^.*?<(.*)>?$");
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if(!ENABLED) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "CheckOverrides processor is turned off!");
return false;
}
setup();
if(!roundEnv.processingOver()) {
for(Element element : roundEnv.getElementsAnnotatedWith(MustUseOverride.class)) {
String className = element.toString();
Class c = null;
try {
c = getClassFromName(className);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CheckOverrides.class.getName()).log(Level.SEVERE, null, ex);
}
if(c != null) {
if(!c.isInterface()) {
StreamUtils.GetSystemErr().println("Only interfaces may be annotated with "
+ MustUseOverride.class.getName());
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
"Only interfaces may be annotated with " + MustUseOverride.class.getName());
}
INTERFACES_WITH_MUST_USE_OVERRIDE.add(c);
}
}
for(Element element : roundEnv.getElementsAnnotatedWith(Override.class)) {
String className = element.getEnclosingElement().toString();
Class c = null;
try {
c = getClassFromName(className);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CheckOverrides.class.getName()).log(Level.SEVERE, null, ex);
}
if(c != null && !c.isInterface()) {
//StreamUtils.GetSystemOut().println("Dealing with " + c.getName() + ".." + element.toString());
//We have to do a bit of massaging to turn "method(java.lang.String[], java.lang.String)
//into a Method object.
Matcher m = METHOD_SIGNATURE.matcher(element.toString());
String methodName = element.getSimpleName().toString();
Class[] argTypes;
boolean isTemplate = false;
if(!m.find()) {
argTypes = new Class[0];
} else {
String inner = m.group(1);
String[] args;
if("".equals(inner.trim())) {
args = ArrayUtils.EMPTY_STRING_ARRAY;
} else {
//Take out generics, since we can't really deal with them, and they make parsing
//the args harder.
inner = removeGenerics(inner);
args = StringUtils.trimSplit(inner, ",");
}
//StreamUtils.GetSystemOut().println("Args length: " + args.length);
argTypes = new Class[args.length];
for(int i = 0; i < args.length; i++) {
try {
argTypes[i] = getClassFromName(args[i]);
} catch (ClassNotFoundException e) {
//It may be a template parameter, so check in the enclosing class name for that
//template type.
String codeClassName = element.getEnclosingElement().asType().toString();
Matcher mm = CLASS_TEMPLATES.matcher(codeClassName);
boolean found = false;
if(mm.find()) {
String[] templates = removeGenerics(mm.group(1)).split(",");
String baseClass = args[i].replaceAll("\\[\\]", "");
for(String template : templates) {
if(baseClass.equals(template)) {
//Ok, it's found.
isTemplate = true;
found = true;
args[i] = args[i].replaceFirst(Pattern.quote(template), "java.lang.Object");
break;
}
}
}
if(!isTemplate || !found) {
//Oh, there aren't any. Well, I don't know why this would happen.
Logger.getLogger(CheckOverrides.class.getName()).log(Level.SEVERE, null, e);
}
try {
argTypes[i] = Class.forName(args[i]);
} catch (ClassNotFoundException ex) {
//Won't happen
}
}
}
}
if(isTemplate) {
//Template parameters that extend something break this, because the annotation
//processor doesn't provide the information to us. So, for instance, if you have
//a template parameter MyClass<T extends List> and a method in that class
//void myMethod(T); then the signature of that method is actually
//void myMethod(List), but since we don't have a way of getting "List"
//from the APT, we can't really do anything to detect if you've overridden
//void myMethod(T) vs void myMethod(Object). So we have to settle here for
//missing an error, instead of erroring out when there isn't actually one,
//and remove all the methods with this name and type. We can, however,
//avoid removing methods with different number of arguments, since we
//can guarantee those aren't overridden.
for(Method method : c.getDeclaredMethods()) {
if(method.getName().equals(methodName)
&& method.getParameterTypes().length == argTypes.length) {
methods.get(c).remove(method);
}
}
} else {
//Arg types are now all provided, and so is the method name.
try {
Method method = c.getDeclaredMethod(methodName, argTypes);
//Ok, remove it from the list of methods, cause we know it's overridden.
//.remove won't work, because we need to also remove co-return types present
Iterator<Method> it = methods.get(c).iterator();
while(it.hasNext()) {
Method next = it.next();
if(next.getName().equals(method.getName())
&& checkSignatureForCompatibility(next.getParameterTypes(), method.getParameterTypes())) {
it.remove();
}
}
} catch (NoSuchMethodException | SecurityException ex) {
Logger.getLogger(CheckOverrides.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(methods.get(c).isEmpty()) {
methods.remove(c);
}
}
}
//Now all the overridden methods have been removed from the list of methods in all the
//classes. We now need to go through and find out which of the remaining methods *could*
//be overriden, as many may not be overrides anyways.
Set<Method> methodsInError = new HashSet<>();
for(Class c : methods.keySet()) {
Set<Method> mm = methods.get(c);
for(Method m : mm) {
//Get the superclass/superinterfaces that this class extends/implements
//all the way up to Object
Set<Class> supers = new HashSet<>();
getAllSupers(c, supers, true);
//Ok, now look through all the superclasses' methods, and find any that
//match the signature. If they do, it's an error.
List<Method> compare = new ArrayList<>();
for(Class s : supers) {
compare.addAll(getOverridableMethods(s));
}
for(Method superM : compare) {
if(m.getName().equals(superM.getName())) {
if(checkSignatureForCompatibility(superM.getParameterTypes(), m.getParameterTypes())) {
//Oops, found a bad method.
methodsInError.add(m);
}
} //else different method altogether
}
}
}
if(!methodsInError.isEmpty()) {
//Some package names are pretty verbose, and will more than
//likely be included with an import, so let's trim the
//error message down some so it's easier to read
final List<String> verbosePackages = Arrays.asList(new String[]{
"java.lang",
"java.util",
"java.io"
});
//Build a sorted set, so these go in order.
SortedSet<String> stringMethodsInError = new TreeSet<>();
for(Method m : methodsInError) {
stringMethodsInError.add(m.getDeclaringClass().getName() + "."
+ m.getName() + "(" + StringUtils.Join(Arrays.asList(m.getParameterTypes()), ", ", ", ", ", ", "", new StringUtils.Renderer<Class<?>>() {
@Override
public String toString(Class<?> item) {
String name = ClassUtils.getCommonName(item);
for(String v : verbosePackages) {
if(name.matches(Pattern.quote(v) + "\\.([^\\.]*?)$")) {
return name.replaceFirst(Pattern.quote(v) + "\\.", "");
}
}
return name;
}
}) + ")");
}
final StringBuilder b = new StringBuilder();
b.append("There ")
.append(StringUtils.PluralTemplateHelper(stringMethodsInError.size(),
"is a method which overrides or implements a method in a super class/super interface,"
+ " but doesn't use the @Override tag. Please tag this method",
"are %d methods which override or implement a method in a super class/super interface"
+ " but don't use the @Override tag. Please tag these methods"))
.append(" with @Override to continue the build process.")
.append(StringUtils.NL)
.append(StringUtils.Join(stringMethodsInError, StringUtils.NL));
StreamUtils.GetSystemErr().println(b.toString());
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, b.toString());
} else {
StreamUtils.GetSystemOut().println("No @Override annotations were found to be missing.");
}
}
return false;
}
private static void getAllSupers(Class c, Set<Class> building, boolean first) {
if(c == null || c == Object.class) {
return;
}
if(!first) {
building.add(c);
} else {
//everything extends Object, and we're gonna use
//that as our stop marker later, so go ahead and
//add this now.
building.add(Object.class);
}
getAllSupers(c.getSuperclass(), building, false);
for(Class cc : c.getInterfaces()) {
if(INTERFACES_WITH_MUST_USE_OVERRIDE.contains(cc)) {
building.add(cc);
}
}
}
/**
* Checks to see if an argument signature is compatible. That is, if the parameter types match.
*
* @param superArgs The super class arguments to check
* @param subArgs The sub class arguments to check
* @return
*/
private static boolean checkSignatureForCompatibility(Class[] superArgs, Class[] subArgs) {
if(superArgs.length != subArgs.length) {
return false;
}
for(int i = 0; i < superArgs.length; i++) {
if(superArgs[i] != subArgs[i]) {
return false;
}
}
return true;
}
/**
* Removes generics from an identifier.
*
* @param identifier
* @return
*/
private static String removeGenerics(String identifier) {
StringBuilder b = new StringBuilder();
int genericCount = 0;
for(int i = 0; i < identifier.length(); i++) {
char ch = identifier.charAt(i);
if(ch == '<') {
genericCount++;
continue;
}
if(ch == '>') {
genericCount--;
continue;
}
if(genericCount == 0) {
b.append(ch);
}
}
return b.toString();
}
private static Class getClassFromName(String className) throws ClassNotFoundException {
return ClassUtils.forCanonicalName(className, false, CheckOverrides.class.getClassLoader());
}
private static void setup() {
if(methods == null) {
methods = new HashMap<>();
List<ClassMirror<?>> classes = ClassDiscovery.getDefaultInstance().getKnownClasses(ClassDiscovery.GetClassContainer(CheckOverrides.class));
for(ClassMirror cm : classes) {
Class c = cm.loadClass(CheckOverrides.class.getClassLoader(), false);
if(c.isInterface()) {
continue;
}
Set<Method> mm = getPotentiallyOverridingMethods(c);
if(!mm.isEmpty()) {
methods.put(c, mm);
}
}
}
}
/**
* Returns a list of potentially overriding methods in a class. That is, the non-private, non-static methods.
*
* @param c
* @return
*/
private static Set<Method> getPotentiallyOverridingMethods(Class c) {
Set<Method> methodList = new HashSet<>();
for(Method m : c.getDeclaredMethods()) {
//Ignore static or public methods, since those can't override anything
if((m.getModifiers() & Modifier.PRIVATE) == 0 && (m.getModifiers() & Modifier.STATIC) == 0
&& !m.isSynthetic()) {
methodList.add(m);
}
}
return methodList;
}
/**
* Returns a list of overridable methods in a class. This includes non-static, non-private, non-final methods.
*
* @param c
* @return
*/
private static List<Method> getOverridableMethods(Class c) {
List<Method> methodList = new ArrayList<>();
for(Method m : c.getDeclaredMethods()) {
if((m.getModifiers() & Modifier.PRIVATE) == 0
&& (m.getModifiers() & Modifier.STATIC) == 0
&& (m.getModifiers() & Modifier.FINAL) == 0
&& !m.isSynthetic()) {
methodList.add(m);
}
}
return methodList;
}
}