Skip to content

Commit ab2fcbe

Browse files
committed
Fixes for serialization and reflection features
1 parent 476bfd0 commit ab2fcbe

File tree

3 files changed

+72
-50
lines changed

3 files changed

+72
-50
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/configure/ReflectionConfigurationParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ private void parseClass(Map<String, Object> data) {
8686
TypeResult<ConfigurationCondition> conditionResult = delegate.resolveCondition(parseCondition(data).getTypeName());
8787
if (!conditionResult.isPresent()) {
8888
handleError("Could not resolve condition " + parseCondition(data).getTypeName() + " for reflection.", conditionResult.getException());
89+
return;
8990
}
9091
ConfigurationCondition condition = conditionResult.get();
9192

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.hosted;
26+
27+
import com.oracle.svm.core.TypeResult;
28+
import com.oracle.svm.core.util.json.JSONParserException;
29+
30+
import jdk.vm.ci.meta.MetaUtil;
31+
32+
public final class ConfigurationTypeResolver {
33+
private final String configurationType;
34+
private final ImageClassLoader classLoader;
35+
private final boolean allowIncompleteClasspath;
36+
37+
public ConfigurationTypeResolver(String configurationType, ImageClassLoader classLoader, boolean allowIncompleteClasspath) {
38+
this.configurationType = configurationType;
39+
this.classLoader = classLoader;
40+
this.allowIncompleteClasspath = allowIncompleteClasspath;
41+
}
42+
43+
public Class<?> resolveType(String typeName) {
44+
String name = typeName;
45+
if (name.indexOf('[') != -1) {
46+
/* accept "int[][]", "java.lang.String[]" */
47+
name = MetaUtil.internalNameToJava(MetaUtil.toInternalName(name), true, true);
48+
}
49+
TypeResult<Class<?>> typeResult = classLoader.findClass(name);
50+
if (!typeResult.isPresent()) {
51+
handleError("Could not resolve " + name + " for " + configurationType + ".");
52+
}
53+
return typeResult.get();
54+
}
55+
56+
private void handleError(String message) {
57+
if (allowIncompleteClasspath) {
58+
System.err.println("Warning: " + message);
59+
} else {
60+
throw new JSONParserException(message + " To allow unresolvable " + configurationType + ", use option --allow-incomplete-classpath");
61+
}
62+
}
63+
}

substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/serialize/hosted/SerializationFeature.java

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.graalvm.nativeimage.impl.ConfigurationCondition;
4747
import org.graalvm.nativeimage.impl.RuntimeSerializationSupport;
4848

49-
import com.oracle.svm.core.TypeResult;
5049
import com.oracle.svm.core.annotate.AutomaticFeature;
5150
import com.oracle.svm.core.configure.ConfigurationFile;
5251
import com.oracle.svm.core.configure.ConfigurationFiles;
@@ -55,20 +54,18 @@
5554
import com.oracle.svm.core.jdk.RecordSupport;
5655
import com.oracle.svm.core.util.UserError;
5756
import com.oracle.svm.core.util.VMError;
58-
import com.oracle.svm.core.util.json.JSONParserException;
57+
import com.oracle.svm.hosted.ConditionalConfigurationRegistry;
58+
import com.oracle.svm.hosted.ConfigurationTypeResolver;
5959
import com.oracle.svm.hosted.FallbackFeature;
6060
import com.oracle.svm.hosted.FeatureImpl;
6161
import com.oracle.svm.hosted.ImageClassLoader;
6262
import com.oracle.svm.hosted.NativeImageOptions;
63-
import com.oracle.svm.hosted.ConditionalConfigurationRegistry;
6463
import com.oracle.svm.hosted.config.ConfigurationParserUtils;
6564
import com.oracle.svm.reflect.hosted.ReflectionFeature;
6665
import com.oracle.svm.reflect.serialize.SerializationRegistry;
6766
import com.oracle.svm.reflect.serialize.SerializationSupport;
6867
import com.oracle.svm.util.ReflectionUtil;
6968

70-
import jdk.vm.ci.meta.MetaUtil;
71-
7269
@AutomaticFeature
7370
public class SerializationFeature implements Feature {
7471
private SerializationBuilder serializationBuilder;
@@ -83,7 +80,7 @@ public List<Class<? extends Feature>> getRequiredFeatures() {
8380
public void duringSetup(DuringSetupAccess a) {
8481
FeatureImpl.DuringSetupAccessImpl access = (FeatureImpl.DuringSetupAccessImpl) a;
8582
ImageClassLoader imageClassLoader = access.getImageClassLoader();
86-
SerializationTypeResolver typeResolver = new SerializationTypeResolver(imageClassLoader, NativeImageOptions.AllowIncompleteClasspath.getValue());
83+
ConfigurationTypeResolver typeResolver = new ConfigurationTypeResolver("serialization configuration", imageClassLoader, NativeImageOptions.AllowIncompleteClasspath.getValue());
8784
SerializationDenyRegistry serializationDenyRegistry = new SerializationDenyRegistry(typeResolver);
8885
serializationBuilder = new SerializationBuilder(serializationDenyRegistry, access, typeResolver);
8986
ImageSingletons.add(RuntimeSerializationSupport.class, serializationBuilder);
@@ -131,44 +128,12 @@ static void println(String str) {
131128
}
132129
}
133130

134-
final class SerializationTypeResolver {
135-
136-
private final ImageClassLoader classLoader;
137-
private final boolean allowIncompleteClasspath;
138-
139-
SerializationTypeResolver(ImageClassLoader classLoader, boolean allowIncompleteClasspath) {
140-
this.classLoader = classLoader;
141-
this.allowIncompleteClasspath = allowIncompleteClasspath;
142-
}
143-
144-
public Class<?> resolveType(String typeName) {
145-
String name = typeName;
146-
if (name.indexOf('[') != -1) {
147-
/* accept "int[][]", "java.lang.String[]" */
148-
name = MetaUtil.internalNameToJava(MetaUtil.toInternalName(name), true, true);
149-
}
150-
TypeResult<Class<?>> typeResult = classLoader.findClass(name);
151-
if (!typeResult.isPresent()) {
152-
handleError("Could not resolve " + name + " for serialization configuration.");
153-
}
154-
return typeResult.get();
155-
}
156-
157-
private void handleError(String message) {
158-
if (allowIncompleteClasspath) {
159-
println("Warning: " + message);
160-
} else {
161-
throw new JSONParserException(message + " To allow unresolvable reflection configuration, use option -H:+AllowIncompleteClasspath");
162-
}
163-
}
164-
}
165-
166131
final class SerializationDenyRegistry implements RuntimeSerializationSupport {
167132

168133
private final Map<Class<?>, Boolean> deniedClasses = new HashMap<>();
169-
private final SerializationTypeResolver typeResolver;
134+
private final ConfigurationTypeResolver typeResolver;
170135

171-
SerializationDenyRegistry(SerializationTypeResolver typeResolver) {
136+
SerializationDenyRegistry(ConfigurationTypeResolver typeResolver) {
172137
this.typeResolver = typeResolver;
173138
}
174139

@@ -212,11 +177,11 @@ final class SerializationBuilder extends ConditionalConfigurationRegistry implem
212177

213178
private final SerializationSupport serializationSupport;
214179
private final SerializationDenyRegistry denyRegistry;
215-
private final SerializationTypeResolver typeResolver;
180+
private final ConfigurationTypeResolver typeResolver;
216181

217182
private boolean sealed;
218183

219-
SerializationBuilder(SerializationDenyRegistry serializationDenyRegistry, FeatureImpl.DuringSetupAccessImpl access, SerializationTypeResolver typeResolver) {
184+
SerializationBuilder(SerializationDenyRegistry serializationDenyRegistry, FeatureImpl.DuringSetupAccessImpl access, ConfigurationTypeResolver typeResolver) {
220185
try {
221186
Class<?> reflectionFactoryClass = access.findClassByName(Package_jdk_internal_reflect.getQualifiedName() + ".ReflectionFactory");
222187
Method getReflectionFactoryMethod = ReflectionUtil.lookupMethod(reflectionFactoryClass, "getReflectionFactory");
@@ -252,15 +217,8 @@ public void registerWithTargetConstructorClass(ConfigurationCondition condition,
252217
abortIfSealed();
253218

254219
Class<?> conditionClass = typeResolver.resolveType(condition.getTypeName());
255-
String msg = "Cannot find condition class %s.";
256220
if (conditionClass == null) {
257-
if (NativeImageOptions.AllowIncompleteClasspath.getValue()) {
258-
// Checkstyle: stop
259-
System.err.println("Warning: " + String.format(msg, condition.getTypeName()));
260-
// Checkstyle: resume
261-
} else {
262-
throw UserError.abort(msg, condition.getTypeName());
263-
}
221+
return;
264222
}
265223

266224
Class<?> serializationTargetClass = typeResolver.resolveType(targetClassName);

0 commit comments

Comments
 (0)