Skip to content

Commit 6da5afb

Browse files
committed
Merge branch 'wip/aspect2' into jakarta-master
2 parents 71359ea + 7ea400b commit 6da5afb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2559
-156
lines changed

blackbox-test-inject/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>avaje-inject-parent</artifactId>
77
<groupId>io.avaje</groupId>
8-
<version>6.17</version>
8+
<version>6.18-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

@@ -24,6 +24,18 @@
2424
<version>${project.version}</version>
2525
</dependency>
2626

27+
<dependency>
28+
<groupId>io.github.resilience4j</groupId>
29+
<artifactId>resilience4j-retry</artifactId>
30+
<version>1.7.1</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>io.github.resilience4j</groupId>
35+
<artifactId>resilience4j-annotations</artifactId>
36+
<version>1.7.1</version>
37+
</dependency>
38+
2739
<!-- annotation processor -->
2840
<dependency>
2941
<groupId>io.avaje</groupId>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.example.myapp;
2+
3+
import jakarta.inject.Singleton;
4+
import org.example.myapp.aspect.MyTimed;
5+
6+
import java.io.IOException;
7+
8+
@MyTimed
9+
@Singleton
10+
public class ExampleService {
11+
12+
final HelloService helloService;
13+
14+
public ExampleService(HelloService helloService) {
15+
this.helloService = helloService;
16+
}
17+
18+
public String other(String param0, int param1) {//} throws IOException, IllegalStateException {
19+
return "other " + param0 + " " + param1;
20+
}
21+
22+
public void runOnly(String param) {
23+
System.out.println("runOnly "+param);
24+
}
25+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,97 @@
11
package org.example.myapp;
22

3+
import io.avaje.inject.PostConstruct;
4+
import io.avaje.inject.PreDestroy;
35
import jakarta.inject.Singleton;
6+
import org.example.myapp.aspect.*;
7+
8+
import java.io.IOException;
9+
import java.io.UncheckedIOException;
410

511
@Singleton
612
public class HelloService {
713

814
private final HelloData data;
915

16+
private int counter;
17+
private String justRunResult;
18+
1019
HelloService(HelloData data) {
1120
this.data = data;
1221
}
1322

1423
public String hello() {
1524
return "hello+" + data.helloData();
1625
}
26+
27+
@MySkip
28+
public String skipExample(String p0) {
29+
// this is never called due to MySkip aspect
30+
return p0;
31+
}
32+
33+
@MyMultiInvoke
34+
int counter() {
35+
return counter++;
36+
}
37+
38+
@MyBefore
39+
public String foo(String param, int overloaded) {
40+
return "foo+" + param;
41+
}
42+
43+
@MyBefore
44+
public String foo(String param) {
45+
return "foo+" + param;
46+
}
47+
48+
@MyAround(name="what")
49+
public String bazz(String param0, int param1) throws IOException {
50+
System.out.println("execute bazz ...");
51+
return "bazz " + param0 + " " + param1;
52+
}
53+
54+
@MyAround
55+
public void justRun(String param0, int param1, int param2) throws IOException, ClassNotFoundException, UncheckedIOException {
56+
System.out.println("justRun ...");
57+
justRunResult = param0+" "+param1+" "+param2;
58+
}
59+
60+
@MyThrowing
61+
public void thisWillThrow() {
62+
System.out.println("this just never gets called");
63+
throw new IllegalCallerException("never happens");
64+
}
65+
66+
@MyAround
67+
public void appCodeThrowsUnchecked() {
68+
throw new IllegalArgumentException("appCodeUnchecked");
69+
}
70+
71+
/**
72+
* Declared exception is NOT wrapped in InvocationException
73+
*/
74+
@MyAround
75+
public void appCodeThrowsDeclared() throws IllegalArgumentException {
76+
throw new IllegalArgumentException("appCodeDeclared");
77+
}
78+
79+
public void multiAspectsHere() {
80+
81+
}
82+
83+
84+
public String justRunResult() {
85+
return justRunResult;
86+
}
87+
88+
@PostConstruct
89+
void postCon() {
90+
91+
}
92+
93+
@PreDestroy
94+
void preDest() {
95+
96+
}
1797
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.example.myapp;
2+
3+
import io.avaje.inject.aop.MethodInterceptor;
4+
import io.avaje.inject.aop.Invocation;
5+
import org.example.myapp.aspect.MyAround;
6+
import org.example.myapp.aspect.MyAroundAspect;
7+
import org.example.myapp.aspect.MyBeforeAspect;
8+
9+
import java.lang.reflect.Method;
10+
11+
//@Proxy
12+
//@Singleton
13+
public class HelloServiceProxy extends HelloService {
14+
15+
final MyBeforeAspect aspect;
16+
final MyAroundAspect aroundAspect;
17+
private final Method fooMethod;
18+
private final Method bazzMethod;
19+
private final MyAround myArround;
20+
private final MethodInterceptor bazzInterceptor;
21+
22+
public HelloServiceProxy(HelloData data, MyBeforeAspect aspect, MyAroundAspect aroundAspect) {
23+
super(data);
24+
this.aspect = aspect;
25+
this.aroundAspect = aroundAspect;
26+
try {
27+
fooMethod = HelloService.class.getDeclaredMethod("foo", String.class);
28+
bazzMethod = HelloService.class.getDeclaredMethod("bazz", String.class, int.class);
29+
myArround = bazzMethod.getAnnotation(MyAround.class);
30+
31+
32+
bazzInterceptor = aroundAspect.interceptor(bazzMethod, myArround);
33+
34+
} catch (Exception e) {
35+
throw new IllegalStateException(e);
36+
}
37+
}
38+
39+
@Override
40+
public String foo(String param) {
41+
//aspect.beforeInvoke(fooMethod);
42+
return super.foo(param);
43+
}
44+
45+
@Override
46+
public String bazz(String param0, int param1) {
47+
var call = new Invocation.Call<>(() -> super.bazz(param0, param1))
48+
.with(this, bazzMethod, param0, param1);
49+
try {
50+
bazzInterceptor.invoke(call);
51+
//aroundAspect.around3(call);
52+
return call.finalResult();
53+
} catch (Throwable e) {
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example.myapp;
2+
3+
import jakarta.inject.Singleton;
4+
import org.example.myapp.aspect.MyAround;
5+
import org.example.myapp.aspect.MyTimed;
6+
7+
@Singleton
8+
public class OtherService {
9+
10+
@MyAround
11+
public String other(String param0, int param1) {
12+
return "other " + param0 + " " + param1;
13+
}
14+
15+
@MyAround
16+
@MyTimed
17+
public void multi() {
18+
System.out.println("sdsd");
19+
}
20+
21+
public void notAopWrapped() {
22+
System.out.println("just a normal method");
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.example.myapp.aspect;
2+
3+
import io.avaje.inject.aop.Aspect;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Aspect(target = MyAroundAspect.class)
11+
@Target(ElementType.METHOD)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface MyAround {
14+
15+
String name() default "";
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.example.myapp.aspect;
2+
3+
import io.avaje.inject.aop.AspectProvider;
4+
import io.avaje.inject.aop.Invocation;
5+
import io.avaje.inject.aop.MethodInterceptor;
6+
import jakarta.inject.Singleton;
7+
8+
import java.lang.reflect.Method;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
@Singleton
14+
//@Aspect(target = MyAround.class)
15+
public class MyAroundAspect implements AspectProvider<MyAround>, MethodInterceptor {
16+
17+
private final List<String> trace = new ArrayList<>();
18+
19+
@Override
20+
public MethodInterceptor interceptor(Method method, MyAround around) {
21+
return this;
22+
}
23+
24+
@Override
25+
public void invoke(Invocation invoke) throws Throwable {
26+
TraceAspect.add("MyAroundAspect-begin");
27+
trace.add(invoke.method().getName() + " args:" + Arrays.toString(invoke.arguments()));
28+
System.out.println("before args: " + Arrays.toString(invoke.arguments()) + " method: " + invoke.method());
29+
try {
30+
invoke.invoke();
31+
invoke.invoke();
32+
invoke.invoke();
33+
} finally {
34+
System.out.println("after");
35+
TraceAspect.add("MyAroundAspect-end");
36+
}
37+
}
38+
39+
public List<String> trace() {
40+
ArrayList<String> copy = new ArrayList<>(trace);
41+
trace.clear();
42+
return copy;
43+
}
44+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.example.myapp.aspect;
2+
3+
import io.avaje.inject.aop.Aspect;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Aspect(target = MyBeforeAspect.class)
11+
@Target(ElementType.METHOD)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface MyBefore {
14+
15+
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.example.myapp.aspect;
2+
3+
import io.avaje.inject.aop.AspectProvider;
4+
import io.avaje.inject.aop.Invocation;
5+
import io.avaje.inject.aop.MethodInterceptor;
6+
import jakarta.inject.Singleton;
7+
8+
import java.lang.reflect.Method;
9+
10+
@Singleton
11+
public class MyBeforeAspect implements AspectProvider<MyBefore> {
12+
13+
@Override
14+
public MethodInterceptor interceptor(Method method, MyBefore aspectAnnotation) {
15+
return new Intercept(method);
16+
}
17+
18+
static final class Intercept implements MethodInterceptor {
19+
final Method method;
20+
21+
Intercept(Method method) {
22+
this.method = method;
23+
}
24+
25+
@Override
26+
public void invoke(Invocation invocation) throws Throwable {
27+
if (method.getName().equals("foo")) {
28+
throw new IllegalStateException("Can't call this method");
29+
}
30+
}
31+
}
32+
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.myapp.aspect;
2+
3+
import io.avaje.inject.aop.Aspect;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Aspect(target = MyMultiInvokeAspect.class)
11+
@Target(ElementType.METHOD)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface MyMultiInvoke {
14+
}

0 commit comments

Comments
 (0)