aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/javac/handlers
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2012-03-21 11:41:59 +0100
committerReinier Zwitserloot <reinier@zwitserloot.com>2012-03-21 11:41:59 +0100
commitcbaa53a9b54589f05a580d63dfc065be03bb4b88 (patch)
tree7285e222b281af312907e14dae2d1cff47c3d2af /src/core/lombok/javac/handlers
parentfb76daf5e4320e31f19dcbd6c7a4036109d71ad7 (diff)
downloadlombok-cbaa53a9b54589f05a580d63dfc065be03bb4b88.tar.gz
lombok-cbaa53a9b54589f05a580d63dfc065be03bb4b88.tar.bz2
lombok-cbaa53a9b54589f05a580d63dfc065be03bb4b88.zip
Implementation of @Accessors.
Diffstat (limited to 'src/core/lombok/javac/handlers')
-rw-r--r--src/core/lombok/javac/handlers/HandleGetter.java11
-rw-r--r--src/core/lombok/javac/handlers/HandleSetter.java19
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java87
3 files changed, 106 insertions, 11 deletions
diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java
index c286ed24..e49b0ccb 100644
--- a/src/core/lombok/javac/handlers/HandleGetter.java
+++ b/src/core/lombok/javac/handlers/HandleGetter.java
@@ -190,9 +190,14 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> {
}
}
- String methodName = toGetterName(fieldDecl);
+ String methodName = toGetterName(fieldNode);
- for (String altName : toAllGetterNames(fieldDecl)) {
+ if (methodName == null) {
+ source.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list.");
+ return;
+ }
+
+ for (String altName : toAllGetterNames(fieldNode)) {
switch (methodExists(altName, fieldNode, false)) {
case EXISTS_BY_LOMBOK:
return;
@@ -231,7 +236,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> {
}
JCBlock methodBody = treeMaker.Block(0, statements);
- Name methodName = field.toName(toGetterName(fieldNode));
+ Name methodName = field.toName(toGetterName(field));
List<JCTypeParameter> methodGenericParams = List.nil();
List<JCVariableDecl> parameters = List.nil();
diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java
index db1fa724..93ea5a5b 100644
--- a/src/core/lombok/javac/handlers/HandleSetter.java
+++ b/src/core/lombok/javac/handlers/HandleSetter.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 The Project Lombok Authors.
+ * Copyright (C) 2009-2012 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
@@ -162,9 +162,14 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
}
JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get();
- String methodName = toSetterName(fieldDecl);
+ String methodName = toSetterName(fieldNode);
- for (String altName : toAllSetterNames(fieldDecl)) {
+ if (methodName == null) {
+ source.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
+ return;
+ }
+
+ for (String altName : toAllSetterNames(fieldNode)) {
switch (methodExists(altName, fieldNode, false)) {
case EXISTS_BY_LOMBOK:
return;
@@ -184,10 +189,14 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
- injectMethod(fieldNode.up(), createSetter(access, fieldNode, fieldNode.getTreeMaker(), source.get()));
+ JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), source.get());
+ injectMethod(fieldNode.up(), createdSetter);
}
private JCMethodDecl createSetter(long access, JavacNode field, TreeMaker treeMaker, JCTree source) {
+ String setterName = toSetterName(field);
+ if (setterName == null) return null;
+
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
JCExpression fieldRef = createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD);
@@ -206,7 +215,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
}
JCBlock methodBody = treeMaker.Block(0, statements);
- Name methodName = field.toName(toSetterName(fieldDecl));
+ Name methodName = field.toName(setterName);
List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables);
JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
//WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6.
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index 244f7d38..245636b7 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 The Project Lombok Authors.
+ * Copyright (C) 2009-2012 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
@@ -37,9 +37,11 @@ import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.core.AnnotationValues;
+import lombok.core.TransformationsUtil;
import lombok.core.TypeResolver;
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues.AnnotationValue;
+import lombok.experimental.Accessors;
import lombok.javac.Javac;
import lombok.javac.JavacNode;
@@ -272,6 +274,84 @@ public class JavacHandlerUtil {
}
/**
+ * Translates the given field into all possible getter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static java.util.List<String> toAllGetterNames(JavacNode field) {
+ String fieldName = field.getName();
+
+ boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean");
+
+ AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static String toGetterName(JavacNode field) {
+ String fieldName = field.getName();
+
+ boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean");
+
+ AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toGetterName(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * Translates the given field into all possible setter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static java.util.List<String> toAllSetterNames(JavacNode field) {
+ String fieldName = field.getName();
+
+ boolean isBoolean = ((JCVariableDecl) field.get()).vartype.toString().equals("boolean");
+
+ AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toAllSetterNames(accessors, fieldName, isBoolean);
+ }
+
+ /**
+ * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
+ */
+ public static String toSetterName(JavacNode field) {
+ String fieldName = field.getName();
+
+ boolean isBoolean = ((JCVariableDecl) field.get()).toString().equals("boolean");
+
+ AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
+
+ return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean);
+ }
+
+ public static AnnotationValues<Accessors> getAccessorsForField(JavacNode field) {
+ for (JavacNode node : field.down()) {
+ if (annotationTypeMatches(Accessors.class, node)) {
+ return createAnnotation(Accessors.class, node);
+ }
+ }
+
+ JavacNode current = field.up();
+ while (current != null) {
+ for (JavacNode node : field.down()) {
+ if (annotationTypeMatches(Accessors.class, node)) {
+ return createAnnotation(Accessors.class, node);
+ }
+ }
+ current = field.up();
+ }
+
+ return AnnotationValues.of(Accessors.class, field);
+ }
+
+ /**
* Checks if there is a field with the provided name.
*
* @param fieldName the field name to check for.
@@ -382,7 +462,7 @@ public class JavacHandlerUtil {
private static GetterMethod findGetter(JavacNode field) {
JCVariableDecl decl = (JCVariableDecl)field.get();
JavacNode typeNode = field.up();
- for (String potentialGetterName : toAllGetterNames(decl)) {
+ for (String potentialGetterName : toAllGetterNames(field)) {
for (JavacNode potentialGetter : typeNode.down()) {
if (potentialGetter.getKind() != Kind.METHOD) continue;
JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
@@ -424,7 +504,8 @@ public class JavacHandlerUtil {
}
if (hasGetterAnnotation) {
- String getterName = toGetterName(decl);
+ String getterName = toGetterName(field);
+ if (getterName == null) return null;
return new GetterMethod(field.toName(getterName), decl.vartype);
}