forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
885 additions
and
74 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
63 changes: 63 additions & 0 deletions
63
...c/main/java/io/quarkus/arc/deployment/staticmethods/InterceptedStaticMethodBuildItem.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,63 @@ | ||
package io.quarkus.arc.deployment.staticmethods; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.MethodInfo; | ||
|
||
import io.quarkus.arc.processor.InterceptorInfo; | ||
import io.quarkus.builder.item.MultiBuildItem; | ||
import io.quarkus.deployment.util.HashUtil; | ||
|
||
/** | ||
* Represents an intercepted static method. | ||
*/ | ||
public final class InterceptedStaticMethodBuildItem extends MultiBuildItem { | ||
|
||
private final MethodInfo method; | ||
private final List<InterceptorInfo> interceptors; | ||
private final Set<AnnotationInstance> bindings; | ||
private final String hash; | ||
|
||
InterceptedStaticMethodBuildItem(MethodInfo method, Set<AnnotationInstance> bindings, List<InterceptorInfo> interceptors) { | ||
this.method = method; | ||
this.interceptors = interceptors; | ||
this.bindings = bindings; | ||
this.hash = HashUtil.sha1(method.declaringClass().name().toString() + method.toString()); | ||
} | ||
|
||
public ClassInfo getTarget() { | ||
return method.declaringClass(); | ||
} | ||
|
||
public MethodInfo getMethod() { | ||
return method; | ||
} | ||
|
||
/** | ||
* | ||
* @return the list of interceptors that should be applied | ||
*/ | ||
public List<InterceptorInfo> getInterceptors() { | ||
return interceptors; | ||
} | ||
|
||
/** | ||
* | ||
* @return the set of interceptor bindings | ||
*/ | ||
public Set<AnnotationInstance> getBindings() { | ||
return bindings; | ||
} | ||
|
||
/** | ||
* | ||
* @return a unique hash that could be used to indentify the method | ||
*/ | ||
public String getHash() { | ||
return hash; | ||
} | ||
|
||
} |
533 changes: 533 additions & 0 deletions
533
.../main/java/io/quarkus/arc/deployment/staticmethods/InterceptedStaticMethodsProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
.../test/java/io/quarkus/arc/test/interceptor/staticmethods/InterceptedStaticMethodTest.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,79 @@ | ||
package io.quarkus.arc.test.interceptor.staticmethods; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.annotation.Priority; | ||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.Interceptor; | ||
import javax.interceptor.InterceptorBinding; | ||
import javax.interceptor.InvocationContext; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class InterceptedStaticMethodTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(InterceptMe.class, Simple.class, SimpleInterceptor.class)); | ||
|
||
@Test | ||
public void testInterceptor() { | ||
assertEquals("OK:PONG", Simple.ping("pong")); | ||
Simple.pong(); | ||
assertEquals(1, SimpleInterceptor.VOID_INTERCEPTIONS.get()); | ||
} | ||
|
||
public static class Simple { | ||
|
||
@InterceptMe | ||
public static String ping(String val) { | ||
return val.toUpperCase(); | ||
} | ||
|
||
@InterceptMe | ||
static void pong() { | ||
} | ||
|
||
} | ||
|
||
@Priority(1) | ||
@Interceptor | ||
@InterceptMe | ||
static class SimpleInterceptor { | ||
|
||
static final AtomicInteger VOID_INTERCEPTIONS = new AtomicInteger(); | ||
|
||
@AroundInvoke | ||
Object aroundInvoke(InvocationContext ctx) throws Exception { | ||
Object ret = ctx.proceed(); | ||
if (ret != null) { | ||
return "OK:" + ctx.proceed(); | ||
} else { | ||
VOID_INTERCEPTIONS.incrementAndGet(); | ||
return ret; | ||
} | ||
} | ||
|
||
} | ||
|
||
@InterceptorBinding | ||
@Target({ TYPE, METHOD }) | ||
@Retention(RUNTIME) | ||
@interface InterceptMe { | ||
|
||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...ns/arc/runtime/src/main/java/io/quarkus/arc/runtime/InterceptedStaticMethodsRecorder.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,22 @@ | ||
package io.quarkus.arc.runtime; | ||
|
||
import io.quarkus.runtime.annotations.Recorder; | ||
|
||
@Recorder | ||
public class InterceptedStaticMethodsRecorder { | ||
|
||
public static final String INTIALIZER_CLASS_NAME = "io.quarkus.arc.runtime.InterceptedStaticMethodsInitializer"; | ||
|
||
public void callInitializer() { | ||
ClassLoader cl = Thread.currentThread().getContextClassLoader(); | ||
if (cl == null) { | ||
cl = InterceptedStaticMethodsRecorder.class.getClassLoader(); | ||
} | ||
try { | ||
Class.forName(INTIALIZER_CLASS_NAME, true, cl); | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.