Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parseClass loadClass groovy

Tags:

groovy

When using the GroovyClassLoader, when should I use loadClass and when should I call parseClass?

If I understand it, the first call to loadClass() will compile the script, and subsequent calls will use the cached class file rather than recompiling it.

But what about parseClass then?

like image 202
anish Avatar asked Dec 05 '25 14:12

anish


1 Answers

The documentation for parseClass shows that it takes a File or a String containing Groovy code, and converts it into a class.

If the function is passed a File, then the GroovyClassLoader will cache this generated class, but if it is passed a String, it will not cache it.

The function loadClass (from the documentation) says:

loads a class from a file or a parent classloader.

What it basically does, is looks for the class already existing in the classLoader, and if it fails to find it, look for the script file on disk with the matching name.

Once this class is loaded, it will be cached. The next time you call loadClass, it will use this cached class unless you pass false for the preferClassOverScript. If you pass false, it will attempt locate the script on disk again, recompile the class if necessary.

like image 153
tim_yates Avatar answered Dec 07 '25 18:12

tim_yates