Skip to content

Commit

Permalink
Now can import Prototype/Lazy Beans (#751)
Browse files Browse the repository at this point in the history
* Now can import prototype scoped beans

* Support lazy as well

* Update ImportedProtoType.java
  • Loading branch information
SentryMan authored Jan 6, 2025
1 parent ba4c6ea commit fa79dda
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ build/
inject-generator/avaje-inject
inject-generator/avaje-inject-generator
inject-generator/avaje-processors.txt
inject-generator/avaje-module-dependencies.csv
inject-generator/avaje-plugins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.other.one;

public class OtherComponent3 {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import io.avaje.config.Config;
import io.avaje.inject.Component;
import io.avaje.inject.Component.Import.Kind;
import io.avaje.inject.spi.ConfigPropertyPlugin;

import io.avaje.spi.ServiceProvider;
import org.other.one.OtherComponent2;
import org.other.one.OtherComponent3;

import java.util.Optional;

@ServiceProvider
@Component.Import(value = OtherComponent2.class)
@Component.Import(value = OtherComponent3.class, kind = Kind.LAZY)
public class ConfigPropertiesPlugin implements ConfigPropertyPlugin {

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

import static io.avaje.inject.generator.APContext.logError;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -54,10 +55,15 @@ final class BeanReader {
this.beanType = beanType;
this.type = beanType.getQualifiedName().toString();
this.shortName = shortName(beanType);
this.prototype = PrototypePrism.isPresent(beanType);
this.prototype =
PrototypePrism.isPresent(beanType)
|| importedComponent && ProcessingContext.isImportedPrototype(beanType);
this.primary = PrimaryPrism.isPresent(beanType);
this.secondary = !primary && SecondaryPrism.isPresent(beanType);
this.lazy = !FactoryPrism.isPresent(beanType) && LazyPrism.isPresent(beanType);
this.lazy =
!FactoryPrism.isPresent(beanType)
&& (LazyPrism.isPresent(beanType)
|| importedComponent && ProcessingContext.isImportedLazy(beanType));
final var beantypes = BeanTypesPrism.getOptionalOn(beanType);
beantypes.ifPresent(p -> Util.validateBeanTypes(beanType, p.value()));
this.typeReader =
Expand Down Expand Up @@ -371,7 +377,7 @@ private Set<String> importTypes() {
}
}
checkImports();
if (!suppressGeneratedImport){
if (!suppressGeneratedImport) {
importTypes.add(Constants.GENERATED);
}
if (!suppressBuilderImport && !isGenerateProxy()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,23 @@ private Set<TypeElement> importedElements(RoundEnvironment roundEnv) {
return maybeElements(roundEnv, ImportPrism.PRISM_TYPE).stream()
.flatMap(Set::stream)
.map(ImportPrism::getInstanceOn)
.flatMap(p -> p.value().stream())
.map(ProcessingContext::asElement)
.filter(this::notAlreadyProvided)
.flatMap(p -> {
var kind = p.kind();
return p.value().stream()
.map(ProcessingContext::asElement)
.filter(this::notAlreadyProvided)
.map(e -> registerImportedKind(e, kind));
})
.collect(Collectors.toSet());
}

private static TypeElement registerImportedKind(TypeElement e, String kind) {
if (!"SINGLETON".equals(kind)) {
ProcessingContext.addImportedKind(e, kind);
}
return e;
}

private boolean notAlreadyProvided(TypeElement e) {
final String type = e.getQualifiedName().toString();
return !moduleFileProvided.contains(type) && !pluginFileProvided.contains(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private ProcessingContext() {}
static final class Ctx {
private final Set<String> uniqueModuleNames = new HashSet<>();
private final Set<String> providedTypes = new HashSet<>();
private final Map<String, String> importedProtoTypes = new HashMap<>();
private final Set<String> optionalTypes = new LinkedHashSet<>();
private final Map<String, AspectImportPrism> aspectImportPrisms = new HashMap<>();
private final List<ModuleData> modules = new ArrayList<>();
Expand Down Expand Up @@ -154,6 +155,22 @@ static void addOptionalType(String paramType, String name) {
}
}

static void addImportedKind(TypeElement element, String kind) {
CTX.get().importedProtoTypes.put(element.getQualifiedName().toString(), kind);
}

static boolean isImportedPrototype(TypeElement element) {
return "prototype".equalsIgnoreCase(importedTypeKind(element));
}

static boolean isImportedLazy(TypeElement element) {
return "lazy".equalsIgnoreCase(importedTypeKind(element));
}

private static String importedTypeKind(TypeElement element) {
return CTX.get().importedProtoTypes.get(element.getQualifiedName().toString());
}

static void addImportedAspects(Map<String, AspectImportPrism> importedMap) {
CTX.get().aspectImportPrisms.putAll(importedMap);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.avaje.inject.generator.models.valid.imported;

import io.avaje.inject.Component;
import io.avaje.inject.Component.Import.Kind;

@Component.Import(value = ImportedProtoType.class, kind = Kind.PROTOTYPE)
public class ImportedProtoType {}
24 changes: 19 additions & 5 deletions inject/src/main/java/io/avaje/inject/Component.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.avaje.inject;

import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.CLASS;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.RetentionPolicy.SOURCE;

/**
* Identify a bean as component with singleton scope that avaje-inject will use.
Expand Down Expand Up @@ -63,13 +64,26 @@
*
* }</pre>
*/
@Retention(CLASS)
@Retention(SOURCE)
@Target({TYPE, PACKAGE, MODULE})
@Repeatable(Imports.class)
@interface Import {

/**
* Types to generate DI classes for.
*/
/** Types to generate DI classes for. */
Class<?>[] value();

/** What kind of bean */
Kind kind() default Kind.SINGLETON;

enum Kind { SINGLETON, PROTOTYPE, LAZY }
}

/**
* @see Import
*/
@Retention(SOURCE)
@Target({TYPE, PACKAGE, MODULE})
@interface Imports {
Import[] value();
}
}

0 comments on commit fa79dda

Please sign in to comment.