Skip to content

Commit fa79dda

Browse files
authored
Now can import Prototype/Lazy Beans (#751)
* Now can import prototype scoped beans * Support lazy as well * Update ImportedProtoType.java
1 parent ba4c6ea commit fa79dda

File tree

8 files changed

+74
-11
lines changed

8 files changed

+74
-11
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ build/
1313
inject-generator/avaje-inject
1414
inject-generator/avaje-inject-generator
1515
inject-generator/avaje-processors.txt
16+
inject-generator/avaje-module-dependencies.csv
17+
inject-generator/avaje-plugins.csv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.other.one;
2+
3+
public class OtherComponent3 {}

blackbox-test-inject/src/main/java/org/example/myapp/ConfigPropertiesPlugin.java

+3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import io.avaje.config.Config;
44
import io.avaje.inject.Component;
5+
import io.avaje.inject.Component.Import.Kind;
56
import io.avaje.inject.spi.ConfigPropertyPlugin;
67

78
import io.avaje.spi.ServiceProvider;
89
import org.other.one.OtherComponent2;
10+
import org.other.one.OtherComponent3;
911

1012
import java.util.Optional;
1113

1214
@ServiceProvider
1315
@Component.Import(value = OtherComponent2.class)
16+
@Component.Import(value = OtherComponent3.class, kind = Kind.LAZY)
1417
public class ConfigPropertiesPlugin implements ConfigPropertyPlugin {
1518

1619
@Override

inject-generator/src/main/java/io/avaje/inject/generator/BeanReader.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.inject.generator;
22

33
import static io.avaje.inject.generator.APContext.logError;
4+
45
import java.util.ArrayList;
56
import java.util.Collections;
67
import java.util.LinkedHashSet;
@@ -54,10 +55,15 @@ final class BeanReader {
5455
this.beanType = beanType;
5556
this.type = beanType.getQualifiedName().toString();
5657
this.shortName = shortName(beanType);
57-
this.prototype = PrototypePrism.isPresent(beanType);
58+
this.prototype =
59+
PrototypePrism.isPresent(beanType)
60+
|| importedComponent && ProcessingContext.isImportedPrototype(beanType);
5861
this.primary = PrimaryPrism.isPresent(beanType);
5962
this.secondary = !primary && SecondaryPrism.isPresent(beanType);
60-
this.lazy = !FactoryPrism.isPresent(beanType) && LazyPrism.isPresent(beanType);
63+
this.lazy =
64+
!FactoryPrism.isPresent(beanType)
65+
&& (LazyPrism.isPresent(beanType)
66+
|| importedComponent && ProcessingContext.isImportedLazy(beanType));
6167
final var beantypes = BeanTypesPrism.getOptionalOn(beanType);
6268
beantypes.ifPresent(p -> Util.validateBeanTypes(beanType, p.value()));
6369
this.typeReader =
@@ -371,7 +377,7 @@ private Set<String> importTypes() {
371377
}
372378
}
373379
checkImports();
374-
if (!suppressGeneratedImport){
380+
if (!suppressGeneratedImport) {
375381
importTypes.add(Constants.GENERATED);
376382
}
377383
if (!suppressBuilderImport && !isGenerateProxy()) {

inject-generator/src/main/java/io/avaje/inject/generator/InjectProcessor.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,23 @@ private Set<TypeElement> importedElements(RoundEnvironment roundEnv) {
211211
return maybeElements(roundEnv, ImportPrism.PRISM_TYPE).stream()
212212
.flatMap(Set::stream)
213213
.map(ImportPrism::getInstanceOn)
214-
.flatMap(p -> p.value().stream())
215-
.map(ProcessingContext::asElement)
216-
.filter(this::notAlreadyProvided)
214+
.flatMap(p -> {
215+
var kind = p.kind();
216+
return p.value().stream()
217+
.map(ProcessingContext::asElement)
218+
.filter(this::notAlreadyProvided)
219+
.map(e -> registerImportedKind(e, kind));
220+
})
217221
.collect(Collectors.toSet());
218222
}
219223

224+
private static TypeElement registerImportedKind(TypeElement e, String kind) {
225+
if (!"SINGLETON".equals(kind)) {
226+
ProcessingContext.addImportedKind(e, kind);
227+
}
228+
return e;
229+
}
230+
220231
private boolean notAlreadyProvided(TypeElement e) {
221232
final String type = e.getQualifiedName().toString();
222233
return !moduleFileProvided.contains(type) && !pluginFileProvided.contains(type);

inject-generator/src/main/java/io/avaje/inject/generator/ProcessingContext.java

+17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private ProcessingContext() {}
2727
static final class Ctx {
2828
private final Set<String> uniqueModuleNames = new HashSet<>();
2929
private final Set<String> providedTypes = new HashSet<>();
30+
private final Map<String, String> importedProtoTypes = new HashMap<>();
3031
private final Set<String> optionalTypes = new LinkedHashSet<>();
3132
private final Map<String, AspectImportPrism> aspectImportPrisms = new HashMap<>();
3233
private final List<ModuleData> modules = new ArrayList<>();
@@ -154,6 +155,22 @@ static void addOptionalType(String paramType, String name) {
154155
}
155156
}
156157

158+
static void addImportedKind(TypeElement element, String kind) {
159+
CTX.get().importedProtoTypes.put(element.getQualifiedName().toString(), kind);
160+
}
161+
162+
static boolean isImportedPrototype(TypeElement element) {
163+
return "prototype".equalsIgnoreCase(importedTypeKind(element));
164+
}
165+
166+
static boolean isImportedLazy(TypeElement element) {
167+
return "lazy".equalsIgnoreCase(importedTypeKind(element));
168+
}
169+
170+
private static String importedTypeKind(TypeElement element) {
171+
return CTX.get().importedProtoTypes.get(element.getQualifiedName().toString());
172+
}
173+
157174
static void addImportedAspects(Map<String, AspectImportPrism> importedMap) {
158175
CTX.get().aspectImportPrisms.putAll(importedMap);
159176
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.avaje.inject.generator.models.valid.imported;
2+
3+
import io.avaje.inject.Component;
4+
import io.avaje.inject.Component.Import.Kind;
5+
6+
@Component.Import(value = ImportedProtoType.class, kind = Kind.PROTOTYPE)
7+
public class ImportedProtoType {}

inject/src/main/java/io/avaje/inject/Component.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.avaje.inject;
22

3+
import java.lang.annotation.Repeatable;
34
import java.lang.annotation.Retention;
45
import java.lang.annotation.Target;
56

67
import static java.lang.annotation.ElementType.*;
7-
import static java.lang.annotation.RetentionPolicy.CLASS;
88
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
import static java.lang.annotation.RetentionPolicy.SOURCE;
910

1011
/**
1112
* Identify a bean as component with singleton scope that avaje-inject will use.
@@ -63,13 +64,26 @@
6364
*
6465
* }</pre>
6566
*/
66-
@Retention(CLASS)
67+
@Retention(SOURCE)
6768
@Target({TYPE, PACKAGE, MODULE})
69+
@Repeatable(Imports.class)
6870
@interface Import {
6971

70-
/**
71-
* Types to generate DI classes for.
72-
*/
72+
/** Types to generate DI classes for. */
7373
Class<?>[] value();
74+
75+
/** What kind of bean */
76+
Kind kind() default Kind.SINGLETON;
77+
78+
enum Kind { SINGLETON, PROTOTYPE, LAZY }
79+
}
80+
81+
/**
82+
* @see Import
83+
*/
84+
@Retention(SOURCE)
85+
@Target({TYPE, PACKAGE, MODULE})
86+
@interface Imports {
87+
Import[] value();
7488
}
7589
}

0 commit comments

Comments
 (0)