diff options
| author | Reinier Zwitserloot <reinier@tipit.to> | 2009-10-16 09:32:36 +0200 |
|---|---|---|
| committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-10-16 09:32:36 +0200 |
| commit | b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb (patch) | |
| tree | 571d13cd7028a6b7d1ebfe84180a4328a20c42d7 /src/lombok/javac | |
| parent | 8629a651a66aa5fba9e0ada7df00803528b0e34f (diff) | |
| download | lombok-b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb.tar.gz lombok-b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb.tar.bz2 lombok-b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb.zip | |
Fixed issue #24 by refactoring the AST.Node class - taken it out, and in the process fixed a lot of type annoyance by adding more generics.
Also changed coding style from for/while/if/switch/catch/do ( expr ) {} to for (expr) {}, hence the changes _everywhere_.
Diffstat (limited to 'src/lombok/javac')
| -rw-r--r-- | src/lombok/javac/HandlerLibrary.java | 32 | ||||
| -rw-r--r-- | src/lombok/javac/Javac.java | 39 | ||||
| -rw-r--r-- | src/lombok/javac/JavacAST.java | 345 | ||||
| -rw-r--r-- | src/lombok/javac/JavacASTAdapter.java | 44 | ||||
| -rw-r--r-- | src/lombok/javac/JavacASTVisitor.java | 118 | ||||
| -rw-r--r-- | src/lombok/javac/JavacAnnotationHandler.java | 2 | ||||
| -rw-r--r-- | src/lombok/javac/JavacNode.java | 200 | ||||
| -rw-r--r-- | src/lombok/javac/apt/Processor.java | 49 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandleCleanup.java | 58 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandleData.java | 52 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandleEqualsAndHashCode.java | 145 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandleGetter.java | 31 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandlePrintAST.java | 8 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandleSetter.java | 28 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandleSneakyThrows.java | 30 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandleSynchronized.java | 18 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/HandleToString.java | 88 | ||||
| -rw-r--r-- | src/lombok/javac/handlers/PKG.java | 112 |
18 files changed, 699 insertions, 700 deletions
diff --git a/src/lombok/javac/HandlerLibrary.java b/src/lombok/javac/HandlerLibrary.java index 20902e82..3d44db7f 100644 --- a/src/lombok/javac/HandlerLibrary.java +++ b/src/lombok/javac/HandlerLibrary.java @@ -72,7 +72,7 @@ public class HandlerLibrary { this.annotationClass = annotationClass; } - public boolean handle(final JavacAST.Node node) { + public boolean handle(final JavacNode node) { return handler.handle(Javac.createAnnotation(annotationClass, node), (JCAnnotation)node.get(), node); } } @@ -97,17 +97,17 @@ public class HandlerLibrary { //No, that seemingly superfluous reference to JavacAnnotationHandler's classloader is not in fact superfluous! Iterator<JavacAnnotationHandler> it = ServiceLoader.load(JavacAnnotationHandler.class, JavacAnnotationHandler.class.getClassLoader()).iterator(); - while ( it.hasNext() ) { + while (it.hasNext()) { try { JavacAnnotationHandler<?> handler = it.next(); Class<? extends Annotation> annotationClass = SpiLoadUtil.findAnnotationClass(handler.getClass(), JavacAnnotationHandler.class); AnnotationHandlerContainer<?> container = new AnnotationHandlerContainer(handler, annotationClass); - if ( lib.annotationHandlers.put(container.annotationClass.getName(), container) != null ) { + if (lib.annotationHandlers.put(container.annotationClass.getName(), container) != null) { lib.javacWarning("Duplicate handlers for annotation type: " + container.annotationClass.getName()); } lib.typeLibrary.addType(container.annotationClass.getName()); - } catch ( ServiceConfigurationError e ) { + } catch (ServiceConfigurationError e) { lib.javacWarning("Can't load Lombok annotation handler for javac", e); } } @@ -118,11 +118,11 @@ public class HandlerLibrary { //No, that seemingly superfluous reference to JavacASTVisitor's classloader is not in fact superfluous! Iterator<JavacASTVisitor> it = ServiceLoader.load(JavacASTVisitor.class, JavacASTVisitor.class.getClassLoader()).iterator(); - while ( it.hasNext() ) { + while (it.hasNext()) { try { JavacASTVisitor handler = it.next(); lib.visitorHandlers.add(handler); - } catch ( ServiceConfigurationError e ) { + } catch (ServiceConfigurationError e) { lib.javacWarning("Can't load Lombok visitor handler for javac", e); } } @@ -146,7 +146,7 @@ public class HandlerLibrary { /** Generates an error in the Messager that was used to initialize this HandlerLibrary. */ public void javacError(String message, Throwable t) { messager.printMessage(Diagnostic.Kind.ERROR, message + (t == null ? "" : (": " + t))); - if ( t != null ) t.printStackTrace(); + if (t != null) t.printStackTrace(); } /** @@ -166,23 +166,23 @@ public class HandlerLibrary { * @param node The Lombok AST Node representing the Annotation AST Node. * @param annotation 'node.get()' - convenience parameter. */ - public boolean handleAnnotation(JCCompilationUnit unit, JavacAST.Node node, JCAnnotation annotation) { + public boolean handleAnnotation(JCCompilationUnit unit, JavacNode 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) ) { + for (String fqn : resolver.findTypeMatches(node, rawType)) { boolean isPrintAST = fqn.equals(PrintAST.class.getName()); - if ( isPrintAST == skipPrintAST ) continue; + if (isPrintAST == skipPrintAST) continue; AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn); - if ( container == null ) continue; + if (container == null) continue; try { handled |= container.handle(node); - } catch ( AnnotationValueDecodeFail fail ) { + } catch (AnnotationValueDecodeFail fail) { fail.owner.setError(fail.getMessage(), fail.idx); - } catch ( Throwable t ) { + } catch (Throwable t) { String sourceName = "(unknown).java"; - if ( unit != null && unit.sourcefile != null ) sourceName = unit.sourcefile.getName(); + if (unit != null && unit.sourcefile != null) sourceName = unit.sourcefile.getName(); javacError(String.format("Lombok annotation handler %s failed on " + sourceName, container.handler.getClass()), t); } } @@ -194,9 +194,9 @@ public class HandlerLibrary { * Will call all registered {@link JavacASTVisitor} instances. */ public void callASTVisitors(JavacAST ast) { - for ( JavacASTVisitor visitor : visitorHandlers ) try { + for (JavacASTVisitor visitor : visitorHandlers) try { ast.traverse(visitor); - } catch ( Throwable t ) { + } catch (Throwable t) { javacError(String.format("Lombok visitor handler %s failed", visitor.getClass()), t); } } diff --git a/src/lombok/javac/Javac.java b/src/lombok/javac/Javac.java index bc6d18f6..99a7c928 100644 --- a/src/lombok/javac/Javac.java +++ b/src/lombok/javac/Javac.java @@ -35,7 +35,6 @@ import lombok.core.TypeLibrary; import lombok.core.TypeResolver; import lombok.core.AST.Kind; import lombok.core.AnnotationValues.AnnotationValue; -import lombok.javac.JavacAST.Node; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCAssign; @@ -60,8 +59,8 @@ public class Javac { * @param type An actual annotation type, such as <code>lombok.Getter.class</code>. * @param node A Lombok AST node representing an annotation in source code. */ - public static boolean annotationTypeMatches(Class<? extends Annotation> type, Node node) { - if ( node.getKind() != Kind.ANNOTATION ) return false; + public static boolean annotationTypeMatches(Class<? extends Annotation> type, JavacNode node) { + if (node.getKind() != Kind.ANNOTATION) return false; String typeName = ((JCAnnotation)node.get()).annotationType.toString(); TypeLibrary library = new TypeLibrary(); @@ -69,8 +68,8 @@ public class Javac { TypeResolver resolver = new TypeResolver(library, node.getPackageDeclaration(), node.getImportStatements()); Collection<String> typeMatches = resolver.findTypeMatches(node, typeName); - for ( String match : typeMatches ) { - if ( match.equals(type.getName()) ) return true; + for (String match : typeMatches) { + if (match.equals(type.getName())) return true; } return false; @@ -82,23 +81,23 @@ public class Javac { * @param type An annotation class type, such as <code>lombok.Getter.class</code>. * @param node A Lombok AST node representing an annotation in source code. */ - public static <A extends Annotation> AnnotationValues<A> createAnnotation(Class<A> type, final Node node) { + public static <A extends Annotation> AnnotationValues<A> createAnnotation(Class<A> type, final JavacNode node) { Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>(); JCAnnotation anno = (JCAnnotation) node.get(); List<JCExpression> arguments = anno.getArguments(); - for ( Method m : type.getDeclaredMethods() ) { - if ( !Modifier.isPublic(m.getModifiers()) ) continue; + for (Method m : type.getDeclaredMethods()) { + if (!Modifier.isPublic(m.getModifiers())) continue; String name = m.getName(); List<String> raws = new ArrayList<String>(); List<Object> guesses = new ArrayList<Object>(); final List<DiagnosticPosition> positions = new ArrayList<DiagnosticPosition>(); boolean isExplicit = false; - for ( JCExpression arg : arguments ) { + for (JCExpression arg : arguments) { String mName; JCExpression rhs; - if ( arg instanceof JCAssign ) { + if (arg instanceof JCAssign) { JCAssign assign = (JCAssign) arg; mName = assign.lhs.toString(); rhs = assign.rhs; @@ -107,11 +106,11 @@ public class Javac { mName = "value"; } - if ( !mName.equals(name) ) continue; + if (!mName.equals(name)) continue; isExplicit = true; - if ( rhs instanceof JCNewArray ) { + if (rhs instanceof JCNewArray) { List<JCExpression> elems = ((JCNewArray)rhs).elems; - for ( JCExpression inner : elems ) { + for (JCExpression inner : elems) { raws.add(inner.toString()); guesses.add(calculateGuess(inner)); positions.add(inner.pos()); @@ -125,11 +124,11 @@ public class Javac { values.put(name, new AnnotationValue(node, raws, guesses, isExplicit) { @Override public void setError(String message, int valueIdx) { - if ( valueIdx < 0 ) node.addError(message); + if (valueIdx < 0) node.addError(message); else node.addError(message, positions.get(valueIdx)); } @Override public void setWarning(String message, int valueIdx) { - if ( valueIdx < 0 ) node.addWarning(message); + if (valueIdx < 0) node.addWarning(message); else node.addWarning(message, positions.get(valueIdx)); } }); @@ -144,18 +143,18 @@ public class Javac { * Will for example turn a TrueLiteral into 'Boolean.valueOf(true)'. */ private static Object calculateGuess(JCExpression expr) { - if ( expr instanceof JCLiteral ) { + if (expr instanceof JCLiteral) { JCLiteral lit = (JCLiteral)expr; - if ( lit.getKind() == com.sun.source.tree.Tree.Kind.BOOLEAN_LITERAL ) { + if (lit.getKind() == com.sun.source.tree.Tree.Kind.BOOLEAN_LITERAL) { return ((Number)lit.value).intValue() == 0 ? false : true; } return lit.value; - } else if ( expr instanceof JCIdent || expr instanceof JCFieldAccess ) { + } else if (expr instanceof JCIdent || expr instanceof JCFieldAccess) { String x = expr.toString(); - if ( x.endsWith(".class") ) x = x.substring(0, x.length() - 6); + if (x.endsWith(".class")) x = x.substring(0, x.length() - 6); else { int idx = x.lastIndexOf('.'); - if ( idx > -1 ) x = x.substring(idx + 1); + if (idx > -1) x = x.substring(idx + 1); } return x; } else return null; diff --git a/src/lombok/javac/JavacAST.java b/src/lombok/javac/JavacAST.java index c68d3c19..2ee3d5be 100644 --- a/src/lombok/javac/JavacAST.java +++ b/src/lombok/javac/JavacAST.java @@ -55,7 +55,7 @@ import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; * Wraps around javac's internal AST view to add useful features as well as the ability to visit parents from children, * something javac's own AST system does not offer. */ -public class JavacAST extends AST<JCTree> { +public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { private final Messager messager; private final Name.Table nameTable; private final TreeMaker treeMaker; @@ -90,8 +90,8 @@ public class JavacAST extends AST<JCTree> { @Override public Collection<String> getImportStatements() { List<String> imports = new ArrayList<String>(); JCCompilationUnit unit = (JCCompilationUnit)top().get(); - for ( JCTree def : unit.defs ) { - if ( def instanceof JCImport ) { + for (JCTree def : unit.defs) { + if (def instanceof JCImport) { imports.add(((JCImport)def).qualid.toString()); } } @@ -107,22 +107,12 @@ public class JavacAST extends AST<JCTree> { top().traverse(visitor); } - private void traverseChildren(JavacASTVisitor visitor, Node node) { - for ( Node child : new ArrayList<Node>(node.down()) ) { + void traverseChildren(JavacASTVisitor visitor, JavacNode node) { + for (JavacNode child : new ArrayList<JavacNode>(node.down())) { child.traverse(visitor); } } - /** {@inheritDoc} */ - @Override public Node top() { - return (Node) super.top(); - } - - /** {@inheritDoc} */ - @Override public Node get(JCTree astNode) { - return (Node) super.get(astNode); - } - /** @return A Name object generated for the proper name table belonging to this AST. */ public Name toName(String name) { return nameTable.fromString(name); @@ -139,8 +129,8 @@ public class JavacAST extends AST<JCTree> { } /** {@inheritDoc} */ - @Override protected Node buildTree(JCTree node, Kind kind) { - switch ( kind ) { + @Override protected JavacNode buildTree(JCTree node, Kind kind) { + switch (kind) { case COMPILATION_UNIT: return buildCompilationUnit((JCCompilationUnit) node); case TYPE: @@ -164,99 +154,100 @@ public class JavacAST extends AST<JCTree> { } } - private Node buildCompilationUnit(JCCompilationUnit top) { - List<Node> childNodes = new ArrayList<Node>(); - for ( JCTree s : top.defs ) { - if ( s instanceof JCClassDecl ) { + private JavacNode buildCompilationUnit(JCCompilationUnit top) { + List<JavacNode> childNodes = new ArrayList<JavacNode>(); + for (JCTree s : top.defs) { + if (s instanceof JCClassDecl) { addIfNotNull(childNodes, buildType((JCClassDecl)s)); } // else they are import statements, which we don't care about. Or Skip objects, whatever those are. } - return new Node(top, childNodes, Kind.COMPILATION_UNIT); + return new JavacNode(this, top, childNodes, Kind.COMPILATION_UNIT); } - private Node buildType(JCClassDecl type) { - if ( setAndGetAsHandled(type) ) return null; - List<Node> childNodes = new ArrayList<Node>(); + private JavacNode buildType(JCClassDecl type) { + if (setAndGetAsHandled(type)) return null; + List<JavacNode> childNodes = new ArrayList<JavacNode>(); - for ( JCTree def : type.defs ) { - for ( JCAnnotation annotation : type.mods.annotations ) addIfNotNull(childNodes, buildAnnotation(annotation)); + for (JCTree def : type.defs) { + for (JCAnnotation annotation : type.mods.annotations) addIfNotNull(childNodes, buildAnnotation(annotation)); /* A def can be: * JCClassDecl for inner types * JCMethodDecl for constructors and methods * JCVariableDecl for fields * JCBlock for (static) initializers */ - if ( def instanceof JCMethodDecl ) addIfNotNull(childNodes, buildMethod((JCMethodDecl)def)); - else if ( def instanceof JCClassDecl ) addIfNotNull(childNodes, buildType((JCClassDecl)def)); - else if ( def instanceof JCVariableDecl ) addIfNotNull(childNodes, buildField((JCVariableDecl)def)); - else if ( def instanceof JCBlock ) addIfNotNull(childNodes, buildInitializer((JCBlock)def)); + if (def instanceof JCMethodDecl) addIfNotNull(childNodes, buildMethod((JCMethodDecl)def)); + else if (def instanceof JCClassDecl) addIfNotNull(childNodes, buildType((JCClassDecl)def)); + else if (def instanceof JCVariableDecl) addIfNotNull(childNodes, buildField((JCVariableDecl)def)); + else if (def instanceof JCBlock) addIfNotNull(childNodes, buildInitializer((JCBlock)def)); } - return putInMap(new Node(type, childNodes, Kind.TYPE)); + return putInMap(new JavacNode(this, type, childNodes, Kind.TYPE)); } - private Node buildField(JCVariableDecl field) { - if ( setAndGetAsHandled(field) ) return null; - List<Node> childNodes = new ArrayList<Node>(); - for ( JCAnnotation annotation : field.mods.annotations ) addIfNotNull(childNodes, buildAnnotation(annotation)); + private JavacNode buildField(JCVariableDecl field) { + if (setAndGetAsHandled(field)) return null; + List<JavacNode> childNodes = new ArrayList<JavacNode>(); + for (JCAnnotation annotation : field.mods.annotations) addIfNotNull(childNodes, buildAnnotation(annotation)); addIfNotNull(childNodes, buildExpression(field.init)); - return putInMap(new Node(field, childNodes, Kind.FIELD)); + return putInMap(new JavacNode(this, field, childNodes, Kind.FIELD)); } - private Node buildLocalVar(JCVariableDecl local, Kind kind) { - if ( setAndGetAsHandled(local) ) return null; - List<Node> childNodes = new ArrayList<Node>(); - for ( JCAnnotation annotation : local.mods.annotations ) addIfNotNull(childNodes, buildAnnotation(annotation)); + private JavacNode buildLocalVar(JCVariableDecl local, Kind kind) { + if (setAndGetAsHandled(local)) return null; + List<JavacNode> childNodes = new ArrayList<JavacNode>(); + for (JCAnnotation annotation : local.mods.annotations) addIfNotNull(childNodes, buildAnnotation(annotation)); addIfNotNull(childNodes, buildExpression(local.init)); - return putInMap(new Node(local, childNodes, kind)); + return putInMap(new JavacNode(this, local, childNodes, kind)); } - private Node buildInitializer(JCBlock initializer) { - if ( setAndGetAsHandled(initializer) ) return null; - List<Node> childNodes = new ArrayList<Node>(); - for ( JCStatement statement: initializer.stats ) addIfNotNull(childNodes, buildStatement(statement)); - return putInMap(new Node(initializer, childNodes, Kind.INITIALIZER)); + private JavacNode buildInitializer(JCBlock initializer) { + if (setAndGetAsHandled(initializer)) return null; + List<JavacNode> childNodes = new ArrayList<JavacNode>(); + for (JCStatement statement: initializer.stats) addIfNotNull(childNodes, buildStatement(statement)); + return putInMap(new JavacNode(this, initializer, childNodes, Kind.INITIALIZER)); } - private Node buildMethod(JCMethodDecl method) { - if ( setAndGetAsHandled(method) ) return null; - List<Node> childNodes = new ArrayList<Node>(); - for ( JCAnnotation annotation : method.mods.annotations ) addIfNotNull(childNodes, buildAnnotation(annotation)); - for ( JCVariableDecl param : method.params ) addIfNotNull(childNodes, buildLocalVar(param, Kind.ARGUMENT)); - if ( method.body != null && method.body.stats != null ) - for ( JCStatement statement : method.body.stats ) addIfNotNull(childNodes, buildStatement(statement)); - return putInMap(new Node(method, childNodes, Kind.METHOD)); + private JavacNode buildMethod(JCMethodDecl method) { + if (setAndGetAsHandled(method)) return null; + List<JavacNode> childNodes = new ArrayList<JavacNode>(); + for (JCAnnotation annotation : method.mods.annotations) addIfNotNull(childNodes, buildAnnotation(annotation)); + for (JCVariableDecl param : method.params) addIfNotNull(childNodes, buildLocalVar(param, Kind.ARGUMENT)); + if (method.body != null && method.body.stats != null) { + for (JCStatement statement : method.body.stats) addIfNotNull(childNodes, buildStatement(statement)); + } + return putInMap(new JavacNode(this, method, childNodes, Kind.METHOD)); } - private Node buildAnnotation(JCAnnotation annotation) { - if ( setAndGetAsHandled(annotation) ) return null; - return putInMap(new Node(annotation, null, Kind.ANNOTATION)); + private JavacNode buildAnnotation(JCAnnotation annotation) { + if (setAndGetAsHandled(annotation)) return null; + return putInMap(new JavacNode(this, annotation, null, Kind.ANNOTATION)); } - private Node buildExpression(JCExpression expression) { + private JavacNode buildExpression(JCExpression expression) { return buildStatementOrExpression(expression); } - private Node buildStatement(JCStatement statement) { + private JavacNode buildStatement(JCStatement statement) { return buildStatementOrExpression(statement); } - private Node buildStatementOrExpression(JCTree statement) { - if ( statement == null ) return null; - if ( statement instanceof JCAnnotation ) return null; - if ( statement instanceof JCClassDecl ) return buildType((JCClassDecl)statement); - if ( statement instanceof JCVariableDecl ) return buildLocalVar((JCVariableDecl)statement, Kind.LOCAL); + private JavacNode buildStatementOrExpression(JCTree statement) { + if (statement == null) return null; + if (statement instanceof JCAnnotation) return null; + if (statement instanceof JCClassDecl) return buildType((JCClassDecl)statement); + if (statement instanceof JCVariableDecl) return buildLocalVar((JCVariableDecl)statement, Kind.LOCAL); - if ( setAndGetAsHandled(statement) ) return null; + if (setAndGetAsHandled(statement)) return null; return drill(statement); } - private Node drill(JCTree statement) { - List<Node> childNodes = new ArrayList<Node>(); - for ( FieldAccess fa : fieldsOf(statement.getClass()) ) childNodes.addAll(buildWithField(Node.class, statement, fa)); - return putInMap(new Node(statement, childNodes, Kind.STATEMENT)); + private JavacNode drill(JCTree statement) { + List<JavacNode> childNodes = new ArrayList<JavacNode>(); + for (FieldAccess fa : fieldsOf(statement.getClass())) childNodes.addAll(buildWithField(JavacNode.class, statement, fa)); + return putInMap(new JavacNode(this, statement, childNodes, Kind.STATEMENT)); } /** For javac, both JCExpression and JCStatement are considered as valid children types. */ @@ -267,200 +258,12 @@ public class JavacAST extends AST<JCTree> { return collection; } - private static void addIfNotNull(Collection<Node> nodes, Node node) { - if ( node != null ) nodes.add(node); - } - - /** - * Javac specific version of the AST.Node class. - */ - public class Node extends AST<JCTree>.Node { - /** - * See the {@link AST.Node} constructor for information. - */ - public Node(JCTree node, List<Node> children, Kind kind) { - super(node, children, kind); - } - - /** - * Visits this node and all child nodes depth-first, calling the provided visitor's visit methods. - */ - public void traverse(JavacASTVisitor visitor) { - switch ( this.getKind() ) { - case COMPILATION_UNIT: - visitor.visitCompilationUnit(this, (JCCompilationUnit)get()); - traverseChildren(visitor, this); - visitor.endVisitCompilationUnit(this, (JCCompilationUnit)get()); - break; - case TYPE: - visitor.visitType(this, (JCClassDecl)get()); - traverseChildren(visitor, this); - visitor.endVisitType(this, (JCClassDecl)get()); - break; - case FIELD: - visitor.visitField(this, (JCVariableDecl)get()); - traverseChildren(visitor, this); - visitor.endVisitField(this, (JCVariableDecl)get()); - break; - case METHOD: - visitor.visitMethod(this, (JCMethodDecl)get()); - traverseChildren(visitor, this); - visitor.endVisitMethod(this, (JCMethodDecl)get()); - break; - case INITIALIZER: - visitor.visitInitializer(this, (JCBlock)get()); - traverseChildren(visitor, this); - visitor.endVisitInitializer(this, (JCBlock)get()); - break; - case ARGUMENT: - JCMethodDecl parent = (JCMethodDecl) up().get(); - visitor.visitMethodArgument(this, (JCVariableDecl)get(), parent); - traverseChildren(visitor, this); - visitor.endVisitMethodArgument(this, (JCVariableDecl)get(), parent); - break; - case LOCAL: - visitor.visitLocal(this, (JCVariableDecl)get()); - traverseChildren(visitor, this); - visitor.endVisitLocal(this, (JCVariableDecl)get()); - break; - case STATEMENT: - visitor.visitStatement(this, get()); - traverseChildren(visitor, this); - visitor.endVisitStatement(this, get()); - break; - case ANNOTATION: - switch ( up().getKind() ) { - case TYPE: - visitor.visitAnnotationOnType((JCClassDecl)up().get(), this, (JCAnnotation)get()); - break; - case FIELD: - visitor.visitAnnotationOnField((JCVariableDecl)up().get(), this, (JCAnnotation)get()); - break; - case METHOD: - visitor.visitAnnotationOnMethod((JCMethodDecl)up().get(), this, (JCAnnotation)get()); - break; - case ARGUMENT: - JCVariableDecl argument = (JCVariableDecl)up().get(); - JCMethodDecl method = (JCMethodDecl)up().up().get(); - visitor.visitAnnotationOnMethodArgument(argument, method, this, (JCAnnotation)get()); - break; - case LOCAL: - visitor.visitAnnotationOnLocal((JCVariableDecl)up().get(), this, (JCAnnotation)get()); - break; - default: - throw new AssertionError("Annotion not expected as child of a " + up().getKind()); - } - break; - default: - throw new AssertionError("Unexpected kind during node traversal: " + getKind()); - } - } - - /** {@inheritDoc} */ - @Override public String getName() { - final Name n; - - if ( node instanceof JCClassDecl ) n = ((JCClassDecl)node).name; - else if ( node instanceof JCMethodDecl ) n = ((JCMethodDecl)node).name; - else if ( node instanceof JCVariableDecl ) n = ((JCVariableDecl)node).name; - else n = null; - - return n == null ? null : n.toString(); - } - - /** {@inheritDoc} */ - @Override protected boolean calculateIsStructurallySignificant() { - if ( node instanceof JCClassDecl ) return true; - if ( node instanceof JCMethodDecl ) return true; - if ( node instanceof JCVariableDecl ) return true; - if ( node instanceof JCCompilationUnit ) return true; - return false; - } - - /** - * Convenient shortcut to the owning JavacAST object's getTreeMaker method. - * - * @see JavacAST#getTreeMaker() - */ - public TreeMaker getTreeMaker() { - return treeMaker; - } - - /** - * Convenient shortcut to the owning JavacAST object's getSymbolTable method. - * - * @see JavacAST#getSymbolTable() - */ - public Symtab getSymbolTable() { - return symtab; - } - - /** - * Convenient shortcut to the owning JavacAST object's toName method. - * - * @see JavacAST#toName(String) - */ - public Name toName(String name) { - return JavacAST.this.toName(name); - } - - /** {@inheritDoc} */ - @Override public Node getNodeFor(JCTree obj) { - return (Node) super.getNodeFor(obj); - } - - /** {@inheritDoc} */ - @Override public Node directUp() { - return (Node) super.directUp(); - } - - /** {@inheritDoc} */ - @Override public Node up() { - return (Node) super.up(); - } - - /** {@inheritDoc} */ - @Override public Node top() { - return (Node) super.top(); - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public Collection<Node> down() { - return (Collection<Node>) super.down(); - } - - /** - * Generates an compiler error focused on the AST node represented by this node object. - */ - public void addError(String message) { - printMessage(Diagnostic.Kind.ERROR, message, this, null); - } - - /** - * Generates an compiler error focused on the AST node represented by this node object. - */ - public void addError(String message, DiagnosticPosition pos) { - printMessage(Diagnostic.Kind.ERROR, message, null, pos); - } - - /** - * Generates a compiler warning focused on the AST node represented by this node object. - */ - public void addWarning(String message) { - printMessage(Diagnostic.Kind.WARNING, message, this, null); - } - - /** - * Generates a compiler warning focused on the AST node represented by this node object. - */ - public void addWarning(String message, DiagnosticPosition pos) { - printMessage(Diagnostic.Kind.WARNING, message, null, pos); - } + private static void addIfNotNull(Collection<JavacNode> nodes, JavacNode node) { + if (node != null) nodes.add(node); } /** Supply either a position or a node (in that case, position of the node is used) */ - private void printMessage(Diagnostic.Kind kind, String message, Node node, DiagnosticPosition pos) { + void printMessage(Diagnostic.Kind kind, String message, JavacNode node, DiagnosticPosition pos) { JavaFileObject oldSource = null; JavaFileObject newSource = null; JCTree astObject = node == null ? null : node.get(); @@ -468,7 +271,7 @@ public class JavacAST extends AST<JCTree> { newSource = top.sourcefile; if (newSource != null) { oldSource = log.useSource(newSource); - if ( pos == null ) pos = astObject.pos(); + if (pos == null) pos = astObject.pos(); } try { switch (kind) { @@ -488,22 +291,20 @@ public class JavacAST extends AST<JCTree> { break; } } finally { - if (oldSource != null) - log.useSource(oldSource); + if (oldSource != null) log.useSource(oldSource); } } /** {@inheritDoc} */ - @SuppressWarnings("unchecked") @Override protected void setElementInASTCollection(Field field, Object refField, List<Collection<?>> chain, Collection<?> collection, int idx, JCTree newN) throws IllegalAccessException { - com.sun.tools.javac.util.List<?> list = setElementInConsList(chain, collection, ((List)collection).get(idx), newN); + com.sun.tools.javac.util.List<?> list = setElementInConsList(chain, collection, ((List<?>)collection).get(idx), newN); field.set(refField, list); } private com.sun.tools.javac.util.List<?> setElementInConsList(List<Collection<?>> chain, Collection<?> current, Object oldO, Object newO) { com.sun.tools.javac.util.List<?> oldL = (com.sun.tools.javac.util.List<?>) current; com.sun.tools.javac.util.List<?> newL = replaceInConsList(oldL, oldO, newO); - if ( chain.isEmpty() ) return newL; + if (chain.isEmpty()) return newL; else { List<Collection<?>> reducedChain = new ArrayList<Collection<?>>(chain); Collection<?> newCurrent = reducedChain.remove(reducedChain.size() -1); @@ -514,14 +315,14 @@ public class JavacAST extends AST<JCTree> { private com.sun.tools.javac.util.List<?> replaceInConsList(com.sun.tools.javac.util.List<?> oldL, Object oldO, Object newO) { boolean repl = false; Object[] a = oldL.toArray(); - for ( int i = 0 ; i < a.length ; i++ ) { - if ( a[i] == oldO ) { + for (int i = 0; i < a.length; i++) { + if (a[i] == oldO) { a[i] = newO; repl = true; } } - if ( repl ) return com.sun.tools.javac.util.List.<Object>from(a); + if (repl) return com.sun.tools.javac.util.List.<Object>from(a); else return oldL; } @@ -529,11 +330,11 @@ public class JavacAST extends AST<JCTree> { try { Field f = messager.getClass().getDeclaredField("errorCount"); f.setAccessible(true); - if ( f.getType() == int.class ) { + if (f.getType() == int.class) { int val = ((Number)f.get(messager)).intValue(); f.set(messager, val +1); } - } catch ( Throwable t ) { + } catch (Throwable t) { //Very unfortunate, but in most cases it still works fine, so we'll silently swallow it. } } diff --git a/src/lombok/javac/JavacASTAdapter.java b/src/lombok/javac/JavacASTAdapter.java index b720ea32..41bc46d3 100644 --- a/src/lombok/javac/JavacASTAdapter.java +++ b/src/lombok/javac/JavacASTAdapter.java @@ -21,8 +21,6 @@ */ package lombok.javac; -import lombok.javac.JavacAST.Node; - import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; @@ -37,64 +35,64 @@ import com.sun.tools.javac.tree.JCTree.JCVariableDecl; */ public class JavacASTAdapter implements JavacASTVisitor { /** {@inheritDoc} */ - @Override public void visitCompilationUnit(Node top, JCCompilationUnit unit) {} + @Override public void visitCompilationUnit(JavacNode top, JCCompilationUnit unit) {} /** {@inheritDoc} */ - @Override public void endVisitCompilationUnit(Node top, JCCompilationUnit unit) {} + @Override public void endVisitCompilationUnit(JavacNode top, JCCompilationUnit unit) {} /** {@inheritDoc} */ - @Override public void visitType(Node typeNode, JCClassDecl type) {} + @Override public void visitType(JavacNode typeNode, JCClassDecl type) {} /** {@inheritDoc} */ - @Override public void visitAnnotationOnType(JCClassDecl type, Node annotationNode, JCAnnotation annotation) {} + @Override public void visitAnnotationOnType(JCClassDecl type, JavacNode annotationNode, JCAnnotation annotation) {} /** {@inheritDoc} */ - @Override public void endVisitType(Node typeNode, JCClassDecl type) {} + @Override public void endVisitType(JavacNode typeNode, JCClassDecl type) {} /** {@inheritDoc} */ - @Override public void visitField(Node fieldNode, JCVariableDecl field) {} + @Override public void visitField(JavacNode fieldNode, JCVariableDecl field) {} /** {@inheritDoc} */ - @Override public void visitAnnotationOnField(JCVariableDecl field, Node annotationNode, JCAnnotation annotation) {} + @Override public void visitAnnotationOnField(JCVariableDecl field, JavacNode annotationNode, JCAnnotation annotation) {} /** {@inheritDoc} */ - @Override public void endVisitField(Node fieldNode, JCVariableDecl field) {} + @Override public void endVisitField(JavacNode fieldNode, JCVariableDecl field) {} /** {@inheritDoc} */ - @Override public void visitInitializer(Node initializerNode, JCBlock initializer) {} + @Override public void visitInitializer(JavacNode initializerNode, JCBlock initializer) {} /** {@inheritDoc} */ - @Override public void endVisitInitializer(Node initializerNode, JCBlock initializer) {} + @Override public void endVisitInitializer(JavacNode initializerNode, JCBlock initializer) {} /** {@inheritDoc} */ - @Override public void visitMethod(Node methodNode, JCMethodDecl method) {} + @Override publi |
