Skip to content

Commit

Permalink
Fixed occurrence of NoClassDefFoundError when Introspector.getBeanInf…
Browse files Browse the repository at this point in the history
…o(cls) got called on a $Impl class; closes #234.
  • Loading branch information
rliesenfeld committed Nov 2, 2015
1 parent 726a960 commit 82cd64f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
40 changes: 23 additions & 17 deletions main/src/mockit/internal/classGeneration/ImplementationClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,31 @@ public final Class<T> generateClass()
@Nonnull
private Class<T> defineNewClass(@Nonnull ClassVisitor modifier)
{
ClassLoader parentLoader = sourceClass.getClassLoader();

if (parentLoader == null) {
parentLoader = ImplementationClass.class.getClassLoader();
}

ClassLoader sourceClassLoader = sourceClass.getClassLoader();
final ClassLoader parentLoader =
sourceClassLoader != null ? sourceClassLoader : ImplementationClass.class.getClassLoader();
final byte[] modifiedClassfile = modifier.toByteArray();

@SuppressWarnings("unchecked")
Class<T> generatedClass = (Class<T>) new ClassLoader(parentLoader) {
@Override
protected Class<?> findClass(String name)
{
return defineClass(name, modifiedClassfile, 0, modifiedClassfile.length);
}
}.findClass(generatedClassName);

generatedBytecode = modifiedClassfile;
return generatedClass;
try {
@SuppressWarnings("unchecked")
Class<T> generatedClass = (Class<T>) new ClassLoader(parentLoader) {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException
{
if (!name.equals(generatedClassName)) {
return parentLoader.loadClass(name);
}

return defineClass(name, modifiedClassfile, 0, modifiedClassfile.length);
}
}.findClass(generatedClassName);

generatedBytecode = modifiedClassfile;
return generatedClass;
}
catch (ClassNotFoundException e) {
throw new RuntimeException("Unable to define class: " + generatedClassName, e);
}
}

@Nullable
Expand Down
11 changes: 11 additions & 0 deletions main/test/mockit/ExpectationsUsingReflectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package mockit;

import java.beans.*;
import java.util.*;

import org.junit.*;
Expand Down Expand Up @@ -462,4 +463,14 @@ public void createNewInnerInstance()
}
};
}

@Test
public void getBeanInfoFromMockedInterface(@Mocked BusinessInterface mock) throws Exception
{
Class<? extends BusinessInterface> mockClass = mock.getClass();

BeanInfo info = Introspector.getBeanInfo(mockClass);

assertNotNull(info);
}
}

0 comments on commit 82cd64f

Please sign in to comment.