diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2013-02-11 22:34:48 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2013-02-11 22:34:48 +0100 |
commit | aafd83079a3000d3deb6e40a182849da2509fbfb (patch) | |
tree | cf87951eee9bb098bb96ecc3c02c6f1ab34c405d /src/core/lombok/eclipse/handlers/HandleSetter.java | |
parent | ef8769d3180b2c6de91a64f69dfa23a2e6e449b9 (diff) | |
download | lombok-aafd83079a3000d3deb6e40a182849da2509fbfb.tar.gz lombok-aafd83079a3000d3deb6e40a182849da2509fbfb.tar.bz2 lombok-aafd83079a3000d3deb6e40a182849da2509fbfb.zip |
BIG commit:
* re-introduction of onMethod/onConstructor/onParam
* tests checking error/warnings rewritten to be more heuristic, in order to accomodate difference in messaging between java6 and java 7
* Ability to eliminate java's own output of erroneous error messages (heh); i.e. those messages that are invalidated by lombok's actions. This mechanism is used for onMethod/onConstructor/onParam
* First steps to unifying a billion setGeneratedBy calls into a single visitor traversal for eclipse' HandleGetter/Setter/Constructor/Wither
* To simplify 'zooming in' the tests on just a few files, added an 'accept' mechanism.
* Updated copyright headers of website to 2013.
Diffstat (limited to 'src/core/lombok/eclipse/handlers/HandleSetter.java')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleSetter.java | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index 8037ed23..9b46b704 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011 The Project Lombok Authors. + * Copyright (C) 2009-2013 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -27,6 +27,7 @@ import static lombok.eclipse.handlers.EclipseHandlerUtil.*; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import lombok.AccessLevel; @@ -111,7 +112,9 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { return; } - createSetterForField(level, fieldNode, fieldNode, pos, false); + List<Annotation> empty = Collections.emptyList(); + + createSetterForField(level, fieldNode, fieldNode, pos, false, empty, empty); } public void handle(AnnotationValues<Setter> annotation, Annotation ast, EclipseNode annotationNode) { @@ -119,24 +122,36 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { AccessLevel level = annotation.getInstance().value(); if (level == AccessLevel.NONE || node == null) return; + List<Annotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Setter(onMethod=", annotationNode); + List<Annotation> onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@Setter(onParam=", annotationNode); + switch (node.getKind()) { case FIELD: - createSetterForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, annotationNode.get(), true); + createSetterForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, annotationNode.get(), true, onMethod, onParam); break; case TYPE: + if (!onMethod.isEmpty()) { + annotationNode.addError("'onMethod' is not supported for @Setter on a type."); + } + if (!onParam.isEmpty()) { + annotationNode.addError("'onParam' is not supported for @Setter on a type."); + } generateSetterForType(node, annotationNode, level, false); break; } } - private void createSetterForFields(AccessLevel level, Collection<EclipseNode> fieldNodes, EclipseNode errorNode, ASTNode source, boolean whineIfExists) { + private void createSetterForFields(AccessLevel level, Collection<EclipseNode> fieldNodes, EclipseNode errorNode, ASTNode source, boolean whineIfExists, List<Annotation> onMethod, List<Annotation> onParam) { for (EclipseNode fieldNode : fieldNodes) { - createSetterForField(level, fieldNode, errorNode, source, whineIfExists); + createSetterForField(level, fieldNode, errorNode, source, whineIfExists, onMethod, onParam); } } - private void createSetterForField(AccessLevel level, - EclipseNode fieldNode, EclipseNode errorNode, ASTNode source, boolean whineIfExists) { + private void createSetterForField( + AccessLevel level, EclipseNode fieldNode, EclipseNode errorNode, + ASTNode source, boolean whineIfExists, List<Annotation> onMethod, + List<Annotation> onParam) { + if (fieldNode.getKind() != Kind.FIELD) { errorNode.addError("@Setter is only supported on a class or a field."); return; @@ -147,6 +162,7 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0; String setterName = toSetterName(fieldNode, isBoolean); boolean shouldReturnThis = shouldReturnThis(fieldNode); + if (setterName == null) { errorNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list."); return; @@ -172,16 +188,15 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { } } - MethodDeclaration method = createSetter((TypeDeclaration) fieldNode.up().get(), fieldNode, setterName, shouldReturnThis, modifier, source); + MethodDeclaration method = createSetter((TypeDeclaration) fieldNode.up().get(), fieldNode, setterName, shouldReturnThis, modifier, source, onMethod, onParam); injectMethod(fieldNode.up(), method); } - private MethodDeclaration createSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, boolean shouldReturnThis, int modifier, ASTNode source) { + private MethodDeclaration createSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, boolean shouldReturnThis, int modifier, ASTNode source, List<Annotation> onMethod, List<Annotation> onParam) { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; MethodDeclaration method = new MethodDeclaration(parent.compilationResult); - setGeneratedBy(method, source); method.modifiers = modifier; if (shouldReturnThis) { method.returnType = cloneSelfType(fieldNode, source); @@ -190,15 +205,18 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { if (method.returnType == null) { method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0); method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE; - setGeneratedBy(method.returnType, source); shouldReturnThis = false; } + Annotation[] deprecated = null; if (isFieldDeprecated(fieldNode)) { - method.annotations = new Annotation[] { generateDeprecatedAnnotation(source) }; + deprecated = new Annotation[] { generateDeprecatedAnnotation(source) }; + } + Annotation[] copiedAnnotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), deprecated); + if (copiedAnnotations.length != 0) { + method.annotations = copiedAnnotations; } Argument param = new Argument(field.name, p, copyType(field.type, source), Modifier.FINAL); param.sourceStart = pS; param.sourceEnd = pE; - setGeneratedBy(param, source); method.arguments = new Argument[] { param }; method.selector = name.toCharArray(); method.binding = null; @@ -207,10 +225,8 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source); NameReference fieldNameRef = new SingleNameReference(field.name, p); - setGeneratedBy(fieldNameRef, source); Assignment assignment = new Assignment(fieldRef, fieldNameRef, (int)p); assignment.sourceStart = pS; assignment.sourceEnd = assignment.statementEnd = pE; - setGeneratedBy(assignment, source); method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; @@ -227,15 +243,15 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { if (shouldReturnThis) { ThisReference thisRef = new ThisReference(pS, pE); - setGeneratedBy(thisRef, source); ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE); - setGeneratedBy(returnThis, source); statements.add(returnThis); } method.statements = statements.toArray(new Statement[0]); - Annotation[] copiedAnnotations = copyAnnotations(source, nonNulls, nullables); - if (copiedAnnotations.length != 0) param.annotations = copiedAnnotations; + Annotation[] copiedAnnotationsParam = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0])); + if (copiedAnnotationsParam.length != 0) param.annotations = copiedAnnotationsParam; + + method.traverse(new SetGeneratedByVisitor(source), parent.scope); return method; } } |