diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2011-06-20 21:04:37 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2011-06-20 21:08:19 +0200 |
commit | 1816ffdebc75a097c9751578e2628b2d52d55b72 (patch) | |
tree | 7b249bc813545e45f54f50c09c235627abd24043 /src/core/lombok/eclipse | |
parent | f923671763424e872dc6dee7c67f73783935baaf (diff) | |
download | lombok-1816ffdebc75a097c9751578e2628b2d52d55b72.tar.gz lombok-1816ffdebc75a097c9751578e2628b2d52d55b72.tar.bz2 lombok-1816ffdebc75a097c9751578e2628b2d52d55b72.zip |
fixed @Synchronized.
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleSynchronized.java | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleSynchronized.java b/src/core/lombok/eclipse/handlers/HandleSynchronized.java index 6a971bc9..b4c87b1e 100644 --- a/src/core/lombok/eclipse/handlers/HandleSynchronized.java +++ b/src/core/lombok/eclipse/handlers/HandleSynchronized.java @@ -61,39 +61,34 @@ public class HandleSynchronized extends EclipseAnnotationHandler<Synchronized> { return true; } - @Override public void handle(AnnotationValues<Synchronized> annotation, Annotation source, EclipseNode annotationNode) { - int p1 = source.sourceStart -1; - int p2 = source.sourceStart -2; - long pos = (((long)p1) << 32) | p2; + @Override public void preHandle(AnnotationValues<Synchronized> annotation, Annotation source, EclipseNode annotationNode) { EclipseNode methodNode = annotationNode.up(); - if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof MethodDeclaration)) { - annotationNode.addError("@Synchronized is legal only on methods."); - return; - } - + if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof MethodDeclaration)) return; MethodDeclaration method = (MethodDeclaration)methodNode.get(); - if (method.isAbstract()) { - annotationNode.addError("@Synchronized is legal only on concrete methods."); - return; - } + if (method.isAbstract()) return; + createLockField(annotation, annotationNode, method.isStatic(), false); + } + + private char[] createLockField(AnnotationValues<Synchronized> annotation, EclipseNode annotationNode, boolean isStatic, boolean reportErrors) { char[] lockName = annotation.getInstance().value().toCharArray(); + Annotation source = (Annotation) annotationNode.get(); boolean autoMake = false; if (lockName.length == 0) { autoMake = true; - lockName = method.isStatic() ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME; + lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME; } - if (fieldExists(new String(lockName), methodNode) == MemberExistsResult.NOT_EXISTS) { + if (fieldExists(new String(lockName), annotationNode) == MemberExistsResult.NOT_EXISTS) { if (!autoMake) { - annotationNode.addError(String.format("The field %s does not exist.", new String(lockName))); - return; + if (reportErrors) annotationNode.addError(String.format("The field %s does not exist.", new String(lockName))); + return lockName; } FieldDeclaration fieldDecl = new FieldDeclaration(lockName, 0, -1); Eclipse.setGeneratedBy(fieldDecl, source); fieldDecl.declarationSourceEnd = -1; - fieldDecl.modifiers = (method.isStatic() ? Modifier.STATIC : 0) | Modifier.FINAL | Modifier.PRIVATE; + fieldDecl.modifiers = (isStatic ? Modifier.STATIC : 0) | Modifier.FINAL | Modifier.PRIVATE; //We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable! ArrayAllocationExpression arrayAlloc = new ArrayAllocationExpression(); @@ -108,6 +103,26 @@ public class HandleSynchronized extends EclipseAnnotationHandler<Synchronized> { injectFieldSuppressWarnings(annotationNode.up().up(), fieldDecl); } + return lockName; + } + + @Override public void handle(AnnotationValues<Synchronized> annotation, Annotation source, EclipseNode annotationNode) { + int p1 = source.sourceStart -1; + int p2 = source.sourceStart -2; + long pos = (((long)p1) << 32) | p2; + EclipseNode methodNode = annotationNode.up(); + if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof MethodDeclaration)) { + annotationNode.addError("@Synchronized is legal only on methods."); + return; + } + + MethodDeclaration method = (MethodDeclaration)methodNode.get(); + if (method.isAbstract()) { + annotationNode.addError("@Synchronized is legal only on concrete methods."); + return; + } + + char[] lockName = createLockField(annotation, annotationNode, method.isStatic(), true); if (method.statements == null) return; Block block = new Block(0); |