svn commit: r1854663 - /ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1854663 - /ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java

mthl
Author: mthl
Date: Sat Mar  2 19:14:24 2019
New Revision: 1854663

URL: http://svn.apache.org/viewvc?rev=1854663&view=rev
Log:
Improved: Rewrite ‘GroovyUtil#parseClass’ (OFBIZ-10822)

The ‘parseClass(InputStream in, String location)’ and
‘parseClass(InputStream in, String location, GroovyClassLoader
groovyClassLoader)’ methods from GroovyUtil are now merged in a
private and documented method.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java?rev=1854663&r1=1854662&r2=1854663&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java Sat Mar  2 19:14:24 2019
@@ -145,11 +145,7 @@ public class GroovyUtil {
                 if (scriptUrl == null) {
                     throw new GeneralException("Script not found at location [" + location + "]");
                 }
-                if (groovyScriptClassLoader != null) {
-                    scriptClass = parseClass(scriptUrl.openStream(), location, groovyScriptClassLoader);
-                } else {
-                    scriptClass = parseClass(scriptUrl.openStream(), location);
-                }
+                scriptClass = parseClass(scriptUrl.openStream(), location);
                 Class<?> scriptClassCached = parsedScripts.putIfAbsent(location, scriptClass);
                 if (scriptClassCached == null) { // putIfAbsent returns null if the class is added to the cache
                     if (Debug.verboseOn()) {
@@ -175,14 +171,27 @@ public class GroovyUtil {
         return classLoader;
     }
 
-    public static Class<?> parseClass(InputStream in, String location) throws IOException {
-        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
-        Class<?> classLoader = groovyClassLoader.parseClass(UtilIO.readString(in), location);
-        groovyClassLoader.close();
-        return classLoader;
-    }
-    public static Class<?> parseClass(InputStream in, String location, GroovyClassLoader groovyClassLoader) throws IOException {
-        return groovyClassLoader.parseClass(UtilIO.readString(in), location);
+    /**
+     * Parses a Groovy class from an input stream.
+     * <p>
+     * This method is useful for parsing a Groovy script referenced by
+     * a flexible location like {@code component://myComponent/script.groovy}.
+     *
+     * @param in  the input stream containing the class source code
+     * @param location  the file name to associate with this class
+     * @return the corresponding class object
+     * @throws IOException when parsing fails
+     */
+    private static Class<?> parseClass(InputStream in, String location) throws IOException {
+        String classText = UtilIO.readString(in);
+        if (groovyScriptClassLoader != null) {
+            return groovyScriptClassLoader.parseClass(classText, location);
+        } else {
+            GroovyClassLoader classLoader = new GroovyClassLoader();
+            Class<?> klass = classLoader.parseClass(classText, location);
+            classLoader.close();
+            return klass;
+        }
     }
 
     public static Class<?> parseClass(String text) throws IOException {