Skip to content
Closed
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 @@ -7,14 +7,15 @@
import org.jspecify.annotations.Nullable;

import java.io.Serializable;
import java.util.GregorianCalendar;
import java.util.Optional;

@Factory
public class LimitedFactory {

@Bean
@Named("factory")
@BeanTypes(LimitedInterface.class)
@BeanTypes(value = LimitedInterface.class, registerTypes = GregorianCalendar.class)
BeanTypeComponent bean() {
return new BeanTypeComponent();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.example.myapp.lazy.generic;

import java.util.Calendar;
import java.util.concurrent.atomic.AtomicBoolean;

import io.avaje.inject.BeanScope;
Expand All @@ -13,7 +14,7 @@
@Lazy
@Singleton
@Named("single")
@BeanTypes(LazyGenericInterface.class)
@BeanTypes(value = LazyGenericInterface.class, registerTypes = Calendar.class)
public class LazyGenericImpl implements LazyGenericInterface<String> {

AtomicBoolean initialized;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

import org.example.myapp.lazy.generic.LazyGenericImpl;
import org.junit.jupiter.api.Test;

import io.avaje.inject.BeanScope;

import java.util.Calendar;
import java.util.GregorianCalendar;

class BeanTypesTest {

@Test
Expand All @@ -17,6 +21,14 @@ void testBeanTypesRestrictingInjection() {
assertThat(scope.get(AbstractSuperClass.class)).isNotNull();
assertThat(scope.get(LimitedInterface.class)).isNotNull();
assertThat(scope.get(CharSequence.class)).isEqualTo("IAmNullable");

Object extraTypeGregorianCalendar = scope.get(Calendar.class);
assertThat(extraTypeGregorianCalendar).isNotNull();
assertThat(extraTypeGregorianCalendar).isInstanceOf(LazyGenericImpl.class);

Object extraTypeCalendar = scope.get(GregorianCalendar.class);
assertThat(extraTypeCalendar).isNotNull();
assertThat(extraTypeCalendar).isInstanceOf(BeanTypeComponent.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class AssistBeanReader {
this.beanType = beanType;
this.type = beanType.getQualifiedName().toString();
this.typeReader =
new TypeReader(List.of(), UType.parse(beanType.asType()), beanType, importTypes, false);
new TypeReader(List.of(), List.of(), UType.parse(beanType.asType()), beanType, importTypes, false);

typeReader.process();
qualifierName = typeReader.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,22 @@ final class BeanReader {
this.primary = PrimaryPrism.isPresent(actualType);
this.secondary = !primary && SecondaryPrism.isPresent(actualType);
this.priority = Util.priority(actualType);

var beanTypes =
BeanTypesPrism.getOptionalOn(actualType)
.map(BeanTypesPrism::value)
.or(() -> proxy ? Optional.of(List.of(actualType.asType())) : Optional.empty());
beanTypes.ifPresent(t -> Util.validateBeanTypes(actualType, t));

var extraTypes =
BeanTypesPrism.getOptionalOn(actualType)
.map(BeanTypesPrism::registerTypes)
.orElse(List.of());

this.typeReader =
new TypeReader(
beanTypes.orElse(List.of()),
extraTypes,
UType.parse(beanType.asType()),
beanType,
importTypes,
Expand Down Expand Up @@ -149,7 +157,12 @@ private boolean shouldDelay() {
.stream()
.flatMap(List::stream);

return Stream.of(constructors, fields, methods, interfaces, superClass, beanTypes)
var beanRegisterTypes = BeanTypesPrism.getOptionalOn(beanType)
.map(BeanTypesPrism::registerTypes)
.stream()
.flatMap(List::stream);

return Stream.of(constructors, fields, methods, interfaces, superClass, beanTypes, beanRegisterTypes)
.flatMap(s -> s)
.map(TypeMirror::getKind)
.anyMatch(TypeKind.ERROR::equals);
Expand Down Expand Up @@ -356,6 +369,10 @@ void buildBeanAbsent(Append writer) {
}
writer.append(typeReader.typesRegister());
writer.append(")) {").eol();
String extraTypesRegister = typeReader.extraTypesRegister();
if (extraTypesRegister != null) {
writer.append(" builder.registerTypes(%s);", extraTypesRegister).eol();
}
}

void buildRegister(Append writer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ final class MethodReader {
} else {
var beanTypes = BeanTypesPrism.getOptionalOn(element).map(BeanTypesPrism::value);
beanTypes.ifPresent(t -> Util.validateBeanTypes(element, t));

var extraTypes = BeanTypesPrism.getOptionalOn(element)
.map(BeanTypesPrism::registerTypes)
.orElse(List.of());

this.typeReader =
new TypeReader(
beanTypes.orElse(List.of()), genericType, returnElement, importTypes, element);
beanTypes.orElse(List.of()), extraTypes, genericType, returnElement, importTypes, element);
typeReader.process();
MethodLifecycleReader lifecycleReader = new MethodLifecycleReader(returnElement, initMethod, destroyMethod);
this.initMethod = lifecycleReader.initMethod();
Expand Down Expand Up @@ -475,6 +480,12 @@ void buildAddFor(Append writer) {
}
}
writer.append(")) {").eol();
if (typeReader != null) {
String extraTypesRegister = typeReader.extraTypesRegister();
if (extraTypesRegister != null) {
writer.append(" builder.registerTypes(%s);", extraTypesRegister).eol();
}
}
}

boolean methodThrows() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ private static Stream<UType> allComponentTypes(UType u) {
Stream.of(u), componentTypes.stream().flatMap(TypeAppender::allComponentTypes));
}

void add(List<UType> sourceTypes) {
TypeAppender add(List<UType> sourceTypes) {
sourceTypes.forEach(this::add);
return this;
}

void addSimpleType(String classType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,41 @@ final class TypeReader {
private final TypeAnnotationReader annotationReader;
private Set<UType> genericTypes;
private String typesRegister;
private String extraTypesRegister;
private final List<UType> injectsTypes;
private final List<UType> extraTypes;

TypeReader(
List<TypeMirror> injectsTypes,
List<TypeMirror> extraTypes,
UType genericType,
TypeElement beanType,
ImportTypeMap importTypes,
boolean factory) {
this(injectsTypes, genericType, true, beanType, importTypes, factory, beanType);
this(injectsTypes, extraTypes, genericType, true, beanType, importTypes, factory, beanType);
}

TypeReader(
List<TypeMirror> injectsTypes,
List<TypeMirror> extraTypes,
UType genericType,
TypeElement returnElement,
ImportTypeMap importTypes,
ExecutableElement source) {
this(injectsTypes, genericType, false, returnElement, importTypes, false, source);
this(injectsTypes, extraTypes, genericType, false, returnElement, importTypes, false, source);
}

private TypeReader(
List<TypeMirror> injectsTypes,
List<TypeMirror> extraTypes,
UType genericType,
boolean forBean,
TypeElement beanType,
ImportTypeMap importTypes,
boolean factory,
Element source) {
this.injectsTypes = injectsTypes.stream().map(UType::parse).collect(toList());
this.extraTypes = extraTypes.stream().map(UType::parse).collect(toList());
this.forBean = forBean;
this.beanType = beanType;
this.importTypes = importTypes;
Expand All @@ -63,6 +69,10 @@ String typesRegister() {
return typesRegister;
}

String extraTypesRegister() {
return extraTypesRegister;
}

List<String> provides() {
var provides = providedTypes();
provides.addAll(autoProvides());
Expand Down Expand Up @@ -156,6 +166,12 @@ private void initRegistrationTypes() {
appender.add(extendsReader.provides());
} else {
appender.add(injectsTypes);
if (!extraTypes.isEmpty()) {
this.extraTypesRegister =
new TypeAppender(importTypes)
.add(extraTypes)
.asString();
}
}
this.genericTypes = appender.genericTypes();
this.typesRegister = appender.asString();
Expand Down
9 changes: 9 additions & 0 deletions inject/src/main/java/io/avaje/inject/BeanTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface BeanTypes {

/** The types the component will register to. */
Class<?>[] value();

/**
* Extra types to register the component to that are not included in the
* <code>isBeanAbsent()</code> check. For testing purposes, when providing
* test doubles, these types are not checked.
*/
Class<?>[] registerTypes() default {};

}
5 changes: 5 additions & 0 deletions inject/src/main/java/io/avaje/inject/spi/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ default boolean isBeanAbsent(Type... types) {
return isBeanAbsent(null, types);
}

/**
* Register extra types that the next registered bean implements and provides.
*/
void registerTypes(Type... types);

/**
* Register the next bean as having Primary priority.
* Highest priority, wired over any other matching beans.
Expand Down
29 changes: 23 additions & 6 deletions inject/src/main/java/io/avaje/inject/spi/DBeanMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,25 @@ void register(Object bean) {
var name = nextBean.name;
qualifiers.add(name);
var entryBean = DContextEntryBean.of(bean, name, nextBean.priority, currentModule);
for (Type type : nextBean.types) {
beans.computeIfAbsent(type.getTypeName(), s -> new DContextEntry()).add(entryBean);
}
registerTypes(entryBean);
}

void register(Provider<?> provider) {
qualifiers.add(nextBean.name);
var entryBean = DContextEntryBean.provider(nextBean.prototype, provider, nextBean.name, nextBean.priority, currentModule);
registerTypes(entryBean);
}

private void registerTypes(DContextEntryBean entryBean) {
for (Type type : nextBean.types) {
beans.computeIfAbsent(type.getTypeName(), s -> new DContextEntry()).add(entryBean);
}
Type[] extraTypes = nextBean.extraTypes;
if (extraTypes != null) {
for (Type type : extraTypes) {
beans.computeIfAbsent(type.getTypeName(), s -> new DContextEntry()).add(entryBean);
}
}
}

/** Check if a non secondary entry exists */
Expand Down Expand Up @@ -132,8 +140,8 @@ <T> T get(Type type, String name) {
return (T) entry.get(name, currentModule);
}

public <T> List<T> listByPriority(Type type) {

@SuppressWarnings("unchecked")
<T> List<T> listByPriority(Type type) {
DContextEntry entry = beans.get(type.getTypeName());
if (entry == null) {
return List.of();
Expand Down Expand Up @@ -255,15 +263,24 @@ Set<String> scopeAnnotations() {
return forScopes;
}

static class NextBean {
void nextBeanRegisterTypes(Type[] types) {
nextBean.registerTypes(types);
}

static final class NextBean {
final String name;
final Type[] types;
Type[] extraTypes;
int priority = BeanEntry.NORMAL;
boolean prototype;

NextBean(String name, Type[] types) {
this.name = name;
this.types = types;
}

private void registerTypes(Type[] extraTypes) {
this.extraTypes = extraTypes;
}
}
}
5 changes: 5 additions & 0 deletions inject/src/main/java/io/avaje/inject/spi/DBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public boolean isBeanAbsent(@Nullable String name, Type... types) {
return true;
}

@Override
public void registerTypes(Type... types) {
beanMap.nextBeanRegisterTypes(types);
}

/**
* Return the types without any annotation types or raw generics.
*
Expand Down