5
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 ;
8
9
9
10
import java .io .File ;
10
11
import java .io .IOException ;
13
14
import java .util .List ;
14
15
import java .util .Map ;
15
16
import java .util .Set ;
17
+ import java .util .regex .Pattern ;
16
18
17
19
import javax .annotation .processing .AbstractProcessor ;
18
20
import javax .annotation .processing .Filer ;
25
27
import javax .lang .model .element .ElementKind ;
26
28
import javax .lang .model .element .Name ;
27
29
import javax .lang .model .element .TypeElement ;
30
+ import javax .lang .model .type .MirroredTypeException ;
28
31
import javax .lang .model .util .Elements ;
29
32
import javax .lang .model .util .Types ;
30
33
import javax .tools .Diagnostic ;
@@ -45,7 +48,8 @@ interface WritableElement {
45
48
private Messager messager ;
46
49
47
50
private String moduleName ;
48
- private String [] importPackages ;
51
+ String [] importPackages ;
52
+ HashMap <String , String > customTypeMappings = new HashMap <>();
49
53
50
54
@ Override
51
55
public synchronized void init (ProcessingEnvironment processingEnv ) {
@@ -103,6 +107,22 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
103
107
SwiftModule swiftModule = annotatedElement .getAnnotation (SwiftModule .class );
104
108
moduleName = swiftModule .moduleName ();
105
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
+ }
106
126
}
107
127
108
128
if (moduleName == null || importPackages == null ) {
@@ -120,7 +140,7 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
120
140
TypeElement typeElement = (TypeElement ) annotatedElement ;
121
141
122
142
try {
123
- SwiftValueDescriptor swiftValueDescriptor = new SwiftValueDescriptor (typeElement , filer , importPackages );
143
+ SwiftValueDescriptor swiftValueDescriptor = new SwiftValueDescriptor (typeElement , filer , this );
124
144
swiftValues .put (swiftValueDescriptor .getSwiftType (), swiftValueDescriptor );
125
145
}
126
146
catch (IllegalArgumentException e ) {
@@ -141,7 +161,7 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
141
161
TypeElement typeElement = (TypeElement ) annotatedElement ;
142
162
143
163
try {
144
- SwiftReferenceDescriptor swiftReferenceDescriptor = new SwiftReferenceDescriptor (typeElement , filer , importPackages );
164
+ SwiftReferenceDescriptor swiftReferenceDescriptor = new SwiftReferenceDescriptor (typeElement , filer , this );
145
165
swiftReferences .put (swiftReferenceDescriptor .getSwiftType (), swiftReferenceDescriptor );
146
166
}
147
167
catch (IllegalArgumentException e ) {
@@ -162,7 +182,7 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
162
182
TypeElement typeElement = (TypeElement ) annotatedElement ;
163
183
164
184
try {
165
- SwiftDelegateDescriptor delegateDescriptor = new SwiftDelegateDescriptor (typeElement , filer , importPackages );
185
+ SwiftDelegateDescriptor delegateDescriptor = new SwiftDelegateDescriptor (typeElement , filer , this );
166
186
swiftDelegates .put (delegateDescriptor .simpleTypeName , delegateDescriptor );
167
187
}
168
188
catch (IllegalArgumentException e ) {
@@ -188,7 +208,7 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
188
208
TypeElement typeElement = (TypeElement ) annotatedElement ;
189
209
190
210
try {
191
- SwiftBlockDescriptor blockDescriptor = new SwiftBlockDescriptor (typeElement , filer , importPackages );
211
+ SwiftBlockDescriptor blockDescriptor = new SwiftBlockDescriptor (typeElement , filer , this );
192
212
swiftBlocks .put (blockDescriptor .simpleTypeName , blockDescriptor );
193
213
}
194
214
catch (IllegalArgumentException e ) {
@@ -311,8 +331,71 @@ static boolean isNullable(Element element) {
311
331
static String replaceLast (String text , char replace , char replacement ) {
312
332
int index = text .lastIndexOf (replace );
313
333
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 );
315
335
}
316
336
return text ;
317
337
}
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
+ }
318
401
}
0 commit comments