aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2011-11-20 17:23:33 +0100
committerReinier Zwitserloot <reinier@zwitserloot.com>2011-11-20 19:04:31 +0100
commit3b921ad7f6a485287ae62e64d0f4a2859683732a (patch)
tree1748aa69689121c032cb31b8e721e6dae42c4719
parent52ee15bdc13ab66c833863d75581f6ccae3081ce (diff)
downloadlombok-3b921ad7f6a485287ae62e64d0f4a2859683732a.tar.gz
lombok-3b921ad7f6a485287ae62e64d0f4a2859683732a.tar.bz2
lombok-3b921ad7f6a485287ae62e64d0f4a2859683732a.zip
Fix for issue 299: labels would break 'val' in javac.
-rw-r--r--src/core/lombok/javac/JavacResolution.java5
-rw-r--r--src/utils/lombok/javac/TreeMirrorMaker.java17
-rw-r--r--test/transform/resource/after-delombok/ValWithLabel.java9
-rw-r--r--test/transform/resource/after-ecj/ValWithLabel.java1
-rw-r--r--test/transform/resource/after-eclipse/ValWithLabel.java1
-rw-r--r--test/transform/resource/before/ValWithLabel.java11
6 files changed, 44 insertions, 0 deletions
diff --git a/src/core/lombok/javac/JavacResolution.java b/src/core/lombok/javac/JavacResolution.java
index e0d56b85..29f261c6 100644
--- a/src/core/lombok/javac/JavacResolution.java
+++ b/src/core/lombok/javac/JavacResolution.java
@@ -221,6 +221,11 @@ public class JavacResolution {
}
}
+ /*
+ * We need to dig down to the level of the method or field declaration or (static) initializer block, then attribute that entire method/field/block using
+ * the appropriate environment. So, we start from the top and walk down the node tree until we hit that method/field/block and stop there, recording both
+ * the environment object (`env`) and the exact tree node (`copyAt`) at which to begin the attr process.
+ */
private static final class EnvFinder extends JCTree.Visitor {
private Env<AttrContext> env = null;
private Enter enter;
diff --git a/src/utils/lombok/javac/TreeMirrorMaker.java b/src/utils/lombok/javac/TreeMirrorMaker.java
index db824505..30915572 100644
--- a/src/utils/lombok/javac/TreeMirrorMaker.java
+++ b/src/utils/lombok/javac/TreeMirrorMaker.java
@@ -26,6 +26,7 @@ import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
+import com.sun.source.tree.LabeledStatementTree;
import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
@@ -33,6 +34,16 @@ import com.sun.tools.javac.tree.TreeCopier;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.List;
+/**
+ * Makes a copy of any AST node, with some exceptions.
+ * Exceptions:<ul>
+ * <li>The symbol ('sym') of a copied variable isn't copied.
+ * <li>all labels are removed.
+ * </ul>
+ *
+ * The purpose of this class is to make a copy, and then the copy is attributed (resolution info is added). These exceptions
+ * are to work around apparent bugs (or at least inconsistencies) in javac sources.
+ */
public class TreeMirrorMaker extends TreeCopier<Void> {
private final IdentityHashMap<JCTree, JCTree> originalToCopy = new IdentityHashMap<JCTree, JCTree>();
@@ -83,4 +94,10 @@ public class TreeMirrorMaker extends TreeCopier<Void> {
copy.sym = ((JCVariableDecl) node).sym;
return copy;
}
+
+ // Fix for NPE in HandleVal. See http://code.google.com/p/projectlombok/issues/detail?id=299
+ // This and visitVariable is rather hacky but we're working around evident bugs or at least inconsistencies in javac.
+ @Override public JCTree visitLabeledStatement(LabeledStatementTree node, Void p) {
+ return node.getStatement().accept(this, p);
+ }
}
diff --git a/test/transform/resource/after-delombok/ValWithLabel.java b/test/transform/resource/after-delombok/ValWithLabel.java
new file mode 100644
index 00000000..82cd4ed7
--- /dev/null
+++ b/test/transform/resource/after-delombok/ValWithLabel.java
@@ -0,0 +1,9 @@
+public class ValWithLabel {
+ {
+ LABEL: for (final java.lang.String x : new String[0]) {
+ if (x.toLowerCase() == null) {
+ continue LABEL;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/test/transform/resource/after-ecj/ValWithLabel.java b/test/transform/resource/after-ecj/ValWithLabel.java
new file mode 100644
index 00000000..cb06d3c1
--- /dev/null
+++ b/test/transform/resource/after-ecj/ValWithLabel.java
@@ -0,0 +1 @@
+//ignore \ No newline at end of file
diff --git a/test/transform/resource/after-eclipse/ValWithLabel.java b/test/transform/resource/after-eclipse/ValWithLabel.java
new file mode 100644
index 00000000..cb06d3c1
--- /dev/null
+++ b/test/transform/resource/after-eclipse/ValWithLabel.java
@@ -0,0 +1 @@
+//ignore \ No newline at end of file
diff --git a/test/transform/resource/before/ValWithLabel.java b/test/transform/resource/before/ValWithLabel.java
new file mode 100644
index 00000000..f7c3402a
--- /dev/null
+++ b/test/transform/resource/before/ValWithLabel.java
@@ -0,0 +1,11 @@
+import lombok.val;
+
+public class ValWithLabel {
+ {
+ LABEL: for (val x : new String[0]) {
+ if (x.toLowerCase() == null) {
+ continue LABEL;
+ }
+ }
+ }
+} \ No newline at end of file