aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/core
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@tipit.to>2009-06-30 18:22:38 +0200
committerReinier Zwitserloot <reinier@tipit.to>2009-06-30 18:22:38 +0200
commit37006cb2954b6fcb23d2dcfaedbc3abac3fbb5d7 (patch)
treede6f4a782753fe81c8809771fdae638f2bd7d8e0 /src/lombok/core
parente0b623a3b85c89028bfc8c52578232b1c539723c (diff)
downloadlombok-37006cb2954b6fcb23d2dcfaedbc3abac3fbb5d7.tar.gz
lombok-37006cb2954b6fcb23d2dcfaedbc3abac3fbb5d7.tar.bz2
lombok-37006cb2954b6fcb23d2dcfaedbc3abac3fbb5d7.zip
After a great many iterations, Cleanup has now been reduced in functionality (exceptions from the cleanup call WILL mask exceptions from the body - this isn't intended, but it's just not possible to fix this without java 7 features or requiring a rewrite of the class file data.
Tried tactics, and why they won't work: - Replace every 'return', 'break', and 'continue' statement (for the latter 2, only if they break/continue out of the try block) with a block that first sets a uniquely named flag before doing the operation. Then, check that flag in the finally block to see if the cleanup call should be guarded by a try/catchThrowable. This doesn't work, because its not possible to instrument the 4th way out of a try block without throwing an exception: Just letting it run its course. Tossing a "#flag = true;" at the end may cause a compile time error if the code is not reachable, but figuring that out requires resolution and quite a bit of analysis. - Put catch blocks in for all relevant exceptions (RuntimeException, Error, all exceptions declared as thrown by the method, and all types of exceptions of the catch blocks of encapsulating try blocks. This doesn't work, partly because it'll fail for sneakily thrown exceptions, but mostly because you can't just catch an exception listed in the 'throws' clause of the method body; catching an exception that no statement in the try block can throw is a compile time error, but it is perfectly allright to declare these as 'thrown'. - Put in a blanket catch Throwable to set the flag. Two problems with this: First, sneaky throw can't be done. Thread.stop invokes a security manager and triggers a warning, Calling a sneakyThrow method creates a runtime dependency on lombok, constructing a sneakyThrow in-class creates visible methods or at least visible class files, and creating a new class via Class.loadClass would be very slow without caching - which gets you the same issues. Secondly, this would mean that any statements in the try body that throw an exception aren't flagged to the user as needing to be handled. The Cleanup annotation now also calls the cleanup method for you, and will call it at the END of the current scope. The following plans have been tried and abandoned: - Cleanup right after the final mention. This doesn't work, because the final mention may not be the final use-place. Example: @Cleanup InputStream in = new FileInputStream(someFile); InputStreamReader reader = new InputStreamReader(in); reader.read(); //oops - in is already closed by now. - Require an explicit var.cleanup() call and consider that the cue to close off the try block. This doesn't work either, because now variables set in between the @Cleanup declaration and the var.cleanup() call become invisible to following statements. Example: @Cleanup InputStream in = new FileInputStream(someFile); int i = in.read(); in.close(); System.out.println(i); //fail - i is inside the generated try block but this isn't, so 'i' is not visible from here. By running to the end of visible scope, all these problems are avoided. This does remove the flexibility of declaring where you want a close call to be executed, but there are two mitigating factors available: 1) Create an explicit scope block. You can just stick { code } in any place where you can legally write a statement, in java. This is relatively unknown, so I expect most users will go for: 2) Just call close explicitly. I've yet to see a cleanup method which isn't idempotent anyway (calling it multiple times is no different than calling it once). During the course of investigating these options, the AST code has been extended to support live replacement of any child node, including updating the actual underlying system AST as well as our own. Unfortunately, this code has NOT been tested. It was rather a lot of work so I'm leaving it in, and at least for eclipse it even seemed to work.
Diffstat (limited to 'src/lombok/core')
-rw-r--r--src/lombok/core/AST.java103
1 files changed, 100 insertions, 3 deletions
diff --git a/src/lombok/core/AST.java b/src/lombok/core/AST.java
index 871f24be..f4d4d628 100644
--- a/src/lombok/core/AST.java
+++ b/src/lombok/core/AST.java
@@ -2,6 +2,7 @@ package lombok.core;
import static lombok.Lombok.sneakyThrow;
+import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
@@ -93,12 +94,12 @@ public abstract class AST<N> {
public abstract class Node {
protected final Kind kind;
protected final N node;
- protected final Collection<? extends Node> children;
+ protected final List<? extends Node> children;
protected Node parent;
protected boolean handled;
protected boolean isStructurallySignificant;
- protected Node(N node, Collection<? extends Node> children, Kind kind) {
+ protected Node(N node, List<? extends Node> children, Kind kind) {
this.kind = kind;
this.node = node;
this.children = children == null ? Collections.<Node>emptyList() : children;
@@ -129,6 +130,22 @@ public abstract class AST<N> {
return node;
}
+ @SuppressWarnings("unchecked")
+ public Node replaceWith(N newN, Kind kind) {
+ Node newNode = buildTree(newN, kind);
+ newNode.parent = parent;
+ for ( int i = 0 ; i < parent.children.size() ; i++ ) {
+ if ( parent.children.get(i) == this ) ((List)parent.children).set(i, newNode);
+ }
+
+ parent.replaceChildNode(get(), newN);
+ return newNode;
+ }
+
+ public void replaceChildNode(N oldN, N newN) {
+ replaceStatementInNode(get(), oldN, newN);
+ }
+
public Kind getKind() {
return kind;
}
@@ -204,7 +221,6 @@ public abstract class AST<N> {
private void gatherAndRemoveChildren(Map<N, Node> map) {
for ( Node child : children ) child.gatherAndRemoveChildren(map);
map.put(get(), this);
- children.clear();
identityDetector.remove(get());
nodeMap.remove(get());
}
@@ -301,6 +317,87 @@ public abstract class AST<N> {
return list;
}
+ protected boolean replaceStatementInNode(N statement, N oldN, N newN) {
+ for ( FieldAccess fa : fieldsOf(statement.getClass()) ) {
+ if ( replaceStatementInField(fa, statement, oldN, newN) ) return true;
+ }
+
+ return false;
+ }
+
+ private boolean replaceStatementInField(FieldAccess fa, N statement, N oldN, N newN) {
+ try {
+ Object o = fa.field.get(statement);
+ if ( o == null ) return false;
+
+ if ( o == oldN ) {
+ fa.field.set(statement, newN);
+ return true;
+ }
+
+ if ( fa.dim > 0 ) {
+ if ( o.getClass().isArray() ) {
+ return replaceStatementInArray(o, oldN, newN);
+ } else if ( Collection.class.isInstance(o) ) {
+ return replaceStatementInCollection(fa.field, statement, new ArrayList<Collection<?>>(), (Collection<?>)o, oldN, newN);
+ }
+ }
+
+ return false;
+ } catch ( IllegalAccessException e ) {
+ throw sneakyThrow(e);
+ }
+
+ }
+
+ private boolean replaceStatementInCollection(Field field, Object fieldRef, List<Collection<?>> chain, Collection<?> collection, N oldN, N newN) throws IllegalAccessException {
+ if ( collection == null ) return false;
+
+ int idx = -1;
+ for ( Object o : collection ) {
+ idx++;
+ if ( o == null ) continue;
+ if ( Collection.class.isInstance(o) ) {
+ Collection<?> newC = (Collection<?>)o;
+ List<Collection<?>> newChain = new ArrayList<Collection<?>>(chain);
+ newChain.add(newC);
+ if ( replaceStatementInCollection(field, fieldRef, newChain, newC, oldN, newN) ) return true;
+ }
+ if ( o == oldN ) {
+ setElementInASTCollection(field, fieldRef, chain, collection, idx, newN);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /** Override if your AST collection does not support the set method. Javac's for example, does not. */
+ @SuppressWarnings("unchecked")
+ protected void setElementInASTCollection(Field field, Object fieldRef, List<Collection<?>> chain, Collection<?> collection, int idx, N newN) throws IllegalAccessException {
+ if ( collection instanceof List<?> ) {
+ ((List)collection).set(idx, newN);
+ }
+ }
+
+ private boolean replaceStatementInArray(Object array, N oldN, N newN) {
+ if ( array == null ) return false;
+
+ int len = Array.getLength(array);
+ for ( int i = 0 ; i < len ; i++ ) {
+ Object o = Array.get(array, i);
+ if ( o == null ) continue;
+ if ( o.getClass().isArray() ) {
+ if ( replaceStatementInArray(o, oldN, newN) ) return true;
+ } else if ( o == oldN ) {
+ Array.set(array, i, newN);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
@SuppressWarnings("unchecked")
private <T extends Node> void buildWithField0(Class<T> nodeType, N child, FieldAccess fa, Collection<T> list) {
try {