mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 22:17:41 +00:00
AnnotationHandlers can now return a boolean to set if they actually handled the annotation or not (previously, the presumption was they always handled the annotation).
This is very useful for PrintAST on eclipse, because before this change, you'd never see method contents (as the initial dietParse would come first). Now Eclipse PrintASTHandler will skip any non-full runs, and only print non-diet. It then returns true only if it printed.
This commit is contained in:
1 parent
8fa5005444
commit
eca3cd7ccc
11 files changed
+65
-42
No files matched your search
@@ -3,5 +3,5 @@ package lombok.eclipse;
|
||||
import lombok.core.AnnotationValues;
|
||||
|
||||
public interface EclipseAnnotationHandler<T extends java.lang.annotation.Annotation> {
|
||||
void handle(AnnotationValues<T> annotation, org.eclipse.jdt.internal.compiler.ast.Annotation ast, EclipseAST.Node annotationNode);
|
||||
boolean handle(AnnotationValues<T> annotation, org.eclipse.jdt.internal.compiler.ast.Annotation ast, EclipseAST.Node annotationNode);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public class HandlerLibrary {
|
||||
this.annotationClass = annotationClass;
|
||||
}
|
||||
|
||||
public void handle(org.eclipse.jdt.internal.compiler.ast.Annotation annotation,
|
||||
public boolean handle(org.eclipse.jdt.internal.compiler.ast.Annotation annotation,
|
||||
final Node annotationNode) {
|
||||
Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
|
||||
|
||||
@@ -94,7 +94,7 @@ public class HandlerLibrary {
|
||||
});
|
||||
}
|
||||
|
||||
handler.handle(new AnnotationValues<T>(annotationClass, values, annotationNode), annotation, annotationNode);
|
||||
return handler.handle(new AnnotationValues<T>(annotationClass, values, annotationNode), annotation, annotationNode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,26 +169,29 @@ public class HandlerLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
public void handle(CompilationUnitDeclaration ast, EclipseAST.Node annotationNode,
|
||||
public boolean handle(CompilationUnitDeclaration ast, EclipseAST.Node annotationNode,
|
||||
org.eclipse.jdt.internal.compiler.ast.Annotation annotation) {
|
||||
String pkgName = annotationNode.getPackageDeclaration();
|
||||
Collection<String> imports = annotationNode.getImportStatements();
|
||||
|
||||
TypeResolver resolver = new TypeResolver(typeLibrary, pkgName, imports);
|
||||
TypeReference rawType = annotation.type;
|
||||
if ( rawType == null ) return;
|
||||
if ( rawType == null ) return false;
|
||||
boolean handled = false;
|
||||
for ( String fqn : resolver.findTypeMatches(annotationNode, toQualifiedName(annotation.type.getTypeName())) ) {
|
||||
AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn);
|
||||
if ( container == null ) continue;
|
||||
|
||||
try {
|
||||
container.handle(annotation, annotationNode);
|
||||
handled |= container.handle(annotation, annotationNode);
|
||||
} catch ( AnnotationValueDecodeFail fail ) {
|
||||
fail.owner.setError(fail.getMessage(), fail.idx);
|
||||
} catch ( Throwable t ) {
|
||||
Eclipse.error(String.format("Lombok annotation handler %s failed", container.handler.getClass()), t);
|
||||
}
|
||||
}
|
||||
|
||||
return handled;
|
||||
}
|
||||
|
||||
public void callASTVisitors(EclipseAST ast) {
|
||||
|
||||
@@ -115,32 +115,37 @@ public class TransformEclipseAST {
|
||||
private static class AnnotationVisitor extends EclipseASTAdapter {
|
||||
@Override public void visitAnnotationOnField(FieldDeclaration field, Node annotationNode, Annotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handle((CompilationUnitDeclaration) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
|
||||
boolean handled = handlers.handle(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, Node annotationNode, Annotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handle((CompilationUnitDeclaration) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
|
||||
boolean handled = handlers.handle(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnLocal(LocalDeclaration local, Node annotationNode, Annotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handle((CompilationUnitDeclaration) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
|
||||
boolean handled = handlers.handle(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnMethod(AbstractMethodDeclaration method, Node annotationNode, Annotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handle((CompilationUnitDeclaration) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
|
||||
boolean handled = handlers.handle(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnType(TypeDeclaration type, Node annotationNode, Annotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handle((CompilationUnitDeclaration) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
|
||||
boolean handled = handlers.handle(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,8 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
annotationNode.addWarning(String.format("Not generating %s(): A method with that name already exists", methodName));
|
||||
}
|
||||
|
||||
@Override public void handle(AnnotationValues<Getter> annotation, Annotation ast, Node annotationNode) {
|
||||
if ( !(annotationNode.up().get() instanceof FieldDeclaration) ) return;
|
||||
@Override public boolean handle(AnnotationValues<Getter> annotation, Annotation ast, Node annotationNode) {
|
||||
if ( !(annotationNode.up().get() instanceof FieldDeclaration) ) return false;
|
||||
FieldDeclaration field = (FieldDeclaration) annotationNode.up().get();
|
||||
TypeReference fieldType = field.type;
|
||||
String getterName = TransformationsUtil.toGetterName(
|
||||
@@ -40,7 +40,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
if ( parent.methods != null ) for ( AbstractMethodDeclaration method : parent.methods ) {
|
||||
if ( method.selector != null && new String(method.selector).equals(getterName) ) {
|
||||
generateDuplicateGetterWarning(annotationNode, getterName);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
newArray[parent.methods.length] = method;
|
||||
parent.methods = newArray;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private int toModifier(AccessLevel value) {
|
||||
|
||||
@@ -11,7 +11,9 @@ import lombok.eclipse.EclipseAST.Node;
|
||||
|
||||
@ProviderFor(EclipseAnnotationHandler.class)
|
||||
public class HandlePrintAST implements EclipseAnnotationHandler<PrintAST> {
|
||||
@Override public void handle(AnnotationValues<PrintAST> annotation, Annotation ast, Node annotationNode) {
|
||||
@Override public boolean handle(AnnotationValues<PrintAST> annotation, Annotation ast, Node annotationNode) {
|
||||
if ( !annotationNode.isCompleteParse() ) return false;
|
||||
annotationNode.up().traverse(new EclipseASTVisitor.EclipseASTPrinter());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -67,7 +67,7 @@ public class HandlerLibrary {
|
||||
} else return null;
|
||||
}
|
||||
|
||||
public void handle(final JavacAST.Node node) {
|
||||
public boolean handle(final JavacAST.Node node) {
|
||||
Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
|
||||
JCAnnotation anno = (JCAnnotation) node.get();
|
||||
List<JCExpression> arguments = anno.getArguments();
|
||||
@@ -104,7 +104,7 @@ public class HandlerLibrary {
|
||||
});
|
||||
}
|
||||
|
||||
handler.handle(new AnnotationValues<T>(annotationClass, values, node), (JCAnnotation)node.get(), node);
|
||||
return handler.handle(new AnnotationValues<T>(annotationClass, values, node), (JCAnnotation)node.get(), node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,21 +168,24 @@ public class HandlerLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
public void handleAnnotation(JCCompilationUnit unit, JavacAST.Node node, JCAnnotation annotation) {
|
||||
public boolean handleAnnotation(JCCompilationUnit unit, JavacAST.Node node, JCAnnotation annotation) {
|
||||
TypeResolver resolver = new TypeResolver(typeLibrary, node.getPackageDeclaration(), node.getImportStatements());
|
||||
String rawType = annotation.annotationType.toString();
|
||||
boolean handled = false;
|
||||
for ( String fqn : resolver.findTypeMatches(node, rawType) ) {
|
||||
AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn);
|
||||
if ( container == null ) continue;
|
||||
|
||||
try {
|
||||
container.handle(node);
|
||||
handled |= container.handle(node);
|
||||
} catch ( AnnotationValueDecodeFail fail ) {
|
||||
fail.owner.setError(fail.getMessage(), fail.idx);
|
||||
} catch ( Throwable t ) {
|
||||
javacError(String.format("Lombok annotation handler %s failed", container.handler.getClass()), t);
|
||||
}
|
||||
}
|
||||
|
||||
return handled;
|
||||
}
|
||||
|
||||
public void callASTVisitors(JavacAST ast) {
|
||||
|
||||
@@ -7,5 +7,5 @@ import lombok.core.AnnotationValues;
|
||||
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
|
||||
|
||||
public interface JavacAnnotationHandler<T extends Annotation> {
|
||||
void handle(AnnotationValues<T> annotation, JCAnnotation ast, JavacAST.Node annotationNode);
|
||||
boolean handle(AnnotationValues<T> annotation, JCAnnotation ast, JavacAST.Node annotationNode);
|
||||
}
|
||||
@@ -66,32 +66,37 @@ public class Processor extends AbstractProcessor {
|
||||
private class AnnotationVisitor extends JavacASTAdapter {
|
||||
@Override public void visitAnnotationOnType(JCClassDecl type, Node annotationNode, JCAnnotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handleAnnotation((JCCompilationUnit) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnField(JCVariableDecl field, Node annotationNode, JCAnnotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handleAnnotation((JCCompilationUnit) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnMethod(JCMethodDecl method, Node annotationNode, JCAnnotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handleAnnotation((JCCompilationUnit) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnMethodArgument(JCVariableDecl argument, JCMethodDecl method, Node annotationNode, JCAnnotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handleAnnotation((JCCompilationUnit) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnLocal(JCVariableDecl local, Node annotationNode, JCAnnotation annotation) {
|
||||
if ( annotationNode.isHandled() ) return;
|
||||
handlers.handleAnnotation((JCCompilationUnit) annotationNode.top().get(), annotationNode, annotation);
|
||||
annotationNode.setHandled();
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if ( handled ) annotationNode.setHandled();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,10 +24,10 @@ import com.sun.tools.javac.util.Name;
|
||||
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
public class HandleGetter implements JavacAnnotationHandler<Getter> {
|
||||
@Override public void handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacAST.Node annotationNode) {
|
||||
@Override public boolean handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacAST.Node annotationNode) {
|
||||
if ( annotationNode.up().getKind() != Kind.FIELD ) {
|
||||
annotationNode.addError("@Getter is only supported on a field.");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
String methodName = toGetterName((JCVariableDecl) annotationNode.up().get());
|
||||
@@ -35,7 +35,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
|
||||
if ( methodExists(methodName, annotationNode.up()) ) {
|
||||
annotationNode.addWarning(
|
||||
String.format("Not generating %s(): A method with that name already exists", methodName));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
Getter getter = annotation.getInstance();
|
||||
@@ -46,6 +46,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
|
||||
|
||||
JCMethodDecl getterMethod = createGetter(access, annotationNode.up(), annotationNode.getTreeMaker());
|
||||
javacClassTree.defs = javacClassTree.defs.append(getterMethod);
|
||||
return true;
|
||||
}
|
||||
|
||||
private JCMethodDecl createGetter(int access, JavacAST.Node field, TreeMaker treeMaker) {
|
||||
|
||||
@@ -12,7 +12,8 @@ import lombok.javac.JavacAST.Node;
|
||||
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
public class HandlePrintAST implements JavacAnnotationHandler<PrintAST> {
|
||||
@Override public void handle(AnnotationValues<PrintAST> annotation, JCAnnotation ast, Node annotationNode) {
|
||||
@Override public boolean handle(AnnotationValues<PrintAST> annotation, JCAnnotation ast, Node annotationNode) {
|
||||
annotationNode.up().traverse(new JavacASTVisitor.JavacASTPrinter());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -26,10 +26,10 @@ import com.sun.tools.javac.util.Name;
|
||||
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
@Override public void handle(AnnotationValues<Setter> annotation, JCAnnotation ast, Node annotationNode) {
|
||||
@Override public boolean handle(AnnotationValues<Setter> annotation, JCAnnotation ast, Node annotationNode) {
|
||||
if ( annotationNode.up().getKind() != Kind.FIELD ) {
|
||||
annotationNode.addError("@Setter is only supported on a field.");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
JCVariableDecl fieldNode = (JCVariableDecl) annotationNode.up().get();
|
||||
@@ -39,7 +39,7 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
annotationNode.addWarning(
|
||||
String.format("Not generating %s(%s %s): A method with that name already exists",
|
||||
methodName, fieldNode.vartype, fieldNode.name));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
Setter setter = annotation.getInstance();
|
||||
@@ -50,6 +50,7 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
|
||||
JCMethodDecl setterMethod = createSetter(access, annotationNode.up(), annotationNode.getTreeMaker());
|
||||
javacClassTree.defs = javacClassTree.defs.append(setterMethod);
|
||||
return true;
|
||||
}
|
||||
|
||||
private JCMethodDecl createSetter(int access, JavacAST.Node field, TreeMaker treeMaker) {
|
||||
|
||||
Reference in new issue
Block a user