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

Add Aspect Auto Requires #271

Merged
merged 4 commits into from
Feb 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ List<AspectPair> read() {
if (aspect != null) {
Meta meta = readTarget(anElement);
if (meta != null) {
aspects.add(new AspectPair(anElement, meta.target, meta.ordering));
aspects.add(new AspectPair(anElement, meta.ordering));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ private void invokeSuper(Append writer, String simpleName) {
}

void writeSetupFields(Append writer) {
writer.append(" private Method %s;", localName).eol();
writer.append(" private final Method %s;", localName).eol();
for (AspectPair aspectPair : aspectPairs) {
String sn = aspectPair.annotationShortName();
writer.append(" private MethodInterceptor %s%s;", localName, sn).eol();
writer.append(" private final MethodInterceptor %s%s;", localName, sn).eol();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@

final class AspectPair implements Comparable<AspectPair> {

private final String target;
private final int ordering;
private final String annotationFullName;
private final String annotationShortName;

AspectPair(Element anElement, String target, int ordering) {
this.target = target;
AspectPair(Element anElement, int ordering) {
this.ordering = ordering;
this.annotationFullName = anElement.asType().toString();
this.annotationShortName = Util.shortName(annotationFullName);
}

String target() {
return target;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class Constants {

static final String REFLECT_METHOD = "java.lang.reflect.Method";
static final String ASPECT = "io.avaje.inject.aop.Aspect";
static final String ASPECT_PROVIDER ="io.avaje.inject.aop.AspectProvider";
static final String INVOCATION = "io.avaje.inject.aop.Invocation";
static final String INVOCATION_EXCEPTION = "io.avaje.inject.aop.InvocationException";
static final String METHOD_INTERCEPTOR = "io.avaje.inject.aop.MethodInterceptor";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ void init() {
for (final Class<?> provide : module.provides()) {
providedTypes.add(provide.getCanonicalName());
}
for (Class<?> provide : module.autoProvides()) {
for (final Class<?> provide : module.autoProvides()) {
providedTypes.add(provide.getCanonicalName());
}
for (final Class<?> provide : module.autoProvidesAspects()) {
providedTypes.add(Constants.ASPECT_PROVIDER + "<" + provide.getCanonicalName() + ">");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ final class MetaDataOrdering {
private final Map<String, ProviderList> providers = new HashMap<>();
private final List<DependencyLink> circularDependencies = new ArrayList<>();
private final Set<String> missingDependencyTypes = new LinkedHashSet<>();
private final Set<String> autoRequires = new TreeSet<>();
private final Set<String> autoRequires = new TreeSet<>();
private final Set<String> autoRequiresAspects = new TreeSet<>();


MetaDataOrdering(Collection<MetaData> values, ProcessingContext context, ScopeInfo scopeInfo) {
this.context = context;
Expand Down Expand Up @@ -190,17 +192,19 @@ private boolean allDependenciesWired(MetaData queuedMeta, boolean includeExterna
if (providerList == null) {
if (!scopeInfo.providedByOther(dependency)) {
if (includeExternal && context.externallyProvided(dependency.name())) {
autoRequires.add(dependency.name());
if (Util.isAspectProvider(dependency.name())) {
autoRequiresAspects.add(Util.extractAspectType(dependency.name()));
} else {
autoRequires.add(dependency.name());
}
queuedMeta.markWithExternalDependency();
} else {
return false;
}
}
} else {
if (!providerList.isAllWired()) {
return false;
}
}
} else if (!providerList.isAllWired()) {
return false;
}
}
}
return true;
Expand All @@ -210,6 +214,10 @@ Set<String> autoRequires() {
return autoRequires;
}

Set<String> autoRequiresAspects() {
return autoRequiresAspects;
}

List<MetaData> ordered() {
return orderedList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ void buildAutoRequires(Append writer, Set<String> autoRequires) {
}
}

void buildAutoRequiresAspects(Append writer, Set<String> autoRequires) {
autoRequires.removeAll(requires);
if (!autoRequires.isEmpty()) {
buildProvidesMethod(writer, "autoRequiresAspects", autoRequires);
}
}

void readModuleMetaData(TypeElement moduleType) {
InjectModule module = moduleType.getAnnotation(InjectModule.class);
details(module.name(), moduleType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ private void writeProvides() {
if (!autoRequires.isEmpty()) {
scopeInfo.buildAutoRequires(writer, autoRequires);
}

Set<String> autoRequiresAspects = ordering.autoRequiresAspects();
if (!autoRequires.isEmpty()) {
scopeInfo.buildAutoRequiresAspects(writer, autoRequiresAspects);
}
}

private void writeClassesMethod() {
Expand Down
26 changes: 22 additions & 4 deletions inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,27 @@ private int processQueuedFactories() {
*/
private boolean satisfiedDependencies(FactoryState factory) {
return satisfiedDependencies(factory.requires())
&& satisfiedDependencies(factory.requiresPackages())
&& satisfiedDependencies(factory.autoRequires());
&& satisfiedDependencies(factory.requiresPackages())
&& satisfiedAspectDependencies(factory.autoRequiresAspects())
&& satisfiedDependencies(factory.autoRequires());
}

private boolean satisfiedDependencies(Class<?>[] requires) {
for (Class<?> dependency : requires) {
if (notProvided(dependency.getTypeName())) {
for (final Class<?> dependency : requires) {
if (notProvided(dependency.getTypeName())) {
return false;
}
}
return true;
}

private boolean satisfiedAspectDependencies(Class<?>[] requiresAspects) {
for (final Class<?> dependency : requiresAspects) {

if (notProvided(
"interface io.avaje.inject.aop.AspectProvider<"
+ dependency.getCanonicalName()
+ ">")) {
return false;
}
}
Expand Down Expand Up @@ -395,6 +409,10 @@ Class<?>[] autoRequires() {
return factory.autoRequires();
}

Class<?>[] autoRequiresAspects() {
return factory.autoRequiresAspects();
}

@Override
public String toString() {
return factory.getClass().getTypeName();
Expand Down
10 changes: 9 additions & 1 deletion inject/src/main/java/io/avaje/inject/spi/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface Module {
/**
* Empty array of classes.
*/
Class<?>[] EMPTY_CLASSES = new Class<?>[]{};
Class<?>[] EMPTY_CLASSES = {};

/**
* Return the set of types this module explicitly provides to other modules.
Expand Down Expand Up @@ -63,6 +63,14 @@ default Class<?>[] autoProvidesAspects() {
default Class<?>[] autoRequires() {
return EMPTY_CLASSES;
}
/**
* These are the apects that this module requires whose implementations are provided by other external
* modules (that are in the classpath at compile time).
*
*/
default Class<?>[] autoRequiresAspects() {
return EMPTY_CLASSES;
}

/**
* Return public classes of the beans that would be registered by this module.
Expand Down