Skip to content

Commit bad3f7c

Browse files
authored
Merge branch 'master' into isbeanAbsent
2 parents 75f014d + aac8204 commit bad3f7c

File tree

29 files changed

+183
-109
lines changed

29 files changed

+183
-109
lines changed

blackbox-aspect/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>avaje-inject-parent</artifactId>
77
<groupId>io.avaje</groupId>
8-
<version>10.5-RC6</version>
8+
<version>10.6-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

blackbox-other/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>avaje-inject-parent</artifactId>
88
<groupId>io.avaje</groupId>
9-
<version>10.5-RC6</version>
9+
<version>10.6-SNAPSHOT</version>
1010
</parent>
1111

1212
<artifactId>blackbox-other</artifactId>

blackbox-test-inject/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>avaje-inject-parent</artifactId>
77
<groupId>io.avaje</groupId>
8-
<version>10.5-RC6</version>
8+
<version>10.6-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

@@ -47,7 +47,7 @@
4747
<dependency>
4848
<groupId>io.avaje</groupId>
4949
<artifactId>avaje-spi-service</artifactId>
50-
<version>2.7</version>
50+
<version>2.8</version>
5151
</dependency>
5252

5353
<dependency>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class LazyBean {
1717

1818
@Inject @Nullable AtomicBoolean initialized;
1919

20+
public LazyBean() throws Exception {}
21+
2022
@PostConstruct
2123
void init(BeanScope scope) {
2224
// note that nested test scopes will not be lazy

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class LazyFactory {
1414

1515
@Bean
1616
@Named("factory")
17-
LazyBean lazyInt(@Nullable AtomicBoolean initialized) {
17+
LazyBean lazyInt(@Nullable AtomicBoolean initialized) throws Exception {
1818

1919
// note that nested test scopes will not be lazy
2020
if (initialized != null) initialized.set(true);

inject-aop/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.avaje</groupId>
77
<artifactId>avaje-inject-parent</artifactId>
8-
<version>10.5-RC6</version>
8+
<version>10.6-SNAPSHOT</version>
99
</parent>
1010
<artifactId>avaje-inject-aop</artifactId>
1111
<dependencies>

inject-events/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.avaje</groupId>
55
<artifactId>avaje-inject-parent</artifactId>
6-
<version>10.5-RC6</version>
6+
<version>10.6-SNAPSHOT</version>
77
</parent>
88
<artifactId>avaje-inject-events</artifactId>
99
<name>avaje inject events</name>

inject-generator/pom.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<parent>
77
<groupId>io.avaje</groupId>
88
<artifactId>avaje-inject-parent</artifactId>
9-
<version>10.5-RC6</version>
9+
<version>10.6-SNAPSHOT</version>
1010
</parent>
1111

1212
<artifactId>avaje-inject-generator</artifactId>
1313
<name>avaje inject generator</name>
1414
<description>annotation processor generating source code for avaje-inject dependency injection</description>
1515
<properties>
16-
<avaje.prisms.version>1.34</avaje.prisms.version>
16+
<avaje.prisms.version>1.35</avaje.prisms.version>
1717
<!-- VALHALLA-START ___
1818
<maven.compiler.enablePreview>false</maven.compiler.enablePreview>
1919
____ VALHALLA-END -->
@@ -37,8 +37,17 @@
3737
<dependency>
3838
<groupId>io.avaje</groupId>
3939
<artifactId>avaje-spi-service</artifactId>
40-
<version>2.7</version>
40+
<version>2.8</version>
4141
</dependency>
42+
43+
<dependency>
44+
<groupId>io.avaje</groupId>
45+
<artifactId>avaje-http-api</artifactId>
46+
<version>2.8</version>
47+
<optional>true</optional>
48+
<scope>provided</scope>
49+
</dependency>
50+
4251
<!-- test dependencies -->
4352
<dependency>
4453
<groupId>io.avaje</groupId>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void addLifecycleCallbacks(Append writer, String indent) {
337337
void prototypePostConstruct(Append writer, String indent) {
338338
if (postConstructMethod != null) {
339339
var postConstruct = (ExecutableElement) postConstructMethod;
340-
writer.append("%s bean.%s(", indent, postConstructMethod.getSimpleName());
340+
writer.indent(indent).append(" bean.%s(", postConstructMethod.getSimpleName());
341341
if (postConstruct.getParameters().isEmpty()) {
342342
writer.append(");").eol();
343343
} else {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ final class Constants {
1818
static final String QUALIFIER = "jakarta.inject.Qualifier";
1919
static final String NAMED = "jakarta.inject.Named";
2020

21-
static final String PATH = "io.avaje.http.api.Path";
22-
static final String CONTROLLER = "io.avaje.http.api.Controller";
23-
static final String HTTP_GENERATED = "io.avaje.http.api.Generated";
24-
2521
static final String AT_SINGLETON = "@Singleton";
2622
static final String AT_PROXY = "@Proxy";
2723
static final String AT_GENERATED = "@Generated(\"io.avaje.inject.generator\")";

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.ArrayList;
1111
import java.util.Arrays;
1212
import java.util.Collection;
13+
import java.util.HashMap;
1314
import java.util.HashSet;
1415
import java.util.List;
1516
import java.util.Map;
@@ -19,6 +20,7 @@
1920
import java.util.TreeSet;
2021
import java.util.stream.Stream;
2122

23+
import javax.lang.model.element.Element;
2224
import javax.lang.model.element.ElementKind;
2325
import javax.lang.model.element.Modifier;
2426
import javax.lang.model.element.ModuleElement;
@@ -80,7 +82,6 @@ static void registerModuleProvidedTypes(Set<String> providedTypes) {
8082
}
8183
for (final var module : modules) {
8284
final var name = module.getClass().getTypeName();
83-
APContext.logNote("Detected Module: %s", name);
8485
final var provides = new TreeSet<String>();
8586
for (final var provide : module.provides()) {
8687
provides.add(provide.getTypeName());
@@ -119,6 +120,7 @@ static void registerPluginProvidedTypes(ScopeInfo defaultScope) {
119120

120121
List<InjectPlugin> plugins = LoadServices.loadPlugins(CLASS_LOADER);
121122
for (final var plugin : plugins) {
123+
ProcessingContext.addExternalInjectSPI(plugin.getClass().getCanonicalName());
122124
var name = plugin.getClass().getTypeName();
123125
if (avajePlugins.containsKey(name)) {
124126
continue;
@@ -142,6 +144,7 @@ private static boolean pluginExists(String relativeName) {
142144
}
143145

144146
static void registerExternalMetaData(String name) {
147+
ProcessingContext.addExternalInjectSPI(name);
145148
Optional.ofNullable(APContext.typeElement(name))
146149
.map(TypeElement::getEnclosedElements)
147150
.map(ElementFilter::methodsIn)
@@ -163,10 +166,13 @@ static void readMetaDataProvides(Collection<String> providedTypes) {
163166
}
164167

165168
static void scanAllInjectPlugins(ScopeInfo defaultScope) {
169+
Map<String, List<String>> plugins = new HashMap<>();
166170
final var hasPlugins = !defaultScope.pluginProvided().isEmpty();
167171
avajePlugins.forEach((k, v) -> {
168172
if (APContext.typeElement(k) != null) {
173+
plugins.put(k, v);
169174
APContext.logNote("Loaded Plugin: %s", k);
175+
ProcessingContext.addExternalInjectSPI(k);
170176
v.forEach(defaultScope::pluginProvided);
171177
}
172178
});
@@ -178,42 +184,50 @@ static void scanAllInjectPlugins(ScopeInfo defaultScope) {
178184
injectExtensions()
179185
.filter(PluginProvidesPrism::isPresent)
180186
.distinct()
181-
.forEach(pluginType -> addPluginToScope(defaultScope, pluginType));
187+
.forEach(pluginType -> addPluginToScope(defaultScope, pluginType, plugins));
182188

183189
if (defaultScope.pluginProvided().isEmpty()) {
184190
APContext.logNote("No external plugins detected");
185191
}
186-
writePluginProvides(defaultScope);
192+
writePluginProvides(plugins);
187193
}
188194

189-
private static void writePluginProvides(ScopeInfo defaultScope) {
195+
private static void writePluginProvides(Map<String, List<String>> plugins) {
190196
// write detected plugins to a text file for test compilation
191-
try (final var pluginWriter = new FileWriter(APContext.getBuildResource("avaje-plugin-provides.txt").toFile())) {
192-
for (var providedType : defaultScope.pluginProvided()) {
193-
pluginWriter.write(providedType);
197+
try (final var pluginWriter = new FileWriter(APContext.getBuildResource("avaje-plugins.csv").toFile())) {
198+
pluginWriter.write("External Plugin Type|Provides");
199+
for (final var plugin : plugins.entrySet()) {
194200
pluginWriter.write("\n");
201+
pluginWriter.write(plugin.getKey());
202+
pluginWriter.write("|");
203+
var provides = String.join(",", plugin.getValue());
204+
pluginWriter.write(provides.isEmpty() ? " " : provides);
195205
}
196206
} catch (IOException e) {
197-
APContext.logWarn("Failed to write avaje-plugin-provides.txt due to %s", e.getMessage());
207+
APContext.logWarn("Failed to write avaje-plugins.csv due to %s", e.getMessage());
198208
}
199209
}
200210

201-
private static void addPluginToScope(ScopeInfo defaultScope, TypeElement pluginType) {
211+
private static void addPluginToScope(ScopeInfo defaultScope, TypeElement pluginType, Map<String, List<String>> plugins) {
202212
final var name = pluginType.getQualifiedName().toString();
203-
if (avajePlugins.containsKey(name)) {
204-
return;
205-
}
213+
ProcessingContext.addExternalInjectSPI(name);
206214
var prism = PluginProvidesPrism.getInstanceOn(pluginType);
215+
List<String> provides = new ArrayList<>();
207216
for (final var provide : prism.value()) {
208217
defaultScope.pluginProvided(provide.toString());
218+
provides.add(provide.toString());
209219
}
210220
for (final var provide : prism.providesStrings()) {
211221
defaultScope.pluginProvided(provide);
222+
provides.add(provide);
212223
}
213224
for (final var provide : prism.providesAspects()) {
214-
defaultScope.pluginProvided(Util.wrapAspect(provide.toString()));
225+
final var wrapAspect = Util.wrapAspect(provide.toString());
226+
defaultScope.pluginProvided(wrapAspect);
227+
provides.add(wrapAspect);
215228
}
216229
APContext.logNote("Loaded Plugin: %s", name);
230+
plugins.put(name, provides);
217231
}
218232

219233
static void scanAllAvajeModules(Collection<String> providedTypes) {
@@ -295,8 +309,8 @@ private static Stream<TypeElement> injectExtensions() {
295309

296310
final var checkEnclosing =
297311
allModules.stream()
298-
.flatMap(m -> m.getEnclosedElements().stream())
299-
.flatMap(p -> p.getEnclosedElements().stream())
312+
.flatMap(ExternalProvider::getEnclosed)
313+
.flatMap(ExternalProvider::getEnclosed)
300314
.map(TypeElement.class::cast)
301315
.filter(t -> t.getKind() == ElementKind.CLASS)
302316
.filter(t -> t.getModifiers().contains(Modifier.PUBLIC))
@@ -311,6 +325,15 @@ private static Stream<TypeElement> injectExtensions() {
311325
return Stream.concat(checkEnclosing, checkDirectives);
312326
}
313327

328+
// when a project's module-info is misconfigured a certain way, getEnclosedElements throws an error
329+
private static Stream<? extends Element> getEnclosed(Element e) {
330+
try {
331+
return e.getEnclosedElements().stream();
332+
} catch (Exception ex) {
333+
return Stream.of();
334+
}
335+
}
336+
314337
private static boolean isInjectExtension(ModuleElement.ProvidesDirective p) {
315338
return "io.avaje.inject.spi.InjectExtension".equals(p.getService().getQualifiedName().toString());
316339
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class IncludeAnnotations {
1515

1616
static {
1717
EXCLUDED_ANNOTATIONS.add(Constants.KOTLIN_METADATA);
18-
EXCLUDED_ANNOTATIONS.add(Constants.PATH);
18+
EXCLUDED_ANNOTATIONS.add(PathPrism.PRISM_TYPE);
1919
}
2020

2121
/**

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import javax.lang.model.element.Element;
1111
import javax.lang.model.element.ElementKind;
1212
import javax.lang.model.element.ExecutableElement;
13-
import javax.lang.model.element.Modifier;
1413
import javax.lang.model.element.TypeElement;
1514
import javax.lang.model.util.ElementFilter;
1615
import javax.lang.model.util.Elements;
@@ -29,12 +28,13 @@
2928
@GenerateUtils
3029
@GenerateAPContext
3130
@GenerateModuleInfoReader
31+
@SupportedOptions("mergeServices")
3232
@SupportedAnnotationTypes({
3333
AspectImportPrism.PRISM_TYPE,
3434
AssistFactoryPrism.PRISM_TYPE,
3535
ComponentPrism.PRISM_TYPE,
3636
Constants.TESTSCOPE,
37-
Constants.CONTROLLER,
37+
ControllerPrism.PRISM_TYPE,
3838
ExternalPrism.PRISM_TYPE,
3939
FactoryPrism.PRISM_TYPE,
4040
ImportPrism.PRISM_TYPE,
@@ -66,7 +66,7 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
6666
super.init(processingEnv);
6767
APContext.init(processingEnv);
6868
loadProvidedFiles();
69-
ProcessingContext.init(moduleFileProvided);
69+
ProcessingContext.registerProvidedTypes(moduleFileProvided);
7070
moduleData.forEach(ProcessingContext::addModule);
7171
this.elementUtils = processingEnv.getElementUtils();
7272
this.allScopes = new AllScopes();
@@ -98,7 +98,6 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
9898
*/
9999
void loadProvidedFiles() {
100100
pluginFileProvided.addAll(lines("avaje-plugin-provides.txt"));
101-
102101
lines("avaje-module-dependencies.csv").stream()
103102
.filter(s -> s.contains("|") && !s.startsWith("External Module Type"))
104103
.distinct()
@@ -110,6 +109,11 @@ void loadProvidedFiles() {
110109
ExternalProvider.readMetaDataProvides(moduleFileProvided);
111110
this.moduleData.add(m);
112111
});
112+
lines("avaje-plugins.csv").stream()
113+
.filter(s -> s.contains("|") && !s.startsWith("External Plugin Type"))
114+
.distinct()
115+
.map(l -> l.split("\\|")[1])
116+
.forEach(pluginFileProvided::add);
113117
}
114118

115119
private List<String> lines(String relativeName) {
@@ -146,7 +150,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
146150

147151
readImported(importedElements(roundEnv));
148152

149-
maybeElements(roundEnv, Constants.CONTROLLER).ifPresent(this::readBeans);
153+
maybeElements(roundEnv, ControllerPrism.PRISM_TYPE).ifPresent(this::readBeans);
150154
maybeElements(roundEnv, ProxyPrism.PRISM_TYPE).ifPresent(this::readBeans);
151155
maybeElements(roundEnv, AssistFactoryPrism.PRISM_TYPE).ifPresent(this::readAssisted);
152156

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
final class ProcessingContext {
2121

2222
private static final String EVENTS_SPI = "io.avaje.inject.events.spi.ObserverManagerPlugin";
23-
private static final ThreadLocal<Ctx> CTX = new ThreadLocal<>();
23+
private static final ThreadLocal<Ctx> CTX = ThreadLocal.withInitial(Ctx::new);
2424
private static boolean processingOver;
2525
private ProcessingContext() {}
2626

@@ -33,15 +33,15 @@ static final class Ctx {
3333
private final List<TypeElement> delayQueue = new ArrayList<>();
3434
private final Set<String> spiServices = new TreeSet<>();
3535
private boolean strictWiring;
36+
private final boolean mergeServices = APContext.getOption("mergeServices").map(Boolean::valueOf).orElse(true);
3637

3738
void registerProvidedTypes(Set<String> moduleFileProvided) {
3839
ExternalProvider.registerModuleProvidedTypes(providedTypes);
3940
providedTypes.addAll(moduleFileProvided);
4041
}
4142
}
4243

43-
static void init(Set<String> moduleFileProvided) {
44-
CTX.set(new Ctx());
44+
static void registerProvidedTypes(Set<String> moduleFileProvided) {
4545
CTX.get().registerProvidedTypes(moduleFileProvided);
4646
addEventSPI();
4747
}
@@ -105,6 +105,12 @@ static void addInjectSPI(String type) {
105105
CTX.get().spiServices.add(type);
106106
}
107107

108+
static void addExternalInjectSPI(String type) {
109+
if (CTX.get().mergeServices) {
110+
CTX.get().spiServices.add(type);
111+
}
112+
}
113+
108114
static FileObject createMetaInfWriterFor(String interfaceType) throws IOException {
109115
return filer().createResource(StandardLocation.CLASS_OUTPUT, "", interfaceType);
110116
}
@@ -205,10 +211,6 @@ static void processingOver(boolean over) {
205211
}
206212

207213
static void writeSPIServicesFile() {
208-
Optional.ofNullable(APContext.getProjectModuleElement())
209-
.filter(m -> "io.avaje.inject.test".equals(m.getQualifiedName().toString()))
210-
.ifPresent(m -> CTX.get().spiServices.remove(EVENTS_SPI));
211-
212214
readExistingMetaInfServices();
213215
if (CTX.get().spiServices.isEmpty()) {
214216
// no services to register

0 commit comments

Comments
 (0)