Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the @RegisterForReflection documentation #16941

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/src/main/asciidoc/amazon-dynamodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ public class Fruit {
}
----

TIP: The `@RegisterForReflection` annotation instructs Quarkus to keep the class and its members during the native compilation. More details about the `@RegisterForReflection` annotation can be found on the xref:writing-native-applications-tips.adoc#registerForReflection[native application tips] page.


Nothing fancy. One important thing to note is that having a default constructor is required by the JSON serialization layer. The static `from` method creates a bean based on the `Map`
object provided by the DynamoDB client response.

Expand Down
3 changes: 3 additions & 0 deletions docs/src/main/asciidoc/amazon-sns.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public class Quark {
----
Then, create a `org.acme.sns.QuarksCannonSyncResource` that will provide an API to shoot quarks into the SNS topic via the SNS synchronous client.

TIP: The `@RegisterForReflection` annotation instructs Quarkus to keep the class and its members during the native compilation. More details about the `@RegisterForReflection` annotation can be found on the xref:writing-native-applications-tips.adoc#registerForReflection[native application tips] page.


[source,java]
----
package org.acme.sns;
Expand Down
3 changes: 3 additions & 0 deletions docs/src/main/asciidoc/amazon-sqs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public class Quark {
----
Then, create a `org.acme.sqs.QuarksCannonSyncResource` that will provide an API to shoot quarks into the SQS queue using the synchronous client.

TIP: The `@RegisterForReflection` annotation instructs Quarkus to keep the class and its members during the native compilation. More details about the `@RegisterForReflection` annotation can be found on the xref:writing-native-applications-tips.adoc#registerForReflection[native application tips] page.


[source,java]
----
package org.acme.sqs;
Expand Down
3 changes: 2 additions & 1 deletion docs/src/main/asciidoc/hibernate-orm-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,11 @@ public class PersonName {
PanacheQuery<PersonName> query = Person.find("status", Status.Alive).project(PersonName.class);
----

1. If you plan to deploy your application as a native executable, you must register manually the projection class for reflection.
1. The `@RegisterForReflection` annotation instructs Quarkus to keep the class and its members during the native compilation. More details about the `@RegisterForReflection` annotation can be found on the xref:writing-native-applications-tips.adoc#registerForReflection[native application tips] page.
2. We use public fields here, but you can use private fields and getters/setters if you prefer.
3. This constructor will be used by Hibernate, it must be the only constructor in your class and have all the class attributes as parameters.


[WARNING]
====
The implementation of the `project(Class)` method uses the constructor's parameter names to build the select clause of the query,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/kafka-streams.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public class WeatherStation {
public String name;
}
----
<1> By adding the `@RegisterForReflection` annotation, it is ensured that this type can be instantiated reflectively when running the application in native mode.
<1> The `@RegisterForReflection` annotation instructs Quarkus to keep the class and its members during the native compilation. More details about the `@RegisterForReflection` annotation can be found on the xref:writing-native-applications-tips.adoc#registerForReflection[native application tips] page.

Then the file `aggregator/src/main/java/org/acme/kafka/streams/aggregator/model/TemperatureMeasurement.java`,
representing temperature measurements for a given station:
Expand Down
1 change: 1 addition & 0 deletions docs/src/main/asciidoc/mongodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ link:https://docs.mongodb.com/manual/core/security-client-side-encryption/[Clien
If you encounter the following error when running your application in native mode: +
`Failed to encode 'MyObject'. Encoding 'myVariable' errored with: Can't find a codec for class org.acme.MyVariable.` +
This means that the `org.acme.MyVariable` class is not known to GraalVM, the remedy is to add the `@RegisterForReflection` annotation to your `MyVariable class`.
More details about the `@RegisterForReflection` annotation can be found on the xref:writing-native-applications-tips.adoc#registerForReflection[native application tips] page.
====

== Conclusion
Expand Down
2 changes: 2 additions & 0 deletions docs/src/main/asciidoc/rest-json.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ public class Legume {
}
----

TIP: The `@RegisterForReflection` annotation instructs Quarkus to keep the class and its members during the native compilation. More details about the `@RegisterForReflection` annotation can be found on the xref:writing-native-applications-tips.adoc#registerForReflection[native application tips] page.

Let's do that and follow the same steps as before:

* hit `Ctrl+C` to stop the application
Expand Down
15 changes: 13 additions & 2 deletions docs/src/main/asciidoc/writing-native-applications-tips.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ An even nastier possible outcome could be for no exception to be thrown, but ins

There are two different ways to fix this type of issues.

[#registerForReflection]
==== Using the @RegisterForReflection annotation

The easiest way to register a class for reflection is to use the `@RegisterForReflection` annotation:
Expand All @@ -176,12 +177,22 @@ If your class is in a third-party jar, you can do it by using an empty class tha

[source,java]
----
@RegisterForReflection(targets=MyClass.class)
@RegisterForReflection(targets={ MyClassRequiringReflection.class, MySecondClassRequiringReflection.class})
public class MyReflectionConfiguration {
}
----

Note that `MyClass` will be registered for reflection but not `MyReflectionConfiguration`.
Note that `MyClassRequiringReflection` and `MySecondClassRequiringReflection` will be registered for reflection but not `MyReflectionConfiguration`.

This feature is handy when using third-party libraries using object mapping features (such as Jackson or GSON):

[source, java]
----
@RegisterForReflection(targets = {User.class, UserImpl.class})
public class MyReflectionConfiguration {

}
----

==== Using a configuration file

Expand Down