From b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 16 Oct 2009 09:32:36 +0200 Subject: 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_. --- src/lombok/eclipse/Eclipse.java | 101 ++++--- src/lombok/eclipse/EclipseAST.java | 330 +++++---------------- src/lombok/eclipse/EclipseASTAdapter.java | 44 ++- src/lombok/eclipse/EclipseASTVisitor.java | 118 ++++---- src/lombok/eclipse/EclipseAnnotationHandler.java | 2 +- src/lombok/eclipse/EclipseNode.java | 175 +++++++++++ src/lombok/eclipse/HandlerLibrary.java | 25 +- src/lombok/eclipse/TransformEclipseAST.java | 43 ++- src/lombok/eclipse/handlers/HandleCleanup.java | 62 ++-- src/lombok/eclipse/handlers/HandleData.java | 43 +-- .../eclipse/handlers/HandleEqualsAndHashCode.java | 141 ++++----- src/lombok/eclipse/handlers/HandleGetter.java | 29 +- src/lombok/eclipse/handlers/HandlePrintAST.java | 10 +- src/lombok/eclipse/handlers/HandleSetter.java | 28 +- .../eclipse/handlers/HandleSneakyThrows.java | 34 +-- .../eclipse/handlers/HandleSynchronized.java | 20 +- src/lombok/eclipse/handlers/HandleToString.java | 96 +++--- src/lombok/eclipse/handlers/PKG.java | 139 +++++---- 18 files changed, 717 insertions(+), 723 deletions(-) create mode 100644 src/lombok/eclipse/EclipseNode.java (limited to 'src/lombok/eclipse') diff --git a/src/lombok/eclipse/Eclipse.java b/src/lombok/eclipse/Eclipse.java index c96c9ce6..91c7a9f5 100644 --- a/src/lombok/eclipse/Eclipse.java +++ b/src/lombok/eclipse/Eclipse.java @@ -36,7 +36,6 @@ import lombok.core.TypeLibrary; import lombok.core.TypeResolver; import lombok.core.AST.Kind; import lombok.core.AnnotationValues.AnnotationValue; -import lombok.eclipse.EclipseAST.Node; import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.IStatus; @@ -107,7 +106,7 @@ public class Eclipse { */ public static void error(CompilationUnitDeclaration cud, String message, String bundleName, Throwable error) { Bundle bundle = Platform.getBundle(bundleName); - if ( bundle == null ) { + if (bundle == null) { System.err.printf("Can't find bundle %s while trying to report error:\n%s\n", bundleName, message); return; } @@ -115,7 +114,7 @@ public class Eclipse { ILog log = Platform.getLog(bundle); log.log(new Status(IStatus.ERROR, bundleName, message, error)); - if ( cud != null ) EclipseAST.addProblemToCompilationResult(cud, false, message + " - See error log.", 0, 0); + if (cud != null) EclipseAST.addProblemToCompilationResult(cud, false, message + " - See error log.", 0, 0); } /** @@ -125,7 +124,7 @@ public class Eclipse { public static String toQualifiedName(char[][] typeName) { StringBuilder sb = new StringBuilder(); boolean first = true; - for ( char[] c : typeName ) { + for (char[] c : typeName) { sb.append(first ? "" : ".").append(c); first = false; } @@ -148,10 +147,10 @@ public class Eclipse { * is complicated and there's no clone method on TypeParameter itself. This method can clone them. */ public static TypeParameter[] copyTypeParams(TypeParameter[] params, ASTNode source) { - if ( params == null ) return null; + if (params == null) return null; TypeParameter[] out = new TypeParameter[params.length]; int idx = 0; - for ( TypeParameter param : params ) { + for (TypeParameter param : params) { TypeParameter o = new TypeParameter(); setGeneratedBy(o, source); o.annotations = param.annotations; @@ -164,10 +163,10 @@ public class Eclipse { o.declarationEnd = param.declarationEnd; o.declarationSourceStart = param.declarationSourceStart; o.declarationSourceEnd = param.declarationSourceEnd; - if ( param.bounds != null ) { + if (param.bounds != null) { TypeReference[] b = new TypeReference[param.bounds.length]; int idx2 = 0; - for ( TypeReference ref : param.bounds ) b[idx2++] = copyType(ref, source); + for (TypeReference ref : param.bounds) b[idx2++] = copyType(ref, source); o.bounds = b; } out[idx++] = o; @@ -180,10 +179,10 @@ public class Eclipse { * {@link #copyType(TypeReference)}. */ public static TypeReference[] copyTypes(TypeReference[] refs, ASTNode source) { - if ( refs == null ) return null; + if (refs == null) return null; TypeReference[] outs = new TypeReference[refs.length]; int idx = 0; - for ( TypeReference ref : refs ) { + for (TypeReference ref : refs) { outs[idx++] = copyType(ref, source); } return outs; @@ -195,18 +194,18 @@ public class Eclipse { * method on TypeReference itself. This method can clone them. */ public static TypeReference copyType(TypeReference ref, ASTNode source) { - if ( ref instanceof ParameterizedQualifiedTypeReference ) { + if (ref instanceof ParameterizedQualifiedTypeReference) { ParameterizedQualifiedTypeReference iRef = (ParameterizedQualifiedTypeReference) ref; TypeReference[][] args = null; - if ( iRef.typeArguments != null ) { + if (iRef.typeArguments != null) { args = new TypeReference[iRef.typeArguments.length][]; int idx = 0; - for ( TypeReference[] inRefArray : iRef.typeArguments ) { - if ( inRefArray == null ) args[idx++] = null; + for (TypeReference[] inRefArray : iRef.typeArguments) { + if (inRefArray == null) args[idx++] = null; else { TypeReference[] outRefArray = new TypeReference[inRefArray.length]; int idx2 = 0; - for ( TypeReference inRef : inRefArray ) { + for (TypeReference inRef : inRefArray) { outRefArray[idx2++] = copyType(inRef, source); } args[idx++] = outRefArray; @@ -218,28 +217,28 @@ public class Eclipse { return typeRef; } - if ( ref instanceof ArrayQualifiedTypeReference ) { + if (ref instanceof ArrayQualifiedTypeReference) { ArrayQualifiedTypeReference iRef = (ArrayQualifiedTypeReference) ref; TypeReference typeRef = new ArrayQualifiedTypeReference(iRef.tokens, iRef.dimensions(), iRef.sourcePositions); setGeneratedBy(typeRef, source); return typeRef; } - if ( ref instanceof QualifiedTypeReference ) { + if (ref instanceof QualifiedTypeReference) { QualifiedTypeReference iRef = (QualifiedTypeReference) ref; TypeReference typeRef = new QualifiedTypeReference(iRef.tokens, iRef.sourcePositions); setGeneratedBy(typeRef, source); return typeRef; } - if ( ref instanceof ParameterizedSingleTypeReference ) { + if (ref instanceof ParameterizedSingleTypeReference) { ParameterizedSingleTypeReference iRef = (ParameterizedSingleTypeReference) ref; TypeReference[] args = null; - if ( iRef.typeArguments != null ) { + if (iRef.typeArguments != null) { args = new TypeReference[iRef.typeArguments.length]; int idx = 0; - for ( TypeReference inRef : iRef.typeArguments ) { - if ( inRef == null ) args[idx++] = null; + for (TypeReference inRef : iRef.typeArguments) { + if (inRef == null) args[idx++] = null; else args[idx++] = copyType(inRef, source); } } @@ -249,14 +248,14 @@ public class Eclipse { return typeRef; } - if ( ref instanceof ArrayTypeReference ) { + if (ref instanceof ArrayTypeReference) { ArrayTypeReference iRef = (ArrayTypeReference) ref; TypeReference typeRef = new ArrayTypeReference(iRef.token, iRef.dimensions(), (long)iRef.sourceStart << 32 | iRef.sourceEnd); setGeneratedBy(typeRef, source); return typeRef; } - if ( ref instanceof Wildcard ) { + if (ref instanceof Wildcard) { Wildcard wildcard = new Wildcard(((Wildcard)ref).kind); wildcard.sourceStart = ref.sourceStart; wildcard.sourceEnd = ref.sourceEnd; @@ -264,7 +263,7 @@ public class Eclipse { return wildcard; } - if ( ref instanceof SingleTypeReference ) { + if (ref instanceof SingleTypeReference) { SingleTypeReference iRef = (SingleTypeReference) ref; TypeReference typeRef = new SingleTypeReference(iRef.token, (long)iRef.sourceStart << 32 | iRef.sourceEnd); setGeneratedBy(typeRef, source); @@ -284,10 +283,10 @@ public class Eclipse { if (annotations2 == null) annotations2 = new Annotation[0]; Annotation[] outs = new Annotation[annotations1.length + annotations2.length]; int idx = 0; - for ( Annotation annotation : annotations1 ) { + for (Annotation annotation : annotations1) { outs[idx++] = copyAnnotation(annotation, source); } - for ( Annotation annotation : annotations2 ) { + for (Annotation annotation : annotations2) { outs[idx++] = copyAnnotation(annotation, source); } return outs; @@ -328,10 +327,10 @@ public class Eclipse { * * This is a guess, but a decent one. */ - public static boolean annotationTypeMatches(Class type, Node node) { - if ( node.getKind() != Kind.ANNOTATION ) return false; + public static boolean annotationTypeMatches(Class type, EclipseNode node) { + if (node.getKind() != Kind.ANNOTATION) return false; TypeReference typeRef = ((Annotation)node.get()).type; - if ( typeRef == null || typeRef.getTypeName() == null ) return false; + if (typeRef == null || typeRef.getTypeName() == null) return false; String typeName = toQualifiedName(typeRef.getTypeName()); TypeLibrary library = new TypeLibrary(); @@ -339,8 +338,8 @@ public class Eclipse { TypeResolver resolver = new TypeResolver(library, node.getPackageDeclaration(), node.getImportStatements()); Collection 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; @@ -350,32 +349,32 @@ public class Eclipse { * Provides AnnotationValues with the data it needs to do its thing. */ public static AnnotationValues - createAnnotation(Class type, final Node annotationNode) { + createAnnotation(Class type, final EclipseNode annotationNode) { final Annotation annotation = (Annotation) annotationNode.get(); Map values = new HashMap(); final MemberValuePair[] pairs = annotation.memberValuePairs(); - 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 raws = new ArrayList(); List guesses = new ArrayList(); Expression fullExpression = null; Expression[] expressions = null; - if ( pairs != null ) for ( MemberValuePair pair : pairs ) { + if (pairs != null) for (MemberValuePair pair : pairs) { char[] n = pair.name; String mName = n == null ? "value" : new String(pair.name); - if ( mName.equals(name) ) fullExpression = pair.value; + if (mName.equals(name)) fullExpression = pair.value; } boolean isExplicit = fullExpression != null; - if ( isExplicit ) { - if ( fullExpression instanceof ArrayInitializer ) { + if (isExplicit) { + if (fullExpression instanceof ArrayInitializer) { expressions = ((ArrayInitializer)fullExpression).expressions; } else expressions = new Expression[] { fullExpression }; - if ( expressions != null ) for ( Expression ex : expressions ) { + if (expressions != null) for (Expression ex : expressions) { StringBuffer sb = new StringBuffer(); ex.print(0, sb); raws.add(sb.toString()); @@ -389,10 +388,10 @@ public class Eclipse { values.put(name, new AnnotationValue(annotationNode, raws, guesses, isExplicit) { @Override public void setError(String message, int valueIdx) { Expression ex; - if ( valueIdx == -1 ) ex = fullExpr; + if (valueIdx == -1) ex = fullExpr; else ex = exprs != null ? exprs[valueIdx] : null; - if ( ex == null ) ex = annotation; + if (ex == null) ex = annotation; int sourceStart = ex.sourceStart; int sourceEnd = ex.sourceEnd; @@ -402,10 +401,10 @@ public class Eclipse { @Override public void setWarning(String message, int valueIdx) { Expression ex; - if ( valueIdx == -1 ) ex = fullExpr; + if (valueIdx == -1) ex = fullExpr; else ex = exprs != null ? exprs[valueIdx] : null; - if ( ex == null ) ex = annotation; + if (ex == null) ex = annotation; int sourceStart = ex.sourceStart; int sourceEnd = ex.sourceEnd; @@ -419,9 +418,9 @@ public class Eclipse { } private static Object calculateValue(Expression e) { - if ( e instanceof Literal ) { + if (e instanceof Literal) { ((Literal)e).computeConstant(); - switch ( e.constant.typeID() ) { + switch (e.constant.typeID()) { case TypeIds.T_int: return e.constant.intValue(); case TypeIds.T_byte: return e.constant.byteValue(); case TypeIds.T_short: return e.constant.shortValue(); @@ -433,11 +432,11 @@ public class Eclipse { case TypeIds.T_JavaLangString: return e.constant.stringValue(); default: return null; } - } else if ( e instanceof ClassLiteralAccess ) { + } else if (e instanceof ClassLiteralAccess) { return Eclipse.toQualifiedName(((ClassLiteralAccess)e).type.getTypeName()); - } else if ( e instanceof SingleNameReference ) { + } else if (e instanceof SingleNameReference) { return new String(((SingleNameReference)e).token); - } else if ( e instanceof QualifiedNameReference ) { + } else if (e instanceof QualifiedNameReference) { String qName = Eclipse.toQualifiedName(((QualifiedNameReference)e).tokens); int idx = qName.lastIndexOf('.'); return idx == -1 ? qName : qName.substring(idx+1); @@ -451,7 +450,7 @@ public class Eclipse { static { try { generatedByField = ASTNode.class.getDeclaredField("$generatedBy"); - } catch ( Throwable t ) { + } catch (Throwable t) { throw Lombok.sneakyThrow(t); } } @@ -459,7 +458,7 @@ public class Eclipse { public static ASTNode getGeneratedBy(ASTNode node) { try { return (ASTNode) generatedByField.get(node); - } catch ( Exception t ) { + } catch (Exception t) { throw Lombok.sneakyThrow(t); } } @@ -471,7 +470,7 @@ public class Eclipse { public static ASTNode setGeneratedBy(ASTNode node, ASTNode source) { try { generatedByField.set(node, source); - } catch ( Exception t ) { + } catch (Exception t) { throw Lombok.sneakyThrow(t); } diff --git a/src/lombok/eclipse/EclipseAST.java b/src/lombok/eclipse/EclipseAST.java index b70e2db6..e42e5de2 100644 --- a/src/lombok/eclipse/EclipseAST.java +++ b/src/lombok/eclipse/EclipseAST.java @@ -34,7 +34,6 @@ import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; -import org.eclipse.jdt.internal.compiler.ast.Clinit; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.ImportReference; @@ -50,7 +49,7 @@ import org.eclipse.jdt.internal.compiler.util.Util; * Wraps around Eclipse's internal AST view to add useful features as well as the ability to visit parents from children, * something Eclipse own AST system does not offer. */ -public class EclipseAST extends AST { +public class EclipseAST extends AST { /** * Creates a new EclipseAST of the provided Compilation Unit. * @@ -74,9 +73,9 @@ public class EclipseAST extends AST { @Override public Collection getImportStatements() { List imports = new ArrayList(); CompilationUnitDeclaration cud = (CompilationUnitDeclaration) top().get(); - if ( cud.imports == null ) return imports; - for ( ImportReference imp : cud.imports ) { - if ( imp == null ) continue; + if (cud.imports == null) return imports; + for (ImportReference imp : cud.imports) { + if (imp == null) continue; imports.add(Eclipse.toQualifiedName(imp.getImportName())); } @@ -91,8 +90,8 @@ public class EclipseAST extends AST { top().traverse(visitor); } - private void traverseChildren(EclipseASTVisitor visitor, Node node) { - for ( Node child : node.down() ) { + void traverseChildren(EclipseASTVisitor visitor, EclipseNode node) { + for (EclipseNode child : node.down()) { child.traverse(visitor); } } @@ -108,17 +107,7 @@ public class EclipseAST extends AST { return completeParse; } - /** {@inheritDoc} */ - @Override public Node top() { - return (Node) super.top(); - } - - /** {@inheritDoc} */ - @Override public Node get(ASTNode node) { - return (Node) super.get(node); - } - - private class ParseProblem { + class ParseProblem { final boolean isWarning; final String message; final int sourceStart; @@ -138,16 +127,16 @@ public class EclipseAST extends AST { } private void propagateProblems() { - if ( queuedProblems.isEmpty() ) return; + if (queuedProblems.isEmpty()) return; CompilationUnitDeclaration cud = (CompilationUnitDeclaration) top().get(); - if ( cud.compilationResult == null ) return; - for ( ParseProblem problem : queuedProblems ) problem.addToCompilationResult(); + if (cud.compilationResult == null) return; + for (ParseProblem problem : queuedProblems) problem.addToCompilationResult(); queuedProblems.clear(); } private final List queuedProblems = new ArrayList(); - private void addProblem(ParseProblem problem) { + void addProblem(ParseProblem problem) { queuedProblems.add(problem); propagateProblems(); } @@ -158,9 +147,9 @@ public class EclipseAST extends AST { */ static void addProblemToCompilationResult(CompilationUnitDeclaration ast, boolean isWarning, String message, int sourceStart, int sourceEnd) { - if ( ast.compilationResult == null ) return; + if (ast.compilationResult == null) return; char[] fileNameArray = ast.getFileName(); - if ( fileNameArray == null ) fileNameArray = "(unknown).java".toCharArray(); + if (fileNameArray == null) fileNameArray = "(unknown).java".toCharArray(); int lineNumber = 0; int columnNumber = 1; CompilationResult result = ast.compilationResult; @@ -197,171 +186,6 @@ public class EclipseAST extends AST { } } - /** - * Eclipse specific version of the AST.Node class. - */ - public final class Node extends AST.Node { - /** - * See the {@link AST.Node} constructor for information. - */ - Node(ASTNode node, List 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(EclipseASTVisitor visitor) { - switch ( getKind() ) { - case COMPILATION_UNIT: - visitor.visitCompilationUnit(this, (CompilationUnitDeclaration)get()); - traverseChildren(visitor, this); - visitor.endVisitCompilationUnit(this, (CompilationUnitDeclaration)get()); - break; - case TYPE: - visitor.visitType(this, (TypeDeclaration)get()); - traverseChildren(visitor, this); - visitor.endVisitType(this, (TypeDeclaration)get()); - break; - case FIELD: - visitor.visitField(this, (FieldDeclaration)get()); - traverseChildren(visitor, this); - visitor.endVisitField(this, (FieldDeclaration)get()); - break; - case INITIALIZER: - visitor.visitInitializer(this, (Initializer)get()); - traverseChildren(visitor, this); - visitor.endVisitInitializer(this, (Initializer)get()); - break; - case METHOD: - if ( get() instanceof Clinit ) return; - visitor.visitMethod(this, (AbstractMethodDeclaration)get()); - traverseChildren(visitor, this); - visitor.endVisitMethod(this, (AbstractMethodDeclaration)get()); - break; - case ARGUMENT: - AbstractMethodDeclaration method = (AbstractMethodDeclaration)up().get(); - visitor.visitMethodArgument(this, (Argument)get(), method); - traverseChildren(visitor, this); - visitor.endVisitMethodArgument(this, (Argument)get(), method); - break; - case LOCAL: - visitor.visitLocal(this, (LocalDeclaration)get()); - traverseChildren(visitor, this); - visitor.endVisitLocal(this, (LocalDeclaration)get()); - break; - case ANNOTATION: - switch ( up().getKind() ) { - case TYPE: - visitor.visitAnnotationOnType((TypeDeclaration)up().get(), this, (Annotation)get()); - break; - case FIELD: - visitor.visitAnnotationOnField((FieldDeclaration)up().get(), this, (Annotation)get()); - break; - case METHOD: - visitor.visitAnnotationOnMethod((AbstractMethodDeclaration)up().get(), this, (Annotation)get()); - break; - case ARGUMENT: - visitor.visitAnnotationOnMethodArgument( - (Argument)parent.get(), - (AbstractMethodDeclaration)parent.directUp().get(), - this, (Annotation)get()); - break; - case LOCAL: - visitor.visitAnnotationOnLocal((LocalDeclaration)parent.get(), this, (Annotation)get()); - break; - default: - throw new AssertionError("Annotion not expected as child of a " + up().getKind()); - } - break; - case STATEMENT: - visitor.visitStatement(this, (Statement)get()); - traverseChildren(visitor, this); - visitor.endVisitStatement(this, (Statement)get()); - break; - default: - throw new AssertionError("Unexpected kind during node traversal: " + getKind()); - } - } - - /** {@inheritDoc} */ - @Override public String getName() { - final char[] n; - if ( node instanceof TypeDeclaration ) n = ((TypeDeclaration)node).name; - else if ( node instanceof FieldDeclaration ) n = ((FieldDeclaration)node).name; - else if ( node instanceof AbstractMethodDeclaration ) n = ((AbstractMethodDeclaration)node).selector; - else if ( node instanceof LocalDeclaration ) n = ((LocalDeclaration)node).name; - else n = null; - - return n == null ? null : new String(n); - } - - /** {@inheritDoc} */ - @Override public void addError(String message) { - this.addError(message, this.get().sourceStart, this.get().sourceEnd); - } - - /** Generate a compiler error that shows the wavy underline from-to the stated character positions. */ - public void addError(String message, int sourceStart, int sourceEnd) { - addProblem(new ParseProblem(false, message, sourceStart, sourceEnd)); - } - - /** {@inheritDoc} */ - @Override public void addWarning(String message) { - this.addWarning(message, this.get().sourceStart, this.get().sourceEnd); - } - - /** Generate a compiler warning that shows the wavy underline from-to the stated character positions. */ - public void addWarning(String message, int sourceStart, int sourceEnd) { - addProblem(new ParseProblem(true, message, sourceStart, sourceEnd)); - } - - /** {@inheritDoc} */ - @Override public Node up() { - return (Node) super.up(); - } - - /** {@inheritDoc} */ - @Override protected boolean calculateIsStructurallySignificant() { - if ( node instanceof TypeDeclaration ) return true; - if ( node instanceof AbstractMethodDeclaration ) return true; - if ( node instanceof FieldDeclaration ) return true; - if ( node instanceof LocalDeclaration ) return true; - if ( node instanceof CompilationUnitDeclaration ) return true; - return false; - } - - /** {@inheritDoc} */ - @Override public Node getNodeFor(ASTNode obj) { - return (Node) super.getNodeFor(obj); - } - - /** {@inheritDoc} */ - public Node directUp() { - return (Node) super.directUp(); - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public Collection down() { - return (Collection) super.down(); - } - - /** {@inheritDoc} */ - @Override public Node top() { - return (Node) super.top(); - } - - /** - * Convenient shortcut to the owning EclipseAST object's isCompleteParse method. - * - * @see EclipseAST#isCompleteParse() - */ - public boolean isCompleteParse() { - return completeParse; - } - } - private final CompilationUnitDeclaration compilationUnitDeclaration; private boolean completeParse; @@ -370,15 +194,15 @@ public class EclipseAST extends AST { } /** - * Call to move an EclipseAST generated for a diet parse to rebuild itself for the full parse - + * Call this method to move an EclipseAST generated for a diet parse to rebuild itself for the full parse - * with filled in method bodies and such. Also propagates problems and errors, which in diet parse * mode can't be reliably added to the problems/warnings view. */ public void reparse() { propagateProblems(); - if ( completeParse ) return; + if (completeParse) return; boolean newCompleteParse = isComplete(compilationUnitDeclaration); - if ( !newCompleteParse ) return; + if (!newCompleteParse) return; top().rebuild(); @@ -390,8 +214,8 @@ public class EclipseAST extends AST { } /** {@inheritDoc} */ - @Override protected Node buildTree(ASTNode node, Kind kind) { - switch ( kind ) { + @Override protected EclipseNode buildTree(ASTNode node, Kind kind) { + switch (kind) { case COMPILATION_UNIT: return buildCompilationUnit((CompilationUnitDeclaration) node); case TYPE: @@ -415,126 +239,126 @@ public class EclipseAST extends AST { } } - private Node buildCompilationUnit(CompilationUnitDeclaration top) { - if ( setAndGetAsHandled(top) ) return null; - List children = buildTypes(top.types); - return putInMap(new Node(top, children, Kind.COMPILATION_UNIT)); + private EclipseNode buildCompilationUnit(CompilationUnitDeclaration top) { + if (setAndGetAsHandled(top)) return null; + List children = buildTypes(top.types); + return putInMap(new EclipseNode(this, top, children, Kind.COMPILATION_UNIT)); } - private void addIfNotNull(Collection collection, Node n) { - if ( n != null ) collection.add(n); + private void addIfNotNull(Collection collection, EclipseNode n) { + if (n != null) collection.add(n); } - private List buildTypes(TypeDeclaration[] children) { - List childNodes = new ArrayList(); - if ( children != null ) for ( TypeDeclaration type : children ) addIfNotNull(childNodes, buildType(type)); + private List buildTypes(TypeDeclaration[] children) { + List childNodes = new ArrayList(); + if (children != null) for (TypeDeclaration type : children) addIfNotNull(childNodes, buildType(type)); return childNodes; } - private Node buildType(TypeDeclaration type) { - if ( setAndGetAsHandled(type) ) return null; - List childNodes = new ArrayList(); + private EclipseNode buildType(TypeDeclaration type) { + if (setAndGetAsHandled(type)) return null; + List childNodes = new ArrayList(); childNodes.addAll(buildFields(type.fields)); childNodes.addAll(buildTypes(type.memberTypes)); childNodes.addAll(buildMethods(type.methods)); childNodes.addAll(buildAnnotations(type.annotations)); - return putInMap(new Node(type, childNodes, Kind.TYPE)); + return putInMap(new EclipseNode(this, type, childNodes, Kind.TYPE)); } - private Collection buildFields(FieldDeclaration[] children) { - List childNodes = new ArrayList(); - if ( children != null ) for ( FieldDeclaration child : children ) addIfNotNull(childNodes, buildField(child)); + private Collection buildFields(FieldDeclaration[] children) { + List childNodes = new ArrayList(); + if (children != null) for (FieldDeclaration child : children) addIfNotNull(childNodes, buildField(child)); return childNodes; } private static List singleton(T item) { List list = new ArrayList(); - if ( item != null ) list.add(item); + if (item != null) list.add(item); return list; } - private Node buildField(FieldDeclaration field) { - if ( field instanceof Initializer ) return buildInitializer((Initializer)field); - if ( setAndGetAsHandled(field) ) return null; - List childNodes = new ArrayList(); + private EclipseNode buildField(FieldDeclaration field) { + if (field instanceof Initializer) return buildInitializer((Initializer)field); + if (setAndGetAsHandled(field)) return null; + List childNodes = new ArrayList(); addIfNotNull(childNodes, buildStatement(field.initialization)); childNodes.addAll(buildAnnotations(field.annotations)); - return putInMap(new Node(field, childNodes, Kind.FIELD)); + return putInMap(new EclipseNode(this, field, childNodes, Kind.FIELD)); } - private Node buildInitializer(Initializer initializer) { - if ( setAndGetAsHandled(initializer) ) return null; - return putInMap(new Node(initializer, singleton(buildStatement(initializer.block)), Kind.INITIALIZER)); + private EclipseNode buildInitializer(Initializer initializer) { + if (setAndGetAsHandled(initializer)) return null; + return putInMap(new EclipseNode(this, initializer, singleton(buildStatement(initializer.block)), Kind.INITIALIZER)); } - private Collection buildMethods(AbstractMethodDeclaration[] children) { - List childNodes = new ArrayList(); - if ( children != null ) for (AbstractMethodDeclaration method : children ) addIfNotNull(childNodes, buildMethod(method)); + private Collection buildMethods(AbstractMethodDeclaration[] children) { + List childNodes = new ArrayList(); + if (children != null) for (AbstractMethodDeclaration method : children) addIfNotNull(childNodes, buildMethod(method)); return childNodes; } - private Node buildMethod(AbstractMethodDeclaration method) { - if ( setAndGetAsHandled(method) ) return null; - List childNodes = new ArrayList(); + private EclipseNode buildMethod(AbstractMethodDeclaration method) { + if (setAndGetAsHandled(method)) return null; + List childNodes = new ArrayList(); childNodes.addAll(buildArguments(method.arguments)); childNodes.addAll(buildStatements(method.statements)); childNodes.addAll(buildAnnotations(method.annotations)); - return putInMap(new Node(method, childNodes, Kind.METHOD)); + return putInMap(new EclipseNode(this, method, childNodes, Kind.METHOD)); } //Arguments are a kind of LocalDeclaration. They can definitely contain lombok annotations, so we care about them. - private Collection buildArguments(Argument[] children) { - List childNodes = new ArrayList(); - if ( children != null ) for ( LocalDeclaration local : children ) { + private Collection buildArguments(Argument[] children) { + List childNodes = new ArrayList(); + if (children != null) for (LocalDeclaration local : children) { addIfNotNull(childNodes, buildLocal(local, Kind.ARGUMENT)); } return childNodes; } - private Node buildLocal(LocalDeclaration local, Kind kind) { - if ( setAndGetAsHandled(local) ) return null; - List childNodes = new ArrayList(); + private EclipseNode buildLocal(LocalDeclaration local, Kind kind) { + if (setAndGetAsHandled(local)) return null; + List childNodes = new ArrayList(); addIfNotNull(childNodes, buildStatement(local.initialization)); childNodes.addAll(buildAnnotations(local.annotations)); - return putInMap(new Node(local, childNodes, kind)); + return putInMap(new EclipseNode(this, local, childNodes, kind)); } - private Collection buildAnnotations(Annotation[] annotations) { - List elements = new ArrayList(); - if ( annotations != null ) for ( Annotation an : annotations ) addIfNotNull(elements, buildAnnotation(an)); + private Collection buildAnnotations(Annotation[] annotations) { + List elements = new ArrayList(); + if (annotations != null) for (Annotation an : annotations) addIfNotNull(elements, buildAnnotation(an)); return elements; } - private Node buildAnnotation(Annotation annotation) { - if ( annotation == null ) return null; - if ( setAndGetAsHandled(annotation) ) return null; - return putInMap(new Node(annotation, null, Kind.ANNOTATION)); + private EclipseNode buildAnnotation(Annotation annotation) { + if (annotation == null) return null; + if (setAndGetAsHandled(annotation)) return null; + return putInMap(new EclipseNode(this, annotation, null, Kind.ANNOTATION)); } - private Collection buildStatements(Statement[] children) { - List childNodes = new ArrayList(); - if ( children != null ) for ( Statement child : children ) addIfNotNull(childNodes, buildStatement(child)); + private Collection buildStatements(Statement[] children) { + List childNodes = new ArrayList(); + if (children != null) for (Statement child : children) addIfNotNull(childNodes, buildStatement(child)); return childNodes; } - private Node buildStatement(Statement child) { - if ( child == null ) return null; - if ( child instanceof TypeDeclaration ) return buildType((TypeDeclaration)child); + private EclipseNode buildStatement(Statement child) { + if (child == null) return null; + if (child instanceof TypeDeclaration) return buildType((TypeDeclaration)child); - if ( child instanceof LocalDeclaration ) return buildLocal((LocalDeclaration)child, Kind.LOCAL); + if (child instanceof LocalDeclaration) return buildLocal((LocalDeclaration)child, Kind.LOCAL); - if ( setAndGetAsHandled(child) ) return null; + if (setAndGetAsHandled(child)) return null; return drill(child); } - private Node drill(Statement statement) { - List childNodes = new ArrayList(); - for ( FieldAccess fa : fieldsOf(statement.getClass()) ) childNodes.addAll(buildWithField(Node.class, statement, fa)); - return putInMap(new Node(statement, childNodes, Kind.STATEMENT)); + private EclipseNode drill(Statement statement) { + List childNodes = new ArrayList(); + for (FieldAccess fa : fieldsOf(statement.getClass())) childNodes.addAll(buildWithField(EclipseNode.class, statement, fa)); + return putInMap(new EclipseNode(this, statement, childNodes, Kind.STATEMENT)); } - /** For Eclipse, only Statement counts, as Expression is a subclass of it, eventhough this isn't + /** For Eclipse, only Statement counts, as Expression is a subclass of it, even though this isn't * entirely correct according to the JLS spec (only some expressions can be used as statements, not all of them). */ @Override protected Collection> getStatementTypes() { return Collections.>singleton(Statement.class); diff --git a/src/lombok/eclipse/EclipseASTAdapter.java b/src/lombok/eclipse/EclipseASTAdapter.java index bb185670..2062619c 100644 --- a/src/lombok/eclipse/EclipseASTAdapter.java +++ b/src/lombok/eclipse/EclipseASTAdapter.java @@ -21,8 +21,6 @@ */ package lombok.eclipse; -import lombok.eclipse.EclipseAST.Node; - import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; @@ -39,65 +37,65 @@ import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; */ public abstract class EclipseASTAdapter implements EclipseASTVisitor { /** {@inheritDoc} */ - public void visitCompilationUnit(Node top, CompilationUnitDeclaration unit) {} + public void visitCompilationUnit(EclipseNode top, CompilationUnitDeclaration unit) {} /** {@inheritDoc} */ - public void endVisitCompilationUnit(Node top, CompilationUnitDeclaration unit) {} + public void endVisitCompilationUnit(EclipseNode top, CompilationUnitDeclaration unit) {} /** {@inheritDoc} */ - public void visitType(Node typeNode, TypeDeclaration type) {} + public void visitType(EclipseNode typeNode, TypeDeclaration type) {} /** {@inheritDoc} */ - public void visitAnnotationOnType(TypeDeclaration type, Node annotationNode, Annotation annotation) {} + public void visitAnnotationOnType(TypeDeclaration type, EclipseNode annotationNode, Annotation annotation) {} /** {@inheritDoc} */ - public void endVisitType(Node typeNode, TypeDeclaration type) {} + public void endVisitType(EclipseNode typeNode, TypeDeclaration type) {} /** {@inheritDoc} */ - public void visitInitializer(Node initializerNode, Initializer initializer) {} + public void visitInitializer(EclipseNode initializerNode, Initializer initializer) {} /** {@inheritDoc} */ - public void endVisitInitializer(Node initializerNode, Initializer initializer) {} + public void endVisitInitializer(EclipseNode initializerNode, Initializer initializer) {} /** {@inheritDoc} */ - public void visitField(Node fieldNode, FieldDeclaration field) {} + public void visitField(EclipseNode fieldNode, FieldDeclaration field) {} /** {@inheritDoc} */ - public void visitAnnotationOnField(FieldDeclaration field, Node annotationNode, Annotation annotation) {} + public void visitAnnotationOnField(FieldDeclaration field, EclipseNode annotationNode, Annotation annotation) {} /** {@inheritDoc} */ - public void endVisitField(Node fieldNode, FieldDeclaration field) {} + public void endVisitField(EclipseNode fieldNode, FieldDeclaration field) {} /** {@inheritDoc} */ - public void visitMethod(Node methodNode, AbstractMethodDeclaration method) {} + public void visitMethod(EclipseNode methodNode, AbstractMethodDeclaration method) {} /** {@inheritDoc} */ - public void visitAnnotationOnMethod(AbstractMethodDeclaration method, Node annotationNode, Annotation annotation) {} + public void visitAnnotationOnMethod(AbstractMethodDeclaration method, EclipseNode annotationNode, Annotation annotation) {} /** {@inheritDoc} */ - public void endVisitMethod(Node methodNode, AbstractMethodDeclaration method) {} + public void endVisitMethod(EclipseNode methodNode, AbstractMethodDeclaration method) {} /** {@inheritDoc} */ - public void visitMethodArgument(Node argNode, Argument arg, AbstractMethodDeclaration method) {} + public void visitMethodArgument(EclipseNode argNode, Argument arg, AbstractMethodDeclaration method) {} /** {@inheritDoc} */ - public void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, Node annotationNode, Annotation annotation) {} + public void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, EclipseNode annotationNode, Annotation annotation) {} /** {@inheritDoc} */ - public void endVisitMethodArgument(Node argNode, Argument arg, AbstractMethodDeclaration method) {} + public void endVisitMethodArgument(EclipseNode argNode, Argument arg, AbstractMethodDeclaration method) {} /** {@inheritDoc} */ - public void visitLocal(Node localNode, LocalDeclaration local) {} + public void visitLocal(EclipseNode localNode, LocalDeclaration local) {} /** {@inheritDoc} */ - public void visitAnnotationOnLocal(LocalDeclaration local, Node annotationNode, Annotation annotation) {} + public void visitAnnotationOnLocal(LocalDeclaration local, EclipseNode annotationNode, Annotation annotation) {} /** {@inheritDoc} */ - public void endVisitLocal(Node localNode, LocalDeclaration local) {} + public void endVisitLocal(EclipseNode localNode, LocalDeclaration local) {} /** {@inheritDoc} */ - public void visitStatement(Node statementNode, Statement statement) {} + public void visitStatement(EclipseNode statementNode, Statement statement) {} /** {@inheritDoc} */ - public void endVisitStatement(Node statementNode, Statement statement) {} + public void endVisitStatement(EclipseNode statementNode, Statement statement) {} } diff --git a/src/lombok/eclipse/EclipseASTVisitor.java b/src/lombok/eclipse/EclipseASTVisitor.java index 39c785d1..3397e1cc 100644 --- a/src/lombok/eclipse/EclipseASTVisitor.java +++ b/src/lombok/eclipse/EclipseASTVisitor.java @@ -24,8 +24,6 @@ package lombok.eclipse; import java.io.PrintStream; import java.lang.reflect.Modifier; -import lombok.eclipse.EclipseAST.Node; - import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; @@ -47,15 +45,15 @@ public interface EclipseASTVisitor { /** * Called at the very beginning and end. */ - void visitCompilationUnit(Node top, CompilationUnitDeclaration unit); - void endVisitCompilationUnit(Node top, CompilationUnitDeclaration unit); + void visitCompilationUnit(EclipseNode top, CompilationUnitDeclaration unit); + void endVisitCompilationUnit(EclipseNode top, CompilationUnitDeclaration unit); /** * Called when visiting a type (a class, interface, annotation, enum, etcetera). */ - void visitType(Node typeNode, TypeDeclaration type); - void visitAnnotationOnType(TypeDeclaration type, Node annotationNode, Annotation annotation); - void endVisitType(Node typeNode, TypeDeclaration type); + void visitType(EclipseNode typeNode, TypeDeclaration type); + void visitAnnotationOnType(TypeDeclaration type, EclipseNode annotationNode, Annotation annotation); + void endVisitType(EclipseNode typeNode, TypeDeclaration type); /** * Called when visiting a field of a class. @@ -63,46 +61,46 @@ public interface EclipseASTVisitor { * which are a subclass of FieldDeclaration, those do NOT result in a call to this method. They result * in a call to the visitInitializer method. */ - void visitField(Node fieldNode, FieldDeclaration field); - void visitAnnotationOnField(FieldDeclaration field, Node annotationNode, Annotation annotation); - void endVisitField(Node fieldNode, FieldDeclaration field); + void visitField(EclipseNode fieldNode, FieldDeclaration field); + void visitAnnotationOnField(FieldDeclaration field, EclipseNode annotationNode, Annotation annotation); + void endVisitField(EclipseNode fieldNode, FieldDeclaration field); /** * Called for static and instance initializers. You can tell the difference via the modifier flag on the * ASTNode (8 for static, 0 for not static). The content is in the 'block', not in the 'initialization', * which would always be null for an initializer instance. */ - void visitInitializer(Node initializerNode, Initializer initializer); - void endVisitInitializer(Node initializerNode, Initializer initializer); + void visitInitializer(EclipseNode initializerNode, Initializer initializer); + void endVisitInitializer(EclipseNode initializerNode, Initializer initializer); /** * Called for both methods (MethodDeclaration) and constructors (ConstructorDeclaration), but not for * Clinit objects, which are a vestigial Eclipse thing that never contain anything. Static initializers * show up as 'Initializer', in the visitInitializer method, with modifier bit STATIC set. */ - void visitMethod(Node methodNode, AbstractMethodDeclaration method); - void visitAnnotationOnMethod(AbstractMethodDeclaration method, Node annotationNode, Annotation annotation); - void endVisitMethod(Node methodNode, AbstractMethodDeclaration method); + void visitMethod(EclipseNode methodNode, AbstractMethodDeclaration method); + void visitAnnotationOnMethod(AbstractMethodDeclaration method, EclipseNode annotationNode, Annotation annotation); + void endVisitMethod(EclipseNode methodNode, AbstractMethodDeclaration method); /** * Visits a method argument */ - void visitMethodArgument(Node argNode, Argument arg, AbstractMethodDeclaration method); - void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, Node annotationNode, Annotation annotation); - void endVisitMethodArgument(Node argNode, Argument arg, AbstractMethodDeclaration method); + void visitMethodArgument(EclipseNode argNode, Argument arg, AbstractMethodDeclaration method); + void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, EclipseNode annotationNode, Annotation annotation); + void endVisitMethodArgument(EclipseNode argNode, Argument arg, AbstractMethodDeclaration method); /** * Visits a local declaration - that is, something like 'int x = 10;' on the method level. */ - void visitLocal(Node localNode, LocalDeclaration local); - void visitAnnotationOnLocal(LocalDeclaration local, Node annotationNode, Annotation annotation); - void endVisitLocal(Node localNode, LocalDeclaration local); + void visitLocal(EclipseNode localNode, LocalDeclaration local); + void visitAnnotationOnLocal(LocalDeclaration local, EclipseNode annotationNode, Annotation annotation); + void endVisitLocal(EclipseNode localNode, LocalDeclaration local); /** * Visits a statement that isn't any of the other visit methods (e.g. TypeDeclaration). */ - void visitStatement(Node statementNode, Statement statement); - void endVisitStatement(Node statementNode, Statement statement); + void visitStatement(EclipseNode statementNode, Statement statement); + void endVisitStatement(EclipseNode statementNode, Statement statement); /** * Prints the structure of an AST. @@ -135,33 +133,33 @@ public interface EclipseASTVisitor { private void forcePrint(String text, Object... params) { StringBuilder sb = new StringBuilder(); - for ( int i = 0 ; i < indent ; i++ ) sb.append(" "); + for (int i = 0; i < indent; i++) sb.append(" "); out.printf(sb.append(text).append('\n').toString(), params); out.flush(); } private void print(String text, Object... params) { - if ( disablePrinting == 0 ) forcePrint(text, params); + if (disablePrinting == 0) forcePrint(text, params); } private String str(char[] c) { - if ( c == null ) return "(NULL)"; + if (c == null) return "(NULL)"; else return new String(c); } private String str(TypeReference type) { - if ( type == null ) return "(NULL)"; + if (type == null) return "(NULL)"; char[][] c = type.getTypeName(); StringBuilder sb = new StringBuilder(); boolean first = true; - for ( char[] d : c ) { + for (char[] d : c) { sb.append(first ? "" : ".").append(new String(d)); first = false; } return sb.toString(); } - public void visitCompilationUnit(Node node, CompilationUnitDeclaration unit) { + public void visitCompilationUnit(EclipseNode node, CompilationUnitDeclaration unit) { out.println("---------------------------------------------------------"); out.println(node.isCompleteParse() ? "COMPLETE" : "incomplete"); @@ -169,31 +167,31 @@ public interface EclipseASTVisitor { indent++; } - public void endVisitCompilationUnit(Node node, CompilationUnitDeclaration unit) { + public void endVisitCompilationUnit(EclipseNode node, CompilationUnitDeclaration unit) { indent--; print(""); } - public void visitType(Node node, TypeDeclaration type) { + public void visitType(EclipseNode node, TypeDeclaration type) { print("", str(type.name), Eclipse.isGenerated(type) ? " (GENERATED)" : ""); indent++; - if ( printContent ) { + if (printContent) { print("%s", type); disablePrinting++; } } - public void visitAnnotationOnType(TypeDeclaration type, Node node, Annotation annotation) { + public void visitAnnotationOnType(TypeDeclaration type, EclipseNode node, Annotation annotation) { forcePrint("", Eclipse.isGenerated(annotation) ? " (GENERATED)" : "", annotation); } - public void endVisitType(Node node, TypeDeclaration type) { - if ( printContent ) disablePrinting--; + public void endVisitType(EclipseNode node, TypeDeclaration type) { + if (printContent) disablePrinting--; indent--; print("", str(type.name)); } - public void visitInitializer(Node node, Initializer initializer) { + public void visitInitializer(EclipseNode node, Initializer initializer) { Block block = initializer.block; boolean s = (block != null && block.statements != null); print("<%s INITIALIZER: %s%s>", @@ -201,95 +199,95 @@ public interface EclipseASTVisitor { s ? "filled" : "blank", Eclipse.isGenerated(initializer) ? " (GENERATED)" : ""); indent++; - if ( printContent ) { - if ( initializer.block != null ) print("%s", initializer.block); + if (printContent) { + if (initializer.block != null) print("%s", initializer.block); disablePrinting++; } } - public void endVisitInitializer(Node node, Initializer initializer) { - if ( printContent ) disablePrinting--; + public void endVisitInitializer(EclipseNode node, Initializer initializer) { + if (printContent) disablePrinting--; indent--; print("", (initializer.modifiers & Modifier.STATIC) != 0 ? "static" : "instance"); } - public void visitField(Node node, FieldDeclaration field) { + public void visitField(EclipseNode node, FieldDeclaration field) { print("", Eclipse.isGenerated(field) ? " (GENERATED)" : "", str(field.type), str(field.name), field.initialization); indent++; - if ( printContent ) { - if ( field.initialization != null ) print("%s", field.initialization); + if (printContent) { + if (field.initialization != null) print("%s", field.initialization); disablePrinting++; } } - public void visitAnnotationOnField(FieldDeclaration field, Node node, Annotation annotation) { + public void visitAnnotationOnField(FieldDeclaration field, EclipseNode node, Annotation annotation) { forcePrint("", Eclipse.isGenerated(annotation) ? " (GENERATED)" : "", annotation); } - public void endVisitField(Node node, FieldDeclaration field) { - if ( printContent ) disablePrinting--; + public void endVisitField(EclipseNode node, FieldDeclaration field) { + if (printContent) disablePrinting--; indent--; print("", str(field.type), str(field.name)); } - public void visitMethod(Node node, AbstractMethodDeclaration method) { + public void visitMethod(EclipseNode node, AbstractMethodDeclaration method) { String type = method instanceof ConstructorDeclaration ? "CONSTRUCTOR" : "METHOD"; print("<%s %s: %s%s>", type, str(method.selector), method.statements != null ? "filled" : "blank", Eclipse.isGenerated(method) ? " (GENERATED)" : ""); indent++; - if ( printContent ) { - if ( method.statements != null ) print("%s", method); + if (printContent) { + if (method.statements != null) print("%s", method); disablePrinting++; } } - public void visitAnnotationOnMethod(AbstractMethodDeclaration method, Node node, Annotation annotation) { + public void visitAnnotationOnMethod(AbstractMethodDeclaration method, EclipseNode node, Annotation annotation) { forcePrint("", Eclipse.isGenerated(method) ? " (GENERATED)" : "", annotation); } - public void endVisitMethod(Node node, AbstractMethodDeclaration method) { - if ( printContent ) disablePrinting--; + public void endVisitMethod(EclipseNode node, AbstractMethodDeclaration method) { + if (printContent) disablePrinting--; String type = method instanceof ConstructorDeclaration ? "CONSTRUCTOR" : "METHOD"; indent--; print("", type, str(method.selector)); } - public void visitMethodArgument(Node node, Argument arg, AbstractMethodDeclaration method) { + public void visitMethodArgument(EclipseNode node, Argument arg, AbstractMethodDeclaration method) { print("", Eclipse.isGenerated(arg) ? " (GENERATED)" : "", str(arg.type), str(arg.name), arg.initialization); indent++; } - public void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, Node node, Annotation annotation) { + public void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, EclipseNode node, Annotation annotation) { print("", Eclipse.isGenerated(annotation) ? " (GENERATED)" : "", annotation); } - public void endVisitMethodArgument(Node node, Argument arg, AbstractMethodDeclaration method) { + public void endVisitMethodArgument(EclipseNode node, Argument arg, AbstractMethodDeclaration method) { indent--; print("", str(arg.type), str(arg.name)); } - public void visitLocal(Node node, LocalDeclaration local) { + public void visitLocal(EclipseNode node, LocalDeclaration local) { print("", Eclipse.isGenerated(local) ? " (GENERATED)" : "", str(local.type), str(local.name), local.initialization); indent++; } - public void visitAnnotationOnLocal(LocalDeclaration local, Node node, Annotation annotation) { + public void visitAnnotationOnLocal(LocalDeclaration local, EclipseNode node, Annotation annotation) { print("", Eclipse.isGenerated(annotation) ? " (GENERATED)" : "", annotation); } - public void endVisitLocal(Node node, LocalDeclaration local) { + public void endVisitLocal(EclipseNode node, LocalDeclaration local) { indent--; print("", str(local.type), str(local.name)); } - public void visitStatement(Node node, Statement statement) { + public void visitStatement(EclipseNode node, Statement statement) { print("<%s%s>", statement.getClass(), Eclipse.isGenerated(statement) ? " (GENERATED)" : ""); indent++; print("%s", statement); } - public void endVisitStatement(Node node, Statement statement) { + public void endVisitStatement(EclipseNode node, Statement statement) { indent--; print("", statement.getClass()); } diff --git a/src/lombok/eclipse/EclipseAnnotationHandler.java b/src/lombok/eclipse/EclipseAnnotationHandler.java index b5c84ebd..96626cca 100644 --- a/src/lombok/eclipse/EclipseAnnotationHandler.java +++ b/src/lombok/eclipse/EclipseAnnotationHandler.java @@ -50,5 +50,5 @@ public interface EclipseAnnotationHandlertrue if you don't want to be called again about this annotation during this * compile session (you've handled it), or false to indicate you aren't done yet. */ - boolean handle(AnnotationValues annotation, org.eclipse.jdt.internal.compiler.ast.Annotation ast, EclipseAST.Node annotationNode); + boolean handle(AnnotationValues annotation, org.eclipse.jdt.internal.compiler.ast.Annotation ast, EclipseNode annotationNode); } diff --git a/src/lombok/eclipse/EclipseNode.java b/src/lombok/eclipse/EclipseNode.java new file mode 100644 index 00000000..668e6a6e --- /dev/null +++ b/src/lombok/eclipse/EclipseNode.java @@ -0,0 +1,175 @@ +/* + * 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.eclipse; + +import java.util.List; + +import lombok.core.AST.Kind; + +import org.eclipse.jdt.internal.compiler.ast.ASTNode; +import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; +import org.eclipse.jdt.internal.compiler.ast.Annotation; +import org.eclipse.jdt.internal.compiler.ast.Argument; +import org.eclipse.jdt.internal.compiler.ast.Clinit; +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; +import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; +import org.eclipse.jdt.internal.compiler.ast.Initializer; +import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; +import org.eclipse.jdt.internal.compiler.ast.Statement; +import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; + +/** + * Eclipse specific version of the LombokNode class. + */ +public class EclipseNode extends lombok.core.LombokNode { + /** {@inheritDoc} */ + EclipseNode(EclipseAST ast, ASTNode node, List children, Kind kind) { + super(ast, node, children, kind); + } + + /** + * Visits this node and all child nodes depth-first, calling the provided visitor's visit methods. + */ + public void traverse(EclipseASTVisitor visitor) { + switch (getKind()) { + case COMPILATION_UNIT: + visitor.visitCompilationUnit(this, (CompilationUnitDeclaration)get()); + ast.traverseChildren(visitor, this); + visitor.endVisitCompilationUnit(this, (CompilationUnitDeclaration)get()); + break; + case TYPE: + visitor.visitType(this, (TypeDeclaration)get()); + ast.traverseChildren(visitor, this); + visitor.endVisitType(this, (TypeDeclaration)get()); + break; + case FIELD: + visitor.visitField(this, (FieldDeclaration)get()); + ast.traverseChildren(visitor, this); + visitor.endVisitField(this, (FieldDeclaration)get()); + break; + case INITIALIZER: + visitor.visitInitializer(this, (Initializer)get()); + ast.traverseChildren(visitor, this); + visitor.endVisitInitializer(this, (Initializer)get()); + break; + case METHOD: + if (get() instanceof Clinit) return; + visitor.visitMethod(this, (AbstractMethodDeclaration)get()); + ast.traverseChildren(visitor, this); + visitor.endVisitMethod(this, (AbstractMethodDeclaration)get()); + break; + case ARGUMENT: + AbstractMethodDeclaration method = (AbstractMethodDeclaration)up().get(); + visitor.visitMethodArgument(this, (Argument)get(), method); + ast.traverseChildren(visitor, this); + visitor.endVisitMethodArgument(this, (Argument)get(), method); + break; + case LOCAL: + visitor.visitLocal(this, (LocalDeclaration)get()); + ast.traverseChildren(visitor, this); + visitor.endVisitLocal(this, (LocalDeclaration)get()); + break; + case ANNOTATION: + switch (up().getKind()) { + case TYPE: + visitor.visitAnnotationOnType((TypeDeclaration)up().get(), this, (Annotation)get()); + break; + case FIELD: + visitor.visitAnnotationOnField((FieldDeclaration)up().get(), this, (Annotation)get()); + break; + case METHOD: + visitor.visitAnnotationOnMethod((AbstractMethodDeclaration)up().get(), this, (Annotation)get()); + break; + case ARGUMENT: + visitor.visitAnnotationOnMethodArgument( + (Argument)parent.get(), + (AbstractMethodDeclaration)parent.directUp().get(), + this, (Annotation)get()); + break; + case LOCAL: + visitor.visitAnnotationOnLocal((LocalDeclaration)parent.get(), this, (Annotation)get()); + break; + default: + throw new AssertionError("Annotion not expected as child of a " + up().getKind()); + } + break; + case STATEMENT: + visitor.visitStatement(this, (Statement)get()); + ast.traverseChildren(visitor, this); + visitor.endVisitStatement(this, (Statement)get()); + break; + default: + throw new AssertionError("Unexpected kind during node traversal: " + getKind()); + } + } + + /** {@inheritDoc} */ + @Override public String getName() { + final char[] n; + if (node instanceof TypeDeclaration) n = ((TypeDeclaration)node).name; + else if (node instanceof FieldDeclaration) n = ((FieldDeclaration)node).name; + else if (node instanceof AbstractMethodDeclaration) n = ((AbstractMethodDeclaration)node).selector; + else if (node instanceof LocalDeclaration) n = ((LocalDeclaration)node).name; + else n = null; + + return n == null ? null : new String(n); + } + + /** {@inheritDoc} */ + @Override public void addError(String message) { + this.addError(message, this.get().sourceStart, this.get().sourceEnd); + } + + /** Generate a compiler error that shows the wavy underline from-to the stated character positions. */ + public void addError(String message, int sourceStart, int sourceEnd) { + ast.addProblem(ast.new ParseProblem(false, message, sourceStart, sourceEnd)); + } + + /** {@inheritDoc} */ + @Override public void addWarning(String message) { + this.addWarning(message, this.get().sourceStart, this.get().sourceEnd); + } + + /** Generate a compiler warning that shows the wavy underline from-to the stated character positions. */ + public void addWarning(String message, int sourceStart, int sourceEnd) { + ast.addProblem(ast.new ParseProblem(true, message, sourceStart, sourceEnd)); + } + + /** {@inheritDoc} */ + @Override protected boolean calculateIsStructurallySignificant() { + if (node instanceof TypeDeclaration) return true; + if (node instanceof AbstractMethodDeclaration) return true; + if (node instanceof FieldDeclaration) return true; + if (node instanceof LocalDeclaration) return true; + if (node instanceof CompilationUnitDeclaration) return true; + return false; + } + + /** + * Convenient shortcut to the owning EclipseAST object's isCompleteParse method. + * + * @see EclipseAST#isCompleteParse() + */ + public boolean isCompleteParse() { + return ast.isCompleteParse(); + } +} diff --git a/src/lombok/eclipse/HandlerLibrary.java b/src/lombok/eclipse/HandlerLibrary.java index ed8196f7..be0e0b14 100644 --- a/src/lombok/eclipse/HandlerLibrary.java +++ b/src/lombok/eclipse/HandlerLibrary.java @@ -37,7 +37,6 @@ import lombok.core.SpiLoadUtil; import lombok.core.TypeLibrary; import lombok.core.TypeResolver; import lombok.core.AnnotationValues.AnnotationValueDecodeFail; -import lombok.eclipse.EclipseAST.Node; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; @@ -67,7 +66,7 @@ public class HandlerLibrary { } public boolean handle(org.eclipse.jdt.internal.compiler.ast.Annotation annotation, - final Node annotationNode) { + final EclipseNode annotationNode) { AnnotationValues annValues = Eclipse.createAnnotation(annotationClass, annotationNode); return handler.handle(annValues, annotation, annotationNode); } @@ -110,7 +109,7 @@ public class HandlerLibrary { Eclipse.error(null, "Can't load Lombok annotation handler for Eclipse: ", t); } } - } catch ( IOException e ) { + } catch (IOException e) { Lombok.sneakyThrow(e); } } @@ -121,7 +120,7 @@ public class HandlerLibrary { for (EclipseASTVisitor visitor : SpiLoadUtil.findServices(EclipseASTVisitor.class)) { lib.visitorHandlers.add(visitor); } - } catch ( Throwable t ) { + } catch (Throwable t) { throw Lombok.sneakyThrow(t); } } @@ -143,27 +142,27 @@ public class HandlerLibrary { * @param annotationNode The Lombok AST Node representing the Annotation AST Node. * @param annotation 'node.get()' - convenience parameter. */ - public boolean handle(CompilationUnitDeclaration ast, EclipseAST.Node annotationNode, + public boolean handle(CompilationUnitDeclaration ast, EclipseNode annotationNode, org.eclipse.jdt.internal.compiler.ast.Annotation annotation) { String pkgName = annotationNode.getPackageDeclaration(); Collection imports = annotationNode.getImportStatements(); TypeResolver resolver = new TypeResolver(typeLibrary, pkgName, imports); TypeReference rawType = annotation.type; - if ( rawType == null ) return false; + if (rawType == null) return false; boolean handled = false; - for ( String fqn : resolver.findTypeMatches(annotationNode, toQualifiedName(annotation.type.getTypeName())) ) { + for (String fqn : resolver.findTypeMatches(annotationNode, toQualifiedName(annotation.type.getTypeName()))) { boolean isPrintAST = fqn.equals(PrintAST.class.getName()); - if ( isPrintAST == s