Improved: Enforce noninstantiability to GroovyUtil Class.
Made class as final, moved default constructor and renamed private data members as per naming convention best practices.
@@ -41,13 +41,14 @@ import groovy.lang.Script;
* Groovy Utilities.
*
*/
-public class GroovyUtil {
+public final class GroovyUtil {
private static final String MODULE = GroovyUtil.class.getName();
+ private static final UtilCache<String, Class<?>> PARSED_SCRIPTS = UtilCache.createUtilCache("script.GroovyLocationParsedCache", 0, 0, false);
+ private static final GroovyClassLoader GROOVY_CLASS_LOADER;
- private static final UtilCache<String, Class<?>> parsedScripts = UtilCache.createUtilCache("script.GroovyLocationParsedCache", 0, 0, false);
+ private GroovyUtil() { }
- private static final GroovyClassLoader groovyScriptClassLoader;
static {
GroovyClassLoader groovyClassLoader = null;
String scriptBaseClass = UtilProperties.getPropertyValue("groovy", "scriptBaseClass");
@@ -56,7 +57,7 @@ public class GroovyUtil {
conf.setScriptBaseClass(scriptBaseClass);
groovyClassLoader = new GroovyClassLoader(GroovyUtil.class.getClassLoader(), conf);
}
- groovyScriptClassLoader = groovyClassLoader;
+ GROOVY_CLASS_LOADER = groovyClassLoader;
}
/**
@@ -139,14 +140,14 @@ public class GroovyUtil {
public static Class<?> getScriptClassFromLocation(String location) throws GeneralException {
try {
- Class<?> scriptClass = parsedScripts.get(location);
+ Class<?> scriptClass = PARSED_SCRIPTS.get(location);
if (scriptClass == null) {
URL scriptUrl = FlexibleLocation.resolveLocation(location);
if (scriptUrl == null) {
throw new GeneralException("Script not found at location [" + location + "]");
}
scriptClass = parseClass(scriptUrl.openStream(), location);
- Class<?> scriptClassCached = parsedScripts.putIfAbsent(location, scriptClass);
+ Class<?> scriptClassCached = PARSED_SCRIPTS.putIfAbsent(location, scriptClass);
if (scriptClassCached == null) { // putIfAbsent returns null if the class is added to the cache
if (Debug.verboseOn()) {
Debug.logVerbose("Cached Groovy script at: " + location, MODULE);
@@ -177,8 +178,8 @@ public class GroovyUtil {
*/
private static Class<?> parseClass(InputStream in, String location) throws IOException {
String classText = UtilIO.readString(in);
- if (groovyScriptClassLoader != null) {
- return groovyScriptClassLoader.parseClass(classText, location);
+ if (GROOVY_CLASS_LOADER != null) {
+ return GROOVY_CLASS_LOADER.parseClass(classText, location);
} else {
GroovyClassLoader classLoader = new GroovyClassLoader();
Class<?> klass = classLoader.parseClass(classText, location);
@@ -214,6 +215,4 @@ public class GroovyUtil {
? script.run()
: script.invokeMethod(methodName, new Object[] { context });
}
-
- private GroovyUtil() {}
}
