Skip to content

Commit c02fc6e

Browse files
committed
[GR-51476] Throw missing registration errors for array instantiation
PullRequest: graal/17049
2 parents ba7d3f3 + 83266c5 commit c02fc6e

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/SubstrateAllocationSnippets.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ private static void arrayHubErrorStub(DynamicHub elementType) {
347347
} else if (elementType == DynamicHub.fromClass(void.class)) {
348348
throw new IllegalArgumentException("Cannot allocate void array.");
349349
} else if (elementType.getArrayHub() == null || !elementType.getArrayHub().isInstantiated()) {
350-
throw new IllegalArgumentException("Class " + DynamicHub.toClass(elementType).getTypeName() + "[] is instantiated reflectively but was never registered." +
351-
"Register the class by adding \"unsafeAllocated\" for the class in " + ConfigurationFile.REFLECTION.getFileName() + ".");
350+
throw MissingReflectionRegistrationUtils.errorForArray(DynamicHub.toClass(elementType), 1);
352351
} else {
353352
VMError.shouldNotReachHereUnexpectedInput(elementType); // ExcludeFromJacocoGeneratedReport
354353
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangReflectSubstitutions.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.lang.reflect.Array;
3030
import java.util.Objects;
3131

32-
import jdk.graal.compiler.word.BarrieredAccess;
3332
import org.graalvm.word.UnsignedWord;
3433

3534
import com.oracle.svm.core.SubstrateUtil;
@@ -38,7 +37,9 @@
3837
import com.oracle.svm.core.config.ConfigurationValues;
3938
import com.oracle.svm.core.hub.DynamicHub;
4039
import com.oracle.svm.core.hub.LayoutEncoding;
41-
import com.oracle.svm.core.util.VMError;
40+
import com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils;
41+
42+
import jdk.graal.compiler.word.BarrieredAccess;
4243

4344
@TargetClass(java.lang.reflect.Array.class)
4445
final class Target_java_lang_reflect_Array {
@@ -406,7 +407,7 @@ private static Object multiNewArray(Class<?> componentType, int[] dimensions) {
406407
for (int i = 0; i < dimensions.length; i++) {
407408
arrayHub = arrayHub.getArrayHub();
408409
if (arrayHub == null) {
409-
throw VMError.unsupportedFeature("Cannot allocate " + dimensions.length + "-dimensional array of " + componentType.getName());
410+
throw MissingReflectionRegistrationUtils.errorForArray(componentType, dimensions.length);
410411
}
411412
}
412413

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/MissingReflectionRegistrationUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ public static MissingReflectionRegistrationError errorForProxy(Class<?>... inter
111111
return exception;
112112
}
113113

114+
public static MissingReflectionRegistrationError errorForArray(Class<?> elementClass, int dimension) {
115+
MissingReflectionRegistrationError exception = new MissingReflectionRegistrationError(errorMessage("instantiate the array class",
116+
elementClass.getTypeName() + "[]".repeat(dimension),
117+
"Add \"unsafeAllocated\" to the array class registration to enable runtime instantiation.", "reflection"),
118+
null, null, null, null);
119+
report(exception);
120+
/*
121+
* If report doesn't throw, we throw the exception anyway since this is a Native
122+
* Image-specific error that is unrecoverable in any case.
123+
*/
124+
return exception;
125+
}
126+
114127
private static String errorMessage(String failedAction, String elementDescriptor) {
115128
return errorMessage(failedAction, elementDescriptor, null, "reflection");
116129
}

0 commit comments

Comments
 (0)