-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC - register client proxies and subclasses for reflection if needed
- introduce ReflectiveBeanClassBuildItem - produce ReflectiveBeanClassBuildItem automatically for bean class annotated with RegisterForReflection - resolves #7507 (cherry picked from commit 5db6271)
- Loading branch information
Showing
12 changed files
with
235 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
.../arc/deployment/src/main/java/io/quarkus/arc/deployment/ReflectiveBeanClassBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.quarkus.arc.deployment; | ||
|
||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; | ||
|
||
/** | ||
* This build item instructs ArC to produce a {@link ReflectiveClassBuildItem} for a client proxy and intercepred | ||
* subclass generated for the given bean class. | ||
*/ | ||
public final class ReflectiveBeanClassBuildItem extends MultiBuildItem { | ||
|
||
private final DotName className; | ||
|
||
public ReflectiveBeanClassBuildItem(ClassInfo classInfo) { | ||
this(classInfo.name()); | ||
} | ||
|
||
public ReflectiveBeanClassBuildItem(String className) { | ||
this.className = DotName.createSimple(className); | ||
} | ||
|
||
public ReflectiveBeanClassBuildItem(DotName className) { | ||
this.className = className; | ||
} | ||
|
||
public DotName getClassName() { | ||
return className; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...rc/deployment/src/main/java/io/quarkus/arc/deployment/ReflectiveBeanClassesProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.arc.deployment; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.Type; | ||
|
||
import io.quarkus.arc.processor.BeanInfo; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
public class ReflectiveBeanClassesProcessor { | ||
|
||
@BuildStep | ||
void implicitReflectiveBeanClasses(BuildProducer<ReflectiveBeanClassBuildItem> reflectiveBeanClasses, | ||
BeanDiscoveryFinishedBuildItem beanDiscoveryFinished) { | ||
DotName registerForReflection = DotName.createSimple(RegisterForReflection.class.getName()); | ||
|
||
for (BeanInfo classBean : beanDiscoveryFinished.beanStream().classBeans()) { | ||
ClassInfo beanClass = classBean.getTarget().get().asClass(); | ||
AnnotationInstance annotation = beanClass.classAnnotation(registerForReflection); | ||
if (annotation != null) { | ||
Type[] targets = annotation.value("targets") != null ? annotation.value("targets").asClassArray() | ||
: new Type[] {}; | ||
String[] classNames = annotation.value("classNames") != null ? annotation.value("classNames").asStringArray() | ||
: new String[] {}; | ||
if (targets.length == 0 && classNames.length == 0) { | ||
reflectiveBeanClasses.produce(new ReflectiveBeanClassBuildItem(beanClass)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
integration-tests/main/src/main/java/io/quarkus/it/arc/IntercepredNormalScopedFoo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.quarkus.it.arc; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import io.quarkus.arc.Lock; | ||
import io.quarkus.arc.Unremovable; | ||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
@RegisterForReflection | ||
@Unremovable | ||
@Lock | ||
@ApplicationScoped | ||
public class IntercepredNormalScopedFoo { | ||
|
||
private int val; | ||
|
||
public int ping() { | ||
return val; | ||
} | ||
|
||
@PostConstruct | ||
void init() { | ||
val = 42; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
integration-tests/main/src/main/java/io/quarkus/it/arc/ReflectiveBeanEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.it.arc; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import io.quarkus.arc.ClientProxy; | ||
|
||
@Path("/reflective-bean") | ||
public class ReflectiveBeanEndpoint { | ||
|
||
@Inject | ||
IntercepredNormalScopedFoo foo; | ||
|
||
@Path("proxy") | ||
@GET | ||
public int proxyPing() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, | ||
InvocationTargetException { | ||
return callPingViaReflection(foo); | ||
} | ||
|
||
@Path("subclass") | ||
@GET | ||
public int subclassPing() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, | ||
InvocationTargetException { | ||
IntercepredNormalScopedFoo subclass = (IntercepredNormalScopedFoo) ((ClientProxy) foo).arc_contextualInstance(); | ||
return callPingViaReflection(subclass); | ||
} | ||
|
||
private int callPingViaReflection(IntercepredNormalScopedFoo foo) throws NoSuchMethodException, SecurityException, | ||
IllegalAccessException, IllegalArgumentException, InvocationTargetException { | ||
Method pingMethod = foo.getClass().getDeclaredMethod("ping"); | ||
return (int) pingMethod.invoke(foo); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-tests/main/src/test/java/io/quarkus/it/main/ReflectiveBeanITCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.quarkus.it.main; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
class ReflectiveBeanITCase extends ReflectiveBeanTest { | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
integration-tests/main/src/test/java/io/quarkus/it/main/ReflectiveBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.it.main; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTest | ||
class ReflectiveBeanTest { | ||
|
||
@Test | ||
public void testReflectiveAccess() { | ||
RestAssured.when().get("/reflective-bean/proxy").then() | ||
.body(is("42")); | ||
RestAssured.when().get("/reflective-bean/subclass").then() | ||
.body(is("42")); | ||
} | ||
|
||
} |