mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-16 06:14:36 +00:00
@Getter now TOTALLY WORKS in eclipse! W00t!
Also made the handling of lombokisms in eclipse a little more frameworky, though there's still plenty to be done.
This commit is contained in:
1 parent
9314db916d
commit
525211b872
4 files changed
+61
-40
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user