Skip to content
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

Use of java.lang.reflect.Proxy generates error at runtime in native image #416

Closed
dsyer opened this issue May 11, 2018 · 2 comments
Closed

Comments

@dsyer
Copy link

dsyer commented May 11, 2018

Here's a HelloWorld app that runs fine from java:

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class HelloWorld {
	public static void main(String[] args) {
		System.err.println("Hello World");
		ClassLoader classLoader = HelloWorld.class.getClassLoader();
		Class<?>[] interfaces = new Class<?>[] { Serializable.class };
		InvocationHandler handler = new TypeProxyInvocationHandler();
		Proxy.newProxyInstance(classLoader, interfaces, handler);
	}
}

class TypeProxyInvocationHandler implements InvocationHandler {
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		return method.invoke(proxy, args);
	}
}

So:

$ javac HelloWorld.java 
$ java HelloWorld 
Hello World

But it fails when compiled to a native image:

$ native-image --no-server -H:Name=hello HelloWorld -H:+ReportUnsupportedElementsAtRuntime
$ ./hello 
Hello World
Exception in thread "main" java.lang.reflect.InvocationTargetException
	at java.lang.Throwable.<init>(Throwable.java:310)
	at java.lang.Exception.<init>(Exception.java:102)
	at java.lang.ReflectiveOperationException.<init>(ReflectiveOperationException.java:89)
	at java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:72)
	at com.oracle.svm.reflect.proxies.Proxy_1_HelloWorld_main.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.oracle.svm.core.JavaMainWrapper.run(JavaMainWrapper.java:199)
	at Lcom/oracle/svm/core/code/CEntryPointCallStubs;.com_002eoracle_002esvm_002ecore_002eJavaMainWrapper_002erun_0028int_002corg_002egraalvm_002enativeimage_002ec_002etype_002eCCharPointerPointer_0029(generated:0)
Caused by: com.oracle.svm.core.jdk.UnsupportedFeatureError: Unsupported field java.lang.reflect.Proxy.proxyClassCache is reachable
	at java.lang.Throwable.<init>(Throwable.java:265)
	at java.lang.Error.<init>(Error.java:70)
	at com.oracle.svm.core.jdk.UnsupportedFeatureError.<init>(UnsupportedFeatureError.java:29)
	at com.oracle.svm.core.jdk.Target_com_oracle_svm_core_util_VMError.unsupportedFeature(VMErrorSubstitutions.java:103)
	at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:419)
	at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:719)
	at HelloWorld.main(HelloWorld.java:12)
	... 4 more

See also #348 (because Proxy is used heavily by Spring).

@cstancu
Copy link
Member

cstancu commented May 12, 2018

@dsyer thank you for your report. java.lang.reflect.Proxy is currently not supported on SubstrateVM. The main reason is that proxies generate bytecode and we cannot support that at run time. We plan to implement this, but, as with reflection, we would need to know ahead-of-time the interfaces that the proxy implements, so we can generate the bytecodes at build time. In your example we could automatically detect the interface, i.e., the Serializable.class, since it is a compile time constant. If the interfaces are loaded from configuration files then they will need to be explicitly configured and made available to the native image builder.

@cstancu cstancu self-assigned this May 12, 2018
flortsch pushed a commit to flortsch/graal that referenced this issue May 22, 2018
@cstancu
Copy link
Member

cstancu commented Jun 13, 2018

144516a adds support for dynamic proxy classes. The example above works out of the box, i.e., the interface list is detected automatically and the proxy is generated at image build time. Please read the documentation for further details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants