Skip to content

jdkProxyObj.getClass().getMethod("xxx") will throw java.lang.NoSuchMethodException: jdk.proxy4.$Proxy61.xxx() in the native-image. #6079

@wangliang181230

Description

@wangliang181230

Jdk proxy object can't get method by the Method Class.getMethod(methodName) in the native-image.

jdkProxyObj.getClass().getMethod("xxx") will throw java.lang.NoSuchMethodException: jdk.proxy4.$Proxy61.xxx() in the native-image.

Example project

https://github.com/wangliang181230/example__oracle_graal_issue-6079

Some code

TestInterface.java

public interface TestInterface {

    void foo();

    void bar();

}

ProxyReflectionExample.java

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyReflectionExample {

    public static void main(String[] args) throws Exception {
        TestInterface proxyObj = (TestInterface)Proxy.newProxyInstance(
            ProxyReflectionExample.class.getClassLoader(),
            new Class[] { TestInterface.class },
            (proxy, method, args1) -> {
                System.out.println("Method: " + method.getName() + ",     Declaring Class: " + method.getDeclaringClass().getName());
                return null;
            });

        try {
            // It will throw `NoSuchMethodException` in `native-image`.
            Method method = proxyObj.getClass().getMethod("foo");
            System.out.println("Method: " + method.getName() + ",     Declaring Class: " + method.getDeclaringClass().getName());
            method.invoke(proxyObj);

            method = proxyObj.getClass().getMethod("bar");
            System.out.println("Method: " + method.getName() + ",     Declaring Class: " + method.getDeclaringClass().getName());
            method.invoke(proxyObj);
        } catch (Throwable t) {
            t.printStackTrace();
        }

        Thread.sleep(10000);
    }
}

reflect-config.json

[
    {
        "name": "TestInterface",
        "methods": [
            { "name": "foo", "parameterTypes": [] },
            { "name": "bar", "parameterTypes": [] }
        ]
    }
]

Console output in JVM

Method: foo,     Declaring Class: jdk.proxy1.$Proxy0
Method: foo,     Declaring Class: TestInterface
Method: bar,     Declaring Class: jdk.proxy1.$Proxy0
Method: bar,     Declaring Class: TestInterface

Error log in native-image

java.lang.NoSuchMethodException: jdk.proxy4.$Proxy60.foo()
        at java.base@17.0.6/java.lang.Class.getMethod(DynamicHub.java:2227)
        at ProxyReflectionExample.main(ProxyReflectionExample.java:17)

Environment and versions:

  1. OS: Windows 10
  2. GraalVM: graalvm-ce-java17-22.3.1 Windows (amd64)

How can I solve this problem?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions