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.
This commit is contained in:
Reinier Zwitserloot committed 2009-07-01 20:16:26 +02:00
1 parent 194c990f64
commit efe3931cf3
6 files changed
+34 -6

No files matched your search

+2
View File
@@ -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:
+3 -1
View File
@@ -7,4 +7,6 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Synchronized {}
public @interface Synchronized {
String value() default "";
}
+19 -2
View File
@@ -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<Cleanup> {
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<Cleanup> {
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<Cleanup> {
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.");
}
}
}
@@ -48,7 +48,8 @@ public class HandleSynchronized implements EclipseAnnotationHandler<Synchronized
return true;
}
char[] lockName = method.isStatic() ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
char[] lockName = annotation.getInstance().value().toCharArray();
if ( lockName.length == 0 ) lockName = method.isStatic() ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
if ( fieldExists(new String(lockName), methodNode) == MemberExistsResult.NOT_EXISTS ) {
FieldDeclaration fieldDecl = new FieldDeclaration(lockName, 0, -1);
+6 -1
View File
@@ -42,6 +42,11 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
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<Cleanup> {
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.");
}
}
}
@@ -41,7 +41,8 @@ public class HandleSynchronized implements JavacAnnotationHandler<Synchronized>
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();