diff options
-rw-r--r-- | src/lombok/agent/eclipse/HandleGetter_ecj.java | 57 | ||||
-rw-r--r-- | src/lombok/agent/eclipse/TransformCompilationUnitDeclaration.java | 39 | ||||
-rw-r--r-- | src/lombok/apt/HandleGetter_javac.java | 2 | ||||
-rw-r--r-- | src/lombok/apt/PKG.java | 3 |
4 files changed, 61 insertions, 40 deletions
diff --git a/src/lombok/agent/eclipse/HandleGetter_ecj.java b/src/lombok/agent/eclipse/HandleGetter_ecj.java new file mode 100644 index 00000000..d43bbbc4 --- /dev/null +++ b/src/lombok/agent/eclipse/HandleGetter_ecj.java @@ -0,0 +1,57 @@ +package lombok.agent.eclipse; + +import java.lang.reflect.Modifier; + +import lombok.transformations.TransformationsUtil; + +import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; +import org.eclipse.jdt.internal.compiler.ast.Expression; +import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; +import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; +import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; +import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; +import org.eclipse.jdt.internal.compiler.ast.Statement; +import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; +import org.eclipse.jdt.internal.compiler.ast.TypeReference; +import org.eclipse.jdt.internal.compiler.lookup.MethodScope; + +public class HandleGetter_ecj { + public void apply(TypeDeclaration type, FieldDeclaration field) { + TypeReference fieldType = field.type; + String getterName = TransformationsUtil.toGetterName(new String(field.name), nameEquals(fieldType.getTypeName(), "boolean")); + + for ( AbstractMethodDeclaration method : type.methods ) { + if ( method.selector != null && new String(method.selector).equals(getterName) ) return; + } + + MethodDeclaration method = new MethodDeclaration(type.compilationResult); + method.modifiers = Modifier.PUBLIC; + method.returnType = field.type; + method.annotations = null; + method.arguments = null; + method.selector = getterName.toCharArray(); + method.binding = null; + method.thrownExceptions = null; + method.typeParameters = null; + method.scope = new MethodScope(type.scope, method, false); + Expression fieldExpression = new SingleNameReference(field.name, (field.declarationSourceStart << 32) | field.declarationSourceEnd); + Statement returnStatement = new ReturnStatement(fieldExpression, field.sourceStart, field.sourceEnd); + method.statements = new Statement[] { returnStatement }; + AbstractMethodDeclaration[] newArray = new AbstractMethodDeclaration[type.methods.length + 1]; + System.arraycopy(type.methods, 0, newArray, 0, type.methods.length); + newArray[type.methods.length] = method; + type.methods = newArray; + } + + private boolean nameEquals(char[][] typeName, String string) { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for ( char[] elem : typeName ) { + if ( first ) first = false; + else sb.append('.'); + sb.append(elem); + } + + return string.contentEquals(sb); + } +} diff --git a/src/lombok/agent/eclipse/TransformCompilationUnitDeclaration.java b/src/lombok/agent/eclipse/TransformCompilationUnitDeclaration.java index 0bcbc9ae..7c29a795 100644 --- a/src/lombok/agent/eclipse/TransformCompilationUnitDeclaration.java +++ b/src/lombok/agent/eclipse/TransformCompilationUnitDeclaration.java @@ -1,19 +1,9 @@ package lombok.agent.eclipse; -import java.lang.reflect.Modifier; - -import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; -import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; -import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; -import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; -import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; -import org.eclipse.jdt.internal.compiler.ast.Statement; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; -import org.eclipse.jdt.internal.compiler.ast.TypeReference; -import org.eclipse.jdt.internal.compiler.lookup.MethodScope; public class TransformCompilationUnitDeclaration { /** This is a 'magic' method signature - it is this one that will be called. Don't rename anything! */ @@ -22,34 +12,11 @@ public class TransformCompilationUnitDeclaration { if ( ast.types != null ) for ( TypeDeclaration type : ast.types ) { if ( type.fields != null ) for ( FieldDeclaration field : type.fields ) { if ( field.annotations != null ) for ( Annotation annotation : field.annotations ) { - if ( annotation.type.toString().equals("Getter") ) addGetter(type); + if ( annotation.type.toString().equals("Getter") ) { + new HandleGetter_ecj().apply(type, field); + } } } } } - - private static void addGetter(TypeDeclaration type) { - for ( AbstractMethodDeclaration method : type.methods ) { - if ( method.selector != null && new String(method.selector).equals("getFoo") ) return; - } - - MethodDeclaration method = new MethodDeclaration(type.compilationResult); - method.modifiers = Modifier.PUBLIC; - method.returnType = TypeReference.baseTypeReference(TypeReference.T_int, 0); - method.annotations = null; - method.arguments = null; - method.selector = "getFoo".toCharArray(); - method.binding = null; - method.thrownExceptions = null; - method.typeParameters = null; - method.scope = new MethodScope(type.scope, method, false); - Expression fieldExpression = new SingleNameReference("foo".toCharArray(), 10); - Statement returnStatement = new ReturnStatement(fieldExpression, 1, 2); - method.statements = new Statement[] { returnStatement }; - AbstractMethodDeclaration[] newArray = new AbstractMethodDeclaration[type.methods.length + 1]; - System.arraycopy(type.methods, 0, newArray, 0, type.methods.length); - newArray[type.methods.length] = method; - type.methods = newArray; - System.out.println("Generated getFoo method"); - } } diff --git a/src/lombok/apt/HandleGetter_javac.java b/src/lombok/apt/HandleGetter_javac.java index d726867c..bfa2746d 100644 --- a/src/lombok/apt/HandleGetter_javac.java +++ b/src/lombok/apt/HandleGetter_javac.java @@ -54,8 +54,6 @@ class HandleGetter_javac extends HandlerForCompiler<Getter> { MethodTree getterMethod = createGetter(element, treeMaker, nameTable); javacClassTree.defs = javacClassTree.defs.append((JCTree)getterMethod); - - messager.printMessage(Diagnostic.Kind.WARNING, "Generated a getter for this field", element); } private MethodTree createGetter(Element field, TreeMaker treeMaker, Name.Table nameTable) { diff --git a/src/lombok/apt/PKG.java b/src/lombok/apt/PKG.java index bbe13379..cf04efaf 100644 --- a/src/lombok/apt/PKG.java +++ b/src/lombok/apt/PKG.java @@ -52,8 +52,7 @@ class PKG { static String toGetterName(Element field) { CharSequence fieldName = field.getSimpleName(); - boolean isBoolean = (field.asType().getKind() == TypeKind.BOOLEAN || - "java.lang.Boolean".equals(field.asType().toString())); + boolean isBoolean = field.asType().getKind() == TypeKind.BOOLEAN; return TransformationsUtil.toGetterName(fieldName, isBoolean); } |