5
5
import com .cosium .json_schema_to_java_record_api .GenerateRecordsFromJsonSchemas ;
6
6
import com .palantir .javapoet .AnnotationSpec ;
7
7
import com .palantir .javapoet .ClassName ;
8
+ import com .palantir .javapoet .CodeBlock ;
9
+ import com .palantir .javapoet .FieldSpec ;
8
10
import com .palantir .javapoet .JavaFile ;
11
+ import com .palantir .javapoet .MethodSpec ;
12
+ import com .palantir .javapoet .ParameterizedTypeName ;
9
13
import com .palantir .javapoet .TypeSpec ;
10
14
import java .io .IOException ;
11
15
import java .io .UncheckedIOException ;
16
+ import java .util .HashMap ;
17
+ import java .util .Map ;
18
+ import java .util .Optional ;
19
+ import java .util .function .Predicate ;
12
20
import javax .annotation .processing .Filer ;
13
21
import javax .annotation .processing .FilerException ;
14
22
import javax .annotation .processing .Generated ;
23
+ import javax .lang .model .element .Modifier ;
15
24
import javax .tools .StandardLocation ;
25
+ import org .jspecify .annotations .Nullable ;
16
26
import org .slf4j .Logger ;
17
27
import org .slf4j .LoggerFactory ;
18
28
19
29
/**
20
30
* @author Réda Housni Alaoui
21
31
*/
22
- class JavaTypes {
32
+ class JavaTypes implements AutoCloseable {
23
33
24
34
private static final Logger LOGGER = LoggerFactory .getLogger (JavaTypes .class );
25
35
26
36
private final Filer filer ;
37
+ @ Nullable private final ClassName reportClassName ;
38
+ private final Map <String , ClassName > classNameBySchemaId = new HashMap <>();
27
39
28
- JavaTypes (Filer filer ) {
40
+ JavaTypes (Filer filer , GenerateRecordsFromJsonSchemas annotation ) {
29
41
this .filer = requireNonNull (filer );
42
+ reportClassName =
43
+ Optional .of (annotation .reportClassQualifiedName ())
44
+ .filter (Predicate .not (String ::isBlank ))
45
+ .map (ClassName ::bestGuess )
46
+ .orElse (null );
30
47
}
31
48
32
49
public boolean existsOnClassPath (ClassName className ) {
@@ -39,12 +56,16 @@ public boolean existsOnClassPath(ClassName className) {
39
56
}
40
57
}
41
58
42
- public void write (String packageName , TypeSpec .Builder typeSpecBuilder ) {
43
- typeSpecBuilder .addAnnotation (
44
- AnnotationSpec .builder (Generated .class )
45
- .addMember ("value" , "$S" , GenerateRecordsFromJsonSchemas .class .getName ())
46
- .build ());
47
- write (JavaFile .builder (packageName , typeSpecBuilder .build ()).build ());
59
+ public void write (
60
+ @ Nullable String schemaId , String packageName , TypeSpec .Builder typeSpecBuilder ) {
61
+
62
+ TypeSpec typeSpec = markAsGenerated (typeSpecBuilder ).build ();
63
+
64
+ if (schemaId != null ) {
65
+ classNameBySchemaId .put (schemaId , ClassName .get (packageName , typeSpec .name ()));
66
+ }
67
+
68
+ write (JavaFile .builder (packageName , typeSpec ).build ());
48
69
}
49
70
50
71
private void write (JavaFile javaFile ) {
@@ -61,4 +82,48 @@ private void write(JavaFile javaFile) {
61
82
throw new UncheckedIOException (e );
62
83
}
63
84
}
85
+
86
+ @ Override
87
+ public void close () {
88
+ if (reportClassName == null ) {
89
+ return ;
90
+ }
91
+
92
+ TypeSpec .Builder reportBuilder =
93
+ markAsGenerated (TypeSpec .classBuilder (reportClassName ))
94
+ .addModifiers (Modifier .PUBLIC )
95
+ .addModifiers (Modifier .FINAL )
96
+ .addMethod (MethodSpec .constructorBuilder ().addModifiers (Modifier .PRIVATE ).build ());
97
+
98
+ CodeBlock classBySchemaIdFieldEntries =
99
+ classNameBySchemaId .entrySet ().stream ()
100
+ .map (
101
+ schemaIdToClassName ->
102
+ CodeBlock .of (
103
+ "$T.entry($S,$T.class)" ,
104
+ Map .class ,
105
+ schemaIdToClassName .getKey (),
106
+ schemaIdToClassName .getValue ()))
107
+ .collect (CodeBlock .joining ("," ));
108
+
109
+ FieldSpec classBySchemaIdField =
110
+ FieldSpec .builder (
111
+ ParameterizedTypeName .get (Map .class , String .class , Class .class ), "classBySchemaId" )
112
+ .addModifiers (Modifier .PUBLIC )
113
+ .addModifiers (Modifier .STATIC )
114
+ .addModifiers (Modifier .FINAL )
115
+ .initializer ("$T.ofEntries($L)" , Map .class , classBySchemaIdFieldEntries )
116
+ .build ();
117
+
118
+ reportBuilder .addField (classBySchemaIdField );
119
+
120
+ write (JavaFile .builder (reportClassName .packageName (), reportBuilder .build ()).build ());
121
+ }
122
+
123
+ private TypeSpec .Builder markAsGenerated (TypeSpec .Builder typeBuilder ) {
124
+ return typeBuilder .addAnnotation (
125
+ AnnotationSpec .builder (Generated .class )
126
+ .addMember ("value" , "$S" , GenerateRecordsFromJsonSchemas .class .getName ())
127
+ .build ());
128
+ }
64
129
}
0 commit comments