Modded consumers
This commit is contained in:
@@ -1,32 +1,29 @@
|
||||
package io.anuke.annotations;
|
||||
|
||||
import com.sun.source.util.TreePath;
|
||||
import com.sun.source.util.Trees;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;
|
||||
import io.anuke.annotations.Annotations.OverrideCallSuper;
|
||||
import com.sun.source.util.*;
|
||||
import com.sun.tools.javac.tree.*;
|
||||
import com.sun.tools.javac.tree.JCTree.*;
|
||||
import io.anuke.annotations.Annotations.*;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.lang.model.*;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.tools.Diagnostic.*;
|
||||
import java.util.*;
|
||||
|
||||
@SupportedAnnotationTypes("java.lang.Override")
|
||||
@SupportedAnnotationTypes({"java.lang.Override"})
|
||||
public class CallSuperAnnotationProcessor extends AbstractProcessor{
|
||||
private Trees trees;
|
||||
|
||||
@Override
|
||||
public void init (ProcessingEnvironment pe) {
|
||||
public void init(ProcessingEnvironment pe){
|
||||
super.init(pe);
|
||||
trees = Trees.instance(pe);
|
||||
}
|
||||
|
||||
public boolean process (Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
for (Element e : roundEnv.getElementsAnnotatedWith(Override.class)) {
|
||||
if (e.getAnnotation(OverrideCallSuper.class) != null) return false;
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv){
|
||||
for(Element e : roundEnv.getElementsAnnotatedWith(Override.class)){
|
||||
if(e.getAnnotation(OverrideCallSuper.class) != null) return false;
|
||||
|
||||
CodeAnalyzerTreeScanner codeScanner = new CodeAnalyzerTreeScanner();
|
||||
codeScanner.setMethodName(e.getSimpleName().toString());
|
||||
@@ -34,10 +31,10 @@ public class CallSuperAnnotationProcessor extends AbstractProcessor{
|
||||
TreePath tp = trees.getPath(e.getEnclosingElement());
|
||||
codeScanner.scan(tp, trees);
|
||||
|
||||
if (codeScanner.isCallSuperUsed()) {
|
||||
if(codeScanner.isCallSuperUsed()){
|
||||
List list = codeScanner.getMethod().getBody().getStatements();
|
||||
|
||||
if (!doesCallSuper(list, codeScanner.getMethodName())) {
|
||||
if(!doesCallSuper(list, codeScanner.getMethodName())){
|
||||
processingEnv.getMessager().printMessage(Kind.ERROR, "Overriding method '" + codeScanner.getMethodName() + "' must explicitly call super method from its parent class.", e);
|
||||
}
|
||||
}
|
||||
@@ -46,12 +43,12 @@ public class CallSuperAnnotationProcessor extends AbstractProcessor{
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean doesCallSuper (List list, String methodName) {
|
||||
for (Object object : list) {
|
||||
if (object instanceof JCTree.JCExpressionStatement) {
|
||||
JCTree.JCExpressionStatement expr = (JCExpressionStatement) object;
|
||||
private boolean doesCallSuper(List list, String methodName){
|
||||
for(Object object : list){
|
||||
if(object instanceof JCTree.JCExpressionStatement){
|
||||
JCTree.JCExpressionStatement expr = (JCExpressionStatement)object;
|
||||
String exprString = expr.toString();
|
||||
if (exprString.startsWith("super." + methodName) && exprString.endsWith(");")) return true;
|
||||
if(exprString.startsWith("super." + methodName) && exprString.endsWith(");")) return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +56,7 @@ public class CallSuperAnnotationProcessor extends AbstractProcessor{
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion () {
|
||||
public SourceVersion getSupportedSourceVersion(){
|
||||
return SourceVersion.RELEASE_8;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user