Skip to content

Commit f2f6136

Browse files
[GR-60293] Throw a ServiceConfigurationError if the service is not JCA-compliant.
PullRequest: graal/19647
2 parents d14b835 + cc876d5 commit f2f6136

13 files changed

+84
-72
lines changed

substratevm/mx.substratevm/mx_substratevm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def native_image_func(args, **kwargs):
334334
yield native_image_func
335335

336336
native_image_context.hosted_assertions = ['-J-ea', '-J-esa']
337-
_native_unittest_features = '--features=com.oracle.svm.test.ImageInfoTest$TestFeature,com.oracle.svm.test.ServiceLoaderTest$TestFeature,com.oracle.svm.test.SecurityServiceTest$TestFeature,com.oracle.svm.test.ReflectionRegistrationTest$TestFeature'
337+
_native_unittest_features = '--features=com.oracle.svm.test.ImageInfoTest$TestFeature,com.oracle.svm.test.services.ServiceLoaderTest$TestFeature,com.oracle.svm.test.services.SecurityServiceTest$TestFeature,com.oracle.svm.test.ReflectionRegistrationTest$TestFeature'
338338

339339
IMAGE_ASSERTION_FLAGS = svm_experimental_options(['-H:+VerifyGraalGraphs', '-H:+VerifyPhases'])
340340

@@ -545,8 +545,8 @@ def native_unittests_task(extra_build_args=None):
545545
out.write(f"Simple file{i}" + '\n')
546546

547547
additional_build_args = svm_experimental_options([
548-
'-H:AdditionalSecurityProviders=com.oracle.svm.test.SecurityServiceTest$NoOpProvider',
549-
'-H:AdditionalSecurityServiceTypes=com.oracle.svm.test.SecurityServiceTest$JCACompliantNoOpService',
548+
'-H:AdditionalSecurityProviders=com.oracle.svm.test.services.SecurityServiceTest$NoOpProvider',
549+
'-H:AdditionalSecurityServiceTypes=com.oracle.svm.test.services.SecurityServiceTest$JCACompliantNoOpService',
550550
'-cp', cp_entry_name
551551
])
552552
if extra_build_args is not None:

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ServiceLoaderFeature.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
4242
import com.oracle.svm.core.feature.InternalFeature;
4343
import com.oracle.svm.core.jdk.ServiceCatalogSupport;
44-
import com.oracle.svm.core.option.HostedOptionKey;
4544
import com.oracle.svm.core.option.AccumulatingLocatableMultiOptionValue;
45+
import com.oracle.svm.core.option.HostedOptionKey;
4646
import com.oracle.svm.hosted.analysis.Inflation;
4747

4848
import jdk.graal.compiler.options.Option;
@@ -218,7 +218,21 @@ void handleServiceClassIsReachable(DuringAnalysisAccess access, Class<?> service
218218
if (nullaryConstructor != null || nullaryProviderMethod != null) {
219219
RuntimeReflection.register(providerClass);
220220
if (nullaryConstructor != null) {
221+
/*
222+
* Registering a constructor with
223+
* RuntimeReflection.registerConstructorLookup(providerClass) does not produce
224+
* the same behavior as using RuntimeReflection.register(nullaryConstructor). In
225+
* the first case, the constructor is marked for query purposes only, so this
226+
* if-statement cannot be eliminated.
227+
*
228+
*/
221229
RuntimeReflection.register(nullaryConstructor);
230+
} else {
231+
/*
232+
* If there's no nullary constructor, register it as negative lookup to avoid
233+
* throwing a MissingReflectionRegistrationError at run time.
234+
*/
235+
RuntimeReflection.registerConstructorLookup(providerClass);
222236
}
223237
if (nullaryProviderMethod != null) {
224238
RuntimeReflection.register(nullaryProviderMethod);
@@ -229,8 +243,14 @@ void handleServiceClassIsReachable(DuringAnalysisAccess access, Class<?> service
229243
*/
230244
RuntimeReflection.registerMethodLookup(providerClass, "provider");
231245
}
232-
registeredProviders.add(provider);
233246
}
247+
/*
248+
* Register the provider in both cases: when it is JCA-compliant (has a nullary
249+
* constructor or a provider method) or when it lacks both. If neither is present, a
250+
* ServiceConfigurationError will be thrown at runtime, consistent with HotSpot
251+
* behavior.
252+
*/
253+
registeredProviders.add(provider);
234254
}
235255
if (!registeredProviders.isEmpty()) {
236256
String serviceResourceLocation = "META-INF/services/" + serviceProvider.getName();

substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Args= \
22
--initialize-at-run-time=com.oracle.svm.test \
33
--initialize-at-build-time=com.oracle.svm.test.AbstractClassSerializationTest,com.oracle.svm.test.SerializationRegistrationTest,com.oracle.svm.test.SerializationRegistrationTest$SerializableTestClass,com.oracle.svm.test.SerializationRegistrationTest$AssociatedRegistrationTestClass,com.oracle.svm.test.LambdaClassSerializationTest,com.oracle.svm.test.LambdaClassDeserializationTest \
44
--features=com.oracle.svm.test.SerializationRegistrationTestFeature \
5-
--features=com.oracle.svm.test.AbstractServiceLoaderTest$TestFeature \
6-
--features=com.oracle.svm.test.NoProviderConstructorServiceLoaderTest$TestFeature \
5+
--features=com.oracle.svm.test.services.AbstractServiceLoaderTest$TestFeature \
6+
--features=com.oracle.svm.test.services.NoProviderConstructorServiceLoaderTest$TestFeature \
77
--features=com.oracle.svm.test.NativeImageResourceUtils$TestFeature \
88
--features=com.oracle.svm.test.jfr.JfrTestFeature \
99
--add-opens=java.base/java.lang=ALL-UNNAMED \

substratevm/src/com.oracle.svm.test/src/META-INF/services/com.oracle.svm.test.AbstractServiceLoaderTest$ServiceInterface

Lines changed: 0 additions & 2 deletions
This file was deleted.

substratevm/src/com.oracle.svm.test/src/META-INF/services/com.oracle.svm.test.NoProviderConstructorServiceLoaderTest$ServiceInterface

Lines changed: 0 additions & 2 deletions
This file was deleted.

substratevm/src/com.oracle.svm.test/src/META-INF/services/com.oracle.svm.test.ServiceLoaderTest$ServiceInterface

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
com.oracle.svm.test.services.AbstractServiceLoaderTest$ConcreteService
2+
com.oracle.svm.test.services.AbstractServiceLoaderTest$AbstractService
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
com.oracle.svm.test.services.NoProviderConstructorServiceLoaderTest$ProperService
2+
com.oracle.svm.test.services.NoProviderConstructorServiceLoaderTest$NoProviderConstructorService
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
com.oracle.svm.test.services.ServiceLoaderTest$ServiceA
2+
com.oracle.svm.test.services.ServiceLoaderTest$ServiceB
3+
com.oracle.svm.test.services.ServiceLoaderTest$ServiceHostedOnly

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/AbstractServiceLoaderTest.java renamed to substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/services/AbstractServiceLoaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25-
package com.oracle.svm.test;
25+
package com.oracle.svm.test.services;
2626

2727
import java.util.HashSet;
2828
import java.util.ServiceConfigurationError;

0 commit comments

Comments
 (0)