aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2010-11-29 22:28:32 +0100
committerReinier Zwitserloot <reinier@zwitserloot.com>2010-12-01 20:00:43 +0100
commite308909def27a6940792cb04c1a9ec618c5df3cc (patch)
tree6b4a7e1157b9aef1d75f028bc34b651f94b8babd /src
parentb0072a17eba21cdc6f8ba51693bb39d1c9aba319 (diff)
downloadlombok-e308909def27a6940792cb04c1a9ec618c5df3cc.tar.gz
lombok-e308909def27a6940792cb04c1a9ec618c5df3cc.tar.bz2
lombok-e308909def27a6940792cb04c1a9ec618c5df3cc.zip
@Delegate in eclipse now uses more of the standard infrastructure to inject methods.
Diffstat (limited to 'src')
-rw-r--r--src/core/lombok/eclipse/TransformEclipseAST.java47
-rw-r--r--src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java63
2 files changed, 62 insertions, 48 deletions
diff --git a/src/core/lombok/eclipse/TransformEclipseAST.java b/src/core/lombok/eclipse/TransformEclipseAST.java
index dff11442..bfc35244 100644
--- a/src/core/lombok/eclipse/TransformEclipseAST.java
+++ b/src/core/lombok/eclipse/TransformEclipseAST.java
@@ -84,6 +84,29 @@ public class TransformEclipseAST {
transform(parser, ast);
}
+ public static EclipseAST getAST(CompilationUnitDeclaration ast) {
+ EclipseAST existing = null;
+ if (astCacheField != null) {
+ try {
+ existing = (EclipseAST)astCacheField.get(ast);
+ } catch (Exception e) {
+ // existing remains null
+ }
+ }
+
+ if (existing == null) {
+ existing = new EclipseAST(ast);
+ if (astCacheField != null) try {
+ astCacheField.set(ast, existing);
+ } catch (Exception ignore) {
+ }
+ } else {
+ existing.reparse();
+ }
+
+ return existing;
+ }
+
/**
* This method is called immediately after Eclipse finishes building a CompilationUnitDeclaration, which is
* the top-level AST node when Eclipse parses a source file. The signature is 'magic' - you should not
@@ -101,11 +124,7 @@ public class TransformEclipseAST {
if (Symbols.hasSymbol("lombok.disable")) return;
try {
- EclipseAST existing = getCache(ast);
- if (existing == null) {
- existing = new EclipseAST(ast);
- setCache(ast, existing);
- } else existing.reparse();
+ EclipseAST existing = getAST(ast);
new TransformEclipseAST(existing).go();
} catch (Throwable t) {
try {
@@ -129,24 +148,6 @@ public class TransformEclipseAST {
}
}
- private static EclipseAST getCache(CompilationUnitDeclaration ast) {
- if (astCacheField == null) return null;
- try {
- return (EclipseAST)astCacheField.get(ast);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- private static void setCache(CompilationUnitDeclaration ast, EclipseAST cache) {
- if (astCacheField != null) try {
- astCacheField.set(ast, cache);
- } catch (Exception ignore) {
- ignore.printStackTrace();
- }
- }
-
public TransformEclipseAST(EclipseAST ast) {
this.ast = ast;
}
diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java
index 6198335c..37374f27 100644
--- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java
+++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java
@@ -33,6 +33,9 @@ import java.util.List;
import java.util.Set;
import lombok.eclipse.Eclipse;
+import lombok.eclipse.EclipseAST;
+import lombok.eclipse.EclipseNode;
+import lombok.eclipse.TransformEclipseAST;
import lombok.eclipse.handlers.EclipseHandlerUtil;
import lombok.patcher.Hook;
import lombok.patcher.MethodTarget;
@@ -47,7 +50,7 @@ import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer;
import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
-import org.eclipse.jdt.internal.compiler.ast.Clinit;
+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.FieldReference;
@@ -87,6 +90,9 @@ public class PatchDelegate {
TypeDeclaration decl = scope.referenceContext;
if (decl == null) return false;
+ CompilationUnitDeclaration cud = null;
+ EclipseAST astNode = null;
+
if (decl.fields != null) for (FieldDeclaration field : decl.fields) {
if (field.annotations == null) continue;
for (Annotation ann : field.annotations) {
@@ -95,6 +101,11 @@ public class PatchDelegate {
if (!charArrayEquals("lombok", tb.qualifiedPackageName())) continue;
if (!charArrayEquals("Delegate", tb.qualifiedSourceName())) continue;
+ if (cud == null) {
+ cud = scope.compilationUnitScope().referenceContext;
+ astNode = TransformEclipseAST.getAST(cud);
+ }
+
List<ClassLiteralAccess> rawTypes = new ArrayList<ClassLiteralAccess>();
for (MemberValuePair pair : ann.memberValuePairs()) {
if (pair.name == null || charArrayEquals("value", pair.name)) {
@@ -121,7 +132,7 @@ public class PatchDelegate {
removeExistingMethods(methodsToDelegate, decl, scope);
- generateDelegateMethods(decl, methodsToDelegate, field.name, ann);
+ generateDelegateMethods(astNode.get(decl), methodsToDelegate, field.name, ann);
}
}
@@ -154,29 +165,31 @@ public class PatchDelegate {
}
}
- private static void generateDelegateMethods(TypeDeclaration type, List<MethodBinding> methods, char[] delegate, ASTNode source) {
+ private static void generateDelegateMethods(EclipseNode typeNode, List<MethodBinding> methods, char[] delegate, ASTNode source) {
+ CompilationUnitDeclaration top = (CompilationUnitDeclaration) typeNode.top().get();
for (MethodBinding binding : methods) {
- MethodDeclaration method = generateDelegateMethod(delegate, binding, type.compilationResult, source);
- if (type.methods == null) {
- type.methods = new AbstractMethodDeclaration[1];
- type.methods[0] = method;
- } else {
- int insertionPoint;
- for (insertionPoint = 0; insertionPoint < type.methods.length; insertionPoint++) {
- AbstractMethodDeclaration current = type.methods[insertionPoint];
- if (current instanceof Clinit) continue;
- if (Eclipse.isGenerated(current)) continue;
- break;
- }
- AbstractMethodDeclaration[] newArray = new AbstractMethodDeclaration[type.methods.length + 1];
- System.arraycopy(type.methods, 0, newArray, 0, insertionPoint);
- if (insertionPoint <= type.methods.length) {
- System.arraycopy(type.methods, insertionPoint, newArray, insertionPoint + 1, type.methods.length - insertionPoint);
- }
-
- newArray[insertionPoint] = method;
- type.methods = newArray;
- }
+ MethodDeclaration method = generateDelegateMethod(delegate, binding, top.compilationResult, source);
+ EclipseHandlerUtil.injectMethod(typeNode, method);
+// if (type.methods == null) {
+// type.methods = new AbstractMethodDeclaration[1];
+// type.methods[0] = method;
+// } else {
+// int insertionPoint;
+// for (insertionPoint = 0; insertionPoint < type.methods.length; insertionPoint++) {
+// AbstractMethodDeclaration current = type.methods[insertionPoint];
+// if (current instanceof Clinit) continue;
+// if (Eclipse.isGenerated(current)) continue;
+// break;
+// }
+// AbstractMethodDeclaration[] newArray = new AbstractMethodDeclaration[type.methods.length + 1];
+// System.arraycopy(type.methods, 0, newArray, 0, insertionPoint);
+// if (insertionPoint <= type.methods.length) {
+// System.arraycopy(type.methods, insertionPoint, newArray, insertionPoint + 1, type.methods.length - insertionPoint);
+// }
+//
+// newArray[insertionPoint] = method;
+// type.methods = newArray;
+// }
}
}
@@ -188,7 +201,7 @@ public class PatchDelegate {
method.sourceStart = pS; method.sourceEnd = pE;
method.modifiers = ClassFileConstants.AccPublic;
method.returnType = Eclipse.makeType(binding.returnType, source, false);
- method.annotations = EclipseHandlerUtil.createSuppressWarningsAll(source, null);
+ method.annotations = null;
if (binding.parameters != null && binding.parameters.length > 0) {
method.arguments = new Argument[binding.parameters.length];
for (int i = 0; i < method.arguments.length; i++) {