Propagated the fact that you get the Node object belonging to the annotation, and not the field/type/local/method it goes with, all the way, so that you can easily generate a warning on an annotation in a handler.

This commit is contained in:
Reinier Zwitserloot committed 2009-06-15 23:58:52 +02:00
1 parent 92ab0e7971
commit 1216239116
4 files changed
+29 -17

No files matched your search

@@ -1,5 +1,5 @@
package lombok.eclipse;
public interface EclipseAnnotationHandler<T extends java.lang.annotation.Annotation> {
void handle(T annotation, org.eclipse.jdt.internal.compiler.ast.Annotation ast, EclipseAST.Node node);
void handle(T annotation, org.eclipse.jdt.internal.compiler.ast.Annotation ast, EclipseAST.Node annotationNode);
}
+8 -8
View File
@@ -45,8 +45,8 @@ public class HandlerLibrary {
@SuppressWarnings("unchecked")
public void handle(Object annInstance,
org.eclipse.jdt.internal.compiler.ast.Annotation annotation,
Node node) {
handler.handle((T) annInstance, annotation, node);
Node annotationNode) {
handler.handle((T) annInstance, annotation, annotationNode);
}
}
@@ -287,19 +287,19 @@ public class HandlerLibrary {
return null;
}
public void handle(CompilationUnitDeclaration ast, EclipseAST.Node node,
public void handle(CompilationUnitDeclaration ast, EclipseAST.Node annotationNode,
org.eclipse.jdt.internal.compiler.ast.Annotation annotation) {
TypeResolver resolver = new TypeResolver(typeLibrary, node.top());
TypeResolver resolver = new TypeResolver(typeLibrary, annotationNode.top());
TypeReference rawType = annotation.type;
if ( rawType == null ) return;
for ( String fqn : resolver.findTypeMatches(node, annotation.type) ) {
for ( String fqn : resolver.findTypeMatches(annotationNode, annotation.type) ) {
HandlerContainer<?> container = handlers.get(fqn);
if ( container == null ) continue;
try {
Object annInstance = createAnnotation(container.annotationClass, ast, annotation);
container.handle(annInstance, annotation, node);
} catch (EnumDecodeFail e) {
node.addError(e.getMessage(), e.pair.sourceStart, e.pair.sourceEnd);
container.handle(annInstance, annotation, annotationNode);
} catch ( EnumDecodeFail e ) {
annotationNode.addError(e.getMessage(), e.pair.sourceStart, e.pair.sourceEnd);
}
}
}
+8 -4
View File
@@ -113,22 +113,26 @@ public class TransformEclipseAST {
private static class AnnotationVisitor extends EclipseASTAdapter {
@Override public void visitAnnotationOnField(FieldDeclaration field, Node annotationNode, Annotation annotation) {
handlers.handle((CompilationUnitDeclaration) annotationNode.top().node, annotationNode.up(), annotation);
if ( annotationNode.isHandled() ) return;
handlers.handle((CompilationUnitDeclaration) annotationNode.top().node, annotationNode, annotation);
annotationNode.setHandled();
}
@Override public void visitAnnotationOnLocal(LocalDeclaration local, Node annotationNode, Annotation annotation) {
handlers.handle((CompilationUnitDeclaration) annotationNode.top().node, annotationNode.up(), annotation);
if ( annotationNode.isHandled() ) return;
handlers.handle((CompilationUnitDeclaration) annotationNode.top().node, annotationNode, annotation);
annotationNode.setHandled();
}
@Override public void visitAnnotationOnMethod(AbstractMethodDeclaration method, Node annotationNode, Annotation annotation) {
handlers.handle((CompilationUnitDeclaration) annotationNode.top().node, annotationNode.up(), annotation);
if ( annotationNode.isHandled() ) return;
handlers.handle((CompilationUnitDeclaration) annotationNode.top().node, annotationNode, annotation);
annotationNode.setHandled();
}
@Override public void visitAnnotationOnType(TypeDeclaration type, Node annotationNode, Annotation annotation) {
handlers.handle((CompilationUnitDeclaration) annotationNode.top().node, annotationNode.up(), annotation);
if ( annotationNode.isHandled() ) return;
handlers.handle((CompilationUnitDeclaration) annotationNode.top().node, annotationNode, annotation);
annotationNode.setHandled();
}
}
@@ -24,15 +24,23 @@ import org.mangosdk.spi.ProviderFor;
@ProviderFor(EclipseAnnotationHandler.class)
public class HandleGetter_ecj implements EclipseAnnotationHandler<Getter> {
@Override public void handle(Getter annotation, Annotation ast, Node node) {
FieldDeclaration field = (FieldDeclaration) node.getEclipseNode();
private void generateDuplicateGetterWarning(Node annotationNode, String methodName) {
annotationNode.addWarning(String.format("Not generating %s(): A method with that name already exists", methodName));
}
@Override public void handle(Getter annotation, Annotation ast, Node annotationNode) {
if ( !(annotationNode.up().getEclipseNode() instanceof FieldDeclaration) ) return;
FieldDeclaration field = (FieldDeclaration) annotationNode.up().getEclipseNode();
TypeReference fieldType = field.type;
String getterName = TransformationsUtil.toGetterName(
new String(field.name), nameEquals(fieldType.getTypeName(), "boolean"));
TypeDeclaration parent = (TypeDeclaration) node.up().getEclipseNode();
TypeDeclaration parent = (TypeDeclaration) annotationNode.up().up().getEclipseNode();
if ( parent.methods != null ) for ( AbstractMethodDeclaration method : parent.methods ) {
if ( method.selector != null && new String(method.selector).equals(getterName) ) return;
if ( method.selector != null && new String(method.selector).equals(getterName) ) {
generateDuplicateGetterWarning(annotationNode, getterName);
return;
}
}
MethodDeclaration method = new MethodDeclaration(parent.compilationResult);