aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac/handlers
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@tipit.to>2009-07-26 09:15:28 +0200
committerReinier Zwitserloot <reinier@tipit.to>2009-07-26 09:15:28 +0200
commita9d29896887ee35903bcb847eb4307637801bcc2 (patch)
treee7947619e7b295588ea67a51008f41d79c5148f9 /src/lombok/javac/handlers
parent0d9bde828d548893108f78a281016150121b1d8d (diff)
downloadlombok-a9d29896887ee35903bcb847eb4307637801bcc2.tar.gz
lombok-a9d29896887ee35903bcb847eb4307637801bcc2.tar.bz2
lombok-a9d29896887ee35903bcb847eb4307637801bcc2.zip
Addresses issue #4:
If boolean fields already start with a typical getter prefix (is, has, or get), lombok's @Getter will no longer generate its own prefix as well, so a field named 'hasFoo' will result in a getter named 'hasFoo()', not 'isHasFoo()'. Also, if any likely getter name already exists for a boolean, a getter will not be generated. Thus, if your field is called 'hasFoo', and you already have a method named 'isFoo', then @Getter will not generate anything (and warn, unless the getter is being generated due to @Data). This last mechanism works by taking the field name *AND* any other likely base names (defined by the field name being named as prefix+baseName, with prefix being is/has/get), and then prefixing all the likely fieldnames with is/has/get, and checking if any method with that name exists. Of course, this means weird things are going to happen if you have 2 fields named 'isFoo' and 'hasFoo', but then, you'd be a real idiot if you did that.
Diffstat (limited to 'src/lombok/javac/handlers')
-rw-r--r--src/lombok/javac/handlers/HandleGetter.java26
-rw-r--r--src/lombok/javac/handlers/PKG.java8
2 files changed, 24 insertions, 10 deletions
diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java
index c993afcd..81915169 100644
--- a/src/lombok/javac/handlers/HandleGetter.java
+++ b/src/lombok/javac/handlers/HandleGetter.java
@@ -90,16 +90,22 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get();
String methodName = toGetterName(fieldDecl);
- switch ( methodExists(methodName, fieldNode) ) {
- case EXISTS_BY_LOMBOK:
- return true;
- case EXISTS_BY_USER:
- if ( whineIfExists ) errorNode.addWarning(
- String.format("Not generating %s(): A method with that name already exists", methodName));
- return true;
- default:
- case NOT_EXISTS:
- //continue with creating the getter
+ for ( String altName : toAllGetterNames(fieldDecl) ) {
+ switch ( methodExists(altName, fieldNode) ) {
+ case EXISTS_BY_LOMBOK:
+ return true;
+ case EXISTS_BY_USER:
+ if ( whineIfExists ) {
+ String altNameExpl = "";
+ if ( !altName.equals(methodName) ) altNameExpl = String.format("(%s)", altName);
+ errorNode.addWarning(
+ String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
+ }
+ return true;
+ default:
+ case NOT_EXISTS:
+ //continue scanning the other alt names.
+ }
}
long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java
index ef6b3eba..205a2b6e 100644
--- a/src/lombok/javac/handlers/PKG.java
+++ b/src/lombok/javac/handlers/PKG.java
@@ -45,6 +45,14 @@ class PKG {
//Prevent instantiation
}
+ static java.util.List<String> toAllGetterNames(JCVariableDecl field) {
+ CharSequence fieldName = field.name;
+
+ boolean isBoolean = field.vartype.toString().equals("boolean");
+
+ return TransformationsUtil.toAllGetterNames(fieldName, isBoolean);
+ }
+
/**
* @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo).
*/