Skip to content

Commit e9b5d69

Browse files
committed
Review native-image/guides/build-with-reflection.md
1 parent a905c9f commit e9b5d69

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

docs/reference-manual/native-image/guides/build-and-run-native-executable-with-jfr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ For other installation options, visit the [Downloads section](https://www.graalv
5252
It creates an event, annotated with `@Label` from the `jdk.jfr.*` package.
5353
If you run this application, it will not print anything and just run that event.
5454

55-
2. Compile the Java file using the GraalVM JDK:
55+
2. Ccompile the application using the GraalVM JDK:
5656
```shell
5757
javac JFRDemo.java
5858
```

docs/reference-manual/native-image/guides/build-and-run-native-executable-with-remote-jmx.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ For other installation options, visit the [Downloads section](https://www.graalv
7979
}
8080
```
8181

82-
2. Compile the Java file using the GraalVM JDK:
82+
2. Ccompile the application using the GraalVM JDK:
8383
```shell
8484
javac SimpleJmx.java
8585
```
8686
This creates _SimpleJmx.class_, _SimpleJmx$Simple.class_, and _SimpleJmx$SimpleMBean.class_ files.
8787

88-
3. Add dynamic proxy configuration. JMX uses dynamic proxies, a [dynamic feature](../DynamicFeatures.md) of Java, to access MBeans. To be able to interact with the custom `SimpleMBean` at run time, you need to provide Native Image with additional [dynamic-proxy metadata](../ReachabilityMetadata.md#reflection) for the MBean interface. For this, create or modify a JSON file named _proxy-config.json_ with the following contents:
88+
3. Add dynamic proxy configuration. JMX uses dynamic proxies, a [dynamic feature](../DynamicFeatures.md) of Java, to access MBeans. To be able to interact with the custom `SimpleMBean` at run time, you need to provide Native Image with additional [dynamic-proxy metadata](../ReachabilityMetadata.md#reflection) for the MBean interface. For this, create or modify a JSON file named _reachability-metadata.json_ with the following contents:
8989
```json
9090
{
9191
"reflection": [

docs/reference-manual/native-image/guides/build-with-reflection.md

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ The agent generates the configuration for you automatically when you run an appl
1717

1818
To learn how to build a native executable with the metadata precomputed in the code, [see the documentation](../ReachabilityMetadata.md).
1919

20-
The example application in this guide uses Java reflection.
21-
The `native-image` tool only partially detects application elements that are accessed using the Java Reflection API.
22-
So, you need to provide it with details about reflectively accessed classes, methods, and fields.
20+
The example application in this guide makes use of reflection.
21+
The `native-image` tool can only partially detect application elements accessed through the Java Reflection API.
22+
Therefore, you need to explicitly provide details about the classes, methods, and fields accessed reflectively.
2323

2424
## Example with No Configuration
2525

26-
The following application demonstrates the use of Java reflection.
27-
2826
### Prerequisite
2927
Make sure you have installed a GraalVM JDK.
3028
The easiest way to get started is with [SDKMAN!](https://sdkman.io/jdks#graal).
@@ -79,9 +77,8 @@ For other installation options, visit the [Downloads section](https://www.graalv
7977

8078
3. Create a native executable, as follows:
8179
```shell
82-
native-image --no-fallback ReflectionExample
80+
native-image ReflectionExample
8381
```
84-
> **NOTE:** The `--no-fallback` option to `native-image` causes the utility to fail if it can not create an executable file.
8582

8683
4. Run the resulting native executable, using the following command:
8784
```bash
@@ -90,9 +87,9 @@ For other installation options, visit the [Downloads section](https://www.graalv
9087
You should see an exception, similar to:
9188
```
9289
Exception in thread "main" java.lang.ClassNotFoundException: StringReverser
93-
at java.lang.Class.forName(DynamicHub.java:1338)
94-
at java.lang.Class.forName(DynamicHub.java:1313)
95-
at ReflectionExample.main(ReflectionExample.java:25)
90+
at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:190)
91+
...
92+
at ReflectionExample.main(ReflectionExample.java:68)
9693
```
9794
This shows that, from its static analysis, the `native-image` tool was unable to determine that class `StringReverser` is used by the application and therefore did not include it in the native executable.
9895

@@ -112,10 +109,17 @@ The following steps demonstrate how to use the agent, and its output, to create
112109
This command creates a file named _rechability-metadata.json_ containing the name of the class `StringReverser` and its `reverse()` method.
113110
```json
114111
{
115-
"reflection": [
112+
"reflection": [
116113
{
117-
"type":"StringReverser",
118-
"methods":[{"name":"reverse","parameterTypes":["java.lang.String"] }]
114+
"type": "StringReverser",
115+
"methods": [
116+
{
117+
"name": "reverse",
118+
"parameterTypes": [
119+
"java.lang.String"
120+
]
121+
}
122+
]
119123
}
120124
]
121125
}
@@ -140,12 +144,12 @@ The following steps demonstrate how to use the agent, and its output, to create
140144
You should see again an exception, similar to:
141145
```
142146
Exception in thread "main" java.lang.ClassNotFoundException: StringCapitalizer
143-
at java.lang.Class.forName(DynamicHub.java:1338)
144-
at java.lang.Class.forName(DynamicHub.java:1313)
145-
at ReflectionExample.main(ReflectionExample.java:25)
147+
at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:190)
148+
...
149+
at ReflectionExample.main(ReflectionExample.java:68)
146150
```
147151
Neither the Tracing Agent nor the `native-image` tool can ensure that the configuration file is complete.
148-
The agent observes and records which program elements are accessed using reflection when you run the program.
152+
The agent observes and records which program elements are accessed using reflection when you run the application.
149153
In this case, the `native-image` tool has not been configured to include references to class `StringCapitalizer`.
150154

151155
5. Update the configuration to include class `StringCapitalizer`.
@@ -159,15 +163,29 @@ The following steps demonstrate how to use the agent, and its output, to create
159163
{
160164
"reflection": [
161165
{
162-
"type":"StringCapitalizer",
163-
"methods":[{"name":"capitalize","parameterTypes":["java.lang.String"] }]
166+
"type": "StringCapitalizer",
167+
"methods": [
168+
{
169+
"name": "capitalize",
170+
"parameterTypes": [
171+
"java.lang.String"
172+
]
173+
}
174+
]
164175
},
165176
{
166-
"type":"StringReverser",
167-
"methods":[{"name":"reverse","parameterTypes":["java.lang.String"] }]
177+
"type": "StringReverser",
178+
"methods": [
179+
{
180+
"name": "reverse",
181+
"parameterTypes": [
182+
"java.lang.String"
183+
]
184+
}
185+
]
168186
}
169187
]
170-
}
188+
}
171189
```
172190

173191
6. Rebuild the native executable and run it.
@@ -183,5 +201,4 @@ The following steps demonstrate how to use the agent, and its output, to create
183201
### Related Documentation
184202

185203
* [Assisted Configuration with Tracing Agent](../AutomaticMetadataCollection.md#tracing-agent)
186-
* [Reachability Metadata: Reflection](../ReachabilityMetadata.md#reflection)
187-
* [java.lang.reflect Javadoc](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/reflect/package-summary.html)
204+
* [Reachability Metadata: Reflection](../ReachabilityMetadata.md#reflection)

0 commit comments

Comments
 (0)