Skip to content

Commit

Permalink
Add tests loading non-public classes
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Oct 7, 2022
1 parent 814f55b commit b06455e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/test/java/io/quarkus/gizmo/LoadClassTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ public void testLoadClassFromTCCL() throws Exception {
Assert.assertEquals(String.class, myInterface.get());
}

@Test
public void testLoadNonPublicClass() throws Exception {
TestClassLoader cl = new TestClassLoader(getClass().getClassLoader());
try (ClassCreator creator = ClassCreator.builder().classOutput(cl).className("com.MyTest").interfaces(Supplier.class).build()) {
MethodCreator method = creator.getMethodCreator("get", Object.class);
ResultHandle stringHandle = method.loadClass("java.util.Collections$EmptyList");
method.returnValue(stringHandle);
}
Class<?> clazz = cl.loadClass("com.MyTest");
Supplier myInterface = (Supplier) clazz.getDeclaredConstructor().newInstance();
Assert.assertThrows(IllegalAccessError.class, myInterface::get);
}

@Test
public void testLoadNonPublicClassFromTCCL() throws Exception {
TestClassLoader cl = new TestClassLoader(getClass().getClassLoader());
try (ClassCreator creator = ClassCreator.builder().classOutput(cl).className("com.MyTest").interfaces(Supplier.class).build()) {
MethodCreator method = creator.getMethodCreator("get", Object.class);
ResultHandle stringHandle = method.loadClassFromTCCL("java.util.Collections$EmptyList");
method.returnValue(stringHandle);
}
Class<?> clazz = cl.loadClass("com.MyTest");
Supplier myInterface = (Supplier) clazz.getDeclaredConstructor().newInstance();
Assert.assertEquals(Class.forName("java.util.Collections$EmptyList"), myInterface.get());
}

@Test
public void testLoadVoidClass() throws Exception {
TestClassLoader cl = new TestClassLoader(getClass().getClassLoader());
Expand Down

0 comments on commit b06455e

Please sign in to comment.