Skip to content

Commit

Permalink
Introduce more flexible type descriptors for JSON configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
loicottet committed Feb 27, 2024
1 parent 94820db commit 642f8b8
Show file tree
Hide file tree
Showing 20 changed files with 463 additions and 63 deletions.
10 changes: 5 additions & 5 deletions docs/reference-manual/native-image/ReachabilityMetadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The JSON file consists of entries that tell Native Image the elements to include
For example, Java reflection metadata is specified in `reflect-config.json`, and a sample entry looks like:
```json
{
"name": "Foo"
"type": "Foo"
}
```
Expand Down Expand Up @@ -147,15 +147,15 @@ Integer.class.getMethod("parseInt", params2);
### Specifying Reflection Metadata in JSON
Reflection metadata should be specified in a _reflect-config.json_ file and conform to the JSON schema defined in
[reflect-config-schema-v1.0.0.json](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/reflect-config-schema-v1.0.0.json).
[reflect-config-schema-v1.1.0.json](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/reflect-config-schema-v1.1.0.json).
The schema also includes further details and explanations how this configuration works. Here is the example of the reflect-config.json:
```json
[
{
"condition": {
"typeReachable": "<condition-class>"
},
"name": "<class>",
"type": "<class>",
"methods": [
{"name": "<methodName>", "parameterTypes": ["<param-one-type>"]}
],
Expand Down Expand Up @@ -199,7 +199,7 @@ looks up the `java.lang.String` class, which can then be used, for example, to i
The generated metadata entry for the above call would look like:
```json
{
"name": "java.lang.String"
"type": "java.lang.String"
}
```
Expand All @@ -209,7 +209,7 @@ It is not possible to specify JNI metadata in code.
### JNI Metadata in JSON
JNI metadata should be specified in a _jni-config.json_ file and conform to the JSON schema defined in
[jni-config-schema-v1.0.0.json](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/jni-config-schema-v1.0.0.json).
[jni-config-schema-v1.1.0.json](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/jni-config-schema-v1.1.0.json).
The schema also includes further details and explanations how this configuration works. The example of jni-config.json is the same
as the example of reflect-config.json described above.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/config-condition-schema-v1.0.0.json",
"title": "JSON schema for the conditions used in GraalVM Native Image configuration files",
"properties": {
"typeReachable": {
"type": "string",
"title": "Fully qualified name of a class that must be reachable in order to register the type <type> for reflection"
}
},
"required": [
"typeReachable"
],
"additionalProperties": false,
"type": "object"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/config-type-schema-v1.0.0.json",
"type": "string",
"title": "JSON schema for the type descriptors GraalVM Native Image configuration files use"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/jni-config-schema-v1.1.0.json",
"default": [],
"items": {
"properties": {
"condition": {
"$ref": "config-condition-schema-v1.0.0.json",
"title": "Condition under which the class should be registered for access through JNI"
},
"type": {
"$ref": "config-type-schema-v1.0.0.json",
"title": "Type descriptor of the class that should be registered for access through JNI"
},
"methods": {
"default": [],
"items": {
"properties": {
"name": {
"type": "string",
"title": "Method name that should be registered for this class"
},
"parameterTypes": {
"default": [],
"items": {
"type": "string",
"title": "List of the method's parameter types"
},
"type": "array"
}
},
"required": [
"name"
],
"additionalProperties": false,
"type": "object",
"title": "List of methods from this class that are registered for access through JNI"
},
"type": "array",
"title": "List of methods that should be registered for the class declared in <name>"
},
"fields": {
"default": [],
"items": {
"properties": {
"name": {
"type": "string",
"title": "Name of the field that should be registered for access through JNI"
}
},
"required": [
"name"
],
"additionalProperties": false,
"type": "object"
},
"type": "array",
"title": "List of fields that should be registered for the class declared in <name>"
},
"allDeclaredMethods": {
"default": false,
"type": "boolean",
"title": "Register methods which would be returned by the java.lang.Class#getDeclaredMethods call"
},
"allDeclaredFields": {
"default": false,
"type": "boolean",
"title": "Register fields which would be returned by the java.lang.Class#getDeclaredFields call"
},
"allDeclaredConstructors": {
"default": false,
"type": "boolean",
"title": "Register constructors which would be returned by the java.lang.Class#getDeclaredConstructors call"
},
"allPublicMethods": {
"default": false,
"type": "boolean",
"title": "Register all public methods which would be returned by the java.lang.Class#getMethods call"
},
"allPublicFields": {
"default": false,
"type": "boolean",
"title": "Register all public fields which would be returned by the java.lang.Class#getFields call"
},
"allPublicConstructors": {
"default": false,
"type": "boolean",
"title": "Register all public constructors which would be returned by the java.lang.Class#getConstructors call"
},
"unsafeAllocated": {
"default": false,
"type": "boolean",
"title": "Allow objects of this class to be instantiated with a call to jdk.internal.misc.Unsafe#allocateInstance"
}
},
"required": [
"type"
],
"additionalProperties": false,
"type": "object"
},
"type": "array",
"title": "JSON schema for the JNI configuration that GraalVM Native Image uses"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/reflect-config-schema-v1.1.0.json",
"default": [],
"items": {
"properties": {
"condition": {
"$ref": "config-condition-schema-v1.0.0.json",
"title": "Condition under which the class should be registered for reflection"
},
"type": {
"$ref": "config-type-schema-v1.0.0.json",
"title": "Type descriptor of the class that should be registered for reflection"
},
"methods": {
"default": [],
"items": {
"properties": {
"name": {
"type": "string",
"title": "Method name that should be registered for this class"
},
"parameterTypes": {
"default": [],
"items": {
"type": "string",
"title": "List of the method's parameter types"
},
"type": "array"
}
},
"required": [
"name"
],
"additionalProperties": false,
"type": "object",
"title": "List of methods from this class that are registered for reflection"
},
"type": "array",
"title": "List of methods that should be registered for the type declared in <type>"
},
"fields": {
"default": [],
"items": {
"properties": {
"name": {
"type": "string",
"title": "Name of the field that should be registered for reflection"
}
},
"required": [
"name"
],
"additionalProperties": false,
"type": "object"
},
"type": "array",
"title": "List of class fields that can be looked up, read, or modified for the type declared in <type>"
},
"allDeclaredMethods": {
"default": false,
"type": "boolean",
"title": "Register methods which would be returned by the java.lang.Class#getDeclaredMethods call"
},
"allDeclaredFields": {
"default": false,
"type": "boolean",
"title": "Register fields which would be returned by the java.lang.Class#getDeclaredFields call"
},
"allDeclaredConstructors": {
"default": false,
"type": "boolean",
"title": "Register constructors which would be returned by the java.lang.Class#getDeclaredConstructors call"
},
"allPublicMethods": {
"default": false,
"type": "boolean",
"title": "Register all public methods which would be returned by the java.lang.Class#getMethods call"
},
"allPublicFields": {
"default": false,
"type": "boolean",
"title": "Register all public fields which would be returned by the java.lang.Class#getFields call"
},
"allPublicConstructors": {
"default": false,
"type": "boolean",
"title": "Register all public constructors which would be returned by the java.lang.Class#getConstructors call"
},
"unsafeAllocated": {
"default": false,
"type": "boolean",
"title": "Allow objects of this class to be instantiated with a call to jdk.internal.misc.Unsafe#allocateInstance"
}
},
"required": [
"type"
],
"additionalProperties": false,
"type": "object"
},
"type": "array",
"title": "JSON schema for the reflection configuration that GraalVM Native Image uses"
}
1 change: 1 addition & 0 deletions substratevm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This changelog summarizes major changes to GraalVM Native Image.
* (GR-47109) Together with Red Hat, we added support for JFR event throttling and the event `ObjectAllocationSample`.
* (GR-52030) Add a stable name for `Proxy` types in Native Image. The name `$Proxy[id]` is replaced by `$Proxy.s[hashCode]` where `hashCode` is computed using the names of the `Proxy` interfaces, the name of the class loader and the name of the module if it is not a dynamic module.
* (GR-47712) Using the `--static` option without the `--libc=musl` option causes the build process to fail (and reports the appropriate error). Static linking is currently only supported with musl.
* (GR-50434) Introduce a `"type"` field in reflection and JNI configuration files to support more than simple named types.

## GraalVM for JDK 22 (Internal Version 24.0.0)
* (GR-48304) Red Hat added support for the JFR event ThreadAllocationStatistics.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -260,11 +260,11 @@ Map<ConfigurationMethod, ConfigurationMemberDeclaration> getMethodsMap(Configura
}

void populateConfig() {
ConfigurationType oldType = new ConfigurationType(UnresolvedConfigurationCondition.alwaysTrue(), getTypeName());
ConfigurationType oldType = new ConfigurationType(UnresolvedConfigurationCondition.alwaysTrue(), getTypeName(), true);
setFlags(oldType);
previousConfig.add(oldType);

ConfigurationType newType = new ConfigurationType(UnresolvedConfigurationCondition.alwaysTrue(), getTypeName());
ConfigurationType newType = new ConfigurationType(UnresolvedConfigurationCondition.alwaysTrue(), getTypeName(), true);
for (Map.Entry<ConfigurationMethod, ConfigurationMemberDeclaration> methodEntry : methodsThatMustExist.entrySet()) {
newType.addMethod(methodEntry.getKey().getName(), methodEntry.getKey().getInternalSignature(), methodEntry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -43,7 +43,7 @@ public static boolean isConstructorName(String name) {
public static String toInternalParamsSignature(List<ConfigurationType> types) {
StringBuilder sb = new StringBuilder("(");
for (ConfigurationType type : types) {
sb.append(MetaUtil.toInternalName(type.getQualifiedJavaName()));
sb.append(MetaUtil.toInternalName(((NamedConfigurationTypeDescriptor) type.getTypeDescriptor()).name()));
}
sb.append(')');
// we are missing the return type, so this is only a partial signature
Expand Down
Loading

0 comments on commit 642f8b8

Please sign in to comment.