reduced calls of traverse invocations

This commit is contained in:
Roland Praml authored and Roel Spilker committed 2018-08-20 20:46:31 +02:00
1 parent 2b0cd53d55
commit 100cff228d
2 files changed
+34 -12

No files matched your search

+12 -6
View File
@@ -32,6 +32,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
@@ -212,21 +213,25 @@ public class HandlerLibrary {
* @param ast The Compilation Unit that contains the Annotation AST Node.
* @param annotationNode The Lombok AST Node representing the Annotation AST Node.
* @param annotation 'node.get()' - convenience parameter.
* @param priority current prioritiy
* @return the priority we want to run - MAX_VALUE means never
*/
public void handleAnnotation(CompilationUnitDeclaration ast, EclipseNode annotationNode, org.eclipse.jdt.internal.compiler.ast.Annotation annotation, long priority) {
public long handleAnnotation(CompilationUnitDeclaration ast, EclipseNode annotationNode, org.eclipse.jdt.internal.compiler.ast.Annotation annotation, long priority) {
TypeResolver resolver = new TypeResolver(annotationNode.getImportList());
TypeReference rawType = annotation.type;
if (rawType == null) return;
if (rawType == null) return Long.MAX_VALUE;
String fqn = resolver.typeRefToFullyQualifiedName(annotationNode, typeLibrary, toQualifiedName(annotation.type.getTypeName()));
if (fqn == null) return;
if (fqn == null) return Long.MAX_VALUE;
AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn);
if (container == null) return;
if (priority != container.getPriority()) return;
if (container == null) return Long.MAX_VALUE;
if (priority < container.getPriority()) return container.getPriority(); // we want to run at this priority
if (priority > container.getPriority()) return Long.MAX_VALUE; // it's over- we do not want to run again
if (!annotationNode.isCompleteParse() && container.deferUntilPostDiet()) {
if (needsHandling(annotation)) container.preHandle(annotation, annotationNode);
return;
return Long.MAX_VALUE;
}
try {
@@ -236,6 +241,7 @@ public class HandlerLibrary {
} catch (Throwable t) {
error(ast, String.format("Lombok annotation handler %s failed", container.handler.getClass()), t);
}
return Long.MAX_VALUE;
}
/**
@@ -24,6 +24,8 @@ package lombok.eclipse;
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import lombok.ConfigurationKeys;
import lombok.core.LombokConfiguration;
@@ -186,42 +188,56 @@ public class TransformEclipseAST {
* then handles any PrintASTs.
*/
public void go() {
long nextPriority = Long.MIN_VALUE;
for (Long d : handlers.getPriorities()) {
ast.traverse(new AnnotationVisitor(d));
if (nextPriority > d) {
continue;
}
AnnotationVisitor visitor = new AnnotationVisitor(d);
ast.traverse(visitor);
// if no visitor interested for this AST, nextPriority would be MAX_VALUE and we bail out immediatetly
nextPriority = visitor.getNextPriority();
handlers.callASTVisitors(ast, d, ast.isCompleteParse());
}
}
private static class AnnotationVisitor extends EclipseASTAdapter {
private final long priority;
// this is the next priority we continue to visit.
// Long.MAX_VALUE means never. Each visit method will potentially reduce the next priority
private long nextPriority = Long.MAX_VALUE;
public AnnotationVisitor(long priority) {
this.priority = priority;
}
public long getNextPriority() {
return nextPriority;
}
@Override public void visitAnnotationOnField(FieldDeclaration field, EclipseNode annotationNode, Annotation annotation) {
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
handlers.handleAnnotation(top, annotationNode, annotation, priority);
nextPriority = Math.min(nextPriority, handlers.handleAnnotation(top, annotationNode, annotation, priority));
}
@Override public void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, EclipseNode annotationNode, Annotation annotation) {
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
handlers.handleAnnotation(top, annotationNode, annotation, priority);
nextPriority = Math.min(nextPriority, handlers.handleAnnotation(top, annotationNode, annotation, priority));
}
@Override public void visitAnnotationOnLocal(LocalDeclaration local, EclipseNode annotationNode, Annotation annotation) {
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
handlers.handleAnnotation(top, annotationNode, annotation, priority);
nextPriority = Math.min(nextPriority, handlers.handleAnnotation(top, annotationNode, annotation, priority));
}
@Override public void visitAnnotationOnMethod(AbstractMethodDeclaration method, EclipseNode annotationNode, Annotation annotation) {
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
handlers.handleAnnotation(top, annotationNode, annotation, priority);
nextPriority = Math.min(nextPriority, handlers.handleAnnotation(top, annotationNode, annotation, priority));
}
@Override public void visitAnnotationOnType(TypeDeclaration type, EclipseNode annotationNode, Annotation annotation) {
CompilationUnitDeclaration top = (CompilationUnitDeclaration) annotationNode.top().get();
handlers.handleAnnotation(top, annotationNode, annotation, priority);
nextPriority = Math.min(nextPriority, handlers.handleAnnotation(top, annotationNode, annotation, priority));
}
}
}