Skip to content

Commit f6e1a90

Browse files
authored
Merge pull request #335 from SentryMan/importAspect
Aspect.Import Annotation
2 parents 5c5d74b + db2636d commit f6e1a90

File tree

7 files changed

+72
-20
lines changed

7 files changed

+72
-20
lines changed

blackbox-aspect/src/main/java/org/example/external/aspect/MyExternalAspect.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package org.example.external.aspect;
22

3-
import io.avaje.inject.aop.Aspect;
4-
53
import java.lang.annotation.ElementType;
64
import java.lang.annotation.Retention;
75
import java.lang.annotation.RetentionPolicy;
86
import java.lang.annotation.Target;
97

10-
@Aspect
8+
119
@Target(ElementType.METHOD)
1210
@Retention(RetentionPolicy.RUNTIME)
1311
public @interface MyExternalAspect {

blackbox-test-inject/src/main/java/org/example/myapp/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.example.myapp;
22

3+
import org.example.external.aspect.MyExternalAspect;
4+
35
import io.avaje.inject.BeanScope;
6+
import io.avaje.inject.aop.Aspect.Import;
47

8+
@Import(MyExternalAspect.class)
59
public class Main {
610

711
public static void main(String[] args) {
@@ -10,7 +14,7 @@ public static void main(String[] args) {
1014

1115
HelloService helloService = beanScope.get(HelloService.class);
1216
String greeting = helloService.hello();
13-
System.out.println("Greeting: "+greeting);
17+
System.out.println("Greeting: " + greeting);
1418

1519
assert greeting.equals("hello+AppHelloData");
1620
}

inject-generator/src/main/java/io/avaje/inject/generator/ProcessingContext.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import java.nio.file.NoSuchFileException;
77
import java.util.ArrayList;
88
import java.util.Collections;
9+
import java.util.HashMap;
910
import java.util.HashSet;
1011
import java.util.LinkedHashSet;
1112
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Optional;
1215
import java.util.Set;
1316

1417
import javax.annotation.processing.Filer;
@@ -39,6 +42,7 @@ static final class Ctx {
3942
private final Set<String> uniqueModuleNames = new HashSet<>();
4043
private final Set<String> providedTypes = new HashSet<>();
4144
private final Set<String> optionalTypes = new LinkedHashSet<>();
45+
private final Map<String, AspectImportPrism> aspectImportPrisms = new HashMap<>();
4246

4347
public Ctx(ProcessingEnvironment processingEnv, Set<String> moduleFileProvided) {
4448

@@ -172,6 +176,14 @@ static void addOptionalType(String paramType) {
172176
}
173177
}
174178

179+
static void addImportedAspects(Map<String, AspectImportPrism> importedMap) {
180+
CTX.get().aspectImportPrisms.putAll(importedMap);
181+
}
182+
183+
static Optional<AspectImportPrism> getImportedAspect(String type) {
184+
return Optional.ofNullable(CTX.get().aspectImportPrisms.get(type));
185+
}
186+
175187
public static void clear() {
176188
CTX.remove();
177189
}

inject-generator/src/main/java/io/avaje/inject/generator/Processor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.avaje.inject.generator;
22

3+
import static io.avaje.inject.generator.ProcessingContext.addImportedAspects;
34
import static io.avaje.inject.generator.ProcessingContext.element;
45
import static io.avaje.inject.generator.ProcessingContext.loadMetaInfCustom;
56
import static io.avaje.inject.generator.ProcessingContext.loadMetaInfServices;
@@ -36,7 +37,8 @@
3637
Constants.SCOPE,
3738
Constants.TESTSCOPE,
3839
Constants.CONTROLLER,
39-
ImportPrism.PRISM_TYPE
40+
ImportPrism.PRISM_TYPE,
41+
AspectImportPrism.PRISM_TYPE
4042
})
4143
public final class Processor extends AbstractProcessor {
4244

@@ -97,6 +99,11 @@ private static String resource(Filer filer, String relativeName, String replace)
9799
@Override
98100
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
99101
readModule(roundEnv);
102+
final var importedAspects =
103+
roundEnv.getElementsAnnotatedWith(element(AspectImportPrism.PRISM_TYPE)).stream()
104+
.map(AspectImportPrism::getInstanceOn)
105+
.collect(Collectors.toMap(p -> p.value().toString(), p -> p));
106+
addImportedAspects(importedAspects);
100107
readScopes(roundEnv.getElementsAnnotatedWith(element(Constants.SCOPE)));
101108
readChangedBeans(roundEnv.getElementsAnnotatedWith(element(Constants.FACTORY)), true);
102109
if (defaultScope.includeSingleton()) {

inject-generator/src/main/java/io/avaje/inject/generator/TypeExtendsInjection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.avaje.inject.generator;
22

3+
import static io.avaje.inject.generator.ProcessingContext.getImportedAspect;
4+
35
import javax.lang.model.element.AnnotationMirror;
46
import javax.lang.model.element.Element;
57
import javax.lang.model.element.ExecutableElement;
@@ -60,6 +62,9 @@ List<AspectPair> readAspects(Element element) {
6062
final var aspect = AspectPrism.getInstanceOn(anElement);
6163
if (aspect != null) {
6264
aspects.add(new AspectPair(anElement, aspect.ordering()));
65+
} else {
66+
getImportedAspect(anElement.asType().toString())
67+
.ifPresent(p -> aspects.add(new AspectPair(anElement, p.ordering())));
6368
}
6469
}
6570
return aspects;

inject-generator/src/main/java/io/avaje/inject/generator/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
@GeneratePrism(Named.class)
1010
@GeneratePrism(Inject.class)
1111
@GeneratePrism(Aspect.class)
12+
@GeneratePrism(value = Aspect.Import.class, name = "AspectImportPrism")
1213
@GeneratePrism(Primary.class)
1314
@GeneratePrism(Secondary.class)
1415
@GeneratePrism(Proxy.class)
Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package io.avaje.inject.aop;
22

3+
import java.lang.annotation.Annotation;
34
import java.lang.annotation.ElementType;
45
import java.lang.annotation.Retention;
56
import java.lang.annotation.RetentionPolicy;
67
import java.lang.annotation.Target;
78

89
/**
910
* Meta annotation used to define an Aspect.
10-
* <p>
11-
* Create an annotation and annotate with {@code @Aspect} to define an aspect annotation. The
12-
* {@link Aspect#target()} specifies the associated type that implements {@link AspectProvider}.
13-
* The aspect provider should be a {@code @Singleton} such that registers with <em>avaje-inject</em>.
11+
*
12+
* <p>Create an annotation and annotate with {@code @Aspect} to define an aspect annotation. The
13+
* associated type that implements {@link AspectProvider} will be used as the target class. The
14+
* aspect provider should be a {@code @Singleton} such that registers with <em>avaje-inject</em>.
15+
*
1416
* <p>
1517
*/
1618
@Target(ElementType.ANNOTATION_TYPE)
@@ -19,20 +21,43 @@
1921

2022
/**
2123
* Specify the priority ordering when multiple aspects are on a method.
22-
* <p>
23-
* When multiple aspects are on a method they are nested. The highest
24-
* ordering value will be the outer-most aspect, the lowest ordering will
25-
* be the inner-most aspect.
26-
* <p>
27-
* The outer-most aspect will have it's <em>before</em> executed first,
28-
* followed by the <em>before</em> of the inner nested aspects ultimately
29-
* down the invocation of the target method.
30-
* <p>
31-
* The reverse ordering occurs for <em>after</em> with the outer-most aspect
32-
* having it's <em>after</em> executed last.
24+
*
25+
* <p>When multiple aspects are on a method they are nested. The highest ordering value will be
26+
* the outer-most aspect, the lowest ordering will be the inner-most aspect.
27+
*
28+
* <p>The outer-most aspect will have it's <em>before</em> executed first, followed by the
29+
* <em>before</em> of the inner nested aspects ultimately down the invocation of the target
30+
* method.
31+
*
32+
* <p>The reverse ordering occurs for <em>after</em> with the outer-most aspect having it's
33+
* <em>after</em> executed last.
3334
*
3435
* @return The ordering of this aspect. High value for outer-most aspect.
3536
*/
3637
int ordering() default 1000;
3738

39+
/** Marks an External Annotation as being used for aspects */
40+
@Target({ElementType.PACKAGE, ElementType.TYPE})
41+
@Retention(RetentionPolicy.SOURCE)
42+
public @interface Import {
43+
44+
Class<? extends Annotation> value();
45+
46+
/**
47+
* Specify the priority ordering when multiple aspects are on a method.
48+
*
49+
* <p>When multiple aspects are on a method they are nested. The highest ordering value will be
50+
* the outer-most aspect, the lowest ordering will be the inner-most aspect.
51+
*
52+
* <p>The outer-most aspect will have it's <em>before</em> executed first, followed by the
53+
* <em>before</em> of the inner nested aspects ultimately down the invocation of the target
54+
* method.
55+
*
56+
* <p>The reverse ordering occurs for <em>after</em> with the outer-most aspect having it's
57+
* <em>after</em> executed last.
58+
*
59+
* @return The ordering of this aspect. High value for outer-most aspect.
60+
*/
61+
int ordering() default 1000;
62+
}
3863
}

0 commit comments

Comments
 (0)