Skip to content

Commit 2eeabe0

Browse files
committed
Predicated reflection and JNI config for NI
Introducing predicates for reflection and JNI configuration. The predicate can be only a type name. For example, "predicate": { "whenTypeReachable": "org.graalvm.ReachableClass" }, will apply configuration only if "type org.graalvm.ReachableClass" is reachable. The default predicate for every reflection entry is "java.lang.Object" and as such it will not be printed. The Native Image agent does not emit predicates, but will fuse predicates accordingly. Instead all config of one type, the fusion will happen by the predicate and the type.
1 parent 36cfbfa commit 2eeabe0

File tree

24 files changed

+559
-154
lines changed

24 files changed

+559
-154
lines changed

docs/reference-manual/native-image/Reflection.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ Here, `reflectconfig` is a JSON file in the following format (use `--expert-opti
107107
{ "name" : "format", "parameterTypes" : ["java.lang.String", "java.lang.Object[]"] }
108108
]
109109
},
110-
{
111-
"name" : "java.lang.String$CaseInsensitiveComparator",
112-
"methods" : [
113-
{ "name" : "compare" }
114-
]
115-
}
110+
{
111+
"name" : "java.lang.String$CaseInsensitiveComparator",
112+
"methods" : [
113+
{ "name" : "compare" }
114+
]
115+
}
116116
]
117117
```
118+
118119
The native image builder generates reflection metadata for all classes, methods, and fields referenced in that file.
119120
The `allPublicConstructors`, `allDeclaredConstructors`, `allPublicMethods`, `allDeclaredMethods`, `allPublicFields`, `allDeclaredFields`, `allPublicClasses`, and `allDeclaredClasses` attributes can be used to automatically include an entire set of members of a class.
120121

@@ -125,6 +126,28 @@ Code may also write non-static final fields like `String.value` in this example,
125126
More than one configuration can be used by specifying multiple paths for `ReflectionConfigurationFiles` and separating them with `,`.
126127
Also, `-H:ReflectionConfigurationResources` can be specified to load one or several configuration files from the native image build's class path, such as from a JAR file.
127128

129+
### Predicated Configuration
130+
131+
With predicates, a class configuration entry is applied only if a provided `predicate` is satisfied. The only currently supported predicate is `whenTypeReachable`, which enables the configuration entry if the specified type is reachable through other code. For example, to support reflective access to `sun.misc.Unsafe.theUnsafe` only when `io.netty.util.internal.PlatformDependent0` is reachable, the configuration should look like:
132+
133+
```json
134+
{
135+
"predicate" : { "whenTypeReachable" : "io.netty.util.internal.PlatformDependent0" },
136+
"name" : "sun.misc.Unsafe",
137+
"fields" : [
138+
{ "name" : "theUnsafe" }
139+
]
140+
}
141+
```
142+
143+
Predicated configuration is the *preferred* way to specify reflection configuration: if code doing a reflective access is not reachable, it is unnecessary to include its corresponding reflection entry. The consistent usage of `predicate` results in *smaller binaries* and *better build times* as the image builder can selectively include reflectively accessed code.
144+
145+
If a `predicate` is omitted, the element is always included. When the same predicate is used for two distinct elements in two configuration entries, both elements will be included when the predicate is satisfied. When a configuration entry should be enabled if one of several types are reachable, it is necessary to add two configuration entries: one entry for each type predicate.
146+
147+
When used with [assisted configuration](BuildConfiguration.md#assisted-configuration-of-native-image-builds), predicated entries of existing configuration will not be fused with agent-collected entries as agent-collected entries are not predicated.
148+
149+
### Configuration with Features
150+
128151
Alternatively, a custom `Feature` implementation can register program elements before and during the analysis phase of the native image build using the `RuntimeReflection` class. For example:
129152
```java
130153
class RuntimeReflectionRegistrationFeature implements Feature {

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/RuntimeReflection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.graalvm.nativeimage.ImageSingletons;
4949
import org.graalvm.nativeimage.Platform;
5050
import org.graalvm.nativeimage.Platforms;
51+
import org.graalvm.nativeimage.impl.ConfigurationPredicate;
5152
import org.graalvm.nativeimage.impl.RuntimeReflectionSupport;
5253

5354
//Checkstyle: allow reflection
@@ -68,7 +69,7 @@ public final class RuntimeReflection {
6869
* @since 19.0
6970
*/
7071
public static void register(Class<?>... classes) {
71-
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(classes);
72+
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(ConfigurationPredicate.objectPredicate(), classes);
7273
}
7374

7475
/**
@@ -79,7 +80,7 @@ public static void register(Class<?>... classes) {
7980
* @since 19.0
8081
*/
8182
public static void register(Executable... methods) {
82-
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(methods);
83+
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(ConfigurationPredicate.objectPredicate(), methods);
8384
}
8485

8586
/**
@@ -90,7 +91,7 @@ public static void register(Executable... methods) {
9091
* @since 19.0
9192
*/
9293
public static void register(Field... fields) {
93-
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(false, fields);
94+
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(ConfigurationPredicate.objectPredicate(), false, fields);
9495
}
9596

9697
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.impl;
42+
43+
import java.util.Objects;
44+
45+
public final class ConfigurationPredicate {
46+
private final String typeName;
47+
private static final ConfigurationPredicate OBJECT_PREDICATE = new ConfigurationPredicate(Object.class.getTypeName());
48+
49+
public static ConfigurationPredicate objectPredicate() {
50+
return OBJECT_PREDICATE;
51+
}
52+
53+
public static ConfigurationPredicate create(String typeReachability) {
54+
if (OBJECT_PREDICATE.typeName.equals(typeReachability)) {
55+
return OBJECT_PREDICATE;
56+
}
57+
return new ConfigurationPredicate(typeReachability);
58+
}
59+
60+
private ConfigurationPredicate(String typeName) {
61+
this.typeName = typeName;
62+
}
63+
64+
public String getTypeName() {
65+
return typeName;
66+
}
67+
68+
@Override
69+
public boolean equals(Object o) {
70+
if (this == o) {
71+
return true;
72+
}
73+
if (o == null || getClass() != o.getClass()) {
74+
return false;
75+
}
76+
ConfigurationPredicate predicate = (ConfigurationPredicate) o;
77+
return Objects.equals(typeName, predicate.typeName);
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hash(typeName);
83+
}
84+
85+
}

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/ReflectionRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
import java.lang.reflect.Field;
4545

4646
public interface ReflectionRegistry {
47-
void register(Class<?>... classes);
47+
void register(ConfigurationPredicate predicate, Class<?>... classes);
4848

49-
void register(Executable... methods);
49+
void register(ConfigurationPredicate predicate, Executable... methods);
5050

51-
void register(boolean finalIsWritable, Field... fields);
51+
void register(ConfigurationPredicate predicate, boolean finalIsWritable, Field... fields);
5252

5353
}

substratevm/src/com.oracle.svm.configure.test/src/com/oracle/svm/configure/test/config/OmitPreviousConfigTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.function.Function;
3636
import java.util.function.Predicate;
3737

38+
import org.graalvm.nativeimage.impl.ConfigurationPredicate;
3839
import org.junit.Assert;
3940
import org.junit.Test;
4041

@@ -145,8 +146,8 @@ private static void doTestTypeConfig(TypeConfiguration typeConfig) {
145146
}
146147

147148
private static void doTestExpectedMissingTypes(TypeConfiguration typeConfig) {
148-
Assert.assertNull(typeConfig.get("FlagTestA"));
149-
Assert.assertNull(typeConfig.get("FlagTestB"));
149+
Assert.assertNull(typeConfig.get(ConfigurationPredicate.objectPredicate(), "FlagTestA"));
150+
Assert.assertNull(typeConfig.get(ConfigurationPredicate.objectPredicate(), "FlagTestB"));
150151
}
151152

152153
private static void doTestTypeFlags(TypeConfiguration typeConfig) {
@@ -199,7 +200,7 @@ private static void doTestSerializationConfig(SerializationConfiguration seriali
199200
}
200201

201202
private static ConfigurationType getConfigTypeOrFail(TypeConfiguration typeConfig, String typeName) {
202-
ConfigurationType type = typeConfig.get(typeName);
203+
ConfigurationType type = typeConfig.get(ConfigurationPredicate.objectPredicate(), typeName);
203204
Assert.assertNotNull(type);
204205
return type;
205206
}
@@ -259,11 +260,11 @@ Map<ConfigurationMethod, ConfigurationMemberKind> getMethodsMap(ConfigurationMem
259260
}
260261

261262
void populateConfig() {
262-
ConfigurationType oldType = new ConfigurationType(getTypeName());
263+
ConfigurationType oldType = new ConfigurationType(ConfigurationPredicate.objectPredicate(), getTypeName());
263264
setFlags(oldType);
264265
previousConfig.add(oldType);
265266

266-
ConfigurationType newType = new ConfigurationType(getTypeName());
267+
ConfigurationType newType = new ConfigurationType(ConfigurationPredicate.objectPredicate(), getTypeName());
267268
for (Map.Entry<ConfigurationMethod, ConfigurationMemberKind> methodEntry : methodsThatMustExist.entrySet()) {
268269
newType.addMethod(methodEntry.getKey().getName(), methodEntry.getKey().getInternalSignature(), methodEntry.getValue());
269270
}
@@ -294,7 +295,7 @@ String getTypeName() {
294295

295296
void doTest() {
296297
String name = getTypeName();
297-
ConfigurationType configurationType = currentConfig.get(name);
298+
ConfigurationType configurationType = currentConfig.get(ConfigurationPredicate.objectPredicate(), name);
298299
if (methodsThatMustExist.size() == 0) {
299300
Assert.assertNull("Generated configuration type " + name + " exists. Expected it to be cleared as it is empty.", configurationType);
300301
} else {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.configure.config;
26+
27+
import java.io.IOException;
28+
29+
import org.graalvm.nativeimage.impl.ConfigurationPredicate;
30+
31+
import com.oracle.svm.configure.json.JsonWriter;
32+
33+
public final class ConfigurationPredicatePrintable {
34+
public static void printJson(ConfigurationPredicate predicate, JsonWriter writer) throws IOException {
35+
if (!predicate.equals(ConfigurationPredicate.objectPredicate())) {
36+
writer.append("\"predicate\":{");
37+
writer.append("\"whenTypeReachable\":").quote(predicate.getTypeName());
38+
writer.append("},").newline();
39+
}
40+
}
41+
}

substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/ConfigurationType.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
import java.util.function.BiPredicate;
3232
import java.util.function.Consumer;
3333

34+
import org.graalvm.nativeimage.impl.ConfigurationPredicate;
35+
3436
import com.oracle.svm.configure.json.JsonPrintable;
3537
import com.oracle.svm.configure.json.JsonPrinter;
3638
import com.oracle.svm.configure.json.JsonWriter;
3739

3840
public class ConfigurationType implements JsonPrintable {
41+
private final ConfigurationPredicate predicate;
3942
private final String qualifiedJavaName;
4043

4144
private Map<String, FieldInfo> fields;
@@ -50,18 +53,21 @@ public class ConfigurationType implements JsonPrintable {
5053
private boolean allDeclaredConstructors;
5154
private boolean allPublicConstructors;
5255

53-
public ConfigurationType(String qualifiedJavaName) {
56+
public ConfigurationType(ConfigurationPredicate predicate, String qualifiedJavaName) {
5457
assert qualifiedJavaName.indexOf('/') == -1 : "Requires qualified Java name, not internal representation";
5558
assert !qualifiedJavaName.startsWith("[") : "Requires Java source array syntax, for example java.lang.String[]";
59+
this.predicate = predicate;
5660
this.qualifiedJavaName = qualifiedJavaName;
5761
}
5862

5963
public ConfigurationType(ConfigurationType other) {
6064
qualifiedJavaName = other.qualifiedJavaName;
65+
predicate = other.predicate;
6166
mergeWith(other);
6267
}
6368

6469
public void mergeWith(ConfigurationType other) {
70+
assert predicate.equals(other.predicate);
6571
assert qualifiedJavaName.equals(other.qualifiedJavaName);
6672
mergeFlagsWith(other);
6773
mergeFieldsWith(other);
@@ -134,6 +140,8 @@ private void maybeRemoveMethods(boolean hasAllDeclaredMethods, boolean hasAllPub
134140
}
135141

136142
public void removeAll(ConfigurationType other) {
143+
assert predicate.equals(other.predicate);
144+
assert qualifiedJavaName.equals(other.qualifiedJavaName);
137145
removeFlags(other);
138146
removeFields(other);
139147
removeMethods(other);
@@ -317,6 +325,9 @@ public void setAllPublicConstructors() {
317325
@Override
318326
public void printJson(JsonWriter writer) throws IOException {
319327
writer.append('{').indent().newline();
328+
329+
ConfigurationPredicatePrintable.printJson(predicate, writer);
330+
320331
writer.quote("name").append(':').quote(qualifiedJavaName);
321332
optionallyPrintJsonBoolean(writer, haveAllDeclaredFields(), "allDeclaredFields");
322333
optionallyPrintJsonBoolean(writer, haveAllPublicFields(), "allPublicFields");
@@ -337,8 +348,8 @@ public void printJson(JsonWriter writer) throws IOException {
337348
Comparator.comparing(ConfigurationMethod::getName).thenComparing(Comparator.nullsFirst(Comparator.comparing(ConfigurationMethod::getInternalSignature))),
338349
JsonPrintable::printJson);
339350
}
340-
writer.unindent().newline();
341-
writer.append('}');
351+
352+
writer.append('}').unindent().newline();
342353
}
343354

344355
private static void printField(Map.Entry<String, FieldInfo> entry, JsonWriter w) throws IOException {
@@ -373,4 +384,8 @@ private static <T, S> Map<T, S> maybeRemove(Map<T, S> fromMap, Consumer<Map<T, S
373384
}
374385
return map;
375386
}
387+
388+
ConfigurationPredicate getPredicate() {
389+
return predicate;
390+
}
376391
}

substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/ParserConfigurationAdapter.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,28 @@
2626

2727
import java.util.List;
2828

29+
import org.graalvm.nativeimage.impl.ConfigurationPredicate;
30+
2931
import com.oracle.svm.core.TypeResult;
3032
import com.oracle.svm.core.configure.ReflectionConfigurationParserDelegate;
3133

3234
public class ParserConfigurationAdapter implements ReflectionConfigurationParserDelegate<ConfigurationType> {
3335

3436
private final TypeConfiguration configuration;
3537

36-
public ParserConfigurationAdapter(TypeConfiguration configuration) {
38+
ParserConfigurationAdapter(TypeConfiguration configuration) {
3739
this.configuration = configuration;
3840
}
3941

4042
@Override
41-
public TypeResult<ConfigurationType> resolveTypeResult(String typeName) {
42-
ConfigurationType type = configuration.get(typeName);
43-
ConfigurationType result = type != null ? type : new ConfigurationType(typeName);
43+
public TypeResult<ConfigurationPredicate> resolvePredicate(String typeName) {
44+
return TypeResult.forType(typeName, ConfigurationPredicate.create(typeName));
45+
}
46+
47+
@Override
48+
public TypeResult<ConfigurationType> resolveType(ConfigurationPredicate predicate, String typeName) {
49+
ConfigurationType type = configuration.get(predicate, typeName);
50+
ConfigurationType result = type != null ? type : new ConfigurationType(predicate, typeName);
4451
return TypeResult.forType(typeName, result);
4552
}
4653

0 commit comments

Comments
 (0)