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

Produce ReflectiveClassBuildItem dynamically #219

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.Objects;
import java.util.Optional;

import io.quarkiverse.dapr.core.DaprTopicRoutes;
import io.quarkiverse.dapr.core.DaprTopicRule;
import jakarta.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -121,6 +123,7 @@ void daprTopicBuildItems(BuildProducer<DaprTopicBuildItem> topicProducer, Combin
DaprConfig daprConfig) {
Map<String, DaprConfig.DaprPubSubConfig> pubSubConfigMap = Optional.ofNullable(daprConfig.pubSub)
.orElse(new HashMap<>(16));
boolean produceReflectiveClasses = false;
for (AnnotationInstance i : indexBuildItem.getIndex().getAnnotations(DAPR_TOPIC)) {
if (i.target().kind() == AnnotationTarget.Kind.METHOD) {

Expand Down Expand Up @@ -165,7 +168,9 @@ void daprTopicBuildItems(BuildProducer<DaprTopicBuildItem> topicProducer, Combin

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
void addTopic(DaprRuntimeRecorder daprRuntimeRecorder, List<DaprTopicBuildItem> daprTopicBuildItems) {
void addTopic(BuildProducer<ReflectiveClassBuildItem> reflectiveClasses, DaprRuntimeRecorder daprRuntimeRecorder,
List<DaprTopicBuildItem> daprTopicBuildItems) {
boolean produceReflectiveClasses = false;
for (DaprTopicBuildItem item : daprTopicBuildItems) {
daprRuntimeRecorder.subscribeToTopics(
item.getPubSubName(),
Expand All @@ -174,6 +179,17 @@ void addTopic(DaprRuntimeRecorder daprRuntimeRecorder, List<DaprTopicBuildItem>
item.getPriority(),
item.getRoute(),
item.getMetadata());

if (!item.getRoute().isBlank()) {
produceReflectiveClasses = true;
}
}

if (produceReflectiveClasses) {
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(
DaprTopicRule.class.getName(),
DaprTopicRoutes.class.getName(),
CloudEventReader.class.getName()).build());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wonder why these classes need to be registered for reflection only in route is not blank if I read the codes correctly?

And it seeem that io.quarkiverse.dapr.core.DaprTopicSubscription is missed here?

Copy link
Member Author

@mcruzdev mcruzdev Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @zhfeng!

Just wonder why these classes need to be registered for reflection only in route is not blank if I read the codes correctly?

I added this because I saw that in the last PR it was necessary to add annotation to the client code after using @Route.
Can I register it even if it is not used in the native image? Is graalvm smart enough not to use the reflection statement?

And it seeem that io.quarkiverse.dapr.core.DaprTopicSubscription is missed here?

This one is produced by void reflectiveClasses(BuildProducer<ReflectiveClassBuildItem> reflectiveClassBuildProducer) method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, in this case, it should be OK to add all of them.

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package io.quarkiverse.dapr.demo;

import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
Expand All @@ -26,13 +24,6 @@
import io.dapr.Topic;

@Path("/dapr")
@ApplicationScoped
@RegisterForReflection(classNames = {
"io.quarkiverse.dapr.core.DaprTopicRule",
"io.quarkiverse.dapr.core.DaprTopicRoutes",
"io.quarkiverse.dapr.core.DaprTopicSubscription",
"io.quarkiverse.dapr.resteasy.CloudEventReader"
})
public class DaprResource {
// add some rest methods here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;

class DaprTopicRoutes {
public class DaprTopicRoutes {
private final List<DaprTopicRule> rules;
@JsonProperty("default")
private final String defaultRoute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import java.util.Objects;

class DaprTopicRule {
public class DaprTopicRule {
private final String match;
private final String path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.quarkus.arc.Unremovable;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.Startup;
import io.quarkus.runtime.annotations.RegisterForReflection;

/**
* DaprProducer
Expand Down
Loading