1
1
package com .readdle .codegen ;
2
2
3
+ import com .google .gson .Gson ;
3
4
import com .readdle .codegen .anotation .SwiftBlock ;
4
5
import com .readdle .codegen .anotation .SwiftDelegate ;
5
- import com .readdle .codegen .anotation .SwiftModule ;
6
6
import com .readdle .codegen .anotation .SwiftReference ;
7
7
import com .readdle .codegen .anotation .SwiftValue ;
8
- import com .readdle .codegen .anotation .TypeMapping ;
9
8
10
9
import java .io .File ;
11
10
import java .io .IOException ;
12
11
import java .util .HashMap ;
12
+ import java .util .HashSet ;
13
13
import java .util .LinkedHashSet ;
14
14
import java .util .List ;
15
15
import java .util .Map ;
27
27
import javax .lang .model .element .ElementKind ;
28
28
import javax .lang .model .element .Name ;
29
29
import javax .lang .model .element .TypeElement ;
30
- import javax .lang .model .type .MirroredTypeException ;
31
30
import javax .lang .model .util .Elements ;
32
31
import javax .lang .model .util .Types ;
33
32
import javax .tools .Diagnostic ;
@@ -41,15 +40,14 @@ interface WritableElement {
41
40
}
42
41
43
42
public static final String FOLDER = "SwiftGenerated" ;
43
+ public static final String PACKAGE_OPTION = "com.readdle.codegen.package" ;
44
44
45
45
private Types typeUtils ;
46
46
private Elements elementUtils ;
47
47
private Filer filer ;
48
48
private Messager messager ;
49
49
50
- private String moduleName ;
51
- String [] importPackages ;
52
- HashMap <String , String > customTypeMappings = new HashMap <>();
50
+ SwiftModuleDescriptor moduleDescriptor ;
53
51
54
52
@ Override
55
53
public synchronized void init (ProcessingEnvironment processingEnv ) {
@@ -59,12 +57,38 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
59
57
filer = processingEnv .getFiler ();
60
58
messager = processingEnv .getMessager ();
61
59
60
+ messager .printMessage (Diagnostic .Kind .NOTE , "JavaSwiftProcessor init started" );
61
+
62
+ String packageJson = processingEnv .getOptions ().get (PACKAGE_OPTION );
63
+
64
+ moduleDescriptor = new Gson ().fromJson (packageJson , SwiftModuleDescriptor .class );
65
+ if (moduleDescriptor == null ) {
66
+ messager .printMessage (Diagnostic .Kind .ERROR , "No package description with option: com.readdle.codegen.package" , null );
67
+ return ;
68
+ }
69
+
70
+ messager .printMessage (Diagnostic .Kind .NOTE , "Package moduleName: " + moduleDescriptor .moduleName );
71
+
72
+ if (moduleDescriptor .importPackages != null ) {
73
+ for (String anImport : moduleDescriptor .importPackages ) {
74
+ messager .printMessage (Diagnostic .Kind .NOTE , "Package import: " + anImport );
75
+ }
76
+ }
77
+
78
+ if (moduleDescriptor .customTypeMappings != null ) {
79
+ for (String key : moduleDescriptor .customTypeMappings .keySet ()) {
80
+ messager .printMessage (Diagnostic .Kind .NOTE , "Package custom mapping: " + key + " -> " + moduleDescriptor .customTypeMappings .get (key ));
81
+ }
82
+ }
83
+
62
84
try {
63
85
generateJavaSwift (filer );
64
86
} catch (IOException e ) {
65
87
e .printStackTrace ();
66
88
error (null , "Can't write to file: " + e .getMessage ());
67
89
}
90
+
91
+ messager .printMessage (Diagnostic .Kind .NOTE , "JavaSwiftProcessor init finished successfully" );
68
92
}
69
93
70
94
@ Override
@@ -82,6 +106,13 @@ public SourceVersion getSupportedSourceVersion() {
82
106
return SourceVersion .latestSupported ();
83
107
}
84
108
109
+ @ Override
110
+ public Set <String > getSupportedOptions () {
111
+ Set <String > options = new HashSet <>(super .getSupportedOptions ());
112
+ options .add (PACKAGE_OPTION );
113
+ return options ;
114
+ }
115
+
85
116
@ Override
86
117
public boolean process (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
87
118
try {
@@ -96,36 +127,23 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
96
127
97
128
private boolean processImpl (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
98
129
Filer filer = processingEnv .getFiler ();
99
- messager .printMessage (Diagnostic .Kind .NOTE , "Start SwiftJava code generation:" );
130
+ messager .printMessage (Diagnostic .Kind .NOTE , "JavaSwiftProcessor start code generation" );
131
+
132
+ messager .printMessage (Diagnostic .Kind .NOTE , "SwiftValue to process: "
133
+ + roundEnv .getElementsAnnotatedWith (SwiftValue .class ).size ());
134
+ messager .printMessage (Diagnostic .Kind .NOTE , "SwiftReference to process: "
135
+ + roundEnv .getElementsAnnotatedWith (SwiftReference .class ).size ());
136
+ messager .printMessage (Diagnostic .Kind .NOTE , "SwiftDelegate to process: "
137
+ + roundEnv .getElementsAnnotatedWith (SwiftDelegate .class ).size ());
138
+ messager .printMessage (Diagnostic .Kind .NOTE , "SwiftBlock to process: "
139
+ + roundEnv .getElementsAnnotatedWith (SwiftBlock .class ).size ());
100
140
101
141
Map <String , SwiftValueDescriptor > swiftValues = new HashMap <>();
102
142
Map <String , SwiftReferenceDescriptor > swiftReferences = new HashMap <>();
103
143
Map <String , SwiftDelegateDescriptor > swiftDelegates = new HashMap <>();
104
144
Map <String , SwiftBlockDescriptor > swiftBlocks = new HashMap <>();
105
145
106
- for (Element annotatedElement : roundEnv .getElementsAnnotatedWith (SwiftModule .class )) {
107
- SwiftModule swiftModule = annotatedElement .getAnnotation (SwiftModule .class );
108
- moduleName = swiftModule .moduleName ();
109
- importPackages = swiftModule .importPackages ();
110
- TypeMapping [] customTypeMappings = swiftModule .customTypeMappings ();
111
- for (TypeMapping customTypeMapping : customTypeMappings ) {
112
- try {
113
- Class clazz = customTypeMapping .javaClass ();
114
- String canonicalName = clazz .getCanonicalName ();
115
- String swiftType = customTypeMapping .swiftType ();
116
- messager .printMessage (Diagnostic .Kind .NOTE , "Added custom mapping from " + canonicalName + " to " + swiftType );
117
- this .customTypeMappings .put (canonicalName , customTypeMapping .swiftType ());
118
- }
119
- catch (MirroredTypeException mirroredTypeException ) {
120
- String canonicalName = mirroredTypeException .getTypeMirror ().toString ();
121
- String swiftType = customTypeMapping .swiftType ();
122
- messager .printMessage (Diagnostic .Kind .NOTE , "Added custom mapping from " + canonicalName + " to " + swiftType );
123
- this .customTypeMappings .put (canonicalName , customTypeMapping .swiftType ());
124
- }
125
- }
126
- }
127
-
128
- if (moduleName == null || importPackages == null ) {
146
+ if (moduleDescriptor == null ) {
129
147
messager .printMessage (Diagnostic .Kind .ERROR , "No package description with SwiftModule.class" , null );
130
148
}
131
149
@@ -279,16 +297,16 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
279
297
}
280
298
}
281
299
282
- messager .printMessage (Diagnostic .Kind .NOTE , "SwiftJava finished successfully!" );
300
+ messager .printMessage (Diagnostic .Kind .NOTE , "JavaSwiftProcessor finished successfully!" );
283
301
284
302
return false ;
285
303
}
286
304
287
305
private void generateJavaSwift (Filer filer ) throws IOException {
288
- String swiftFilePath = filer .createResource (StandardLocation .SOURCE_OUTPUT , FOLDER , "SwiftJava.swift" , ( Element ) null ).toUri ().getPath ();
306
+ String swiftFilePath = filer .getResource (StandardLocation .SOURCE_OUTPUT , FOLDER , "SwiftJava.swift" ).toUri ().getPath ();
289
307
File swiftExtensionFile = new File (swiftFilePath );
290
308
swiftExtensionFile .getParentFile ().mkdir ();
291
- messager .printMessage (Diagnostic .Kind .NOTE , "SwiftJava will generate sources int0 : " + swiftExtensionFile .getParent ());
309
+ messager .printMessage (Diagnostic .Kind .NOTE , "JavaSwiftProcessor will generate sources at : " + swiftExtensionFile .getParent ());
292
310
SwiftWriter swiftWriter = new SwiftWriter (swiftExtensionFile );
293
311
swiftWriter .emitImports (new String [0 ]);
294
312
swiftWriter .emitEmptyLine ();
@@ -337,8 +355,8 @@ static String replaceLast(String text, char replace, char replacement) {
337
355
}
338
356
339
357
public SwiftEnvironment .Type parseJavaType (String javaType ) {
340
- if (customTypeMappings .containsKey (javaType )) {
341
- return new SwiftEnvironment .Type (customTypeMappings .get (javaType ), javaType );
358
+ if (moduleDescriptor . customTypeMappings .containsKey (javaType )) {
359
+ return new SwiftEnvironment .Type (moduleDescriptor . customTypeMappings .get (javaType ), javaType );
342
360
}
343
361
switch (javaType ) {
344
362
case "void" :
0 commit comments