Skip to content
Permalink
Browse files

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.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1854663 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Mar 2, 2019
1 parent 7c5cebb commit dea9ca9445b8067d8a72121609fd0cf663ea7d65
Showing with 22 additions and 13 deletions.
  1. +22 −13 framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java
@@ -145,11 +145,7 @@ public static Binding getBinding(Map<String, Object> context) {
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 static Binding getBinding(Map<String, Object> context) {
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 {

0 comments on commit dea9ca9

Please sign in to comment.
You can’t perform that action at this time.