Skip to content

Commit

Permalink
#312 Follow up for @requires - read repeatable container annotations
Browse files Browse the repository at this point in the history
- Read the repeatable Container annotations
- Move the annotation reading into BeanConditions
- Rename the inner repeatable annotations to Container (so IDE autocompletion
  for Requires* only sees our 2 @RequiresBean and @RequiresProperty annotations
  • Loading branch information
rbygrave committed Mar 10, 2023
1 parent eeec5a2 commit 10757ca
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@Factory
@RequiresProperty(value = "factory", equalTo = "bird", missingProperties = "neverExisted")
@RequiresProperty(value = "somethingElse", notEqualTo = "testRepeatable")
public class BirdFactory {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.example.myapp.conditional;

import io.avaje.inject.RequiresBean;
import jakarta.inject.Singleton;

import java.awt.*;
import java.util.Properties;

@Singleton
@RequiresBean(missingBeans = Properties.class)
@RequiresBean(missingBeans = SystemColor.class)
@RequiresBird
public class BirdWatcher {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.avaje.inject.generator;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import java.util.*;

final class BeanConditions {
Expand All @@ -12,13 +14,38 @@ final class BeanConditions {
final Map<String, String> propertyEquals = new HashMap<>();
final Map<String, String> propertyNotEquals = new HashMap<>();

void read(RequiresBeanPrism prism) {

void readAll(Element element) {
readMetaAnnotations(element);
readAllDirect(element);
}

private void readAllDirect(Element element) {
RequiresBeanPrism.getAllInstancesOn(element).forEach(this::read);
RequiresBeanContainerPrism.getOptionalOn(element).ifPresent( container -> {
container.value().forEach(this::read);
});
RequiresPropertyPrism.getAllInstancesOn(element).forEach(this::read);
RequiresPropertyContainerPrism.getOptionalOn(element).ifPresent( container -> {
container.value().forEach(this::read);
});
}

private void readMetaAnnotations(Element element) {
element.getAnnotationMirrors().forEach(this::findRequiresOnAnnotation);
}

private void findRequiresOnAnnotation(AnnotationMirror a) {
readAllDirect(a.getAnnotationType().asElement());
}

private void read(RequiresBeanPrism prism) {
prism.value().forEach(t -> requireTypes.add(t.toString()));
prism.missingBeans().forEach(t -> missingTypes.add(t.toString()));
qualifierNames.addAll(prism.qualifiers());
}

void read(RequiresPropertyPrism prism) {
private void read(RequiresPropertyPrism prism) {
if (!prism.value().isBlank()) {
if (!prism.notEqualTo().isBlank()) {
propertyNotEquals.put(prism.value(), prism.notEqualTo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ final class BeanReader {
this.proxy = ProxyPrism.isPresent(beanType);
this.typeReader = new TypeReader(GenericType.parse(type), beanType, importTypes, factory);

beanType.getAnnotationMirrors().forEach(this::findRequiresOnAnnotation);

RequiresBeanPrism.getAllInstancesOn(beanType).forEach(this::processBeanPrism);
RequiresPropertyPrism.getAllInstancesOn(beanType).forEach(this::processPropertyPrism);
conditions.readAll(beanType);

typeReader.process();
this.requestParams = new BeanRequestParams(type);
Expand All @@ -66,21 +63,6 @@ public String toString() {
return beanType.toString();
}

void processBeanPrism(RequiresBeanPrism prism) {
conditions.read(prism);
}

void processPropertyPrism(RequiresPropertyPrism prism) {
conditions.read(prism);
}

private void findRequiresOnAnnotation(AnnotationMirror a) {
final var annotationElement = a.getAnnotationType().asElement();

RequiresBeanPrism.getAllInstancesOn(annotationElement).forEach(this::processBeanPrism);
RequiresPropertyPrism.getAllInstancesOn(annotationElement).forEach(this::processPropertyPrism);
}

TypeElement beanType() {
return beanType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ final class MethodReader {
primary = PrimaryPrism.isPresent(element);
secondary = SecondaryPrism.isPresent(element);

element.getAnnotationMirrors().forEach(this::findRequiresOnAnnotation);
RequiresBeanPrism.getAllInstancesOn(element).forEach(this::processBeanPrism);
RequiresPropertyPrism.getAllInstancesOn(element).forEach(this::processPropertyPrism);
conditions.readAll(element);

} else {
prototype = false;
Expand Down Expand Up @@ -98,21 +96,6 @@ public String toString() {
'}';
}

void processBeanPrism(RequiresBeanPrism prism) {
conditions.read(prism);
}

void processPropertyPrism(RequiresPropertyPrism prism) {
conditions.read(prism);
}

private void findRequiresOnAnnotation(AnnotationMirror a) {
final var annotationElement = a.getAnnotationType().asElement();

RequiresBeanPrism.getAllInstancesOn(annotationElement).forEach(this::processBeanPrism);
RequiresPropertyPrism.getAllInstancesOn(annotationElement).forEach(this::processPropertyPrism);
}

void addDependsOnGeneric(Set<GenericType> set) {
for (MethodParam param : params) {
param.addDependsOnGeneric(set);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@
@GeneratePrism(Generated.class)
@GeneratePrism(RequiresBean.class)
@GeneratePrism(RequiresProperty.class)
@GeneratePrism(value = RequiresBean.Container.class, name = "RequiresBeanContainerPrism")
@GeneratePrism(value = RequiresProperty.Container.class, name = "RequiresPropertyContainerPrism")
package io.avaje.inject.generator;

import io.avaje.inject.Bean;
import io.avaje.inject.Component;
import io.avaje.inject.Factory;
import io.avaje.inject.InjectModule;
import io.avaje.inject.Primary;
import io.avaje.inject.Prototype;
import io.avaje.inject.QualifiedMap;
import io.avaje.inject.RequiresBean;
import io.avaje.inject.RequiresProperty;
import io.avaje.inject.Secondary;
import io.avaje.inject.*;
import io.avaje.inject.aop.Aspect;
import io.avaje.inject.spi.DependencyMeta;
import io.avaje.inject.spi.Generated;
Expand Down
4 changes: 2 additions & 2 deletions inject/src/main/java/io/avaje/inject/RequiresBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* OtherService} is already registered in the {@link BeanScope}.
*/
@Retention(RUNTIME)
@Repeatable(RequiresBean.RequireBeans.class)
@Repeatable(RequiresBean.Container.class)
@Target({TYPE, METHOD, ANNOTATION_TYPE})
public @interface RequiresBean {

Expand All @@ -55,7 +55,7 @@
String[] qualifiers() default {};
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@interface RequireBeans {
@interface Container {

/** @return The required dependencies */
RequiresBean[] value();
Expand Down
4 changes: 2 additions & 2 deletions inject/src/main/java/io/avaje/inject/RequiresProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* system properties / Avaje Config.
*/
@Retention(RUNTIME)
@Repeatable(RequiresProperty.RequireProperties.class)
@Repeatable(RequiresProperty.Container.class)
@Target({TYPE, METHOD, ANNOTATION_TYPE})
public @interface RequiresProperty {

Expand Down Expand Up @@ -64,7 +64,7 @@

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@interface RequireProperties {
@interface Container {

/** @return The required dependencies */
RequiresProperty[] value();
Expand Down

0 comments on commit 10757ca

Please sign in to comment.