Reputation: 15
I customised Groovy class loader for loading the classes from a directory in file system.
Now I have groovy classes below for example,
ICommentService.groovy
public interface ICommentService {
public void a();
}
CommentService.groovy
public class CommentService implements ICommentService {
public void a() {
//doing something...
}
}
IMomentService.groovy
public interface IMomentService {
public void b();
}
MomentService.groovy
import ICommentService;
import CommentService;
public class MomentService implements IMomentService {
public void b() {
ICommentService commentService = new CommentService();
commentService.a();
//doing something
}
}
My customised classloader code is below,
public class MyGroovyClassLoader extends GroovyClassLoader {
private long version;
private HashMap<String, ClassHolder> classCache;
private HashSet<String> pendingGroovyClasses;
public MyGroovyClassLoader(ClassLoader parentClassLoader,
CompilerConfiguration cc) {
super(parentClassLoader, cc);
classCache = new HashMap<>();
}
public Class<?> parseGroovyClass(String key, File classFile)
throws CoreException {
ClassHolder holder = classCache.get(key);
if(holder != null && holder.getParsedClass() != null) {
LoggerEx.info(TAG, "Load groovy class " + key
+ " from cache");
return holder.getParsedClass();
}
try {
Class<?> parsedClass = parseClass(classFile);
if (parsedClass != null) {
holder = new ClassHolder();
holder.parsedClass = parsedClass;
classCache.put(key, holder);
}
LoggerEx.info(TAG, "Parse groovy class " + key
+ " successfully");
return parsedClass;
} catch (Throwable e) {
e.printStackTrace();
throw new CoreException(
ChatErrorCodes.ERROR_GROOVY_PARSECLASS_FAILED,
"Parse class " + classFile + " failed, "
+ e.getMessage());
}
}
@Override
public Class loadClass(String name, boolean lookupScriptFiles,
boolean preferClassOverScript) throws ClassNotFoundException,
CompilationFailedException {
// TODO Auto-generated method stub
Class<?> loadedClass = null;
if(pendingGroovyClasses.contains(name)) {
String key = name.replace(".", "/") + ".groovy";
try {
loadedClass = parseGroovyClass(key, new File(path + key));
if(loadedClass != null)
return loadedClass;
} catch (CoreException e) {
e.printStackTrace();
LoggerEx.error(TAG, "parse groovy class failed while load class, " + e.getMessage());
}
}
try {
loadedClass = super.loadClass(name, lookupScriptFiles,
preferClassOverScript);
} catch (ClassNotFoundException e) {
}
return loadedClass;
}
public ClassHolder getClass(String classPath) {
return classCache.get(classPath);
}
public long getVersion() {
return version;
}
public String toString() {
return MyGroovyClassLoader.class.getSimpleName() + "#" + version;
}
}
Now when we parse those groovy files in a directory. Then we found a infinite loop logs as below,
Parse groovy class ICommentService.groovy successfully
Parse groovy class IMomentService.groovy successfully
Load groovy class ICommentService.groovy from cache
Load groovy class IMomentService.groovy from cache
Load groovy class ICommentService.groovy from cache
Load groovy class IMomentService.groovy from cache
Load groovy class ICommentService.groovy from cache
Load groovy class IMomentService.groovy from cache
Load groovy class ICommentService.groovy from cache
Load groovy class IMomentService.groovy from cache
Load groovy class ICommentService.groovy from cache
Load groovy class IMomentService.groovy from cache
Load groovy class ICommentService.groovy from cache
Load groovy class IMomentService.groovy from cache
The issue is two classes are imported each other, can understand why the loop happened. But java classloader seems can handle this well. Then how can we resolve this issue in Groovy?
Please help! Thanks a lot!
Upvotes: 0
Views: 171
Reputation: 6508
The flag lookupScriptFiles is false for internal lookups done by the compiler to avoid exactly this problem. Instead of overwriting loadClass
you should use a GroovyResourceLoader
via set/getResourceLoader or overwrite getResource(String)
on GroovyClassLoader (this method is called from the resource loader). That way the compiler will be able to lookup the sources itself and add it into the compilation queue automatically.
Upvotes: 1