55import com .readdle .codegen .anotation .SwiftModule ;
66import com .readdle .codegen .anotation .SwiftReference ;
77import com .readdle .codegen .anotation .SwiftValue ;
8+ import com .readdle .codegen .anotation .TypeMapping ;
89
910import java .io .File ;
1011import java .io .IOException ;
1314import java .util .List ;
1415import java .util .Map ;
1516import java .util .Set ;
17+ import java .util .regex .Pattern ;
1618
1719import javax .annotation .processing .AbstractProcessor ;
1820import javax .annotation .processing .Filer ;
2527import javax .lang .model .element .ElementKind ;
2628import javax .lang .model .element .Name ;
2729import javax .lang .model .element .TypeElement ;
30+ import javax .lang .model .type .MirroredTypeException ;
2831import javax .lang .model .util .Elements ;
2932import javax .lang .model .util .Types ;
3033import javax .tools .Diagnostic ;
@@ -45,7 +48,8 @@ interface WritableElement {
4548 private Messager messager ;
4649
4750 private String moduleName ;
48- private String [] importPackages ;
51+ String [] importPackages ;
52+ HashMap <String , String > customTypeMappings = new HashMap <>();
4953
5054 @ Override
5155 public synchronized void init (ProcessingEnvironment processingEnv ) {
@@ -103,6 +107,22 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
103107 SwiftModule swiftModule = annotatedElement .getAnnotation (SwiftModule .class );
104108 moduleName = swiftModule .moduleName ();
105109 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+ }
106126 }
107127
108128 if (moduleName == null || importPackages == null ) {
@@ -120,7 +140,7 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
120140 TypeElement typeElement = (TypeElement ) annotatedElement ;
121141
122142 try {
123- SwiftValueDescriptor swiftValueDescriptor = new SwiftValueDescriptor (typeElement , filer , importPackages );
143+ SwiftValueDescriptor swiftValueDescriptor = new SwiftValueDescriptor (typeElement , filer , this );
124144 swiftValues .put (swiftValueDescriptor .getSwiftType (), swiftValueDescriptor );
125145 }
126146 catch (IllegalArgumentException e ) {
@@ -141,7 +161,7 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
141161 TypeElement typeElement = (TypeElement ) annotatedElement ;
142162
143163 try {
144- SwiftReferenceDescriptor swiftReferenceDescriptor = new SwiftReferenceDescriptor (typeElement , filer , importPackages );
164+ SwiftReferenceDescriptor swiftReferenceDescriptor = new SwiftReferenceDescriptor (typeElement , filer , this );
145165 swiftReferences .put (swiftReferenceDescriptor .getSwiftType (), swiftReferenceDescriptor );
146166 }
147167 catch (IllegalArgumentException e ) {
@@ -162,7 +182,7 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
162182 TypeElement typeElement = (TypeElement ) annotatedElement ;
163183
164184 try {
165- SwiftDelegateDescriptor delegateDescriptor = new SwiftDelegateDescriptor (typeElement , filer , importPackages );
185+ SwiftDelegateDescriptor delegateDescriptor = new SwiftDelegateDescriptor (typeElement , filer , this );
166186 swiftDelegates .put (delegateDescriptor .simpleTypeName , delegateDescriptor );
167187 }
168188 catch (IllegalArgumentException e ) {
@@ -188,7 +208,7 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
188208 TypeElement typeElement = (TypeElement ) annotatedElement ;
189209
190210 try {
191- SwiftBlockDescriptor blockDescriptor = new SwiftBlockDescriptor (typeElement , filer , importPackages );
211+ SwiftBlockDescriptor blockDescriptor = new SwiftBlockDescriptor (typeElement , filer , this );
192212 swiftBlocks .put (blockDescriptor .simpleTypeName , blockDescriptor );
193213 }
194214 catch (IllegalArgumentException e ) {
@@ -311,8 +331,71 @@ static boolean isNullable(Element element) {
311331 static String replaceLast (String text , char replace , char replacement ) {
312332 int index = text .lastIndexOf (replace );
313333 if (index >= 0 ) {
314- return text .substring (0 , index ) + replacement + text .substring (index + 1 , text . length () );
334+ return text .substring (0 , index ) + replacement + text .substring (index + 1 );
315335 }
316336 return text ;
317337 }
338+
339+ public SwiftEnvironment .Type parseJavaType (String javaType ) {
340+ if (customTypeMappings .containsKey (javaType )) {
341+ return new SwiftEnvironment .Type (customTypeMappings .get (javaType ), javaType );
342+ }
343+ switch (javaType ) {
344+ case "void" :
345+ return null ;
346+ case "java.lang.Integer" :
347+ return new SwiftEnvironment .Type ("Int" , javaType );
348+ case "java.lang.Byte" :
349+ return new SwiftEnvironment .Type ("Int8" , javaType );
350+ case "java.lang.Short" :
351+ return new SwiftEnvironment .Type ("Int16" , javaType );
352+ case "java.lang.Long" :
353+ return new SwiftEnvironment .Type ("Int64" , javaType );
354+ case "java.math.BigInteger" :
355+ return new SwiftEnvironment .Type ("UInt64" , javaType );
356+ case "java.lang.Boolean" :
357+ return new SwiftEnvironment .Type ("Bool" , javaType );
358+ case "java.lang.String" :
359+ return new SwiftEnvironment .Type ("String" , javaType );
360+ case "android.net.Uri" :
361+ return new SwiftEnvironment .Type ("URL" , javaType );
362+ case "java.util.Date" :
363+ return new SwiftEnvironment .Type ("Date" , javaType );
364+ case "java.nio.ByteBuffer" :
365+ return new SwiftEnvironment .Type ("Data" , javaType );
366+ case "java.lang.Exception" :
367+ return new SwiftEnvironment .Type ("Error" , javaType , "NSError" );
368+ default :
369+ try {
370+ if (javaType .startsWith ("java.util.ArrayList<" )) {
371+ SwiftEnvironment .Type subType = parseJavaType (javaType .substring ("java.util.ArrayList<" .length (), javaType .length () - 1 ));
372+ return new SwiftEnvironment .Type ("[" + subType .swiftType + "]" , javaType );
373+ }
374+ else if (javaType .startsWith ("java.util.HashSet<" )) {
375+ SwiftEnvironment .Type subType = parseJavaType (javaType .substring ("java.util.HashSet<" .length (), javaType .length () - 1 ));
376+ return new SwiftEnvironment .Type ("Set<" + subType .swiftType + ">" , javaType );
377+ }
378+ else if (javaType .startsWith ("java.util.HashMap<" )) {
379+ String substring = javaType .substring ("java.util.HashMap<" .length (), javaType .length () - 1 );
380+ int commaIndex = substring .indexOf ("," );
381+ SwiftEnvironment .Type keyType = parseJavaType (substring .substring (0 , commaIndex ));
382+ SwiftEnvironment .Type valueType = parseJavaType (substring .substring (commaIndex + 1 ));
383+ return new SwiftEnvironment .Type ("[" + keyType .swiftType + ":" + valueType .swiftType + "]" , javaType );
384+ }
385+ else {
386+ // Try found enclosing typename
387+ String [] parts = javaType .split (Pattern .quote ("$" ));
388+ if (parts .length == 1 ) {
389+ // If not found enclosing, find typename
390+ parts = javaType .split (Pattern .quote ("." ));
391+ }
392+ String swiftType = parts [parts .length - 1 ];
393+ return new SwiftEnvironment .Type (swiftType , javaType );
394+ }
395+ }
396+ catch (Exception e ) {
397+ throw new IllegalArgumentException (javaType );
398+ }
399+ }
400+ }
318401}
0 commit comments