aboutsummaryrefslogtreecommitdiff
path: root/src/lombok
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@tipit.to>2009-06-17 21:35:51 +0200
committerReinier Zwitserloot <reinier@tipit.to>2009-06-17 21:35:51 +0200
commit8fa50054449d88380ce45ba91881df6655737f20 (patch)
tree08117208cfa6d2ed870e76489c031ee6f74c5aea /src/lombok
parent1929a28c9def0dd276dcf3fdb70ada8b84b1c64a (diff)
downloadlombok-8fa50054449d88380ce45ba91881df6655737f20.tar.gz
lombok-8fa50054449d88380ce45ba91881df6655737f20.tar.bz2
lombok-8fa50054449d88380ce45ba91881df6655737f20.zip
Added 'does setX/getX already exist' check to javac's HandleGetter/Setter. If they exist (with any returntype/paramlist), no getter/setter is generated, and instead a warning is added on the annotation.
Diffstat (limited to 'src/lombok')
-rw-r--r--src/lombok/javac/handlers/HandleGetter.java9
-rw-r--r--src/lombok/javac/handlers/HandleSetter.java19
-rw-r--r--src/lombok/javac/handlers/PKG.java22
3 files changed, 43 insertions, 7 deletions
diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java
index 781598a2..da1b7107 100644
--- a/src/lombok/javac/handlers/HandleGetter.java
+++ b/src/lombok/javac/handlers/HandleGetter.java
@@ -25,12 +25,19 @@ import com.sun.tools.javac.util.Name;
@ProviderFor(JavacAnnotationHandler.class)
public class HandleGetter implements JavacAnnotationHandler<Getter> {
@Override public void handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacAST.Node annotationNode) {
- //TODO Check for existence of the getter and skip it (+ warn) if it's already there.
if ( annotationNode.up().getKind() != Kind.FIELD ) {
annotationNode.addError("@Getter is only supported on a field.");
return;
}
+ String methodName = toGetterName((JCVariableDecl) annotationNode.up().get());
+
+ if ( methodExists(methodName, annotationNode.up()) ) {
+ annotationNode.addWarning(
+ String.format("Not generating %s(): A method with that name already exists", methodName));
+ return;
+ }
+
Getter getter = annotation.getInstance();
JCClassDecl javacClassTree = (JCClassDecl) annotationNode.up().up().get();
diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java
index 30702701..0fbfe8ae 100644
--- a/src/lombok/javac/handlers/HandleSetter.java
+++ b/src/lombok/javac/handlers/HandleSetter.java
@@ -1,10 +1,6 @@
package lombok.javac.handlers;
-import static lombok.javac.handlers.PKG.toJavacModifier;
-import static lombok.javac.handlers.PKG.toSetterName;
-
-import org.mangosdk.spi.ProviderFor;
-
+import static lombok.javac.handlers.PKG.*;
import lombok.Setter;
import lombok.core.AnnotationValues;
import lombok.core.AST.Kind;
@@ -12,6 +8,8 @@ import lombok.javac.JavacAST;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacAST.Node;
+import org.mangosdk.spi.ProviderFor;
+
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCAssign;
@@ -29,12 +27,21 @@ import com.sun.tools.javac.util.Name;
@ProviderFor(JavacAnnotationHandler.class)
public class HandleSetter implements JavacAnnotationHandler<Setter> {
@Override public void handle(AnnotationValues<Setter> annotation, JCAnnotation ast, Node annotationNode) {
- //TODO Check for existence of the setter and skip it (+ warn) if it's already there.
if ( annotationNode.up().getKind() != Kind.FIELD ) {
annotationNode.addError("@Setter is only supported on a field.");
return;
}
+ JCVariableDecl fieldNode = (JCVariableDecl) annotationNode.up().get();
+ String methodName = toSetterName(fieldNode);
+
+ if ( methodExists(methodName, annotationNode.up()) ) {
+ annotationNode.addWarning(
+ String.format("Not generating %s(%s %s): A method with that name already exists",
+ methodName, fieldNode.vartype, fieldNode.name));
+ return;
+ }
+
Setter setter = annotation.getInstance();
JCClassDecl javacClassTree = (JCClassDecl) annotationNode.up().up().get();
diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java
index 4ea44274..161248c8 100644
--- a/src/lombok/javac/handlers/PKG.java
+++ b/src/lombok/javac/handlers/PKG.java
@@ -2,10 +2,14 @@ package lombok.javac.handlers;
import java.lang.reflect.Modifier;
+import com.sun.tools.javac.tree.JCTree;
+import com.sun.tools.javac.tree.JCTree.JCClassDecl;
+import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import lombok.AccessLevel;
import lombok.core.TransformationsUtil;
+import lombok.javac.JavacAST;
class PKG {
static String toGetterName(JCVariableDecl field) {
@@ -22,6 +26,24 @@ class PKG {
return TransformationsUtil.toSetterName(fieldName);
}
+ static boolean methodExists(String methodName, JavacAST.Node node) {
+ while ( node != null && !(node.get() instanceof JCClassDecl) ) {
+ node = node.up();
+ }
+
+ if ( node.get() instanceof JCClassDecl ) {
+ for ( JCTree def : ((JCClassDecl)node.get()).defs ) {
+ if ( def instanceof JCMethodDecl ) {
+ if ( ((JCMethodDecl)def).name.contentEquals(methodName) ) {
+ return true;
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
static int toJavacModifier(AccessLevel accessLevel) {
switch ( accessLevel ) {
case MODULE: