Skip to content

Commit

Permalink
Refactor: Use io.quarkus.gizmo.BytecodeCreator#loadClassFromTCCL
Browse files Browse the repository at this point in the history
Use `loadClassFromTCCL` instead of manually getting the TCCL and loading
classes in `NativeImageFeatureStep`
  • Loading branch information
zakkak authored and igorregis committed Oct 16, 2022
1 parent fa41bf2 commit a36a5e1
Showing 1 changed file with 3 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public class NativeImageFeatureStep {
private static final MethodDescriptor LOOKUP_METHOD = ofMethod(
ReflectionUtil.class,
"lookupMethod", Method.class, Class.class, String.class, Class[].class);
private static final MethodDescriptor FOR_NAME = ofMethod(
Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class);
private static final MethodDescriptor INVOKE = ofMethod(
Method.class, "invoke", Object.class, Object.class, Object[].class);
static final String RUNTIME_REFLECTION = RuntimeReflection.class.getName();
Expand Down Expand Up @@ -468,14 +466,7 @@ public void write(String s, byte[] bytes) {

TryBlock tc = mv.tryBlock();

ResultHandle currentThread = tc
.invokeStaticMethod(ofMethod(Thread.class, "currentThread", Thread.class));
ResultHandle tccl = tc.invokeVirtualMethod(
ofMethod(Thread.class, "getContextClassLoader", ClassLoader.class),
currentThread);
ResultHandle clazz = tc.invokeStaticMethod(
ofMethod(Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class),
tc.load(entry.getKey()), tc.load(false), tccl);
ResultHandle clazz = tc.loadClassFromTCCL(entry.getKey());
//we call these methods first, so if they are going to throw an exception it happens before anything has been registered
ResultHandle constructors = tc
.invokeVirtualMethod(ofMethod(Class.class, "getDeclaredConstructors", Constructor[].class), clazz);
Expand Down Expand Up @@ -575,14 +566,7 @@ public void write(String s, byte[] bytes) {

TryBlock tc = mv.tryBlock();

ResultHandle currentThread = tc
.invokeStaticMethod(ofMethod(Thread.class, "currentThread", Thread.class));
ResultHandle tccl = tc.invokeVirtualMethod(
ofMethod(Thread.class, "getContextClassLoader", ClassLoader.class),
currentThread);
ResultHandle clazz = tc.invokeStaticMethod(
ofMethod(Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class),
tc.load(className), tc.load(false), tccl);
ResultHandle clazz = tc.loadClassFromTCCL(className);
//we call these methods first, so if they are going to throw an exception it happens before anything has been registered
ResultHandle constructors = tc
.invokeVirtualMethod(ofMethod(Class.class, "getDeclaredConstructors", Constructor[].class), clazz);
Expand Down Expand Up @@ -666,15 +650,7 @@ private MethodDescriptor createRegisterSerializationForClassMethod(ClassCreator

TryBlock tc = addSerializationForClass.tryBlock();

ResultHandle currentThread = tc
.invokeStaticMethod(ofMethod(Thread.class, "currentThread", Thread.class));
ResultHandle tccl = tc.invokeVirtualMethod(
ofMethod(Thread.class, "getContextClassLoader", ClassLoader.class),
currentThread);

ResultHandle runtimeSerializationClass = tc.invokeStaticMethod(FOR_NAME,
tc.load("org.graalvm.nativeimage.hosted.RuntimeSerialization"),
tc.load(false), tccl);
ResultHandle runtimeSerializationClass = tc.loadClassFromTCCL("org.graalvm.nativeimage.hosted.RuntimeSerialization");
ResultHandle registerArgTypes = tc.newArray(Class.class, tc.load(1));
tc.writeArrayValue(registerArgTypes, 0, tc.loadClassFromTCCL(Class[].class));
ResultHandle registerLookupMethod = tc.invokeStaticMethod(LOOKUP_METHOD, runtimeSerializationClass,
Expand Down

0 comments on commit a36a5e1

Please sign in to comment.