mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 02:17:12 +00:00
Experiment to try and make java 'self referential' by deleting the Enter and MemberEnter state. So far it doesn't work yet.
This commit is contained in:
1 parent
04d041a7dc
commit
0951ea38fe
20 files changed
+130
-31
No files matched your search
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2009-2010 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
|
||||
* Copyright © 2010 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -51,7 +51,7 @@ public class HandlerLibrary {
|
||||
private final Map<String, AnnotationHandlerContainer<?>> annotationHandlers = new HashMap<String, AnnotationHandlerContainer<?>>();
|
||||
private final Collection<JavacASTVisitor> visitorHandlers = new ArrayList<JavacASTVisitor>();
|
||||
private final Messager messager;
|
||||
private boolean skipPrintAST = true;
|
||||
private int phase = 0;
|
||||
|
||||
/**
|
||||
* Creates a new HandlerLibrary that will report any problems or errors to the provided messager.
|
||||
@@ -70,6 +70,10 @@ public class HandlerLibrary {
|
||||
this.annotationClass = annotationClass;
|
||||
}
|
||||
|
||||
public boolean isResolutionBased() {
|
||||
return handler.isResolutionBased();
|
||||
}
|
||||
|
||||
public boolean handle(final JavacNode node) {
|
||||
return handler.handle(Javac.createAnnotation(annotationClass, node), (JCAnnotation)node.get(), node);
|
||||
}
|
||||
@@ -160,12 +164,14 @@ public class HandlerLibrary {
|
||||
boolean handled = false;
|
||||
for (String fqn : resolver.findTypeMatches(node, rawType)) {
|
||||
boolean isPrintAST = fqn.equals(PrintAST.class.getName());
|
||||
if (isPrintAST == skipPrintAST) continue;
|
||||
if (isPrintAST && phase != 2) continue;
|
||||
if (!isPrintAST && phase == 2) continue;
|
||||
AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn);
|
||||
if (container == null) continue;
|
||||
|
||||
try {
|
||||
handled |= container.handle(node);
|
||||
if (container.isResolutionBased() && phase == 1) handled |= container.handle(node);
|
||||
if (!container.isResolutionBased() && phase == 0) handled |= container.handle(node);
|
||||
} catch (AnnotationValueDecodeFail fail) {
|
||||
fail.owner.setError(fail.getMessage(), fail.idx);
|
||||
} catch (Throwable t) {
|
||||
@@ -183,13 +189,8 @@ public class HandlerLibrary {
|
||||
*/
|
||||
public void callASTVisitors(JavacAST ast) {
|
||||
for (JavacASTVisitor visitor : visitorHandlers) try {
|
||||
if (!visitor.isResolutionBased()) ast.traverse(visitor);
|
||||
} catch (Throwable t) {
|
||||
javacError(String.format("Lombok visitor handler %s failed", visitor.getClass()), t);
|
||||
}
|
||||
|
||||
for (JavacASTVisitor visitor : visitorHandlers) try {
|
||||
if (visitor.isResolutionBased()) ast.traverse(visitor);
|
||||
if (!visitor.isResolutionBased() && phase == 0) ast.traverse(visitor);
|
||||
if (visitor.isResolutionBased() && phase == 1) ast.traverse(visitor);
|
||||
} catch (Throwable t) {
|
||||
javacError(String.format("Lombok visitor handler %s failed", visitor.getClass()), t);
|
||||
}
|
||||
@@ -197,17 +198,17 @@ public class HandlerLibrary {
|
||||
|
||||
/**
|
||||
* Lombok does not currently support triggering annotations in a specified order; the order is essentially
|
||||
* random right now. This lack of order is particularly annoying for the {@code PrintAST} annotation,
|
||||
* which is almost always intended to run last. Hence, this hack, which lets it in fact run last.
|
||||
*
|
||||
* @see #skipAllButPrintAST()
|
||||
* random right now. As a temporary hack we've identified 3 important phases.
|
||||
*/
|
||||
public void skipPrintAST() {
|
||||
skipPrintAST = true;
|
||||
public void setPreResolutionPhase() {
|
||||
phase = 0;
|
||||
}
|
||||
|
||||
/** @see #skipPrintAST() */
|
||||
public void skipAllButPrintAST() {
|
||||
skipPrintAST = false;
|
||||
public void setPostResolutionPhase() {
|
||||
phase = 1;
|
||||
}
|
||||
|
||||
public void setPrintASTPhase() {
|
||||
phase = 2;
|
||||
}
|
||||
}
|
||||
@@ -55,4 +55,9 @@ public interface JavacAnnotationHandler<T extends Annotation> {
|
||||
* compile session (you've handled it), or {@code false} to indicate you aren't done yet.
|
||||
*/
|
||||
boolean handle(AnnotationValues<T> annotation, JCAnnotation ast, JavacNode annotationNode);
|
||||
|
||||
/**
|
||||
* Return true if this handler requires resolution.
|
||||
*/
|
||||
boolean isResolutionBased();
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import javax.lang.model.type.TypeKind;
|
||||
import javax.tools.DiagnosticListener;
|
||||
|
||||
import com.sun.tools.javac.code.BoundKind;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
import com.sun.tools.javac.code.Symbol.TypeSymbol;
|
||||
import com.sun.tools.javac.code.Symtab;
|
||||
import com.sun.tools.javac.code.Type.ArrayType;
|
||||
|
||||
@@ -22,16 +22,18 @@
|
||||
package lombok.javac;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.processing.Messager;
|
||||
|
||||
import com.sun.tools.javac.comp.Enter;
|
||||
import com.sun.tools.javac.comp.MemberEnter;
|
||||
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
|
||||
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
import com.sun.tools.javac.util.List;
|
||||
|
||||
public class JavacTransformer {
|
||||
private final HandlerLibrary handlers;
|
||||
@@ -42,18 +44,39 @@ public class JavacTransformer {
|
||||
this.handlers = HandlerLibrary.load(messager);
|
||||
}
|
||||
|
||||
public boolean transform(Context context, Iterable<JCCompilationUnit> compilationUnits) {
|
||||
List<JavacAST> asts = new ArrayList<JavacAST>();
|
||||
public boolean transform(Context context, java.util.List<JCCompilationUnit> compilationUnitsRaw) {
|
||||
List<JCCompilationUnit> compilationUnits;
|
||||
if (compilationUnitsRaw instanceof List<?>) {
|
||||
compilationUnits = (List<JCCompilationUnit>)compilationUnitsRaw;
|
||||
} else {
|
||||
compilationUnits = List.nil();
|
||||
for (int i = compilationUnitsRaw.size() -1; i >= 0; i--) {
|
||||
compilationUnits = compilationUnits.prepend(compilationUnitsRaw.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
java.util.List<JavacAST> asts = new ArrayList<JavacAST>();
|
||||
|
||||
for (JCCompilationUnit unit : compilationUnits) asts.add(new JavacAST(messager, context, unit));
|
||||
|
||||
handlers.skipPrintAST();
|
||||
handlers.setPreResolutionPhase();
|
||||
for (JavacAST ast : asts) {
|
||||
ast.traverse(new AnnotationVisitor());
|
||||
handlers.callASTVisitors(ast);
|
||||
}
|
||||
|
||||
handlers.skipAllButPrintAST();
|
||||
context.put(Enter.class, (Enter) null);
|
||||
context.put(MemberEnter.class, (MemberEnter) null);
|
||||
Enter.instance(context).main(compilationUnits);
|
||||
|
||||
handlers.setPostResolutionPhase();
|
||||
for (JavacAST ast : asts) {
|
||||
ast.traverse(new AnnotationVisitor());
|
||||
handlers.callASTVisitors(ast);
|
||||
}
|
||||
|
||||
|
||||
handlers.setPrintASTPhase();
|
||||
for (JavacAST ast : asts) {
|
||||
ast.traverse(new AnnotationVisitor());
|
||||
}
|
||||
@@ -61,6 +84,7 @@ public class JavacTransformer {
|
||||
for (JavacAST ast : asts) {
|
||||
if (ast.isChanged()) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
@@ -172,7 +173,7 @@ public class Processor extends AbstractProcessor {
|
||||
if (unit != null) units.put(unit, null);
|
||||
}
|
||||
|
||||
transformer.transform(processingEnv.getContext(), units.keySet());
|
||||
transformer.transform(processingEnv.getContext(), new ArrayList<JCCompilationUnit>(units.keySet()));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2009 Reinier Zwitserloot and Roel Spilker.
|
||||
* Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -154,4 +154,8 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,10 @@ public class HandleConstructor {
|
||||
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, false, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
@@ -86,6 +90,10 @@ public class HandleConstructor {
|
||||
new HandleConstructor().generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, false, suppressConstructorProperties);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static List<JavacNode> findRequiredFields(JavacNode typeNode) {
|
||||
@@ -131,6 +139,10 @@ public class HandleConstructor {
|
||||
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, false, suppressConstructorProperties);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void generateRequiredArgsConstructor(JavacNode typeNode, AccessLevel level, String staticName, boolean skipIfConstructorExists) {
|
||||
|
||||
@@ -63,4 +63,8 @@ public class HandleData implements JavacAnnotationHandler<Data> {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -479,4 +479,8 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
|
||||
private JCStatement returnBool(TreeMaker maker, boolean bool) {
|
||||
return maker.Return(maker.Literal(TypeTags.BOOLEAN, bool ? 1 : 0));
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -202,4 +202,8 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
|
||||
return treeMaker.MethodDef(treeMaker.Modifiers(access, nonNulls.appendList(nullables)), methodName, methodType,
|
||||
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -121,6 +121,10 @@ public class HandleLog {
|
||||
@Override public boolean handle(AnnotationValues<lombok.extern.apachecommons.Log> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
return processAnnotation(LoggingFramework.COMMONS, annotation, annotationNode);
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,7 +135,11 @@ public class HandleLog {
|
||||
@Override public boolean handle(AnnotationValues<lombok.extern.jul.Log> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
return processAnnotation(LoggingFramework.JUL, annotation, annotationNode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the {@link lombok.extern.log4j.Log} annotation for javac.
|
||||
@@ -141,6 +149,10 @@ public class HandleLog {
|
||||
@Override public boolean handle(AnnotationValues<lombok.extern.log4j.Log> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
return processAnnotation(LoggingFramework.LOG4J, annotation, annotationNode);
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,6 +163,10 @@ public class HandleLog {
|
||||
@Override public boolean handle(AnnotationValues<lombok.extern.slf4j.Log> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
return processAnnotation(LoggingFramework.SLF4J, annotation, annotationNode);
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
enum LoggingFramework {
|
||||
|
||||
@@ -54,4 +54,8 @@ public class HandlePrintAST implements JavacAnnotationHandler<PrintAST> {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -238,4 +238,8 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
return v.visitNoType(this, p);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -110,4 +110,8 @@ public class HandleSneakyThrows implements JavacAnnotationHandler<SneakyThrows>
|
||||
|
||||
return maker.Try(tryBlock, List.of(maker.Catch(catchParam, catchBody)), null);
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -107,4 +107,8 @@ public class HandleSynchronized implements JavacAnnotationHandler<Synchronized>
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -247,4 +247,8 @@ public class HandleToString implements JavacAnnotationHandler<ToString> {
|
||||
}
|
||||
return typeName;
|
||||
}
|
||||
|
||||
@Override public boolean isResolutionBased() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2010 Reinier Zwitserloot and Roel Spilker.
|
||||
* Copyright © 2010 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -31,8 +31,10 @@ import org.mangosdk.spi.ProviderFor;
|
||||
import com.sun.tools.javac.code.Flags;
|
||||
import com.sun.tools.javac.code.Type;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;
|
||||
import com.sun.tools.javac.tree.JCTree.JCExpression;
|
||||
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCNewArray;
|
||||
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
|
||||
|
||||
|
||||
@@ -383,7 +383,8 @@ public class Delombok {
|
||||
compiler.enterTrees(toJavacList(roots));
|
||||
|
||||
for (JCCompilationUnit unit : roots) {
|
||||
boolean changed = new JavacTransformer(messager).transform(context, Collections.singleton(unit));
|
||||
// Run one single massive transform instead of a lot of singleton calls, as this causes a heck of a lot of refilling of the enter cache.
|
||||
boolean changed = new JavacTransformer(messager).transform(context, Collections.singletonList(unit));
|
||||
DelombokResult result = new DelombokResult(commentsMap.get(unit).comments, unit, force || changed);
|
||||
if (verbose) feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged");
|
||||
Writer rawWriter;
|
||||
|
||||
@@ -69,7 +69,7 @@ public class NetbeansEntryPoint implements TaskListener {
|
||||
if (TaskEvent.Kind.PARSE == event.getKind()) {
|
||||
JavacTransformer transformer = new JavacTransformer(new DummyMessager()); //TODO hook into netbeans error reporting!
|
||||
JCCompilationUnit compilationUnit = (JCCompilationUnit) event.getCompilationUnit();
|
||||
transformer.transform(context, Collections.singleton(compilationUnit));
|
||||
transformer.transform(context, Collections.singletonList(compilationUnit));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user