java: fixed a bug that javassist cannot find class files that were loaded by custom class loader

This commit is contained in:
Muga Nishizawa
2010-11-18 22:26:58 +09:00
parent fa0b576a45
commit 39ad071c4f
2 changed files with 16 additions and 3 deletions

View File

@@ -46,21 +46,29 @@ import org.msgpack.template.NullableTemplate;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
class DynamicCodeGen extends DynamicCodeGenBase implements Constants { public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
private static Logger LOG = LoggerFactory.getLogger(DynamicCodeGen.class); private static Logger LOG = LoggerFactory.getLogger(DynamicCodeGen.class);
private static DynamicCodeGen INSTANCE; private static DynamicCodeGen INSTANCE;
public static DynamicCodeGen getInstance() { public static DynamicCodeGen getInstance() {
return getInstance(null);
}
public static DynamicCodeGen getInstance(ClassLoader cl) {
if (INSTANCE == null) { if (INSTANCE == null) {
LOG.info("create an instance of the type: " LOG.info("create an instance of the type: "
+ DynamicCodeGen.class.getName()); + DynamicCodeGen.class.getName());
INSTANCE = new DynamicCodeGen(); INSTANCE = new DynamicCodeGen();
if (cl != null) {
INSTANCE.setClassLoader(cl);
}
} }
return INSTANCE; return INSTANCE;
} }
private ConcurrentHashMap<String, Template[]> tmplCache; private ConcurrentHashMap<String, Template[]> tmplCache;
DynamicCodeGen() { DynamicCodeGen() {

View File

@@ -38,6 +38,7 @@ import javassist.CtField;
import javassist.CtMethod; import javassist.CtMethod;
import javassist.CtNewConstructor; import javassist.CtNewConstructor;
import javassist.CtNewMethod; import javassist.CtNewMethod;
import javassist.LoaderClassPath;
import javassist.NotFoundException; import javassist.NotFoundException;
import org.msgpack.CustomConverter; import org.msgpack.CustomConverter;
@@ -151,6 +152,10 @@ public class DynamicCodeGenBase implements Constants {
pool = ClassPool.getDefault(); pool = ClassPool.getDefault();
} }
protected void setClassLoader(ClassLoader cl) {
pool.appendClassPath(new LoaderClassPath(cl));
}
protected void checkTypeValidation(Class<?> type) { protected void checkTypeValidation(Class<?> type) {
DynamicCodeGenException e = new DynamicCodeGenException(String.format( DynamicCodeGenException e = new DynamicCodeGenException(String.format(
"Fatal error: %s", new Object[] { type.getName() })); "Fatal error: %s", new Object[] { type.getName() }));