From efe3931cf3d9134d7d8e5adf792b05a47ce2c423 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 1 Jul 2009 20:16:26 +0200 Subject: Added ability to @Synchronized to explicitly name the lock object. Spruced up @Cleanup's position settings and also forced initialization, because the error appears in a screwed up place if you don't, and we can't seem to move it. --- doc/TODO.txt | 2 ++ src/lombok/Synchronized.java | 4 +++- src/lombok/eclipse/handlers/HandleCleanup.java | 21 +++++++++++++++++++-- src/lombok/eclipse/handlers/HandleSynchronized.java | 3 ++- src/lombok/javac/handlers/HandleCleanup.java | 7 ++++++- src/lombok/javac/handlers/HandleSynchronized.java | 3 ++- 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/doc/TODO.txt b/doc/TODO.txt index 944ef5c6..2bc8f14d 100644 --- a/doc/TODO.txt +++ b/doc/TODO.txt @@ -1,5 +1,7 @@ ## Fix Eclipse's HandleCleanup +Consider requiring an initializer and warn when the varname gets reassigned, if the declaration wasn't already final. Think about this more. + Right now exceptions thrown by the cleanup method will mask any exceptions thrown in the main body, which is not wanted. This does not appear to be doable without java 7 features or very extensive additions to the lombok framework. A lot has been tried: diff --git a/src/lombok/Synchronized.java b/src/lombok/Synchronized.java index f7ab900c..0c813eb7 100644 --- a/src/lombok/Synchronized.java +++ b/src/lombok/Synchronized.java @@ -7,4 +7,6 @@ import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) -public @interface Synchronized {} +public @interface Synchronized { + String value() default ""; +} diff --git a/src/lombok/eclipse/handlers/HandleCleanup.java b/src/lombok/eclipse/handlers/HandleCleanup.java index fa97e76d..b5251e8a 100644 --- a/src/lombok/eclipse/handlers/HandleCleanup.java +++ b/src/lombok/eclipse/handlers/HandleCleanup.java @@ -16,6 +16,7 @@ import org.eclipse.jdt.internal.compiler.ast.Block; import org.eclipse.jdt.internal.compiler.ast.CaseStatement; import org.eclipse.jdt.internal.compiler.ast.CastExpression; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; +import org.eclipse.jdt.internal.compiler.ast.MemberValuePair; import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.Statement; @@ -39,6 +40,11 @@ public class HandleCleanup implements EclipseAnnotationHandler { LocalDeclaration decl = (LocalDeclaration)annotationNode.up().get(); + if ( decl.initialization == null ) { + annotationNode.addError("@Cleanup variable declarations need to be initialized."); + return true; + } + Node ancestor = annotationNode.up().directUp(); ASTNode blockNode = ancestor.get(); @@ -110,7 +116,18 @@ public class HandleCleanup implements EclipseAnnotationHandler { Statement[] finallyBlock = new Statement[1]; MessageSend unsafeClose = new MessageSend(); - unsafeClose.receiver = new SingleNameReference(decl.name, 0); + unsafeClose.sourceStart = ast.sourceStart; + unsafeClose.sourceEnd = ast.sourceEnd; + SingleNameReference receiver = new SingleNameReference(decl.name, 0); + unsafeClose.receiver = receiver; + long nameSourcePosition = (long)ast.sourceStart << 32 | ast.sourceEnd; + if ( ast.memberValuePairs() != null ) for ( MemberValuePair pair : ast.memberValuePairs() ) { + if ( pair.name != null && new String(pair.name).equals("cleanupMethod") ) { + nameSourcePosition = (long)pair.value.sourceStart << 32 | pair.value.sourceEnd; + break; + } + } + unsafeClose.nameSourcePosition = nameSourcePosition; unsafeClose.selector = cleanupName.toCharArray(); finallyBlock[0] = unsafeClose; tryStatement.finallyBlock = new Block(0); @@ -148,7 +165,7 @@ public class HandleCleanup implements EclipseAnnotationHandler { if ( Arrays.equals(((SingleNameReference)statement).token, varName) ) { Node problemNode = node.getNodeFor(statement); if ( problemNode != null ) problemNode.addWarning( - "You're assigning a guarded variable to something else. This is a bad idea."); + "You're assigning an auto-cleanup variable to something else. This is a bad idea."); } } } diff --git a/src/lombok/eclipse/handlers/HandleSynchronized.java b/src/lombok/eclipse/handlers/HandleSynchronized.java index 28e8bd1f..e1d4ed6a 100644 --- a/src/lombok/eclipse/handlers/HandleSynchronized.java +++ b/src/lombok/eclipse/handlers/HandleSynchronized.java @@ -48,7 +48,8 @@ public class HandleSynchronized implements EclipseAnnotationHandler { JCVariableDecl decl = (JCVariableDecl)annotationNode.up().get(); + if ( decl.init == null ) { + annotationNode.addError("@Cleanup variable declarations need to be initialized."); + return true; + } + Node ancestor = annotationNode.up().directUp(); JCTree blockNode = ancestor.get(); @@ -109,7 +114,7 @@ public class HandleCleanup implements JavacAnnotationHandler { if ( ((JCIdent)statement).name.contentEquals(name) ) { Node problemNode = node.getNodeFor(statement); if ( problemNode != null ) problemNode.addWarning( - "You're assigning a guarded variable to something else. This is a bad idea."); + "You're assigning an auto-cleanup variable to something else. This is a bad idea."); } } } diff --git a/src/lombok/javac/handlers/HandleSynchronized.java b/src/lombok/javac/handlers/HandleSynchronized.java index 4acb6e27..84508171 100644 --- a/src/lombok/javac/handlers/HandleSynchronized.java +++ b/src/lombok/javac/handlers/HandleSynchronized.java @@ -41,7 +41,8 @@ public class HandleSynchronized implements JavacAnnotationHandler return true; } boolean isStatic = (method.mods.flags & Flags.STATIC) != 0; - String lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME; + String lockName = annotation.getInstance().value(); + if ( lockName.length() == 0 ) lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME; TreeMaker maker = methodNode.getTreeMaker(); -- cgit