This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new f664a72521 close short-lived `GroovyClassLoader`
f664a72521 is described below

commit f664a725216d7beb4e245309d0846d608f33d689
Author: Eric Milles <[email protected]>
AuthorDate: Mon Oct 10 10:24:24 2022 -0500

    close short-lived `GroovyClassLoader`
---
 .../transform/stc/StaticTypeCheckingSupport.java   | 29 +++++++++++++---------
 1 file changed, 17 insertions(+), 12 deletions(-)

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 4278f045f5..29379194d8 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -49,7 +49,6 @@ import org.codehaus.groovy.tools.GroovyClass;
 import org.codehaus.groovy.transform.trait.Traits;
 import org.objectweb.asm.Opcodes;
 
-import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -2151,22 +2150,28 @@ public abstract class StaticTypeCheckingSupport {
      * @return the result of the expression
      */
     public static Object evaluateExpression(final Expression expr, final 
CompilerConfiguration config) {
-        String className = "Expression$" + 
UUID.randomUUID().toString().replace('-', '$');
-        ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, 
OBJECT_TYPE);
-        ReturnStatement code = new ReturnStatement(expr);
-        addGeneratedMethod(node, "eval", Opcodes.ACC_PUBLIC + 
Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, 
code);
+        String className = 
"Expression$"+UUID.randomUUID().toString().replace('-', '$');
+        ClassNode classNode = new ClassNode(className, Opcodes.ACC_PUBLIC, 
OBJECT_TYPE);
+        addGeneratedMethod(classNode, "eval", Opcodes.ACC_PUBLIC | 
Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, 
new ReturnStatement(expr));
+
         CompilerConfiguration copyConf = new CompilerConfiguration(config);
-        // disable preview features so class can be inspected by this JVM
+        // disable preview features so class can be executed by this JVM
         copyConf.setPreviewFeatures(false);
+        copyConf.setScriptBaseClass(null);
+        
copyConf.setTargetBytecode(CompilerConfiguration.DEFAULT.getTargetBytecode());
+
         CompilationUnit cu = new CompilationUnit(copyConf);
-        cu.addClassNode(node);
-        cu.compile(Phases.CLASS_GENERATION);
-        List<GroovyClass> classes = cu.getClasses();
-        Class<?> aClass = cu.getClassLoader().defineClass(className, 
classes.get(0).getBytes());
         try {
-            return aClass.getMethod("eval").invoke(null);
-        } catch (IllegalAccessException | NoSuchMethodException | 
InvocationTargetException e) {
+            cu.addClassNode(classNode);
+            cu.compile(Phases.CLASS_GENERATION);
+            GroovyClass gc = cu.getClasses().get(0);
+            Class<?> c = cu.getClassLoader().defineClass(className, 
gc.getBytes());
+            // invoke method to produce return value
+            return c.getMethod("eval").invoke(null);
+        } catch (ReflectiveOperationException e) {
             throw new GroovyBugError(e);
+        } finally {
+            
org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport.closeQuietly(cu.getClassLoader());
         }
     }
 

Reply via email to