mirror of
https://github.com/Anuken/Mindustry.git
synced 2026-01-22 20:41:58 -08:00
WIP save refactoring
This commit is contained in:
parent
13969bdd29
commit
af67690e75
20 changed files with 387 additions and 44 deletions
|
|
@ -4,12 +4,27 @@ import java.lang.annotation.*;
|
|||
|
||||
public class Annotations{
|
||||
|
||||
/** Indicates that a method should always call its super version. */
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface CallSuper{
|
||||
|
||||
}
|
||||
|
||||
/** Annotation that allows overriding CallSuper annotation. To be used on method that overrides method with CallSuper annotation from parent class.*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface OverrideCallSuper {
|
||||
}
|
||||
|
||||
/** Indicates that a method return or field can be null.*/
|
||||
@Target({ElementType.METHOD, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface Nullable{
|
||||
|
||||
}
|
||||
|
||||
/** Indicates that a method return or field cannot be null.*/
|
||||
@Target({ElementType.METHOD, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface NonNull{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
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 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;
|
||||
|
||||
@SupportedAnnotationTypes("java.lang.Override")
|
||||
public class CallSuperAnnotationProcessor extends AbstractProcessor{
|
||||
private Trees trees;
|
||||
|
||||
@Override
|
||||
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;
|
||||
|
||||
CodeAnalyzerTreeScanner codeScanner = new CodeAnalyzerTreeScanner();
|
||||
codeScanner.setMethodName(e.getSimpleName().toString());
|
||||
|
||||
TreePath tp = trees.getPath(e.getEnclosingElement());
|
||||
codeScanner.scan(tp, trees);
|
||||
|
||||
if (codeScanner.isCallSuperUsed()) {
|
||||
List list = codeScanner.getMethod().getBody().getStatements();
|
||||
|
||||
if (!doesCallSuper(list, codeScanner.getMethodName())) {
|
||||
processingEnv.getMessager().printMessage(Kind.ERROR, "Overriding method '" + codeScanner.getMethodName() + "' must explicitly call super method from its parent class.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion () {
|
||||
return SourceVersion.RELEASE_8;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
package io.anuke.annotations;
|
||||
|
||||
import com.sun.source.tree.*;
|
||||
import com.sun.source.util.TreePathScanner;
|
||||
import com.sun.source.util.Trees;
|
||||
import com.sun.tools.javac.code.Scope;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
import com.sun.tools.javac.code.Symbol.MethodSymbol;
|
||||
import com.sun.tools.javac.code.Type.ClassType;
|
||||
import com.sun.tools.javac.tree.JCTree.JCIdent;
|
||||
import com.sun.tools.javac.tree.JCTree.JCTypeApply;
|
||||
import io.anuke.annotations.Annotations.CallSuper;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
class CodeAnalyzerTreeScanner extends TreePathScanner<Object, Trees> {
|
||||
private String methodName;
|
||||
private MethodTree method;
|
||||
private boolean callSuperUsed;
|
||||
|
||||
@Override
|
||||
public Object visitClass (ClassTree classTree, Trees trees) {
|
||||
Tree extendTree = classTree.getExtendsClause();
|
||||
|
||||
if (extendTree instanceof JCTypeApply) { //generic classes case
|
||||
JCTypeApply generic = (JCTypeApply) extendTree;
|
||||
extendTree = generic.clazz;
|
||||
}
|
||||
|
||||
if (extendTree instanceof JCIdent) {
|
||||
JCIdent tree = (JCIdent) extendTree;
|
||||
Scope members = tree.sym.members();
|
||||
|
||||
if (checkScope(members))
|
||||
return super.visitClass(classTree, trees);
|
||||
|
||||
if (checkSuperTypes((ClassType) tree.type))
|
||||
return super.visitClass(classTree, trees);
|
||||
|
||||
}
|
||||
callSuperUsed = false;
|
||||
|
||||
return super.visitClass(classTree, trees);
|
||||
}
|
||||
|
||||
public boolean checkSuperTypes (ClassType type) {
|
||||
if (type.supertype_field != null && type.supertype_field.tsym != null) {
|
||||
if (checkScope(type.supertype_field.tsym.members()))
|
||||
return true;
|
||||
else
|
||||
return checkSuperTypes((ClassType) type.supertype_field);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean checkScope (Scope members) {
|
||||
for (Symbol s : members.getElements()) {
|
||||
if (s instanceof MethodSymbol) {
|
||||
MethodSymbol ms = (MethodSymbol) s;
|
||||
|
||||
if (ms.getSimpleName().toString().equals(methodName)) {
|
||||
Annotation annotation = ms.getAnnotation(CallSuper.class);
|
||||
if (annotation != null) {
|
||||
callSuperUsed = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visitMethod (MethodTree methodTree, Trees trees) {
|
||||
if (methodTree.getName().toString().equals(methodName))
|
||||
method = methodTree;
|
||||
|
||||
return super.visitMethod(methodTree, trees);
|
||||
}
|
||||
|
||||
public void setMethodName (String methodName) {
|
||||
this.methodName = methodName;
|
||||
}
|
||||
|
||||
public String getMethodName () {
|
||||
return methodName;
|
||||
}
|
||||
|
||||
public MethodTree getMethod () {
|
||||
return method;
|
||||
}
|
||||
|
||||
public boolean isCallSuperUsed () {
|
||||
return callSuperUsed;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue