Skip to content

Commit 364c473

Browse files
author
Tatsuhiko Inoue
committed
fixed the issue that serialization fails with non-default class loader.
1 parent 5b80f41 commit 364c473

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

src/main/java/org/msgpack/template/builder/BuildContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ protected void buildReadMethod() throws CannotCompileException, NotFoundExceptio
151151
protected abstract String buildReadMethodBody();
152152

153153
protected Class<?> createClass() throws CannotCompileException {
154-
return (Class<?>) tmplCtClass.toClass(null, getClass().getProtectionDomain());
154+
return (Class<?>) tmplCtClass.toClass(director.getClassLoader(), getClass().getProtectionDomain());
155155
}
156156

157157
protected void saveClass(final String directoryName) throws CannotCompileException, IOException {

src/main/java/org/msgpack/template/builder/JavassistTemplateBuilder.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,24 @@ public JavassistTemplate(Class<T> targetClass, Template<?>[] templates) {
5151

5252
protected int seqId = 0;
5353

54+
protected ClassLoader loader;
55+
5456
public JavassistTemplateBuilder(TemplateRegistry registry) {
57+
this(registry, null);
58+
}
59+
60+
public JavassistTemplateBuilder(TemplateRegistry registry, ClassLoader cl) {
5561
super(registry);
5662
pool = new ClassPool();
5763
boolean appended = false;
58-
ClassLoader cl = null;
59-
try {
60-
cl = Thread.currentThread().getContextClassLoader();
61-
if (cl != null) {
62-
pool.appendClassPath(new LoaderClassPath(cl));
63-
appended = true;
64-
}
65-
} catch (SecurityException e) {
66-
LOG.fine("Cannot append a search path of context classloader");
67-
e.printStackTrace();
64+
loader = cl;
65+
if (loader == null) {
66+
loader = pool.getClassLoader();
6867
}
68+
6969
try {
70-
ClassLoader cl2 = getClass().getClassLoader();
71-
if (cl2 != null && cl2 != cl) {
72-
pool.appendClassPath(new LoaderClassPath(cl2));
70+
if (loader != null) {
71+
pool.appendClassPath(new LoaderClassPath(loader));
7372
appended = true;
7473
}
7574
} catch (SecurityException e) {
@@ -91,10 +90,6 @@ public boolean matchType(Type targetType, boolean hasAnnotation) {
9190
return matched;
9291
}
9392

94-
public void addClassLoader(ClassLoader cl) {
95-
pool.appendClassPath(new LoaderClassPath(cl));
96-
}
97-
9893
protected CtClass makeCtClass(String className) {
9994
return pool.makeClass(className);
10095
}
@@ -171,4 +166,8 @@ public <T> Template<T> loadTemplate(Type targetType) {
171166
BuildContext bc = createBuildContext();
172167
return bc.loadTemplate(targetClass, entries, tmpls);
173168
}
169+
170+
protected ClassLoader getClassLoader() {
171+
return loader;
172+
}
174173
}

src/main/java/org/msgpack/template/builder/TemplateBuilderChain.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ protected void reset(final TemplateRegistry registry, final ClassLoader cl) {
5252
// Javassist{,Beans}TemplateBuilder should be created with reflection for android.
5353

5454
// forceBuilder
55-
forceBuilder = new JavassistTemplateBuilder(registry);
56-
if (cl != null) {
57-
((JavassistTemplateBuilder) forceBuilder).addClassLoader(cl);
58-
}
55+
forceBuilder = new JavassistTemplateBuilder(registry, cl);
5956

6057
// builder
6158
TemplateBuilder builder;

0 commit comments

Comments
 (0)