Skip to content

[GR-42737] Native-image-agent generates non existent classes for Swing application. #5586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ private static boolean forName(JNIEnvironment jni, JNIObjectHandle thread, Break
JNIObjectHandle callerClass = state.getDirectCallerClass();
JNIObjectHandle name = getObjectArgument(thread, 0);
String className = fromJniString(jni, name);
if (className == null) {
return false; /* No point in tracing this. */
}

boolean classLoaderValid = true;
WordPointer classLoaderPtr = StackValue.get(WordPointer.class);
Expand All @@ -234,11 +237,13 @@ private static boolean forName(JNIEnvironment jni, JNIObjectHandle thread, Break
* recursion checks keep us from seeing events of interest during initialization.
*/
int initialize = 0;
JNIObjectHandle loadedClass = Support.callStaticObjectMethodLIL(jni, bp.clazz, agent.handles().javaLangClassForName3, name, initialize, classLoaderPtr.read());
if (clearException(jni)) {
loadedClass = nullHandle();
}
result = loadedClass.notEqual(nullHandle());
Support.callStaticObjectMethodLIL(jni, bp.clazz, agent.handles().javaLangClassForName3, name, initialize, classLoaderPtr.read());
JNIObjectHandle exception = handleException(jni, true);
/*
* To throw the right exceptions at run time, we need to ensure that the image builder
* sees them, so we trace all calls except those that throw a ClassNotFoundException.
*/
result = exception.equal(nullHandle()) || !jniFunctions().getIsInstanceOf().invoke(jni, exception, agent.handles().javaLangClassNotFoundException);
}

traceReflectBreakpoint(jni, bp.clazz, nullHandle(), callerClass, bp.specification.methodName, result, state.getFullStackTraceOrNull(), className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class NativeImageAgentJNIHandleSet extends JNIHandleSet {

final JNIObjectHandle javaLangClass;
final JNIMethodId javaLangClassForName3;
final JNIObjectHandle javaLangClassNotFoundException;
final JNIMethodId javaLangClassGetName;
final JNIMethodId javaLangClassGetInterfaces;

Expand Down Expand Up @@ -87,6 +88,7 @@ public class NativeImageAgentJNIHandleSet extends JNIHandleSet {
super(env);
javaLangClass = newClassGlobalRef(env, "java/lang/Class");
javaLangClassForName3 = getMethodId(env, javaLangClass, "forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", true);
javaLangClassNotFoundException = newClassGlobalRef(env, "java/lang/ClassNotFoundException");
javaLangClassGetName = getMethodId(env, javaLangClass, "getName", "()Ljava/lang/String;", false);
javaLangClassGetInterfaces = getMethodId(env, javaLangClass, "getInterfaces", "()[Ljava/lang/Class;", false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ReflectionProcessor extends AbstractProcessor {
public void processEntry(EconomicMap<String, ?> entry, ConfigurationSet configurationSet) {
boolean invalidResult = Boolean.FALSE.equals(entry.get("result"));
ConfigurationCondition condition = ConfigurationCondition.alwaysTrue();
if (invalidResult && !reportReflectiveFailure(entry)) {
if (invalidResult) {
return;
}

Expand Down Expand Up @@ -311,15 +311,4 @@ private void addDynamicProxyUnchecked(List<?> checkedInterfaceList, List<?> unch
interfaces.addAll(uncheckedInterfaces);
proxyConfiguration.add(ConfigurationCondition.alwaysTrue(), interfaces);
}

private static boolean reportReflectiveFailure(EconomicMap<String, ?> entry) {
String function = (String) entry.get("function");
switch (function) {
case "forName":
List<?> args = (List<?>) entry.get("args");
String name = singleElement(args);
return name != null;
}
return false;
}
}