8
8
import static io .avaje .jsonb .generator .APContext .logWarn ;
9
9
10
10
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 .*;
16
14
import javax .annotation .processing .ProcessingEnvironment ;
17
15
import javax .lang .model .element .Element ;
18
16
import javax .lang .model .element .TypeElement ;
@@ -30,8 +28,7 @@ private static final class Ctx {
30
28
private boolean validated ;
31
29
32
30
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 ;
35
32
}
36
33
}
37
34
@@ -52,20 +49,16 @@ static FileObject createMetaInfWriterFor(String interfaceType) throws IOExceptio
52
49
53
50
static void addImportedPrism (ImportPrism prism , Element element ) {
54
51
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" );
57
53
return ;
58
54
}
59
55
final var json = CTX .get ().importedJsonMap ;
60
56
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
+ });
69
62
}
70
63
71
64
static Optional <JsonPrism > importedJson (TypeElement type ) {
@@ -76,35 +69,54 @@ static List<SubTypePrism> importedSubtypes(TypeElement type) {
76
69
return CTX .get ().importedSubtypeMap .getOrDefault (type .asType ().toString (), List .of ());
77
70
}
78
71
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
+
79
92
static void validateModule (String fqn ) {
80
93
var module = getProjectModuleElement ();
81
94
if (module != null && !CTX .get ().validated && !module .isUnnamed ()) {
82
-
83
95
var injectPresent = CTX .get ().injectPresent ;
84
96
CTX .get ().validated = true ;
85
97
86
98
try (var reader = getModuleInfoReader ()) {
87
-
88
99
var moduleInfo = new ModuleInfoReader (module , reader );
89
100
90
101
boolean noInjectPlugin =
91
- injectPresent && !moduleInfo .containsOnModulePath ("io.avaje.jsonb.plugin" );
102
+ injectPresent && !moduleInfo .containsOnModulePath ("io.avaje.jsonb.plugin" );
92
103
93
104
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 ));
97
108
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 );
101
112
}
102
113
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 );
108
120
}
109
121
110
122
} catch (Exception e ) {
0 commit comments