Skip to content

Commit a5b9761

Browse files
committed
[GR-40263] Provide JSON schemas for Native Image config files
PullRequest: graal/14435
2 parents 2f219e4 + 75aa2c0 commit a5b9761

7 files changed

+752
-38
lines changed

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

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Computing metadata in code can be achieved in two ways:
6161
return aClass;
6262
}
6363
}
64-
```
64+
```
6565

6666
When metadata is computed in code, the dynamically accessed elements will be included into the native executable's heap only if that part of the heap is reachable through an enclosing method (for example, `ReflectiveAccess#fetchFoo`) or a static field (for example, `InitializedAtBuildTime.aClass`).
6767
@@ -89,6 +89,8 @@ A condition is specified in the following way:
8989
An entry with a `typeReachable` condition is considered only when the fully-qualified class is reachable.
9090
Currently, we support only `typeReachable` as a condition.
9191
92+
Find more examples of the configuration files in the [GraalVM Reachability Metadata repository](https://github.com/oracle/graalvm-reachability-metadata).
93+
9294
## Metadata Types
9395
9496
Native Image accepts the following types of reachability metadata:
@@ -144,8 +146,9 @@ Integer.class.getMethod("parseInt", params2);
144146
145147
### Specifying Reflection Metadata in JSON
146148
147-
Reflection metadata can be specified in the `reflect-config.json` file.
148-
The JSON file is an array of reflection entries:
149+
Reflection metadata should be specified in a _reflect-config.json_ file and conform to the JSON schema defined in
150+
[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).
151+
The schema also includes further details and explanations how this configuration works. Here is the example of the reflect-config.json:
149152
```json
150153
[
151154
{
@@ -162,12 +165,18 @@ The JSON file is an array of reflection entries:
162165
"fields": [
163166
{"name": "<fieldName>"}
164167
],
168+
"allDeclaredClasses": true,
165169
"allDeclaredMethods": true,
166-
"allDeclaredFields": true,
170+
"allDeclaredFields": true,
167171
"allDeclaredConstructors": true,
172+
"allPublicClasses": true,
168173
"allPublicMethods": true,
169174
"allPublicFields": true,
170175
"allPublicConstructors": true,
176+
"allRecordComponents": true,
177+
"allNestMembers": true,
178+
"allSigners": true,
179+
"allPermittedSubclasses": true,
171180
"queryAllDeclaredMethods": true,
172181
"queryAllDeclaredConstructors": true,
173182
"queryAllPublicMethods": true,
@@ -177,21 +186,6 @@ The JSON file is an array of reflection entries:
177186
]
178187
```
179188
180-
The fields in a reflection entry have the following meaning:
181-
- `condition`: See [Conditional Metadata Entries](#specifying-metadata-with-json)
182-
- `name`: Name of the class that will be reflectively looked up. This property is mandatory.
183-
- `methods`: List class methods that can be looked up and executed reflectively.
184-
Each method is described by its name and a list of parameter types.
185-
The parameter types are fully qualified Java class names.
186-
- `queriedMethods`: List of class methods that can only be looked up.
187-
The description of each method is identical to the `methods` list.
188-
- `fields`: List of class fields that can be looked up, read, or modified.
189-
- `all<access>(Methods/Fields/Constructors)`: Registers all methods/fields/constructors for lookup. Methods and constructors can also be invoked.
190-
`<access>` refers to different ways of querying these members in Java and can be either `Declared` or `Public`.
191-
For more information, see `java.lang.Class.getDeclaredMethods()` and `java.lang.Class.getPublicMethods()`.
192-
- `queryAll<access>(Methods/Constructors)`: Registers all methods/constructors for lookup only.
193-
- `unsafeAllocated`: Allows objects of this class to be allocated using `Unsafe.allocateInstance`.
194-
195189
## Java Native Interface
196190
197191
Java Native Interface (JNI) allows native code to access arbitrary Java types and type members.
@@ -213,8 +207,11 @@ The generated metadata entry for the above call would look like:
213207
It is not possible to specify JNI metadata in code.
214208
215209
### JNI Metadata in JSON
216-
Metadata for JNI is provided in `jni-config.json` files.
217-
The JSON schema of JNI metadata is identical to the [Reflection metadata schema](#specifying-reflection-metadata-in-json).
210+
211+
JNI metadata should be specified in a _jni-config.json_ file and conform to the JSON schema defined in
212+
[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).
213+
The schema also includes further details and explanations how this configuration works. The example of jni-config.json is the same
214+
as the example of reflect-config.json described above.
218215
219216
## Resources and Resource Bundles
220217
Java is capable of accessing any resource on the application class path, or the module path for which the requesting code has permission to access.
@@ -241,14 +238,17 @@ class Example {
241238
```
242239
243240
### Resource Metadata in JSON
244-
Metadata for resources is provided in `resource-config.json` files.
241+
242+
Resource metadata should be specified in a _resource-config.json_ file and conform to the JSON schema defined in
243+
[resource-config-schema-v1.0.0.json](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/resource-config-schema-v1.0.0.json).
244+
The schema also includes further details and explanations how this configuration works. Here is the example of the resource-config.json:
245245
```json
246246
{
247247
"resources": {
248248
"includes": [
249249
{
250250
"condition": {
251-
"typeReachable": "<condition-class>"
251+
"typeReachable": "<condition-class>"
252252
},
253253
"pattern": ".*\\.txt"
254254
}
@@ -269,15 +269,21 @@ Metadata for resources is provided in `resource-config.json` files.
269269
},
270270
"name": "fully.qualified.bundle.name",
271271
"locales": ["en", "de", "sk"]
272+
},
273+
{
274+
"condition": {
275+
"typeReachable": "<condition-class>"
276+
},
277+
"name": "fully.qualified.bundle.name",
278+
"classNames": [
279+
"fully.qualified.bundle.name_en",
280+
"fully.qualified.bundle.name_de"
281+
]
272282
}
273283
]
274284
}
275285
```
276286
277-
Native Image will iterate over all resources and match their relative paths against the Java regex specified in `includes`.
278-
If the path matches the regex, the resource is included.
279-
The `excludes` statement instructs `native-image` to omit certain included resources that match the given `pattern`.
280-
281287
## Dynamic Proxy
282288
283289
The JDK supports generating proxy classes for a given interface list.
@@ -316,7 +322,10 @@ The following methods are evaluated at build time when called with constant argu
316322
- `java.lang.reflect.Proxy.newProxyInstance`
317323
318324
### Dynamic Proxy Metadata in JSON
319-
Metadata for dynamic proxies is provided in `proxy-config.json` files.
325+
326+
Dynamic proxy metadata should be specified in a _proxy-config.json_ file and conform to the JSON schema defined in
327+
[proxy-config-schema-v1.0.0.json](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/proxy-config-schema-v1.0.0.json).
328+
The schema also includes further details and explanations how this configuration works. Here is the example of the proxy-config.json:
320329
```json
321330
[
322331
{
@@ -366,7 +375,10 @@ To create a custom constructor for serialization use:
366375
Proxy classes can only be registered for serialization via the JSON files.
367376
368377
### Serialization Metadata in JSON
369-
Metadata for serialization is provided in `serialization-config.json` files.
378+
379+
Serialization metadata should be specified in a _serialization-config.json_ file and conform to the JSON schema defined in
380+
[serialization-config-schema-v1.0.0.json](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/serialization-config-schema-v1.0.0.json).
381+
The schema also includes further details and explanations how this configuration works. Here is the example of the serialization-config.json:
370382
```json
371383
{
372384
"types": [
@@ -397,12 +409,6 @@ Metadata for serialization is provided in `serialization-config.json` files.
397409
}
398410
```
399411
400-
Each entry in `types` enables serializing and deserializing objects of the class given by `name`.
401-
402-
Each entry in `lambdaCapturingTypes` enables lambda serialization: all lambdas declared in the methods of the class given by `name` can be serialized and deserialized.
403-
404-
Each entry in `proxies` enables the [Proxy](https://docs.oracle.com/javase/8/docs/technotes/guides/reflection/proxy.html) serialization by providing an interface list that a proxy implements.
405-
406412
## Predefined Classes
407413
408414
Native Image requires all classes to be known at build time (a "closed-world assumption").
@@ -418,7 +424,10 @@ At runtime, if there is an attempt to load a class with the same name and byteco
418424
It is not possible to specify predefined classes in code.
419425
420426
### Predefined Classes Metadata in JSON
421-
Metadata for predefined classes is provided in `predefined-classes-config.json` files.
427+
428+
Predefined classes metadata should be specified in a _predefined-classes-config.json_ file and conform to the JSON schema defined in
429+
[predefined-classes-config-schema-v1.0.0.json](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/predefined-classes-config-schema-v1.0.0.json).
430+
The schema also includes further details and explanations how this configuration works. Here is the example of the predefined-classes-config.json:
422431
```json
423432
[
424433
{
@@ -433,8 +442,6 @@ Metadata for predefined classes is provided in `predefined-classes-config.json`
433442
]
434443
```
435444
436-
The JSON schema is accompanied by the `agent-extracted-predefined-classes` directory that contains the bytecode of the listed classes.
437-
438445
### Further Reading
439446
440447
* [Metadata Collection with the Tracing Agent](AutomaticMetadataCollection.md)
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2019-09/schema",
3+
"$id": "https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/assets/jni-config-schema-v1.0.0.json",
4+
"default": [],
5+
"items": {
6+
"properties": {
7+
"condition": {
8+
"properties": {
9+
"typeReachable": {
10+
"type": "string",
11+
"title": "Fully qualified class name of the class that must be reachable in order to register the class <name> for access through JNI"
12+
}
13+
},
14+
"required": [
15+
"typeReachable"
16+
],
17+
"additionalProperties": false,
18+
"type": "object"
19+
},
20+
"name": {
21+
"type": "string",
22+
"title": "Name of the class that should be registered for access through JNI"
23+
},
24+
"methods": {
25+
"default": [],
26+
"items": {
27+
"properties": {
28+
"name": {
29+
"type": "string",
30+
"title": "Method name that should be registered for this class"
31+
},
32+
"parameterTypes": {
33+
"default": [],
34+
"items": {
35+
"type": "string",
36+
"title": "List of types for the parameters of the this method"
37+
},
38+
"type": "array"
39+
}
40+
},
41+
"required": [
42+
"name"
43+
],
44+
"additionalProperties": false,
45+
"type": "object",
46+
"title": "List of methods from this class that are registered for access through JNI"
47+
},
48+
"type": "array",
49+
"title": "List of methods that should be registered for the class declared in <name>"
50+
},
51+
"queriedMethods": {
52+
"default": [],
53+
"items": {
54+
"properties": {
55+
"name": {
56+
"type": "string",
57+
"title": "Method name that are queried for this class"
58+
},
59+
"parameterTypes": {
60+
"default": [],
61+
"items": {
62+
"type": "string",
63+
"title": "List of types for the parameters of the this method"
64+
},
65+
"type": "array",
66+
"title": "List of methods to register for this class that are only looked up but not invoked."
67+
}
68+
},
69+
"required": [
70+
"name"
71+
],
72+
"additionalProperties": false,
73+
"type": "object"
74+
},
75+
"type": "array",
76+
"title": "List of methods that are queried for the class declared in <name>"
77+
},
78+
"fields": {
79+
"default": [],
80+
"items": {
81+
"properties": {
82+
"name": {
83+
"type": "string",
84+
"title": "Name of the field that should be registered for access through JNI"
85+
}
86+
},
87+
"required": [
88+
"name"
89+
],
90+
"additionalProperties": false,
91+
"type": "object"
92+
},
93+
"type": "array",
94+
"title": "List of fields that should be registered for the class declared in <name>"
95+
},
96+
"allDeclaredClasses": {
97+
"default": false,
98+
"type": "boolean",
99+
"title": "Register classes which would be returned by the java.lang.Class#getDeclaredClasses call"
100+
},
101+
"allDeclaredMethods": {
102+
"default": false,
103+
"type": "boolean",
104+
"title": "Register methods which would be returned by the java.lang.Class#getDeclaredMethods call"
105+
},
106+
"allDeclaredFields": {
107+
"default": false,
108+
"type": "boolean",
109+
"title": "Register fields which would be returned by the java.lang.Class#getDeclaredFields call"
110+
},
111+
"allDeclaredConstructors": {
112+
"default": false,
113+
"type": "boolean",
114+
"title": "Register constructors which would be returned by the java.lang.Class#getDeclaredConstructors call"
115+
},
116+
"allPublicClasses": {
117+
"default": false,
118+
"type": "boolean",
119+
"title": "Register all public classes which would be returned by the java.lang.Class#getClasses call"
120+
},
121+
"allPublicMethods": {
122+
"default": false,
123+
"type": "boolean",
124+
"title": "Register all public methods which would be returned by the java.lang.Class#getMethods call"
125+
},
126+
"allPublicFields": {
127+
"default": false,
128+
"type": "boolean",
129+
"title": "Register all public fields which would be returned by the java.lang.Class#getFields call"
130+
},
131+
"allPublicConstructors": {
132+
"default": false,
133+
"type": "boolean",
134+
"title": "Register all public constructors which would be returned by the java.lang.Class#getConstructors call"
135+
},
136+
"allRecordComponents": {
137+
"default": false,
138+
"type": "boolean",
139+
"title": "Register record components which would be returned by the java.lang.Class#getRecordComponents call"
140+
},
141+
"allPermittedSubclasses": {
142+
"default": false,
143+
"type": "boolean",
144+
"title": "Register permitted subclasses which would be returned by the java.lang.Class#getPermittedSubclasses call"
145+
},
146+
"allNestMembers": {
147+
"default": false,
148+
"type": "boolean",
149+
"title": "Register nest members which would be returned by the java.lang.Class#getNestMembers call"
150+
},
151+
"allSigners": {
152+
"default": false,
153+
"type": "boolean",
154+
"title": "Register signers which would be returned by the java.lang.Class#getSigners call"
155+
},
156+
"queryAllDeclaredMethods": {
157+
"default": false,
158+
"type": "boolean",
159+
"title": "Register methods which would be returned by the java.lang.Class#getDeclaredMethods call but only for lookup"
160+
},
161+
"queryAllDeclaredConstructors": {
162+
"default": false,
163+
"type": "boolean",
164+
"title": "Register constructors which would be returned by the java.lang.Class#getDeclaredConstructors call but only for lookup"
165+
},
166+
"queryAllPublicMethods": {
167+
"default": false,
168+
"type": "boolean",
169+
"title": "Register all public methods which would be returned by the java.lang.Class#getMethods call but only for lookup"
170+
},
171+
"queryAllPublicConstructors": {
172+
"default": false,
173+
"type": "boolean",
174+
"title": "Register all public constructors which would be returned by the java.lang.Class#getConstructors call but only for lookup"
175+
},
176+
"unsafeAllocated": {
177+
"default": false,
178+
"type": "boolean",
179+
"title": "Allow objects of this class to be instantiated with a call to jdk.internal.misc.Unsafe#allocateInstance"
180+
}
181+
},
182+
"required": [
183+
"name"
184+
],
185+
"additionalProperties": false,
186+
"type": "object"
187+
},
188+
"type": "array",
189+
"title": "JSON schema for the jni-config that GraalVM Native Image uses"
190+
}

0 commit comments

Comments
 (0)