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

Use Aspect Provider instead of Concrete Aspect Target #268

Merged
merged 3 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ List<MethodReader.MethodParam> initParams(List<? extends VariableElement> parame

void addTargets(Set<String> targets) {
for (AspectPair aspectPair : aspectPairs) {
targets.add(aspectPair.target());
targets.add(aspectPair.annotationShortName());
}
}

Expand Down Expand Up @@ -123,8 +123,7 @@ void writeSetupForMethods(Append writer, String shortName) {
}
writer.append(");").eol();
for (AspectPair aspect : aspectPairs) {
String target = aspect.target();
String name = aspectTargetShortName(target);
String name = Util.initLower(aspect.annotationShortName());
String sn = aspect.annotationShortName();
writer.append(" %s%s = %s.interceptor(%s, %s.getAnnotation(%s.class));", localName, sn, name, localName, localName, sn).eol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ String target() {
}

void addImports(ImportTypeMap importTypes) {
importTypes.add(target);
importTypes.add("io.avaje.inject.aop.AspectProvider");
importTypes.add(annotationFullName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,41 @@ final class BeanAspects {
static final BeanAspects EMPTY = new BeanAspects();

private final List<AspectMethod> aspectMethods;
private final Set<String> targets;
private final Set<String> aspectNames;

private BeanAspects() {
this.aspectMethods = Collections.emptyList();
this.targets = Collections.emptySet();
this.aspectNames = Collections.emptySet();
}

BeanAspects(List<AspectMethod> aspectMethods) {
this.aspectMethods = aspectMethods;
this.targets = initTargets();
this.aspectNames = initAspectNames();
}

boolean hasAspects() {
return !aspectMethods.isEmpty();
}

Set<String> targets() {
return targets;
Set<String> aspectNames() {
return aspectNames;
}

void extraImports(ImportTypeMap importTypes) {
for (AspectMethod aspectMethod : aspectMethods) {
for (final AspectMethod aspectMethod : aspectMethods) {
aspectMethod.addImports(importTypes);
}
}

Set<String> initTargets() {
Set<String> targets = new LinkedHashSet<>();
for (AspectMethod aspectMethod : aspectMethods) {
Set<String> initAspectNames() {
final Set<String> targets = new LinkedHashSet<>();
for (final AspectMethod aspectMethod : aspectMethods) {
aspectMethod.addTargets(targets);
}
return targets;
}

void writeFields(Append writer) {
for (String target : targets) {
String type = Util.shortName(target);
String name = Util.initLower(type);
writer.append(" private final %s %s;", type, name).eol();
}
writer.eol();
}

List<AspectMethod> methods() {
return aspectMethods;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ private void writeMethods() {
}

private void writeFields() {
aspects.writeFields(writer);
for (AspectMethod method : aspects.methods()) {
method.writeSetupFields(writer);
}
Expand All @@ -57,21 +56,17 @@ private void writeFields() {
private void writeConstructor() {
writer.append(" public %s%s(", shortName, suffix);
int count = 0;
for (String target : aspects.targets()) {
for (final String aspectName : aspects.aspectNames()) {
if (count++ > 0) {
writer.append(", ");
}
final String type = Util.shortName(target);
String name = Util.initLower(type);
final var type = "AspectProvider<" + aspectName + ">";
final var name = Util.initLower(aspectName);
writer.append(type).append(" ").append(name);
}
beanReader.writeConstructorParams(writer);
writer.append(") {").eol();
beanReader.writeConstructorInit(writer);
for (String target : aspects.targets()) {
String name = AspectMethod.aspectTargetShortName(target);
writer.append(" this.%s = %s;", name, name).eol();
}
writeSetupForMethods();
writer.append(" }").eol();
}
Expand Down
2 changes: 1 addition & 1 deletion inject/src/main/java/io/avaje/inject/aop/Aspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Specify the {@link AspectProvider} for this aspect.
*/
Class<?> target();
Class<? extends AspectProvider> target();

/**
* Specify the priority ordering when multiple aspects are on a method.
Expand Down