From 1816ffdebc75a097c9751578e2628b2d52d55b72 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 20 Jun 2011 21:04:37 +0200 Subject: [PATCH] fixed @Synchronized. --- src/core/lombok/Synchronized.java | 2 +- .../eclipse/handlers/HandleSynchronized.java | 77 +++++++++++-------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/core/lombok/Synchronized.java b/src/core/lombok/Synchronized.java index 72c44c71..303567fb 100644 --- a/src/core/lombok/Synchronized.java +++ b/src/core/lombok/Synchronized.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2011 Reinier Zwitserloot and Roel Spilker. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal 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,6 +61,51 @@ public class HandleSynchronized extends EclipseAnnotationHandler { return true; } + @Override public void preHandle(AnnotationValues annotation, Annotation source, EclipseNode annotationNode) { + EclipseNode methodNode = annotationNode.up(); + if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof MethodDeclaration)) return; + MethodDeclaration method = (MethodDeclaration)methodNode.get(); + if (method.isAbstract()) return; + + createLockField(annotation, annotationNode, method.isStatic(), false); + } + + private char[] createLockField(AnnotationValues 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 = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME; + } + + if (fieldExists(new String(lockName), annotationNode) == MemberExistsResult.NOT_EXISTS) { + if (!autoMake) { + 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 = (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(); + Eclipse.setGeneratedBy(arrayAlloc, source); + arrayAlloc.dimensions = new Expression[] { new IntLiteral(new char[] { '0' }, 0, 0) }; + Eclipse.setGeneratedBy(arrayAlloc.dimensions[0], source); + arrayAlloc.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }); + Eclipse.setGeneratedBy(arrayAlloc.type, source); + fieldDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }); + Eclipse.setGeneratedBy(fieldDecl.type, source); + fieldDecl.initialization = arrayAlloc; + injectFieldSuppressWarnings(annotationNode.up().up(), fieldDecl); + } + + return lockName; + } + @Override public void handle(AnnotationValues annotation, Annotation source, EclipseNode annotationNode) { int p1 = source.sourceStart -1; int p2 = source.sourceStart -2; @@ -77,37 +122,7 @@ public class HandleSynchronized extends EclipseAnnotationHandler { return; } - char[] lockName = annotation.getInstance().value().toCharArray(); - boolean autoMake = false; - if (lockName.length == 0) { - autoMake = true; - lockName = method.isStatic() ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME; - } - - if (fieldExists(new String(lockName), methodNode) == MemberExistsResult.NOT_EXISTS) { - if (!autoMake) { - annotationNode.addError(String.format("The field %s does not exist.", new String(lockName))); - return; - } - 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; - - //We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable! - ArrayAllocationExpression arrayAlloc = new ArrayAllocationExpression(); - Eclipse.setGeneratedBy(arrayAlloc, source); - arrayAlloc.dimensions = new Expression[] { new IntLiteral(new char[] { '0' }, 0, 0) }; - Eclipse.setGeneratedBy(arrayAlloc.dimensions[0], source); - arrayAlloc.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }); - Eclipse.setGeneratedBy(arrayAlloc.type, source); - fieldDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }); - Eclipse.setGeneratedBy(fieldDecl.type, source); - fieldDecl.initialization = arrayAlloc; - injectFieldSuppressWarnings(annotationNode.up().up(), fieldDecl); - } - + char[] lockName = createLockField(annotation, annotationNode, method.isStatic(), true); if (method.statements == null) return; Block block = new Block(0);