Skip to content

Commit 857b073

Browse files
committed
support build plugin
1 parent baa16e9 commit 857b073

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

validator-generator/src/main/java/io/avaje/validation/generator/ProcessingContext.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
import static io.avaje.validation.generator.APContext.getProjectModuleElement;
66
import static io.avaje.validation.generator.APContext.logError;
77
import static io.avaje.validation.generator.APContext.logWarn;
8+
import static java.util.stream.Collectors.toSet;
89

910
import java.io.IOException;
10-
import java.util.concurrent.atomic.AtomicBoolean;
11-
11+
import java.net.URI;
12+
import java.net.URISyntaxException;
1213
import javax.annotation.processing.ProcessingEnvironment;
13-
import javax.lang.model.element.Element;
14-
import javax.lang.model.element.ModuleElement;
1514
import javax.tools.FileObject;
1615
import javax.tools.StandardLocation;
1716

17+
import io.avaje.validation.generator.ModuleInfoReader.Requires;
18+
1819
final class ProcessingContext {
1920

2021
private static final ThreadLocal<Ctx> CTX = new ThreadLocal<>();
@@ -65,22 +66,32 @@ static void validateModule(String fqn) {
6566
var warnHttp = CTX.get().warnHttp;
6667

6768
try (var reader = getModuleInfoReader()) {
68-
6969
var moduleInfo = new ModuleInfoReader(module, reader);
70+
var buildPluginAvailable = buildPluginAvailable();
71+
var requireSet =
72+
moduleInfo.requires().stream()
73+
.map(Requires::getDependency)
74+
.map(m -> m.getQualifiedName().toString())
75+
.collect(toSet());
76+
7077
boolean noHttpPlugin =
71-
injectPresent && warnHttp && !moduleInfo.containsOnModulePath("io.avaje.validation.http");
78+
injectPresent
79+
&& (!buildPluginAvailable || !requireSet.contains("io.avaje.http.api"))
80+
&& warnHttp
81+
&& !moduleInfo.containsOnModulePath("io.avaje.validation.http");
7282

7383
boolean noInjectPlugin =
7484
noHttpPlugin
7585
&& injectPresent
86+
&& (!buildPluginAvailable || !requireSet.contains("io.avaje.validation"))
7687
&& !moduleInfo.containsOnModulePath("io.avaje.validation.plugin");
7788

7889
var noProvides =
7990
moduleInfo.provides().stream()
8091
.flatMap(s -> s.implementations().stream())
8192
.noneMatch(s -> s.contains(fqn));
8293

83-
if (noProvides) {
94+
if (!buildPluginAvailable && noProvides) {
8495
logError(
8596
module,
8697
"Missing `provides io.avaje.validation.Validator.GeneratedComponent with %s;`",
@@ -105,11 +116,27 @@ static void validateModule(String fqn) {
105116
}
106117
}
107118

108-
static ModuleElement getModuleElement(Element e) {
109-
if (e == null || e instanceof ModuleElement) {
110-
return (ModuleElement) e;
119+
private static boolean buildPluginAvailable() {
120+
121+
return resource("target/avaje-plugin-exists.txt", "/target/classes")
122+
|| resource("build/avaje-plugin-exists.txt", "/build/classes/java/main");
123+
}
124+
125+
private static boolean resource(String relativeName, String replace) {
126+
try (var inputStream =
127+
new URI(
128+
filer()
129+
.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName)
130+
.toUri()
131+
.toString()
132+
.replace(replace, ""))
133+
.toURL()
134+
.openStream()) {
135+
136+
return inputStream.available() > 0;
137+
} catch (IOException | URISyntaxException e) {
138+
return false;
111139
}
112-
return getModuleElement(e.getEnclosingElement());
113140
}
114141

115142
static void clear() {

0 commit comments

Comments
 (0)