Skip to content

Update Aspect generation - add aspectProvides() meta data #270

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

Merged
merged 11 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
38 changes: 38 additions & 0 deletions blackbox-aspect/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>avaje-inject-parent</artifactId>
<groupId>io.avaje</groupId>
<version>8.11</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>blackbox-aspect</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<!-- annotation processor -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.example.external.aspect;

import io.avaje.inject.aop.Aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Aspect
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyExternalAspect {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@InjectModule(name = "example-external-aspect")
package org.example.external.aspect;

import io.avaje.inject.InjectModule;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example.external.aspect.sub;

import io.avaje.inject.Component;
import io.avaje.inject.aop.AspectProvider;
import io.avaje.inject.aop.Invocation;
import io.avaje.inject.aop.MethodInterceptor;
import org.example.external.aspect.MyExternalAspect;

import java.lang.reflect.Method;
import java.util.Arrays;

@Component
class MyAspectImplementation implements AspectProvider<MyExternalAspect>, MethodInterceptor {

@Override
public MethodInterceptor interceptor(Method method, MyExternalAspect aspectAnnotation) {
return this;
}

@Override
public void invoke(Invocation invoke) throws Throwable {
System.out.println("before args: " + Arrays.toString(invoke.arguments()) + " method: " + invoke.method());
try {
invoke.invoke();
} finally {
System.out.println("after method: " + invoke.method());
}
}
}
6 changes: 6 additions & 0 deletions blackbox-test-inject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

<dependencies>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>blackbox-aspect</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.example.myapp;

import io.avaje.inject.Component;
import org.example.external.aspect.MyExternalAspect;

@Component
public class SomeExternalAspectUser {

@MyExternalAspect
public String hello() {
return "hello";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Aspect(target = MyAroundAspect.class)
@Aspect
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAround {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.List;

@Singleton
//@Aspect(target = MyAround.class)
public class MyAroundAspect implements AspectProvider<MyAround>, MethodInterceptor {

private final List<String> trace = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ MyInterface myInterface2() {
return new MyInterface() {};
}

@Bean
MyAbstract myAbstract() {
return new MyAbstract() {};
}

public static class Builder {
}

Expand Down Expand Up @@ -104,6 +109,10 @@ public MyPrim(String val) {
}
}

public abstract class MyAbstract {

}

@Component
public static class BuilderUser {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.example.myapp;

import io.avaje.inject.BeanScope;
import io.avaje.inject.test.TestBeanScope;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class SomeExternalAspectUserTest {

@Test
void hello() {
try (BeanScope scope = TestBeanScope.builder().build()) {
var some = scope.get(SomeExternalAspectUser.class);
assertThat(some.hello()).isEqualTo("hello");
}
}
}
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 All @@ -44,9 +44,7 @@ private Meta readTarget(Element anElement) {
Meta meta = new Meta();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) {
String key = entry.getKey().toString();
if (key.equals("target()")) {
meta.target(entry.getValue().getValue().toString());
} else if (key.equals("ordering()")) {
if (key.equals("ordering()")) {
meta.ordering(Integer.parseInt(entry.getValue().getValue().toString()));
}
}
Expand All @@ -59,21 +57,15 @@ private Meta readTarget(Element anElement) {

private static class Meta {

String target;

int ordering = Constants.ORDERING_DEFAULT;

void target(String target) {
this.target = target;
}

void ordering(int ordering) {
this.ordering = ordering;
}

@Override
public String toString() {
return "Meta(target=" + target + ", ordering=" + ordering + ')';
return "Meta(ordering=" + ordering + ')';
}
}
}
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 @@ -119,6 +119,10 @@ String autoProvides() {
return typeReader.autoProvides();
}

String providesAspect() {
return typeReader.providesAspect();
}

Set<GenericType> allGenericTypes() {
if (allGenericTypes != null) {
return allGenericTypes;
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 @@ -2,9 +2,7 @@

import io.avaje.inject.spi.Module;

import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.*;

/**
* The types provided by other modules in the classpath at compile time.
Expand All @@ -16,13 +14,22 @@ final class ExternalProvider {
private final Set<String> providedTypes = new HashSet<>();

void init() {
for (final Module module :
ServiceLoader.load(Module.class, ExternalProvider.class.getClassLoader())) {
for (final Class<?> provide : module.provides()) {
providedTypes.add(provide.getCanonicalName());
}
for (Class<?> provide : module.autoProvides()) {
providedTypes.add(provide.getCanonicalName());
ServiceLoader<Module> load = ServiceLoader.load(Module.class, ExternalProvider.class.getClassLoader());
Iterator<Module> iterator = load.iterator();
while (iterator.hasNext()) {
try {
Module module = iterator.next();
for (final Class<?> provide : module.provides()) {
providedTypes.add(provide.getCanonicalName());
}
for (Class<?> provide : module.autoProvides()) {
providedTypes.add(provide.getCanonicalName());
}
for (final Class<?> provide : module.autoProvidesAspects()) {
providedTypes.add(Util.wrapAspect(provide.getCanonicalName()));
}
} catch (ServiceConfigurationError expected) {
// ignore expected error reading the module that we are also writing
}
}
}
Expand Down
Loading