Skip to content

Commit f876306

Browse files
committed
external
1 parent 13c8300 commit f876306

File tree

4 files changed

+87
-13
lines changed

4 files changed

+87
-13
lines changed

inject-generator/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
<groupId>io.avaje</groupId>
2020
<artifactId>avaje-inject</artifactId>
2121
<version>8.12-RC4</version>
22-
<optional>true</optional>
2322
<scope>provided</scope>
2423
</dependency>
24+
2525
<dependency>
2626
<groupId>com.jolira</groupId>
2727
<artifactId>hickory</artifactId>
@@ -50,7 +50,7 @@
5050
<configuration>
5151
<source>11</source>
5252
<target>11</target>
53-
<compilerArgument>-proc:none</compilerArgument>
53+
<!-- <compilerArgument>-proc:none</compilerArgument> -->
5454
</configuration>
5555
</plugin>
5656

@@ -65,7 +65,7 @@
6565
</plugin>
6666

6767

68-
<plugin>
68+
<!-- <plugin>
6969
<groupId>org.bsc.maven</groupId>
7070
<artifactId>maven-processor-plugin</artifactId>
7171
<version>4.0-rc1</version>
@@ -110,7 +110,7 @@
110110
</configuration>
111111
</execution>
112112
</executions>
113-
</plugin>
113+
</plugin> -->
114114
</plugins>
115115
</build>
116116

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.avaje.inject.generator;
2+
3+
import java.util.ServiceConfigurationError;
4+
import java.util.ServiceLoader;
5+
import java.util.Set;
6+
7+
import io.avaje.inject.spi.Module;
8+
import io.avaje.inject.spi.Plugin;
9+
10+
/**
11+
* The types provided by other modules in the classpath at compile time.
12+
*
13+
* <p>When we depend on these types they add to the module autoRequires() classes.
14+
*/
15+
final class ExternalProvider {
16+
17+
private static final boolean moduleLoaded = moduleCP();
18+
19+
private ExternalProvider() {}
20+
21+
private static boolean moduleCP() {
22+
try {
23+
Class.forName(Constants.MODULE);
24+
return true;
25+
} catch (final ClassNotFoundException e) {
26+
27+
return false;
28+
}
29+
}
30+
31+
public static Set<String> registerModuleProvidedTypes(Set<String> providedTypes) {
32+
33+
if (!moduleLoaded) return Set.of();
34+
35+
final ServiceLoader<Module> load =
36+
ServiceLoader.load(Module.class, ExternalProvider.class.getClassLoader());
37+
for (final Module module : load) {
38+
try {
39+
for (final Class<?> provide : module.provides()) {
40+
providedTypes.add(provide.getCanonicalName());
41+
}
42+
for (final Class<?> provide : module.autoProvides()) {
43+
providedTypes.add(provide.getCanonicalName());
44+
}
45+
for (final Class<?> provide : module.autoProvidesAspects()) {
46+
providedTypes.add(Util.wrapAspect(provide.getCanonicalName()));
47+
}
48+
} catch (final ServiceConfigurationError expected) {
49+
// ignore expected error reading the module that we are also writing
50+
}
51+
}
52+
return providedTypes;
53+
}
54+
55+
/**
56+
* Register types provided by the plugin so no compiler error when we have a dependency on these
57+
* types and the only thing providing them is the plugin.
58+
*
59+
* @param defaultScope
60+
*/
61+
public static void registerPluginProvidedTypes(ScopeInfo defaultScope) {
62+
63+
if (!moduleLoaded) return;
64+
65+
for (final Plugin plugin : ServiceLoader.load(Plugin.class, Processor.class.getClassLoader())) {
66+
for (final Class<?> provide : plugin.provides()) {
67+
defaultScope.pluginProvided(provide.getCanonicalName());
68+
}
69+
}
70+
}
71+
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.FileNotFoundException;
44
import java.io.IOException;
55
import java.io.LineNumberReader;
6-
import java.io.Reader;
76
import java.nio.file.NoSuchFileException;
87
import java.util.ArrayList;
98
import java.util.Collections;
@@ -41,6 +40,9 @@ final class ProcessingContext {
4140
this.filer = processingEnv.getFiler();
4241
this.elementUtils = processingEnv.getElementUtils();
4342
this.typeUtils = processingEnv.getTypeUtils();
43+
44+
ExternalProvider.registerModuleProvidedTypes(providedTypes);
45+
4446
providedTypes.addAll(moduleFileProvided);
4547
}
4648

@@ -64,7 +66,7 @@ void logDebug(String msg, Object... args) {
6466
}
6567

6668
String loadMetaInfServices() {
67-
final List<String> lines = loadMetaInf(Constants.META_INF_MODULE);
69+
final var lines = loadMetaInf(Constants.META_INF_MODULE);
6870
return lines.isEmpty() ? null : lines.get(0);
6971
}
7072

@@ -74,11 +76,11 @@ List<String> loadMetaInfCustom() {
7476

7577
private List<String> loadMetaInf(String fullName) {
7678
try {
77-
FileObject fileObject = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", fullName);
79+
final var fileObject = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", fullName);
7880
if (fileObject != null) {
79-
List<String> lines = new ArrayList<>();
80-
Reader reader = fileObject.openReader(true);
81-
LineNumberReader lineReader = new LineNumberReader(reader);
81+
final List<String> lines = new ArrayList<>();
82+
final var reader = fileObject.openReader(true);
83+
final var lineReader = new LineNumberReader(reader);
8284
String line;
8385
while ((line = lineReader.readLine()) != null) {
8486
line = line.trim();
@@ -92,10 +94,10 @@ private List<String> loadMetaInf(String fullName) {
9294
} catch (FileNotFoundException | NoSuchFileException e) {
9395
// logDebug("no services file yet");
9496

95-
} catch (FilerException e) {
97+
} catch (final FilerException e) {
9698
logDebug("FilerException reading services file");
9799

98-
} catch (Exception e) {
100+
} catch (final Exception e) {
99101
e.printStackTrace();
100102
logWarn("Error reading services file: " + e.getMessage());
101103
}
@@ -110,7 +112,7 @@ JavaFileObject createWriter(String cls) throws IOException {
110112
}
111113

112114
FileObject createMetaInfWriter(ScopeInfo.Type scopeType) throws IOException {
113-
String serviceName = scopeType == ScopeInfo.Type.DEFAULT ? Constants.META_INF_MODULE : Constants.META_INF_TESTMODULE;
115+
final var serviceName = scopeType == ScopeInfo.Type.DEFAULT ? Constants.META_INF_MODULE : Constants.META_INF_TESTMODULE;
114116
return createMetaInfWriterFor(serviceName);
115117
}
116118

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
5656
this.elementUtils = processingEnv.getElementUtils();
5757
this.allScopes = new AllScopes(context);
5858
this.defaultScope = allScopes.defaultScope();
59+
ExternalProvider.registerPluginProvidedTypes(defaultScope);
5960
pluginFileProvided.forEach(defaultScope::pluginProvided);
6061
}
6162

0 commit comments

Comments
 (0)