Skip to content

Commit bd249e3

Browse files
author
Christian Wimmer
committed
Do not use a separate class for each reflectively invoked method
1 parent 39e2597 commit bd249e3

File tree

20 files changed

+885
-1023
lines changed

20 files changed

+885
-1023
lines changed

substratevm/mx.substratevm/suite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
"requiresConcealed" : {
205205
"java.base" : [
206206
"jdk.internal.module",
207+
"jdk.internal.reflect",
207208
"jdk.internal.misc",
208209
"jdk.internal.logger",
209210
"jdk.internal.loader",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.jdk11.reflect;
26+
27+
// Checkstyle: allow reflection
28+
29+
import java.lang.reflect.Executable;
30+
31+
import org.graalvm.nativeimage.ImageSingletons;
32+
import org.graalvm.nativeimage.c.function.CFunctionPointer;
33+
import org.graalvm.nativeimage.hosted.Feature;
34+
35+
import com.oracle.svm.core.annotate.AutomaticFeature;
36+
import com.oracle.svm.core.reflect.SubstrateConstructorAccessor;
37+
import com.oracle.svm.core.reflect.SubstrateMethodAccessor;
38+
import com.oracle.svm.core.reflect.SubstrateReflectionAccessorFactory;
39+
40+
final class SubstrateReflectionAccessorFactoryJDK11 implements SubstrateReflectionAccessorFactory {
41+
@Override
42+
public SubstrateMethodAccessor createMethodAccessor(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
43+
return new SubstrateMethodAccessorJDK11(member, invokeFunctionPointer, invokeSpecialFunctionPointer);
44+
}
45+
46+
@Override
47+
public SubstrateConstructorAccessor createConstructorAccessor(Executable member, CFunctionPointer newInstanceFunctionPointer) {
48+
return new SubstrateConstructorAccessorJDK11(member, newInstanceFunctionPointer);
49+
}
50+
}
51+
52+
@AutomaticFeature
53+
final class SubstrateReflectionAccessorFactoryJDK11Feature implements Feature {
54+
@Override
55+
public void afterRegistration(AfterRegistrationAccess access) {
56+
ImageSingletons.add(SubstrateReflectionAccessorFactory.class, new SubstrateReflectionAccessorFactoryJDK11());
57+
}
58+
}
59+
60+
final class SubstrateMethodAccessorJDK11 extends SubstrateMethodAccessor implements jdk.internal.reflect.MethodAccessor {
61+
SubstrateMethodAccessorJDK11(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
62+
super(member, invokeFunctionPointer, invokeSpecialFunctionPointer);
63+
}
64+
}
65+
66+
final class SubstrateConstructorAccessorJDK11 extends SubstrateConstructorAccessor implements jdk.internal.reflect.ConstructorAccessor {
67+
SubstrateConstructorAccessorJDK11(Executable member, CFunctionPointer newInstanceFunctionPointer) {
68+
super(member, newInstanceFunctionPointer);
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.jdk8.reflect;
26+
27+
// Checkstyle: allow reflection
28+
29+
import java.lang.reflect.Executable;
30+
31+
import org.graalvm.nativeimage.ImageSingletons;
32+
import org.graalvm.nativeimage.c.function.CFunctionPointer;
33+
import org.graalvm.nativeimage.hosted.Feature;
34+
35+
import com.oracle.svm.core.annotate.AutomaticFeature;
36+
import com.oracle.svm.core.reflect.SubstrateConstructorAccessor;
37+
import com.oracle.svm.core.reflect.SubstrateMethodAccessor;
38+
import com.oracle.svm.core.reflect.SubstrateReflectionAccessorFactory;
39+
40+
final class SubstrateReflectionAccessorFactoryJDK8 implements SubstrateReflectionAccessorFactory {
41+
@Override
42+
public SubstrateMethodAccessor createMethodAccessor(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
43+
return new SubstrateMethodAccessorJDK8(member, invokeFunctionPointer, invokeSpecialFunctionPointer);
44+
}
45+
46+
@Override
47+
public SubstrateConstructorAccessor createConstructorAccessor(Executable member, CFunctionPointer newInstanceFunctionPointer) {
48+
return new SubstrateConstructorAccessorJDK8(member, newInstanceFunctionPointer);
49+
}
50+
}
51+
52+
@AutomaticFeature
53+
final class SubstrateReflectionAccessorFactoryJDK8Feature implements Feature {
54+
@Override
55+
public void afterRegistration(AfterRegistrationAccess access) {
56+
ImageSingletons.add(SubstrateReflectionAccessorFactory.class, new SubstrateReflectionAccessorFactoryJDK8());
57+
}
58+
}
59+
60+
final class SubstrateMethodAccessorJDK8 extends SubstrateMethodAccessor implements sun.reflect.MethodAccessor {
61+
SubstrateMethodAccessorJDK8(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
62+
super(member, invokeFunctionPointer, invokeSpecialFunctionPointer);
63+
}
64+
}
65+
66+
final class SubstrateConstructorAccessorJDK8 extends SubstrateConstructorAccessor implements sun.reflect.ConstructorAccessor {
67+
SubstrateConstructorAccessorJDK8(Executable member, CFunctionPointer newInstanceFunctionPointer) {
68+
super(member, newInstanceFunctionPointer);
69+
}
70+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.reflect;
26+
27+
import com.oracle.svm.core.jdk.InternalVMMethod;
28+
import com.oracle.svm.core.util.VMError;
29+
30+
/**
31+
* This class is used as the declaring class for reflection invocation methods. These methods have
32+
* manually created Graal IR, and are invoked via a function pointer call from
33+
* {@link SubstrateMethodAccessor} and {@link SubstrateConstructorAccessor}
34+
*/
35+
@InternalVMMethod
36+
@SuppressWarnings("unused")
37+
public final class ReflectionAccessorHolder {
38+
39+
/**
40+
* Signature prototype for invoking a method via a {@link SubstrateMethodAccessor}.
41+
*/
42+
private static Object invokePrototype(Object obj, Object[] args) {
43+
throw VMError.shouldNotReachHere("Only used as a prototype for generated methods");
44+
}
45+
46+
/**
47+
* Signature prototype for allocating a new instance via a {@link SubstrateConstructorAccessor}.
48+
*/
49+
private static Object newInstancePrototype(Object[] args) {
50+
throw VMError.shouldNotReachHere("Only used as a prototype for generated methods");
51+
}
52+
53+
/*
54+
* Methods for throwing exceptions when a method or constructor is used in an illegal way.
55+
*/
56+
57+
private static Object invokeSpecialError(Object obj, Object[] args) {
58+
throw new IllegalArgumentException("Static or abstract method cannot be invoked using invokeSpecial");
59+
}
60+
61+
private static Object newInstanceError(Object[] args) throws InstantiationException {
62+
throw new InstantiationException("Only non-abstract instance classes can be instantiated using reflection");
63+
}
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.reflect;
26+
27+
// Checkstyle: allow reflection
28+
29+
import java.lang.reflect.Executable;
30+
31+
import org.graalvm.nativeimage.c.function.CFunctionPointer;
32+
33+
import com.oracle.svm.core.annotate.InvokeJavaFunctionPointer;
34+
import com.oracle.svm.core.jdk.InternalVMMethod;
35+
import com.oracle.svm.core.util.VMError;
36+
37+
@InternalVMMethod
38+
public abstract class SubstrateConstructorAccessor {
39+
40+
interface ConstructorNewInstanceFunctionPointer extends CFunctionPointer {
41+
@InvokeJavaFunctionPointer
42+
Object invoke(Object[] args);
43+
}
44+
45+
private final Executable member;
46+
private final CFunctionPointer newInstanceFunctionPointer;
47+
48+
protected SubstrateConstructorAccessor(Executable member, CFunctionPointer newInstanceFunctionPointer) {
49+
this.member = member;
50+
this.newInstanceFunctionPointer = newInstanceFunctionPointer;
51+
}
52+
53+
public Object newInstance(Object[] args) {
54+
ConstructorNewInstanceFunctionPointer functionPointer = (ConstructorNewInstanceFunctionPointer) this.newInstanceFunctionPointer;
55+
if (functionPointer.isNull()) {
56+
throw newInstanceError();
57+
}
58+
return functionPointer.invoke(args);
59+
}
60+
61+
private RuntimeException newInstanceError() {
62+
throw VMError.shouldNotReachHere("No SubstrateConstructorAccessor.newInstanceFunctionPointer for " + member);
63+
}
64+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.reflect;
26+
27+
// Checkstyle: allow reflection
28+
29+
import java.lang.reflect.Executable;
30+
31+
import org.graalvm.nativeimage.c.function.CFunctionPointer;
32+
33+
import com.oracle.svm.core.annotate.InvokeJavaFunctionPointer;
34+
import com.oracle.svm.core.jdk.InternalVMMethod;
35+
import com.oracle.svm.core.util.VMError;
36+
37+
@InternalVMMethod
38+
public abstract class SubstrateMethodAccessor {
39+
40+
interface MethodInvokeFunctionPointer extends CFunctionPointer {
41+
@InvokeJavaFunctionPointer
42+
Object invoke(Object obj, Object[] args);
43+
}
44+
45+
private final Executable member;
46+
private final CFunctionPointer invokeFunctionPointer;
47+
private final CFunctionPointer invokeSpecialFunctionPointer;
48+
49+
protected SubstrateMethodAccessor(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
50+
this.member = member;
51+
this.invokeFunctionPointer = invokeFunctionPointer;
52+
this.invokeSpecialFunctionPointer = invokeSpecialFunctionPointer;
53+
}
54+
55+
public Object invoke(Object obj, Object[] args) {
56+
MethodInvokeFunctionPointer functionPointer = (MethodInvokeFunctionPointer) this.invokeFunctionPointer;
57+
if (functionPointer.isNull()) {
58+
throw invokeError();
59+
}
60+
return functionPointer.invoke(obj, args);
61+
}
62+
63+
private RuntimeException invokeError() {
64+
throw VMError.shouldNotReachHere("No SubstrateMethodAccessor.invokeFunctionPointer for " + member);
65+
}
66+
67+
public Object invokeSpecial(Object obj, Object[] args) {
68+
MethodInvokeFunctionPointer functionPointer = (MethodInvokeFunctionPointer) this.invokeSpecialFunctionPointer;
69+
if (functionPointer.isNull()) {
70+
throw invokeSpecialError();
71+
}
72+
return functionPointer.invoke(obj, args);
73+
}
74+
75+
private RuntimeException invokeSpecialError() {
76+
throw VMError.shouldNotReachHere("No SubstrateMethodAccessor.invokeSpecialFunctionPointer for " + member);
77+
}
78+
}

substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/helpers/ReflectionProxy.java renamed to substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/SubstrateReflectionAccessorFactory.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,10 +22,16 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25-
package com.oracle.svm.reflect.helpers;
25+
package com.oracle.svm.core.reflect;
2626

27-
/**
28-
* Marker interface for generated accessor classes.
29-
*/
30-
public interface ReflectionProxy {
27+
// Checkstyle: allow reflection
28+
29+
import java.lang.reflect.Executable;
30+
31+
import org.graalvm.nativeimage.c.function.CFunctionPointer;
32+
33+
public interface SubstrateReflectionAccessorFactory {
34+
SubstrateMethodAccessor createMethodAccessor(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer);
35+
36+
SubstrateConstructorAccessor createConstructorAccessor(Executable member, CFunctionPointer newInstanceFunctionPointer);
3137
}

0 commit comments

Comments
 (0)