Skip to content

Commit b1ed170

Browse files
committed
Add handlings for classes which are available in Thread.currentThread().getContextClassLoader()
1 parent ecfe3f4 commit b1ed170

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,30 @@ private void resolveCreators()
378378
List<AnnotatedMethod> creatorMethods = null;
379379

380380
// Then static methods which are potential factory methods
381-
for (Method m : _class.getDeclaredMethods()) {
381+
382+
Method[] classMethods;
383+
try{
384+
classMethods = _class.getDeclaredMethods();
385+
}catch(final NoClassDefFoundError ex){
386+
// One of the methods had a class that was not found in the cls.getClassLoader.
387+
// Maybe the developer was nice and has a different class loader for this context.
388+
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
389+
if(loader == null){
390+
// Nope... this is going to end poorly
391+
throw ex;
392+
}
393+
final Class<?> contextClass;
394+
try {
395+
contextClass = loader.loadClass(_class.getName());
396+
}
397+
catch (ClassNotFoundException e) {
398+
ex.addSuppressed(e);
399+
throw ex;
400+
}
401+
classMethods = contextClass.getDeclaredMethods(); // Cross fingers
402+
}
403+
404+
for (Method m : classMethods) {
382405
if (!Modifier.isStatic(m.getModifiers())) {
383406
continue;
384407
}
@@ -596,9 +619,29 @@ protected void _addMemberMethods(Class<?> cls, AnnotatedMethodMap methods,
596619
if (cls == null) { // just so caller need not check when passing super-class
597620
return;
598621
}
599-
622+
Method[] classMethods;
623+
try{
624+
classMethods = cls.getDeclaredMethods();
625+
}catch(final NoClassDefFoundError ex){
626+
// One of the methods had a class that was not found in the cls.getClassLoader.
627+
// Maybe the developer was nice and has a different class loader for this context.
628+
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
629+
if (loader == null) {
630+
// Nope... this is going to end poorly
631+
throw ex;
632+
}
633+
final Class<?> contextClass;
634+
try {
635+
contextClass = loader.loadClass(cls.getName());
636+
}
637+
catch (ClassNotFoundException e) {
638+
ex.addSuppressed(e);
639+
throw ex;
640+
}
641+
classMethods = contextClass.getDeclaredMethods(); // Cross fingers
642+
}
600643
// then methods from the class itself
601-
for (Method m : cls.getDeclaredMethods()) {
644+
for (Method m : classMethods) {
602645
if (!_isIncludableMemberMethod(m)) {
603646
continue;
604647
}

0 commit comments

Comments
 (0)