Question

GroovyClassloader behaviour understanding ,

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
GroovyScriptEngineImpl groovyEngineImpl = (GroovyScriptEngineImpl) engine;

in a loop,

for (int i = 0; i < 10; i++) {
            long startTime = System.currentTimeMillis();
            classLoader = new GroovyClassLoader(groovyEngineImpl.getClassLoader().getParent());
            fileName = fileName + i;
            Class groovyClass = classLoader.parseClass(s,fileName);
            long endTime = System.currentTimeMillis();
            System.out.println("Total elapsed time in execution o  " + (endTime-startTime));
            startTime = System.currentTimeMillis();
            groovyClass = classLoader.parseClass(s,fileName);
            endTime = System.currentTimeMillis();
            System.out.println("Second Time Total elapsed time in execution o  " + (endTime-startTime));


}

I have a couple of questions regarding the above code:

  1. In a for loop I'm creating a new groovyclassloder object, and parsing the groovy script twice. When the loop iterates for the second time, and tries to parse the groovyscript again, what will occur ?
  2. What will happen on the second time when another object is created. Will the classloader manage to get the class form the classpath or again recompile it again?
  3. When recompile is triggered, how does groovy know what the source is changed?

No correct solution

OTHER TIPS

  1. Each time round the loop you are throwing away the classloader and creating a new one. This new classLoader will have no knowledge of classes loaded by the classLoader you have thrown away
  2. It depends on the type of s. If it is a file, it will check if it needs a recompile, and if not it will use the same class. If it is a String or something, then it will have to recompile the class from theis String again
  3. https://github.com/groovy/groovy-core/blob/master/src/main/groovy/lang/GroovyClassLoader.java#L845
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top