mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 02:16:18 +00:00
Merge branch 'delombok'
This commit is contained in:
commit
9678c58ec8
36 files changed
+2398
-68
No files matched your search
+6
-1
@@ -3,15 +3,20 @@
|
||||
<classpathentry kind="src" path="src/core"/>
|
||||
<classpathentry kind="src" path="src/eclipseAgent"/>
|
||||
<classpathentry kind="src" path="src/installer"/>
|
||||
<classpathentry kind="src" path="src/delombok"/>
|
||||
<classpathentry kind="src" path="experimental/src"/>
|
||||
<classpathentry kind="src" path="test/delombok/src"/>
|
||||
<classpathentry kind="src" path="test/core/src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="lib" path="deps/lombok/org.eclipse.jdt.core_3.5.0.v_963.jar"/>
|
||||
<classpathentry kind="lib" path="deps/lombok/org.eclipse.jdt.ui_3.5.1.r351_v20090821-0800.jar"/>
|
||||
<classpathentry kind="lib" path="deps/lombok/org.eclipse.core.runtime_3.5.0.v20090525.jar"/>
|
||||
<classpathentry kind="lib" path="deps/lombok/org.eclipse.osgi_3.5.0.v20090520.jar"/>
|
||||
<classpathentry kind="lib" path="deps/lombok/org.eclipse.equinox.common_3.5.0.v20090520-1800.jar"/>
|
||||
<classpathentry kind="lib" path="deps/junit-4.7.jar"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/lombok.patcher"/>
|
||||
<classpathentry kind="lib" path="deps/lombok/tools.jar"/>
|
||||
<classpathentry kind="lib" path="deps/lombok/tools.jar" sourcepath="C:/javasource/javasources.zip"/>
|
||||
<classpathentry kind="lib" path="deps/lombok/spi-0.2.4.jar"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/com.zwitserloot.cmdreader"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -83,6 +83,7 @@ lombok code including the various agents.
|
||||
<sourcePath path="src/core" />
|
||||
<sourcePath path="src/installer" />
|
||||
<sourcePath path="src/eclipseAgent" />
|
||||
<sourcePath path="src/delombok" />
|
||||
<class location="build/lombok/lombok" />
|
||||
</findbugs>
|
||||
</target>
|
||||
@@ -111,6 +112,7 @@ lombok code including the various agents.
|
||||
<src path="src/core" />
|
||||
<src path="src/installer" />
|
||||
<src path="src/eclipseAgent" />
|
||||
<src path="src/delombok" />
|
||||
<exclude name="lombok/javac/**" />
|
||||
<classpath refid="deps.path" />
|
||||
<classpath refid="libs.path" />
|
||||
@@ -119,6 +121,7 @@ lombok code including the various agents.
|
||||
<src path="src/core" />
|
||||
<src path="src/installer" />
|
||||
<src path="src/eclipseAgent" />
|
||||
<src path="src/delombok" />
|
||||
<include name="lombok/javac/**" />
|
||||
<classpath refid="deps.path" />
|
||||
<classpath refid="libs.path" />
|
||||
@@ -133,6 +136,9 @@ lombok code including the various agents.
|
||||
<fileset dir="src/eclipseAgent">
|
||||
<patternset refid="copyables" />
|
||||
</fileset>
|
||||
<fileset dir="src/delombok">
|
||||
<patternset refid="copyables" />
|
||||
</fileset>
|
||||
</copy>
|
||||
|
||||
<mkdir dir="build/lombok/META-INF" />
|
||||
|
||||
Vendored
BIN
Binary file not shown.
Binary file not shown.
@@ -54,11 +54,24 @@ public abstract class AST<A extends AST<A, L, N>, L extends LombokNode<A, L, N>,
|
||||
private final String fileName;
|
||||
Map<N, Void> identityDetector = new IdentityHashMap<N, Void>();
|
||||
private Map<N, L> nodeMap = new IdentityHashMap<N, L>();
|
||||
private boolean changed = false;
|
||||
|
||||
protected AST(String fileName) {
|
||||
this.fileName = fileName == null ? "(unknown).java" : fileName;
|
||||
}
|
||||
|
||||
protected void setChanged() {
|
||||
this.changed = true;
|
||||
}
|
||||
|
||||
protected void clearChanged() {
|
||||
this.changed = false;
|
||||
}
|
||||
|
||||
public boolean isChanged() {
|
||||
return changed;
|
||||
}
|
||||
|
||||
/** Set the node object that wraps the internal Compilation Unit node. */
|
||||
protected void setTop(L top) {
|
||||
this.top = top;
|
||||
|
||||
@@ -126,6 +126,7 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A,
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public L replaceWith(N newN, Kind newNodeKind) {
|
||||
ast.setChanged();
|
||||
L newNode = ast.buildTree(newN, newNodeKind);
|
||||
newNode.parent = parent;
|
||||
for (int i = 0; i < parent.children.size(); i++) {
|
||||
@@ -142,6 +143,7 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A,
|
||||
* Also affects the underlying (Eclipse/javac) AST.
|
||||
*/
|
||||
public void replaceChildNode(N oldN, N newN) {
|
||||
ast.setChanged();
|
||||
ast.replaceStatementInNode(get(), oldN, newN);
|
||||
}
|
||||
|
||||
@@ -228,6 +230,7 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A,
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public L add(N newChild, Kind newChildKind) {
|
||||
ast.setChanged();
|
||||
L n = ast.buildTree(newChild, newChildKind);
|
||||
if (n == null) return null;
|
||||
n.parent = (L) this;
|
||||
@@ -247,6 +250,8 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A,
|
||||
|
||||
L newNode = ast.buildTree(get(), kind);
|
||||
|
||||
ast.setChanged();
|
||||
|
||||
ast.replaceNewWithExistingOld(oldNodes, newNode);
|
||||
}
|
||||
|
||||
@@ -265,6 +270,7 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A,
|
||||
* Does not change the underlying (javac/Eclipse) AST, only the wrapped view.
|
||||
*/
|
||||
public void removeChild(L child) {
|
||||
ast.setChanged();
|
||||
children.remove(child);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ public class EclipseAST extends AST<EclipseAST, EclipseNode, ASTNode> {
|
||||
this.compilationUnitDeclaration = ast;
|
||||
setTop(buildCompilationUnit(ast));
|
||||
this.completeParse = isComplete(ast);
|
||||
clearChanged();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@@ -201,12 +202,14 @@ public class EclipseAST extends AST<EclipseAST, EclipseNode, ASTNode> {
|
||||
public void reparse() {
|
||||
propagateProblems();
|
||||
if (completeParse) return;
|
||||
boolean changed = isChanged();
|
||||
boolean newCompleteParse = isComplete(compilationUnitDeclaration);
|
||||
if (!newCompleteParse) return;
|
||||
|
||||
top().rebuild();
|
||||
|
||||
this.completeParse = true;
|
||||
if (!changed) clearChanged();
|
||||
}
|
||||
|
||||
private static boolean isComplete(CompilationUnitDeclaration unit) {
|
||||
|
||||
@@ -32,7 +32,6 @@ import javax.tools.JavaFileObject;
|
||||
|
||||
import lombok.core.AST;
|
||||
|
||||
import com.sun.source.util.Trees;
|
||||
import com.sun.tools.javac.code.Symtab;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.TreeMaker;
|
||||
@@ -66,13 +65,11 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> {
|
||||
/**
|
||||
* Creates a new JavacAST of the provided Compilation Unit.
|
||||
*
|
||||
* @param trees The trees instance to use to inspect the compilation unit. Generate via:
|
||||
* {@code Trees.getInstance(env)}
|
||||
* @param messager A Messager for warning and error reporting.
|
||||
* @param context A Context object for interfacing with the compiler.
|
||||
* @param top The compilation unit, which serves as the top level node in the tree to be built.
|
||||
*/
|
||||
public JavacAST(Trees trees, Messager messager, Context context, JCCompilationUnit top) {
|
||||
public JavacAST(Messager messager, Context context, JCCompilationUnit top) {
|
||||
super(top.sourcefile == null ? null : top.sourcefile.toString());
|
||||
setTop(buildCompilationUnit(top));
|
||||
this.context = context;
|
||||
@@ -81,6 +78,7 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> {
|
||||
this.nameTable = Name.Table.instance(context);
|
||||
this.treeMaker = TreeMaker.instance(context);
|
||||
this.symtab = Symtab.instance(context);
|
||||
clearChanged();
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright © 2009 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package lombok.javac;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.processing.Messager;
|
||||
|
||||
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;
|
||||
|
||||
public class JavacTransformer {
|
||||
private final HandlerLibrary handlers;
|
||||
private final Messager messager;
|
||||
|
||||
public JavacTransformer(Messager messager) {
|
||||
this.messager = messager;
|
||||
this.handlers = HandlerLibrary.load(messager);
|
||||
}
|
||||
|
||||
public boolean transform(Context context, Iterable<JCCompilationUnit> compilationUnits) {
|
||||
List<JavacAST> asts = new ArrayList<JavacAST>();
|
||||
|
||||
for (JCCompilationUnit unit : compilationUnits) asts.add(new JavacAST(messager, context, unit));
|
||||
|
||||
handlers.skipPrintAST();
|
||||
for (JavacAST ast : asts) {
|
||||
ast.traverse(new AnnotationVisitor());
|
||||
handlers.callASTVisitors(ast);
|
||||
}
|
||||
|
||||
handlers.skipAllButPrintAST();
|
||||
for (JavacAST ast : asts) {
|
||||
ast.traverse(new AnnotationVisitor());
|
||||
}
|
||||
|
||||
for (JavacAST ast : asts) {
|
||||
if (ast.isChanged()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private class AnnotationVisitor extends JavacASTAdapter {
|
||||
@Override public void visitAnnotationOnType(JCClassDecl type, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if (handled) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnField(JCVariableDecl field, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if (handled) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnMethod(JCMethodDecl method, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
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, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if (handled) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnLocal(JCVariableDecl local, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if (handled) annotationNode.setHandled();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,7 @@
|
||||
*/
|
||||
package lombok.javac.apt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.processing.AbstractProcessor;
|
||||
@@ -36,19 +34,12 @@ import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
|
||||
import lombok.javac.HandlerLibrary;
|
||||
import lombok.javac.JavacAST;
|
||||
import lombok.javac.JavacASTAdapter;
|
||||
import lombok.javac.JavacNode;
|
||||
import lombok.javac.JavacTransformer;
|
||||
|
||||
import com.sun.source.util.TreePath;
|
||||
import com.sun.source.util.Trees;
|
||||
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
|
||||
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;
|
||||
|
||||
|
||||
/**
|
||||
@@ -66,7 +57,7 @@ import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
|
||||
public class Processor extends AbstractProcessor {
|
||||
private ProcessingEnvironment rawProcessingEnv;
|
||||
private JavacProcessingEnvironment processingEnv;
|
||||
private HandlerLibrary handlers;
|
||||
private JavacTransformer transformer;
|
||||
private Trees trees;
|
||||
private String errorToShow;
|
||||
|
||||
@@ -86,7 +77,7 @@ public class Processor extends AbstractProcessor {
|
||||
this.errorToShow = null;
|
||||
} else {
|
||||
this.processingEnv = (JavacProcessingEnvironment) procEnv;
|
||||
handlers = HandlerLibrary.load(procEnv.getMessager());
|
||||
transformer = new JavacTransformer(procEnv.getMessager());
|
||||
trees = Trees.instance(procEnv);
|
||||
this.errorToShow = null;
|
||||
}
|
||||
@@ -111,61 +102,11 @@ public class Processor extends AbstractProcessor {
|
||||
if (unit != null) units.put(unit, null);
|
||||
}
|
||||
|
||||
List<JavacAST> asts = new ArrayList<JavacAST>();
|
||||
transformer.transform(processingEnv.getContext(), units.keySet());
|
||||
|
||||
for (JCCompilationUnit unit : units.keySet()) asts.add(
|
||||
new JavacAST(trees, processingEnv.getMessager(), processingEnv.getContext(), unit));
|
||||
|
||||
handlers.skipPrintAST();
|
||||
for (JavacAST ast : asts) {
|
||||
ast.traverse(new AnnotationVisitor());
|
||||
handlers.callASTVisitors(ast);
|
||||
}
|
||||
|
||||
handlers.skipAllButPrintAST();
|
||||
for (JavacAST ast : asts) {
|
||||
ast.traverse(new AnnotationVisitor());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private class AnnotationVisitor extends JavacASTAdapter {
|
||||
@Override public void visitAnnotationOnType(JCClassDecl type, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if (handled) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnField(JCVariableDecl field, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if (handled) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnMethod(JCMethodDecl method, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
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, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if (handled) annotationNode.setHandled();
|
||||
}
|
||||
|
||||
@Override public void visitAnnotationOnLocal(JCVariableDecl local, JavacNode annotationNode, JCAnnotation annotation) {
|
||||
if (annotationNode.isHandled()) return;
|
||||
JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get();
|
||||
boolean handled = handlers.handleAnnotation(top, annotationNode, annotation);
|
||||
if (handled) annotationNode.setHandled();
|
||||
}
|
||||
}
|
||||
|
||||
private JCCompilationUnit toUnit(Element element) {
|
||||
TreePath path = trees == null ? null : trees.getPath(element);
|
||||
if (path == null) return null;
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
package lombok.javac.handlers;
|
||||
|
||||
import static lombok.javac.handlers.JavacHandlerUtil.markAnnotationAsProcessed;
|
||||
import lombok.Cleanup;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.AST.Kind;
|
||||
@@ -53,6 +54,7 @@ import com.sun.tools.javac.util.Name;
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
|
||||
@Override public boolean handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
markAnnotationAsProcessed(annotationNode, Cleanup.class);
|
||||
String cleanupName = annotation.getInstance().value();
|
||||
if (cleanupName.length() == 0) {
|
||||
annotationNode.addError("cleanupName cannot be the empty string.");
|
||||
|
||||
@@ -59,6 +59,7 @@ import com.sun.tools.javac.util.List;
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
public class HandleData implements JavacAnnotationHandler<Data> {
|
||||
@Override public boolean handle(AnnotationValues<Data> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
markAnnotationAsProcessed(annotationNode, Data.class);
|
||||
JavacNode typeNode = annotationNode.up();
|
||||
JCClassDecl typeDecl = null;
|
||||
if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl)typeNode.get();
|
||||
|
||||
@@ -72,6 +72,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
|
||||
}
|
||||
|
||||
@Override public boolean handle(AnnotationValues<EqualsAndHashCode> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
markAnnotationAsProcessed(annotationNode, EqualsAndHashCode.class);
|
||||
EqualsAndHashCode ann = annotation.getInstance();
|
||||
List<String> excludes = List.from(ann.exclude());
|
||||
List<String> includes = List.from(ann.of());
|
||||
|
||||
@@ -80,8 +80,10 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
|
||||
}
|
||||
|
||||
@Override public boolean handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
markAnnotationAsProcessed(annotationNode, Getter.class);
|
||||
JavacNode fieldNode = annotationNode.up();
|
||||
AccessLevel level = annotation.getInstance().value();
|
||||
|
||||
if (level == AccessLevel.NONE) return true;
|
||||
|
||||
return createGetterForField(level, fieldNode, annotationNode, true);
|
||||
|
||||
@@ -82,8 +82,10 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
}
|
||||
|
||||
@Override public boolean handle(AnnotationValues<Setter> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
markAnnotationAsProcessed(annotationNode, Setter.class);
|
||||
JavacNode fieldNode = annotationNode.up();
|
||||
AccessLevel level = annotation.getInstance().value();
|
||||
|
||||
if (level == AccessLevel.NONE) return true;
|
||||
|
||||
return createSetterForField(level, fieldNode, annotationNode, true);
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
package lombok.javac.handlers;
|
||||
|
||||
import static lombok.javac.handlers.JavacHandlerUtil.chainDots;
|
||||
import static lombok.javac.handlers.JavacHandlerUtil.markAnnotationAsProcessed;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -49,6 +50,7 @@ import com.sun.tools.javac.util.List;
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
public class HandleSneakyThrows implements JavacAnnotationHandler<SneakyThrows> {
|
||||
@Override public boolean handle(AnnotationValues<SneakyThrows> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
markAnnotationAsProcessed(annotationNode, SneakyThrows.class);
|
||||
Collection<String> exceptionNames = annotation.getRawExpressions("value");
|
||||
|
||||
List<JCExpression> memberValuePairs = ast.getArguments();
|
||||
|
||||
@@ -51,10 +51,12 @@ public class HandleSynchronized implements JavacAnnotationHandler<Synchronized>
|
||||
private static final String STATIC_LOCK_NAME = "$LOCK";
|
||||
|
||||
@Override public boolean handle(AnnotationValues<Synchronized> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
markAnnotationAsProcessed(annotationNode, Synchronized.class);
|
||||
JavacNode methodNode = annotationNode.up();
|
||||
|
||||
if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof JCMethodDecl)) {
|
||||
annotationNode.addError("@Synchronized is legal only on methods.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -62,6 +64,7 @@ public class HandleSynchronized implements JavacAnnotationHandler<Synchronized>
|
||||
|
||||
if ((method.mods.flags & Flags.ABSTRACT) != 0) {
|
||||
annotationNode.addError("@Synchronized is legal only on concrete methods.");
|
||||
|
||||
return true;
|
||||
}
|
||||
boolean isStatic = (method.mods.flags & Flags.STATIC) != 0;
|
||||
|
||||
@@ -68,6 +68,8 @@ public class HandleToString implements JavacAnnotationHandler<ToString> {
|
||||
}
|
||||
|
||||
@Override public boolean handle(AnnotationValues<ToString> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
markAnnotationAsProcessed(annotationNode, ToString.class);
|
||||
|
||||
ToString ann = annotation.getInstance();
|
||||
List<String> excludes = List.from(ann.exclude());
|
||||
List<String> includes = List.from(ann.of());
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
package lombok.javac.handlers;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
@@ -34,7 +35,9 @@ import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.TreeMaker;
|
||||
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.JCExpression;
|
||||
import com.sun.tools.javac.tree.JCTree.JCImport;
|
||||
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCStatement;
|
||||
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
|
||||
@@ -57,6 +60,54 @@ public class JavacHandlerUtil {
|
||||
return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(typeName).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the annotation from javac's AST, then removes it from lombok's AST,
|
||||
* then removes any import statement that imports this exact annotation (not star imports).
|
||||
*/
|
||||
public static void markAnnotationAsProcessed(JavacNode annotation, Class<? extends Annotation> annotationType) {
|
||||
JavacNode parentNode = annotation.directUp();
|
||||
switch (parentNode.getKind()) {
|
||||
case FIELD:
|
||||
case ARGUMENT:
|
||||
case LOCAL:
|
||||
JCVariableDecl variable = (JCVariableDecl) parentNode.get();
|
||||
variable.mods.annotations = filterList(variable.mods.annotations, annotation.get());
|
||||
break;
|
||||
case METHOD:
|
||||
JCMethodDecl method = (JCMethodDecl) parentNode.get();
|
||||
method.mods.annotations = filterList(method.mods.annotations, annotation.get());
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("Don't know how to remove annotations from: " + parentNode.getKind());
|
||||
}
|
||||
parentNode.removeChild(annotation);
|
||||
|
||||
JCCompilationUnit unit = (JCCompilationUnit) annotation.top().get();
|
||||
deleteImportFromCompilationUnit(unit, annotationType.getName());
|
||||
}
|
||||
|
||||
private static void deleteImportFromCompilationUnit(JCCompilationUnit unit, String name) {
|
||||
List<JCTree> newDefs = List.nil();
|
||||
|
||||
for (JCTree def : unit.defs) {
|
||||
boolean delete = false;
|
||||
if (def instanceof JCImport) {
|
||||
JCImport imp0rt = (JCImport)def;
|
||||
delete = (!imp0rt.staticImport && imp0rt.qualid.toString().equals(name));
|
||||
}
|
||||
if (!delete) newDefs = newDefs.append(def);
|
||||
}
|
||||
unit.defs = newDefs;
|
||||
}
|
||||
|
||||
private static List<JCAnnotation> filterList(List<JCAnnotation> annotations, JCTree jcTree) {
|
||||
List<JCAnnotation> newAnnotations = List.nil();
|
||||
for (JCAnnotation ann : annotations) {
|
||||
if (jcTree != ann) newAnnotations = newAnnotations.append(ann);
|
||||
}
|
||||
return newAnnotations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates the given field into all possible getter names.
|
||||
* Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(CharSequence, boolean)}.
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright © 2009 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package lombok.delombok;
|
||||
|
||||
public final class Comment {
|
||||
final int pos;
|
||||
final int prevEndPos;
|
||||
final String content;
|
||||
final int endPos;
|
||||
final boolean newLine;
|
||||
|
||||
public Comment(int prevEndPos, int pos, int endPos, String content, boolean newLine) {
|
||||
this.pos = pos;
|
||||
this.prevEndPos = prevEndPos;
|
||||
this.endPos = endPos;
|
||||
this.content = content;
|
||||
this.newLine = newLine;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return !newLine && prevEndPos == pos;
|
||||
}
|
||||
|
||||
public String summary() {
|
||||
return String.format("%d: %s", pos, content);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright © 2009 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package lombok.delombok;
|
||||
|
||||
import java.nio.CharBuffer;
|
||||
|
||||
import lombok.delombok.CommentPreservingParser.Comments;
|
||||
|
||||
import com.sun.tools.javac.parser.Scanner;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
|
||||
public class CommentCollectingScanner extends Scanner {
|
||||
private final Comments comments;
|
||||
|
||||
/** A factory for creating scanners. */
|
||||
public static class Factory extends Scanner.Factory {
|
||||
private final Context context;
|
||||
|
||||
public static void preRegister(final Context context) {
|
||||
context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() {
|
||||
public CommentCollectingScanner.Factory make() {
|
||||
return new Factory(context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Create a new scanner factory. */
|
||||
protected Factory(Context context) {
|
||||
super(context);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Scanner newScanner(CharSequence input) {
|
||||
if (input instanceof CharBuffer) {
|
||||
return new CommentCollectingScanner(this, (CharBuffer)input, context.get(Comments.class));
|
||||
}
|
||||
char[] array = input.toString().toCharArray();
|
||||
return newScanner(array, array.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Scanner newScanner(char[] input, int inputLength) {
|
||||
return new CommentCollectingScanner(this, input, inputLength, context.get(Comments.class));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public CommentCollectingScanner(CommentCollectingScanner.Factory factory, CharBuffer charBuffer, Comments comments) {
|
||||
super(factory, charBuffer);
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
public CommentCollectingScanner(CommentCollectingScanner.Factory factory, char[] input, int inputLength, Comments comments) {
|
||||
super(factory, input, inputLength);
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processComment(CommentStyle style) {
|
||||
int prevEndPos = prevEndPos();
|
||||
int pos = pos();
|
||||
boolean newLine = containsNewLine(prevEndPos, pos);
|
||||
String content = new String(getRawCharacters(pos, endPos()));
|
||||
comments.add(prevEndPos, pos, endPos(), content, newLine);
|
||||
}
|
||||
|
||||
private boolean containsNewLine(int from, int to) {
|
||||
for (char c : getRawCharacters(from, to)) {
|
||||
if (c == '\n' || c == '\r') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright © 2009 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package lombok.delombok;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.annotation.processing.Messager;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.AnnotationValue;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
|
||||
import lombok.javac.JavacTransformer;
|
||||
|
||||
import com.sun.tools.javac.main.JavaCompiler;
|
||||
import com.sun.tools.javac.main.OptionName;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Options;
|
||||
|
||||
public class CommentPreservingParser {
|
||||
private final String encoding;
|
||||
|
||||
public CommentPreservingParser() {
|
||||
this("utf-8");
|
||||
}
|
||||
|
||||
public CommentPreservingParser(String encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
public ParseResult parse(String fileName, boolean forceProcessing) throws IOException {
|
||||
Context context = new Context();
|
||||
|
||||
Options.instance(context).put(OptionName.ENCODING, encoding);
|
||||
|
||||
CommentCollectingScanner.Factory.preRegister(context);
|
||||
|
||||
JavaCompiler compiler = new JavaCompiler(context) {
|
||||
@Override
|
||||
protected boolean keepComments() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
compiler.genEndPos = true;
|
||||
|
||||
Comments comments = new Comments();
|
||||
context.put(Comments.class, comments);
|
||||
|
||||
comments.comments = List.nil();
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
JCCompilationUnit cu = compiler.parse(fileName);
|
||||
|
||||
boolean changed = new JavacTransformer(messager).transform(context, Collections.singleton(cu));
|
||||
return new ParseResult(comments.comments, cu, forceProcessing || changed);
|
||||
}
|
||||
|
||||
private static final Messager messager = new Messager() {
|
||||
@Override public void printMessage(Kind kind, CharSequence msg) {
|
||||
System.out.printf("%s: %s\n", kind, msg);
|
||||
}
|
||||
|
||||
@Override public void printMessage(Kind kind, CharSequence msg, Element e) {
|
||||
System.out.printf("%s: %s\n", kind, msg);
|
||||
}
|
||||
|
||||
@Override public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a) {
|
||||
System.out.printf("%s: %s\n", kind, msg);
|
||||
}
|
||||
|
||||
@Override public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) {
|
||||
System.out.printf("%s: %s\n", kind, msg);
|
||||
}
|
||||
};
|
||||
|
||||
static class Comments {
|
||||
List<Comment> comments = List.nil();
|
||||
|
||||
void add(int prevEndPos, int pos, int endPos, String content, boolean newLine) {
|
||||
comments = comments.append(new Comment(prevEndPos, pos, endPos, content, newLine));
|
||||
}
|
||||
}
|
||||
|
||||
public static class ParseResult {
|
||||
private final List<Comment> comments;
|
||||
private final JCCompilationUnit compilationUnit;
|
||||
private final boolean changed;
|
||||
|
||||
private ParseResult(List<Comment> comments, JCCompilationUnit compilationUnit, boolean changed) {
|
||||
this.comments = comments;
|
||||
this.compilationUnit = compilationUnit;
|
||||
this.changed = changed;
|
||||
}
|
||||
|
||||
public void print(Writer out) throws IOException {
|
||||
if (!changed) {
|
||||
JavaFileObject sourceFile = compilationUnit.getSourceFile();
|
||||
if (sourceFile != null) {
|
||||
out.write(sourceFile.getCharContent(true).toString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
out.write("// Generated by delombok at " + new Date() + "\n");
|
||||
compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments));
|
||||
}
|
||||
|
||||
public boolean isChanged() {
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
package lombok.delombok;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintStream;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.delombok.CommentPreservingParser.ParseResult;
|
||||
|
||||
import com.zwitserloot.cmdreader.CmdReader;
|
||||
import com.zwitserloot.cmdreader.Description;
|
||||
import com.zwitserloot.cmdreader.Excludes;
|
||||
import com.zwitserloot.cmdreader.InvalidCommandLineException;
|
||||
import com.zwitserloot.cmdreader.Mandatory;
|
||||
import com.zwitserloot.cmdreader.Parameterized;
|
||||
import com.zwitserloot.cmdreader.Sequential;
|
||||
import com.zwitserloot.cmdreader.Shorthand;
|
||||
|
||||
public class Delombok {
|
||||
private Charset charset = Charset.defaultCharset();
|
||||
private CommentPreservingParser parser = new CommentPreservingParser();
|
||||
private PrintStream feedback = System.err;
|
||||
private boolean verbose;
|
||||
private boolean force = false;
|
||||
|
||||
/** If null, output to standard out. */
|
||||
private File output = null;
|
||||
|
||||
private static class CmdArgs {
|
||||
@Shorthand("v")
|
||||
@Description("Print the name of each file as it is being delombok-ed.")
|
||||
@Excludes("quiet")
|
||||
private boolean verbose;
|
||||
|
||||
@Shorthand("q")
|
||||
@Description("No warnings or errors will be emitted to standard error")
|
||||
@Excludes("verbose")
|
||||
private boolean quiet;
|
||||
|
||||
@Shorthand("e")
|
||||
@Description("Sets the encoding of your source files. Defaults to the system default charset. Example: \"UTF-8\"")
|
||||
@Parameterized
|
||||
private String encoding;
|
||||
|
||||
@Shorthand("p")
|
||||
@Description("Print delombok-ed code to standard output instead of saving it in target directory")
|
||||
private boolean print;
|
||||
|
||||
@Shorthand("d")
|
||||
@Description("Directory to save delomboked files to")
|
||||
@Mandatory(onlyIfNot="print")
|
||||
@Parameterized
|
||||
private String target;
|
||||
|
||||
@Description("Files to delombok. Provide either a file, or a directory. If you use a directory, all files in it (recursive) are delombok-ed")
|
||||
@Sequential
|
||||
@Parameterized
|
||||
private List<String> input = new ArrayList<String>();
|
||||
|
||||
private boolean help;
|
||||
}
|
||||
|
||||
public static void main(String[] rawArgs) {
|
||||
CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class);
|
||||
CmdArgs args;
|
||||
try {
|
||||
args = reader.make(rawArgs);
|
||||
} catch (InvalidCommandLineException e) {
|
||||
System.err.println("ERROR: " + e.getMessage());
|
||||
System.err.println(reader.generateCommandLineHelp("delombok"));
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.help || args.input.isEmpty()) {
|
||||
if (args.input.isEmpty()) System.err.println("ERROR: no files or directories to delombok specified.");
|
||||
System.err.println(reader.generateCommandLineHelp("delombok"));
|
||||
System.exit(args.input.isEmpty() ? 1 : 0);
|
||||
return;
|
||||
}
|
||||
|
||||
Delombok delombok = new Delombok();
|
||||
|
||||
if (args.quiet) delombok.setFeedback(new PrintStream(new OutputStream() {
|
||||
@Override public void write(int b) throws IOException {
|
||||
//dummy - do nothing.
|
||||
}
|
||||
}));
|
||||
|
||||
if (args.encoding != null) {
|
||||
try {
|
||||
delombok.setCharset(args.encoding);
|
||||
} catch (UnsupportedCharsetException e) {
|
||||
System.err.println("ERROR: Not a known charset: " + args.encoding);
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (args.verbose) delombok.setVerbose(true);
|
||||
if (args.print) delombok.setOutputToStandardOut();
|
||||
else delombok.setOutput(new File(args.target));
|
||||
|
||||
for (String in : args.input) {
|
||||
try {
|
||||
File f = new File(in);
|
||||
if (f.isFile()) {
|
||||
delombok.delombok(f.getParentFile(), f.getName());
|
||||
} else if (f.isDirectory()) {
|
||||
delombok.delombok(f);
|
||||
} else if (!f.exists()) {
|
||||
if (!args.quiet) System.err.println("WARNING: does not exist - skipping: " + f);
|
||||
} else {
|
||||
if (!args.quiet) System.err.println("WARNING: not a standard file or directory - skipping: " + f);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!args.quiet) {
|
||||
String msg = e.getMessage();
|
||||
if (msg != null && msg.startsWith("DELOMBOK: ")) System.err.println(msg.substring("DELOMBOK: ".length()));
|
||||
else {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setCharset(String charsetName) throws UnsupportedCharsetException {
|
||||
charset = Charset.forName(charsetName);
|
||||
}
|
||||
|
||||
public void setForceProcess(boolean force) {
|
||||
this.force = force;
|
||||
}
|
||||
|
||||
public void setFeedback(PrintStream feedback) {
|
||||
this.feedback = feedback;
|
||||
}
|
||||
|
||||
public void setVerbose(boolean verbose) {
|
||||
this.verbose = verbose;
|
||||
}
|
||||
|
||||
public void setOutput(File dir) {
|
||||
if (dir.isFile() || (!dir.isDirectory() && dir.getName().endsWith(".java"))) throw new IllegalArgumentException(
|
||||
"DELOMBOK: delombok will only write to a directory. " +
|
||||
"If you want to delombok a single file, use -p to output to standard output, then redirect this to a file:\n" +
|
||||
"delombok MyJavaFile.java -p >MyJavaFileDelombok.java");
|
||||
output = dir;
|
||||
}
|
||||
|
||||
public void setOutputToStandardOut() {
|
||||
this.output = null;
|
||||
}
|
||||
|
||||
public void delombok(File base) throws IOException {
|
||||
delombok0(base, "", 0);
|
||||
}
|
||||
|
||||
private void delombok0(File base, String suffix, int loop) throws IOException {
|
||||
File dir = suffix.isEmpty() ? base : new File(base, suffix);
|
||||
String name = suffix;
|
||||
|
||||
if (dir.isDirectory()) {
|
||||
if (loop >= 100) {
|
||||
feedback.printf("Over 100 subdirectories? I'm guessing there's a loop in your directory structure. Skipping: %s\n", suffix);
|
||||
} else {
|
||||
for (File f : dir.listFiles()) {
|
||||
delombok0(base, suffix + (suffix.isEmpty() ? "" : File.separator) + f.getName(), loop + 1);
|
||||
}
|
||||
}
|
||||
} else if (dir.isFile()) {
|
||||
String extension = getExtension(dir);
|
||||
if (extension.equals("java")) delombok(base, name);
|
||||
else if (extension.equals("class")) skipClass(name);
|
||||
else copy(base, name);
|
||||
} else if (!dir.exists()) {
|
||||
feedback.printf("Skipping %s because it does not exist.\n", canonical(dir));
|
||||
} else {
|
||||
feedback.printf("Skipping %s because it is a special file type.\n", canonical(dir));
|
||||
}
|
||||
}
|
||||
|
||||
private void skipClass(String fileName) {
|
||||
if (verbose) feedback.printf("Skipping class file: %s\n", fileName);
|
||||
}
|
||||
|
||||
private void copy(File base, String fileName) throws IOException {
|
||||
if (output == null) {
|
||||
feedback.printf("Skipping resource file: %s\n", fileName);
|
||||
return;
|
||||
}
|
||||
if (verbose) feedback.printf("Copying resource file: %s\n", fileName);
|
||||
byte[] b = new byte[65536];
|
||||
File outFile = new File(base, fileName);
|
||||
outFile.getParentFile().mkdirs();
|
||||
FileInputStream in = new FileInputStream(outFile);
|
||||
try {
|
||||
FileOutputStream out = new FileOutputStream(new File(output, fileName));
|
||||
try {
|
||||
while (true) {
|
||||
int r = in.read(b);
|
||||
if (r == -1) break;
|
||||
out.write(b, 0, r);
|
||||
}
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void delombok(String file, Writer writer) throws IOException {
|
||||
ParseResult result = parser.parse(file, force);
|
||||
|
||||
result.print(writer);
|
||||
}
|
||||
|
||||
public void delombok(File base, String fileName) throws IOException {
|
||||
if (output != null && canonical(base).equals(canonical(output))) throw new IOException(
|
||||
"DELOMBOK: Output file and input file refer to the same filesystem location. Specify a separate path for output.");
|
||||
|
||||
ParseResult result = parser.parse(new File(base, fileName).getAbsolutePath(), force);
|
||||
|
||||
if (verbose) feedback.printf("File: %s [%s]\n", fileName, result.isChanged() ? "delombok-ed" : "unchanged");
|
||||
|
||||
Writer rawWriter = output == null ? createStandardOutWriter() : createFileWriter(output, fileName);
|
||||
BufferedWriter writer = new BufferedWriter(rawWriter);
|
||||
|
||||
try {
|
||||
result.print(writer);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static String canonical(File dir) {
|
||||
try {
|
||||
return dir.getCanonicalPath();
|
||||
} catch (Exception e) {
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
|
||||
private static String getExtension(File dir) {
|
||||
String name = dir.getName();
|
||||
int idx = name.lastIndexOf('.');
|
||||
return idx == -1 ? "" : name.substring(idx+1);
|
||||
}
|
||||
|
||||
private Writer createFileWriter(File base, String fileName) throws IOException {
|
||||
File outFile = new File(base, fileName);
|
||||
outFile.getParentFile().mkdirs();
|
||||
FileOutputStream out = new FileOutputStream(outFile);
|
||||
return new OutputStreamWriter(out, charset);
|
||||
}
|
||||
|
||||
private Writer createStandardOutWriter() {
|
||||
return new OutputStreamWriter(System.out, charset);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -64,6 +64,7 @@ import javax.swing.UIManager;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
|
||||
import lombok.core.Version;
|
||||
import lombok.delombok.Delombok;
|
||||
import lombok.installer.EclipseFinder.OS;
|
||||
import lombok.installer.EclipseLocation.InstallException;
|
||||
import lombok.installer.EclipseLocation.NotAnEclipseException;
|
||||
@@ -95,6 +96,12 @@ public class Installer {
|
||||
private JButton installButton;
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length > 0 && args[0].equals("delombok")) {
|
||||
String[] newArgs = new String[args.length-1];
|
||||
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
|
||||
Delombok.main(newArgs);
|
||||
return;
|
||||
}
|
||||
if (args.length > 0 && (args[0].equals("install") || args[0].equals("uninstall"))) {
|
||||
boolean uninstall = args[0].equals("uninstall");
|
||||
if (args.length < 3 || !args[1].equals("eclipse")) {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright © 2009 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package lombok;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.delombok.Delombok;
|
||||
|
||||
public class TestViaDelombok {
|
||||
private static Delombok delombok = new Delombok();
|
||||
|
||||
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||
|
||||
public static void runComparison(File beforeDir, File afterDir) throws IOException {
|
||||
File[] listFiles = beforeDir.listFiles();
|
||||
|
||||
for (File file : listFiles) {
|
||||
delombok.setVerbose(false);
|
||||
delombok.setForceProcess(true);
|
||||
delombok.setCharset("UTF-8");
|
||||
StringWriter writer = new StringWriter();
|
||||
delombok.delombok(file.getAbsolutePath(), writer);
|
||||
compare(file.getName(), readAfter(afterDir, file), writer.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static void compare(String name, String expectedFile, String actualFile) {
|
||||
String[] expectedLines = expectedFile.split("(\\r?\\n)");
|
||||
String[] actualLines = actualFile.split("(\\r?\\n)");
|
||||
if (actualLines[0].startsWith("// Generated by delombok at ")) {
|
||||
actualLines[0] = "";
|
||||
}
|
||||
expectedLines = removeBlanks(expectedLines);
|
||||
actualLines = removeBlanks(actualLines);
|
||||
int size = Math.min(expectedLines.length, actualLines.length);
|
||||
for (int i = 0; i < size; i++) {
|
||||
String expected = expectedLines[i];
|
||||
String actual = actualLines[i];
|
||||
if (!expected.equals(actual)) {
|
||||
fail(String.format("Difference in line %s(%d):\n`%s`\n`%s`\n", name, i, expected, actual));
|
||||
}
|
||||
}
|
||||
if (expectedLines.length > actualLines.length) {
|
||||
fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size]));
|
||||
}
|
||||
if (expectedLines.length < actualLines.length) {
|
||||
fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size]));
|
||||
}
|
||||
}
|
||||
|
||||
private static String readAfter(File afterDir, File file) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(new File(afterDir, file.getName())));
|
||||
StringBuilder result = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
result.append(line);
|
||||
result.append(LINE_SEPARATOR);
|
||||
}
|
||||
reader.close();
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static String[] removeBlanks(String[] in) {
|
||||
List<String> out = new ArrayList<String>();
|
||||
for (String s : in) {
|
||||
if (!s.trim().isEmpty()) out.add(s);
|
||||
}
|
||||
return out.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import java.util.*;
|
||||
public class Cast {
|
||||
public void test(List<?> list) {
|
||||
RandomAccess r = (/*before*/
|
||||
RandomAccess /*after*/)list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
public class ForLoop {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// before loop
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// start of block
|
||||
System.out.println(i);
|
||||
// end of block
|
||||
}
|
||||
// after loop
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// Cool Comments
|
||||
|
||||
public class WithComments {
|
||||
// Also inside the body
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import java.util.*;
|
||||
public class Cast {
|
||||
public void test(List<?> list) {
|
||||
RandomAccess r = (/*before*/ RandomAccess /*after*/)list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
public class ForLoop {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// before loop
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// start of block
|
||||
System.out.println(i);
|
||||
// end of block
|
||||
}
|
||||
// after loop
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// Cool Comments
|
||||
public class WithComments {
|
||||
// Also inside the body
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright © 2009 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package lombok.delombok;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import lombok.TestViaDelombok;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestLombokFiles {
|
||||
private static final File BEFORE_DIR = new File("test/lombok/resource/before");
|
||||
private static final File AFTER_DIR = new File("test/lombok/resource/after");
|
||||
|
||||
@Test
|
||||
public void testSources() throws Exception {
|
||||
TestViaDelombok.runComparison(BEFORE_DIR, AFTER_DIR);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright © 2009 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package lombok.delombok;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import lombok.TestViaDelombok;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestSourceFiles {
|
||||
private static final File BEFORE_DIR = new File("test/delombok/resource/before");
|
||||
private static final File AFTER_DIR = new File("test/delombok/resource/after");
|
||||
|
||||
@Test
|
||||
public void testSources() throws Exception {
|
||||
TestViaDelombok.runComparison(BEFORE_DIR, AFTER_DIR);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/* cmt */
|
||||
/* cmt2 */
|
||||
/* cmt3 */
|
||||
/*bla */
|
||||
public class CommentsInterspersed {
|
||||
/** javadoc for field */
|
||||
private int x;
|
||||
|
||||
/* bla2 */
|
||||
private String test = "foo"; //$NON-NLS-1$
|
||||
|
||||
/** Javadoc on method */
|
||||
public native void gwtTest(); /*-{
|
||||
javascript;
|
||||
}-*/
|
||||
|
||||
public String getTest() {
|
||||
return test;
|
||||
}
|
||||
} //haha!
|
||||
@@ -0,0 +1,14 @@
|
||||
import /* cmt */ lombok./* cmt2 */Getter /* cmt3 */ ;
|
||||
|
||||
public /*bla */ class CommentsInterspersed {
|
||||
/** javadoc for field */
|
||||
private int x;
|
||||
|
||||
private /* bla2 */ @Getter String test = "foo"; //$NON-NLS-1$
|
||||
|
||||
/** Javadoc on method */
|
||||
public native void gwtTest(); /*-{
|
||||
javascript;
|
||||
}-*/
|
||||
} //haha!
|
||||
|
||||
Reference in new issue
Block a user