You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/11/08 14:54:09 UTC
[groovy] branch master updated: GROOVY-9541: STC: add `GroovyClassLoader` to `evaluateExpression` params
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new bf405f70bc GROOVY-9541: STC: add `GroovyClassLoader` to `evaluateExpression` params
bf405f70bc is described below
commit bf405f70bc8b2ff24150f8d5249023a29da224ed
Author: jonnybot0 <>
AuthorDate: Tue Nov 8 08:29:06 2022 -0600
GROOVY-9541: STC: add `GroovyClassLoader` to `evaluateExpression` params
---
.../ASTTransformationCollectorCodeVisitor.java | 4 ++--
.../transform/stc/StaticTypeCheckingSupport.java | 28 +++++++++++++++-------
2 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/ASTTransformationCollectorCodeVisitor.java b/src/main/java/org/codehaus/groovy/transform/ASTTransformationCollectorCodeVisitor.java
index 3b578dc20a..7c0a26279f 100644
--- a/src/main/java/org/codehaus/groovy/transform/ASTTransformationCollectorCodeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/ASTTransformationCollectorCodeVisitor.java
@@ -183,7 +183,7 @@ public class ASTTransformationCollectorCodeVisitor extends ClassCodeVisitorSuppo
if (annotation.getClassNode().getName().equals(AnnotationCollector.class.getName())) {
Expression mode = annotation.getMember("mode");
modes.put(index, Optional.ofNullable(mode)
- .map(exp -> evaluateExpression(exp, source.getConfiguration()))
+ .map(exp -> evaluateExpression(exp, source.getConfiguration(), transformLoader))
.map(val -> (AnnotationCollectorMode) val)
.orElse(AnnotationCollectorMode.DUPLICATE)
);
@@ -191,7 +191,7 @@ public class ASTTransformationCollectorCodeVisitor extends ClassCodeVisitorSuppo
Expression processor = annotation.getMember("processor");
AnnotationCollectorTransform act = null;
if (processor != null) {
- String className = (String) evaluateExpression(processor, source.getConfiguration());
+ String className = (String) evaluateExpression(processor, source.getConfiguration(), transformLoader);
Class<?> klass = loadTransformClass(className, alias);
if (klass != null) {
try {
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 8f7ebac010..037a458a19 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -18,6 +18,7 @@
*/
package org.codehaus.groovy.transform.stc;
+import groovy.lang.GroovyClassLoader;
import org.apache.groovy.util.Maps;
import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.ast.ClassNode;
@@ -2182,24 +2183,34 @@ public abstract class StaticTypeCheckingSupport {
}
/**
- * A helper method that can be used to evaluate expressions as found in annotation
- * parameters. For example, it will evaluate a constant, be it referenced directly as
- * an integer or as a reference to a field.
+ * @deprecated Use {@link #evaluateExpression(Expression, CompilerConfiguration, GroovyClassLoader)} instead
+ */
+ @Deprecated
+ public static Object evaluateExpression(final Expression expr, final CompilerConfiguration config) {
+ return evaluateExpression(expr, config, null);
+ }
+
+ /**
+ * Evaluates expressions as found in annotation parameters. For example, it
+ * will evaluate a constant, be it referenced directly as an integer or as a
+ * reference to a field.
* <p>
- * If this method throws an exception, then the expression cannot be evaluated on its own.
+ * If the expression cannot be evaluated on its own, an exception is thrown.
*
* @param expr the expression to be evaluated
* @param config the compiler configuration
+ * @param loader the compiler class loader
* @return the result of the expression
+ * @throws GroovyBugError
*/
- public static Object evaluateExpression(final Expression expr, final CompilerConfiguration config) {
+ public static Object evaluateExpression(final Expression expr, final CompilerConfiguration config, /*@Nullable*/ final GroovyClassLoader loader) {
Expression ce = expr instanceof CastExpression ? ((CastExpression) expr).getExpression() : expr;
if (ce instanceof ConstantExpression) {
if (expr.getType().equals(ce.getType()))
return ((ConstantExpression) ce).getValue();
} else if (ce instanceof ListExpression) {
if (expr.getType().isArray() && expr.getType().getComponentType().equals(STRING_TYPE))
- return ((ListExpression) ce).getExpressions().stream().map(e -> evaluateExpression(e, config)).toArray(String[]::new);
+ return ((ListExpression) ce).getExpressions().stream().map(e -> evaluateExpression(e, config, loader)).toArray(String[]::new);
}
String className = "Expression$"+UUID.randomUUID().toString().replace('-', '$');
@@ -2212,7 +2223,7 @@ public abstract class StaticTypeCheckingSupport {
cc.setScriptBaseClass(null);
cc.setTargetBytecode(CompilerConfiguration.DEFAULT.getTargetBytecode());
- CompilationUnit cu = new CompilationUnit(cc);
+ CompilationUnit cu = new CompilationUnit(cc, null, loader);
try {
cu.addClassNode(classNode);
cu.compile(Phases.CLASS_GENERATION);
@@ -2223,7 +2234,8 @@ public abstract class StaticTypeCheckingSupport {
} catch (ReflectiveOperationException e) {
throw new GroovyBugError(e);
} finally {
- closeQuietly(cu.getClassLoader());
+ if (loader == null)
+ closeQuietly(cu.getClassLoader());
}
}