GroovyClassloader behaviour
-
22-04-2021 - |
Pergunta
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:
- 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 ?
- 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?
- When recompile is triggered, how does groovy know what the source is changed?
Nenhuma solução correta
Outras dicas
- 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
- 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 aString
or something, then it will have to recompile the class from theis String again - https://github.com/groovy/groovy-core/blob/master/src/main/groovy/lang/GroovyClassLoader.java#L845
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow