aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/lombok/javac/JavacResolution.java60
-rw-r--r--src/stubs/com/sun/tools/javac/comp/ArgumentAttr.java12
-rw-r--r--src/utils/lombok/javac/TreeMirrorMaker.java18
-rw-r--r--test/transform/resource/after-delombok/ValSwitchExpression.java11
-rw-r--r--test/transform/resource/after-ecj/ValSwitchExpression.java16
-rw-r--r--test/transform/resource/before/ValSwitchExpression.java13
6 files changed, 119 insertions, 11 deletions
diff --git a/src/core/lombok/javac/JavacResolution.java b/src/core/lombok/javac/JavacResolution.java
index e96079e0..f1109f4e 100644
--- a/src/core/lombok/javac/JavacResolution.java
+++ b/src/core/lombok/javac/JavacResolution.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2011-2020 The Project Lombok Authors.
+ * Copyright (C) 2011-2021 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -29,6 +29,7 @@ import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
@@ -45,6 +46,7 @@ import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Type.TypeVar;
import com.sun.tools.javac.code.Type.WildcardType;
import com.sun.tools.javac.code.Types;
+import com.sun.tools.javac.comp.ArgumentAttr;
import com.sun.tools.javac.comp.Attr;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Enter;
@@ -68,6 +70,7 @@ import lombok.core.debug.AssertionLogger;
import lombok.permit.Permit;
public class JavacResolution {
+ private final Context context;
private final Attr attr;
private final CompilerMessageSuppressor messageSuppressor;
@@ -82,6 +85,7 @@ public class JavacResolution {
}
public JavacResolution(Context context) {
+ this.context = context;
attr = Attr.instance(context);
messageSuppressor = new CompilerMessageSuppressor(context);
}
@@ -245,10 +249,19 @@ public class JavacResolution {
} catch (Throwable ignore) {
// This addresses issue #1553 which involves JDK9; if it doesn't exist, we probably don't need to set it.
}
- if (tree instanceof JCBlock) attr.attribStat(tree, env);
- else if (tree instanceof JCMethodDecl) attr.attribStat(((JCMethodDecl) tree).body, env);
- else if (tree instanceof JCVariableDecl) attr.attribStat(tree, env);
- else throw new IllegalStateException("Called with something that isn't a block, method decl, or variable decl");
+
+ Map<?,?> cache = null;
+ try {
+ cache = ArgumentAttrReflect.enableTempCache(context);
+
+ if (tree instanceof JCBlock) attr.attribStat(tree, env);
+ else if (tree instanceof JCMethodDecl) attr.attribStat(((JCMethodDecl) tree).body, env);
+ else if (tree instanceof JCVariableDecl) attr.attribStat(tree, env);
+ else throw new IllegalStateException("Called with something that isn't a block, method decl, or variable decl");
+ } finally {
+ ArgumentAttrReflect.restoreCache(cache, context);
+ }
+
}
public static class TypeNotConvertibleException extends Exception {
@@ -283,6 +296,43 @@ public class JavacResolution {
}
}
+ /**
+ * ArgumentAttr was added in Java 9 and caches some method arguments. Lombok should cleanup its changes after resolution.
+ */
+ private static class ArgumentAttrReflect {
+ private static Field ARGUMENT_TYPE_CACHE;
+
+ static {
+ if (Javac.getJavaCompilerVersion() >= 9) {
+ try {
+ ARGUMENT_TYPE_CACHE = Permit.getField(ArgumentAttr.class, "argumentTypeCache");
+ } catch (Exception ignore) {}
+ }
+ }
+
+ public static Map<?, ?> enableTempCache(Context context) {
+ if (ARGUMENT_TYPE_CACHE == null) return null;
+
+ ArgumentAttr argumentAttr = ArgumentAttr.instance(context);
+ try {
+ Map<?, ?> cache = (Map<?, ?>) Permit.get(ARGUMENT_TYPE_CACHE, argumentAttr);
+ Permit.set(ARGUMENT_TYPE_CACHE, argumentAttr, new LinkedHashMap<Object, Object>(cache));
+ return cache;
+ } catch (Exception ignore) { }
+
+ return null;
+ }
+
+ public static void restoreCache(Map<?, ?> cache, Context context) {
+ if (ARGUMENT_TYPE_CACHE == null) return;
+
+ ArgumentAttr argumentAttr = ArgumentAttr.instance(context);
+ try {
+ Permit.set(ARGUMENT_TYPE_CACHE, argumentAttr, cache);
+ } catch (Exception ignore) { }
+ }
+ }
+
public static Type ifTypeIsIterableToComponent(Type type, JavacAST ast) {
if (type == null) return null;
Types types = Types.instance(ast.getContext());
diff --git a/src/stubs/com/sun/tools/javac/comp/ArgumentAttr.java b/src/stubs/com/sun/tools/javac/comp/ArgumentAttr.java
new file mode 100644
index 00000000..954a4592
--- /dev/null
+++ b/src/stubs/com/sun/tools/javac/comp/ArgumentAttr.java
@@ -0,0 +1,12 @@
+/*
+ * These are stub versions of various bits of javac-internal API (for various different versions of javac). Lombok is compiled against these.
+ */
+package com.sun.tools.javac.comp;
+
+import com.sun.tools.javac.tree.JCTree;
+import com.sun.tools.javac.util.Context;
+
+// JDK9+
+public class ArgumentAttr extends JCTree.Visitor {
+ public static ArgumentAttr instance(Context context) { return null; }
+} \ No newline at end of file
diff --git a/src/utils/lombok/javac/TreeMirrorMaker.java b/src/utils/lombok/javac/TreeMirrorMaker.java
index 3cb79412..a67a4ec1 100644
--- a/src/utils/lombok/javac/TreeMirrorMaker.java
+++ b/src/utils/lombok/javac/TreeMirrorMaker.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2015 The Project Lombok Authors.
+ * Copyright (C) 2010-2021 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -56,14 +56,14 @@ public class TreeMirrorMaker extends TreeCopier<Void> {
@Override public <T extends JCTree> T copy(T original) {
T copy = super.copy(original);
- originalToCopy.put(original, copy);
+ putIfAbsent(originalToCopy, original, copy);
return copy;
}
@Override public <T extends JCTree> T copy(T original, Void p) {
T copy = super.copy(original, p);
- originalToCopy.put(original, copy);
- return copy;
+ putIfAbsent(originalToCopy, original, copy);
+ return copy;
}
@Override public <T extends JCTree> List<T> copy(List<T> originals) {
@@ -71,7 +71,7 @@ public class TreeMirrorMaker extends TreeCopier<Void> {
if (originals != null) {
Iterator<T> it1 = originals.iterator();
Iterator<T> it2 = copies.iterator();
- while (it1.hasNext()) originalToCopy.put(it1.next(), it2.next());
+ while (it1.hasNext()) putIfAbsent(originalToCopy, it1.next(), it2.next());
}
return copies;
}
@@ -81,7 +81,7 @@ public class TreeMirrorMaker extends TreeCopier<Void> {
if (originals != null) {
Iterator<T> it1 = originals.iterator();
Iterator<T> it2 = copies.iterator();
- while (it1.hasNext()) originalToCopy.put(it1.next(), it2.next());
+ while (it1.hasNext()) putIfAbsent(originalToCopy, it1.next(), it2.next());
}
return copies;
}
@@ -120,4 +120,10 @@ public class TreeMirrorMaker extends TreeCopier<Void> {
@Override public JCTree visitLabeledStatement(LabeledStatementTree node, Void p) {
return node.getStatement().accept(this, p);
}
+
+ private <K, V> void putIfAbsent(Map<K, V> map, K key, V value) {
+ if (!map.containsKey(key)) {
+ map.put(key, value);
+ }
+ }
}
diff --git a/test/transform/resource/after-delombok/ValSwitchExpression.java b/test/transform/resource/after-delombok/ValSwitchExpression.java
new file mode 100644
index 00000000..a8fa9b0f
--- /dev/null
+++ b/test/transform/resource/after-delombok/ValSwitchExpression.java
@@ -0,0 +1,11 @@
+// version 14:
+public class ValSwitchExpression {
+ public void method(int arg) {
+ final int x = switch (arg) {
+ default -> {
+ final java.lang.String s = "string";
+ yield arg;
+ }
+ };
+ }
+} \ No newline at end of file
diff --git a/test/transform/resource/after-ecj/ValSwitchExpression.java b/test/transform/resource/after-ecj/ValSwitchExpression.java
new file mode 100644
index 00000000..59b503cb
--- /dev/null
+++ b/test/transform/resource/after-ecj/ValSwitchExpression.java
@@ -0,0 +1,16 @@
+// version 14:
+import lombok.val;
+public class ValSwitchExpression {
+ public ValSwitchExpression() {
+ super();
+ }
+ public void method(int arg) {
+ final @val int x = switch (arg) {
+ default ->
+ {
+ final @val java.lang.String s = "string";
+ yield arg;
+ }
+ };
+ }
+} \ No newline at end of file
diff --git a/test/transform/resource/before/ValSwitchExpression.java b/test/transform/resource/before/ValSwitchExpression.java
new file mode 100644
index 00000000..c3838acc
--- /dev/null
+++ b/test/transform/resource/before/ValSwitchExpression.java
@@ -0,0 +1,13 @@
+// version 14:
+import lombok.val;
+
+public class ValSwitchExpression {
+ public void method(int arg) {
+ val x = switch (arg) {
+ default -> {
+ val s = "string";
+ yield arg;
+ }
+ };
+ }
+}