11package com .readdle .codegen ;
22
3+ import com .google .gson .Gson ;
34import com .readdle .codegen .anotation .SwiftBlock ;
45import com .readdle .codegen .anotation .SwiftDelegate ;
5- import com .readdle .codegen .anotation .SwiftModule ;
66import com .readdle .codegen .anotation .SwiftReference ;
77import com .readdle .codegen .anotation .SwiftValue ;
8- import com .readdle .codegen .anotation .TypeMapping ;
98
109import java .io .File ;
1110import java .io .IOException ;
1211import java .util .HashMap ;
12+ import java .util .HashSet ;
1313import java .util .LinkedHashSet ;
1414import java .util .List ;
1515import java .util .Map ;
2727import javax .lang .model .element .ElementKind ;
2828import javax .lang .model .element .Name ;
2929import javax .lang .model .element .TypeElement ;
30- import javax .lang .model .type .MirroredTypeException ;
3130import javax .lang .model .util .Elements ;
3231import javax .lang .model .util .Types ;
3332import javax .tools .Diagnostic ;
@@ -41,15 +40,14 @@ interface WritableElement {
4140 }
4241
4342 public static final String FOLDER = "SwiftGenerated" ;
43+ public static final String PACKAGE_OPTION = "com.readdle.codegen.package" ;
4444
4545 private Types typeUtils ;
4646 private Elements elementUtils ;
4747 private Filer filer ;
4848 private Messager messager ;
4949
50- private String moduleName ;
51- String [] importPackages ;
52- HashMap <String , String > customTypeMappings = new HashMap <>();
50+ SwiftModuleDescriptor moduleDescriptor ;
5351
5452 @ Override
5553 public synchronized void init (ProcessingEnvironment processingEnv ) {
@@ -59,12 +57,38 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
5957 filer = processingEnv .getFiler ();
6058 messager = processingEnv .getMessager ();
6159
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+
6284 try {
6385 generateJavaSwift (filer );
6486 } catch (IOException e ) {
6587 e .printStackTrace ();
6688 error (null , "Can't write to file: " + e .getMessage ());
6789 }
90+
91+ messager .printMessage (Diagnostic .Kind .NOTE , "JavaSwiftProcessor init finished successfully" );
6892 }
6993
7094 @ Override
@@ -82,6 +106,13 @@ public SourceVersion getSupportedSourceVersion() {
82106 return SourceVersion .latestSupported ();
83107 }
84108
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+
85116 @ Override
86117 public boolean process (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
87118 try {
@@ -96,36 +127,23 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
96127
97128 private boolean processImpl (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
98129 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 ());
100140
101141 Map <String , SwiftValueDescriptor > swiftValues = new HashMap <>();
102142 Map <String , SwiftReferenceDescriptor > swiftReferences = new HashMap <>();
103143 Map <String , SwiftDelegateDescriptor > swiftDelegates = new HashMap <>();
104144 Map <String , SwiftBlockDescriptor > swiftBlocks = new HashMap <>();
105145
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 ) {
129147 messager .printMessage (Diagnostic .Kind .ERROR , "No package description with SwiftModule.class" , null );
130148 }
131149
@@ -279,16 +297,16 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
279297 }
280298 }
281299
282- messager .printMessage (Diagnostic .Kind .NOTE , "SwiftJava finished successfully!" );
300+ messager .printMessage (Diagnostic .Kind .NOTE , "JavaSwiftProcessor finished successfully!" );
283301
284302 return false ;
285303 }
286304
287305 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 ();
289307 File swiftExtensionFile = new File (swiftFilePath );
290308 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 ());
292310 SwiftWriter swiftWriter = new SwiftWriter (swiftExtensionFile );
293311 swiftWriter .emitImports (new String [0 ]);
294312 swiftWriter .emitEmptyLine ();
@@ -337,8 +355,8 @@ static String replaceLast(String text, char replace, char replacement) {
337355 }
338356
339357 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 );
342360 }
343361 switch (javaType ) {
344362 case "void" :
0 commit comments