Skip to content

Commit 95c29e9

Browse files
SentryManrbygrave
andauthored
Support the Avaje Build Plugin (#214)
* support the build plugin * gradle * Change resourceExists() for plugin check and format --------- Co-authored-by: Rob Bygrave <robin.bygrave@gmail.com>
1 parent a475181 commit 95c29e9

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

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

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
import static io.avaje.jsonb.generator.APContext.logWarn;
99

1010
import java.io.IOException;
11-
import java.util.HashMap;
12-
import java.util.List;
13-
import java.util.Map;
14-
import java.util.Optional;
15-
11+
import java.net.URI;
12+
import java.nio.file.Paths;
13+
import java.util.*;
1614
import javax.annotation.processing.ProcessingEnvironment;
1715
import javax.lang.model.element.Element;
1816
import javax.lang.model.element.TypeElement;
@@ -30,8 +28,7 @@ private static final class Ctx {
3028
private boolean validated;
3129

3230
Ctx(ProcessingEnvironment env) {
33-
this.injectPresent =
34-
env.getElementUtils().getTypeElement("io.avaje.inject.Component") != null;
31+
this.injectPresent = env.getElementUtils().getTypeElement("io.avaje.inject.Component") != null;
3532
}
3633
}
3734

@@ -52,20 +49,16 @@ static FileObject createMetaInfWriterFor(String interfaceType) throws IOExceptio
5249

5350
static void addImportedPrism(ImportPrism prism, Element element) {
5451
if (!prism.subtypes().isEmpty() && prism.value().size() > 1) {
55-
logError(
56-
element, "subtypes cannot be used when an import annotation imports more than one class");
52+
logError(element, "subtypes cannot be used when an import annotation imports more than one class");
5753
return;
5854
}
5955
final var json = CTX.get().importedJsonMap;
6056
final var subtypes = CTX.get().importedSubtypeMap;
61-
prism
62-
.value()
63-
.forEach(
64-
m -> {
65-
final var type = m.toString();
66-
json.put(type, prism.jsonSettings());
67-
subtypes.put(type, prism.subtypes());
68-
});
57+
prism.value().forEach(m -> {
58+
final var type = m.toString();
59+
json.put(type, prism.jsonSettings());
60+
subtypes.put(type, prism.subtypes());
61+
});
6962
}
7063

7164
static Optional<JsonPrism> importedJson(TypeElement type) {
@@ -76,35 +69,54 @@ static List<SubTypePrism> importedSubtypes(TypeElement type) {
7669
return CTX.get().importedSubtypeMap.getOrDefault(type.asType().toString(), List.of());
7770
}
7871

72+
private static boolean buildPluginAvailable() {
73+
return resourceExists("target/avaje-plugin-exists.txt")
74+
|| resourceExists("build/avaje-plugin-exists.txt");
75+
}
76+
77+
private static boolean resourceExists(String relativeName) {
78+
try {
79+
final String resource =
80+
filer()
81+
.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName)
82+
.toUri()
83+
.toString()
84+
.replaceFirst("/target/classes", "")
85+
.replaceFirst("/build/classes/java/main", "");
86+
return Paths.get(new URI(resource)).toFile().exists();
87+
} catch (final Exception e) {
88+
return false;
89+
}
90+
}
91+
7992
static void validateModule(String fqn) {
8093
var module = getProjectModuleElement();
8194
if (module != null && !CTX.get().validated && !module.isUnnamed()) {
82-
8395
var injectPresent = CTX.get().injectPresent;
8496
CTX.get().validated = true;
8597

8698
try (var reader = getModuleInfoReader()) {
87-
8899
var moduleInfo = new ModuleInfoReader(module, reader);
89100

90101
boolean noInjectPlugin =
91-
injectPresent && !moduleInfo.containsOnModulePath("io.avaje.jsonb.plugin");
102+
injectPresent && !moduleInfo.containsOnModulePath("io.avaje.jsonb.plugin");
92103

93104
var noProvides =
94-
moduleInfo.provides().stream()
95-
.flatMap(s -> s.implementations().stream())
96-
.noneMatch(s -> s.contains(fqn));
105+
moduleInfo.provides().stream()
106+
.flatMap(s -> s.implementations().stream())
107+
.noneMatch(s -> s.contains(fqn));
97108

98-
if (noProvides) {
99-
logError(
100-
module, "Missing `provides io.avaje.jsonb.Jsonb.GeneratedComponent with %s;`", fqn);
109+
var buildPluginAvailable = buildPluginAvailable();
110+
if (noProvides && !buildPluginAvailable) {
111+
logError(module, "Missing `provides io.avaje.jsonb.Jsonb.GeneratedComponent with %s;`", fqn);
101112
}
102113

103-
if (noInjectPlugin) {
104-
logWarn(
105-
module,
106-
"`requires io.avaje.jsonb.plugin` must be explicity added or else avaje-inject may fail to detect and wire the default Jsonb instance",
107-
fqn);
114+
final var noDirectJsonb =
115+
moduleInfo.requires().stream()
116+
.noneMatch(r -> r.getDependency().getQualifiedName().contentEquals("io.avaje.jsonb"));
117+
118+
if (noInjectPlugin && (!buildPluginAvailable || noDirectJsonb)) {
119+
logWarn(module, "`requires io.avaje.jsonb.plugin` must be explicity added or else avaje-inject may fail to detect and wire the default Jsonb instance", fqn);
108120
}
109121

110122
} catch (Exception e) {

0 commit comments

Comments
 (0)