Skip to content

Commit 3e41c19

Browse files
committed
Dev: fix full Kotlin support
add more tests
1 parent 512395c commit 3e41c19

50 files changed

Lines changed: 3341 additions & 61 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/src/main/java/com/readdle/codegen/JavaSwiftProcessor.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,11 @@ private void generateJavaSwift(Filer filer) throws IOException {
330330
swiftWriter.close();
331331
}
332332

333-
private void note(String msg) {
333+
void note(String msg) {
334334
messager.printMessage(Diagnostic.Kind.WARNING, msg);
335335
}
336336

337-
private void error(Element e, String msg, Object... args) {
337+
void error(Element e, String msg, Object... args) {
338338
messager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args), e);
339339
}
340340

@@ -362,6 +362,17 @@ boolean isNullable(Element element) {
362362
return true;
363363
}
364364

365+
boolean isUnsigned(Element element) {
366+
List<? extends AnnotationMirror> mirrors = element.getAnnotationMirrors();
367+
for (AnnotationMirror mirror : mirrors) {
368+
Name simpleName = mirror.getAnnotationType().asElement().getSimpleName();
369+
if (simpleName.contentEquals("Unsigned")) {
370+
return true;
371+
}
372+
}
373+
return false;
374+
}
375+
365376
static String replaceLast(String text, char replace, char replacement) {
366377
int index = text.lastIndexOf(replace);
367378
if (index >= 0) {

compiler/src/main/java/com/readdle/codegen/SwiftBlockDescriptor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ class SwiftBlockDescriptor {
8080
// Except init. We generate it's manually
8181
this.funcName = executableElement.getSimpleName().toString();
8282
this.isThrown = executableElement.getThrownTypes() != null && executableElement.getThrownTypes().size() > 0;
83-
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString());
8483
this.isReturnTypeOptional = processor.isNullable(executableElement);
84+
boolean isReturnTypeUnsigned = processor.isUnsigned(executableElement);
85+
if (isReturnTypeUnsigned) {
86+
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString()).makeUnsigned();
87+
}
88+
else {
89+
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString());
90+
}
8591

8692
this.sig = "(";
8793

compiler/src/main/java/com/readdle/codegen/SwiftCallbackFuncDescriptor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ public class SwiftCallbackFuncDescriptor {
3434

3535
this.isStatic = executableElement.getModifiers().contains(Modifier.STATIC);
3636
this.isThrown = executableElement.getThrownTypes() != null && executableElement.getThrownTypes().size() > 0;
37-
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString());
3837
this.isReturnTypeOptional = processor.isNullable(executableElement);
38+
boolean isReturnTypeUnsigned = processor.isUnsigned(executableElement);
39+
if (isReturnTypeUnsigned) {
40+
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString()).makeUnsigned();
41+
}
42+
else {
43+
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString());
44+
}
3945

4046
StringBuilder signatureBuilder = new StringBuilder("(");
4147

compiler/src/main/java/com/readdle/codegen/SwiftEnvironment.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ boolean isPrimitiveType() {
3838
swiftType.equals("UInt8") ||
3939
swiftType.equals("UInt16") ||
4040
swiftType.equals("UInt32") ||
41+
swiftType.equals("UInt64") ||
4142
swiftType.equals("Float") ||
4243
swiftType.equals("Double");
4344
}
@@ -52,16 +53,17 @@ String javaSigType(boolean isOptional) {
5253
return "jboolean";
5354
case "Int":
5455
case "Int32":
55-
case "UInt16":
56+
case "UInt":
57+
case "UInt32":
5658
return "jint";
5759
case "Int8":
60+
case "UInt8":
5861
return "jbyte";
5962
case "Int16":
60-
case "UInt8":
63+
case "UInt16":
6164
return "jshort";
6265
case "Int64":
63-
case "UInt":
64-
case "UInt32":
66+
case "UInt64":
6567
return "jlong";
6668
case "Float":
6769
return "jfloat";
@@ -83,16 +85,17 @@ String returnTypeFunc(boolean isOptional) {
8385
return "CallBooleanMethod";
8486
case "Int":
8587
case "Int32":
86-
case "UInt16":
88+
case "UInt":
89+
case "UInt32":
8790
return "CallIntMethod";
8891
case "Int8":
92+
case "UInt8":
8993
return "CallByteMethod";
9094
case "Int16":
91-
case "UInt8":
95+
case "UInt16":
9296
return "CallShortMethod";
9397
case "Int64":
94-
case "UInt":
95-
case "UInt32":
98+
case "UInt64":
9699
return "CallLongMethod";
97100
case "Float":
98101
return "CallFloatMethod";
@@ -114,16 +117,17 @@ String staticReturnTypeFunc(boolean isOptional) {
114117
return "CallStaticBooleanMethod";
115118
case "Int":
116119
case "Int32":
117-
case "UInt16":
120+
case "UInt":
121+
case "UInt32":
118122
return "CallStaticIntMethod";
119123
case "Int8":
124+
case "UInt8":
120125
return "CallStaticByteMethod";
121126
case "Int16":
122-
case "UInt8":
127+
case "UInt16":
123128
return "CallStaticShortMethod";
124129
case "Int64":
125-
case "UInt":
126-
case "UInt32":
130+
case "UInt64":
127131
return "CallStaticLongMethod";
128132
case "Float":
129133
return "CallStaticFloatMethod";
@@ -134,6 +138,19 @@ String staticReturnTypeFunc(boolean isOptional) {
134138
}
135139
}
136140
}
141+
142+
Type makeUnsigned() {
143+
switch (swiftType) {
144+
case "Int":
145+
case "Int8":
146+
case "Int16":
147+
case "Int32":
148+
case "Int64":
149+
return new Type("U" + swiftType, javaType, "U" + swiftConstructorType);
150+
default:
151+
throw new IllegalStateException(swiftType + " can't be unsigned");
152+
}
153+
}
137154
}
138155

139156
}

compiler/src/main/java/com/readdle/codegen/SwiftFuncDescriptor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ class SwiftFuncDescriptor implements JavaSwiftProcessor.WritableElement {
3232

3333
this.isStatic = executableElement.getModifiers().contains(Modifier.STATIC);
3434
this.isThrown = executableElement.getThrownTypes() != null && executableElement.getThrownTypes().size() > 0;
35-
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString());
3635
this.isReturnTypeOptional = processor.isNullable(executableElement);
36+
boolean isReturnTypeUnsigned = processor.isUnsigned(executableElement);
37+
if (isReturnTypeUnsigned) {
38+
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString()).makeUnsigned();
39+
}
40+
else {
41+
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString());
42+
}
3743

3844
int paramsSize = executableElement.getParameters().size();
3945
this.params = new ArrayList<>(paramsSize);

compiler/src/main/java/com/readdle/codegen/SwiftGetterDescriptor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ class SwiftGetterDescriptor implements JavaSwiftProcessor.WritableElement {
1717

1818
private SwiftEnvironment.Type returnSwiftType;
1919
private boolean isReturnTypeOptional;
20+
private boolean isReturnTypeUnsigned;
2021

2122
SwiftGetterDescriptor(ExecutableElement executableElement, SwiftGetter getterAnnotation, JavaSwiftProcessor processor) {
2223
this.javaName = executableElement.getSimpleName().toString();
2324
this.isStatic = executableElement.getModifiers().contains(Modifier.STATIC);
24-
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString());
2525
this.isReturnTypeOptional = processor.isNullable(executableElement);
26+
boolean isReturnTypeUnsigned = processor.isUnsigned(executableElement);
27+
if (isReturnTypeUnsigned) {
28+
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString()).makeUnsigned();
29+
}
30+
else {
31+
this.returnSwiftType = processor.parseJavaType(executableElement.getReturnType().toString());
32+
}
2633

2734
if (executableElement.getThrownTypes().size() != 0) {
2835
throw new SwiftMappingException("Getter can't throw", executableElement);

compiler/src/main/java/com/readdle/codegen/SwiftParamDescriptor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ public class SwiftParamDescriptor {
1313

1414
SwiftParamDescriptor(VariableElement variableElement, JavaSwiftProcessor processor) {
1515
this.name = variableElement.getSimpleName().toString();
16-
this.swiftType = requireNonNull(processor.parseJavaType(Utils.typeToString(variableElement.asType())));
16+
boolean isUnsigned = processor.isUnsigned(variableElement);
17+
if (isUnsigned) {
18+
this.swiftType = requireNonNull(processor.parseJavaType(Utils.typeToString(variableElement.asType()))).makeUnsigned();
19+
}
20+
else {
21+
this.swiftType = requireNonNull(processor.parseJavaType(Utils.typeToString(variableElement.asType())));
22+
}
1723

1824
SwiftBlock swiftParam = variableElement.getAnnotation(SwiftBlock.class);
1925
if (swiftParam != null) {

compiler/src/main/java/com/readdle/codegen/Utils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,14 @@ private static PackageElement getPackage(Element type) {
175175
}
176176

177177
static void handleRuntimeError(SwiftWriter swiftWriter) throws IOException {
178+
swiftWriter.emitStatement("if let codingError = error as? JavaCodingErrorDescription {");
179+
swiftWriter.emitStatement("_ = JNI.api.ThrowNew(JNI.env, SwiftRuntimeErrorClass, codingError.detailedDescription)");
180+
swiftWriter.emitStatement("}");
181+
swiftWriter.emitStatement("else {");
178182
swiftWriter.emitStatement("let errorType = type(of: error)");
179183
swiftWriter.emitStatement("let errorString = \"\\(errorType) (\\(error.localizedDescription))\"");
180184
swiftWriter.emitStatement("_ = JNI.api.ThrowNew(JNI.env, SwiftRuntimeErrorClass, errorString)");
185+
swiftWriter.emitStatement("}");
181186
}
182187

183188
static void handleError(SwiftWriter swiftWriter) throws IOException {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.readdle.codegen.anotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) @Retention(RetentionPolicy.CLASS)
9+
public @interface Unsigned {
10+
11+
String value() default "";
12+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.readdle.swiftjava.sample;
2+
3+
import android.support.test.runner.AndroidJUnit4;
4+
5+
import com.readdle.codegen.anotation.JavaSwift;
6+
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
@RunWith(AndroidJUnit4.class)
13+
public class BoolTests {
14+
15+
@Before
16+
public void setUp() {
17+
System.loadLibrary("SampleAppCore");
18+
JavaSwift.init();
19+
SwiftEnvironment.initEnvironment();
20+
}
21+
22+
@Test
23+
public void testZero() {
24+
Assert.assertTrue(BoolTest.testYes());
25+
}
26+
27+
@Test
28+
public void testMin() {
29+
Assert.assertFalse(BoolTest.testNo());
30+
}
31+
32+
@Test
33+
public void testParam() {
34+
Assert.assertTrue(BoolTest.testParam(true));
35+
Assert.assertFalse(BoolTest.testParam(false));
36+
}
37+
38+
@Test
39+
public void testReturnType() {
40+
Assert.assertTrue(BoolTest.testReturnType());
41+
}
42+
43+
@Test
44+
public void testOptionalParam() {
45+
Assert.assertTrue(BoolTest.testOptionalParam(true));
46+
Assert.assertFalse(BoolTest.testOptionalParam(false));
47+
}
48+
49+
public void testOptionalReturnType() {
50+
Boolean result = BoolTest.testOptionalReturnType();
51+
Assert.assertNotNull(result);
52+
Assert.assertEquals(result, true);
53+
}
54+
55+
@Test
56+
public void testProtocolParam() {
57+
boolean result = BoolTest.testProtocolParam(param -> param);
58+
Assert.assertTrue(result);
59+
}
60+
61+
@Test
62+
public void testProtocolReturnType() {
63+
boolean result = BoolTest.testProtocolReturnType(() -> true);
64+
Assert.assertTrue(result);
65+
}
66+
67+
@Test
68+
public void testProtocolOptionalParam() {
69+
boolean result = BoolTest.testProtocolOptionalParam(param -> param != null && param);
70+
Assert.assertTrue(result);
71+
}
72+
73+
@Test
74+
public void testProtocolOptionalReturnType() {
75+
Boolean result = BoolTest.testProtocolOptionalReturnType(() -> true);
76+
Assert.assertNotNull(result);
77+
Assert.assertEquals(result, true);
78+
}
79+
80+
@Test
81+
public void testEncode() {
82+
BoolTestStruct result = BoolTest.testEncode();
83+
Assert.assertEquals(result, new BoolTestStruct());
84+
}
85+
86+
@Test
87+
public void testDecode() {
88+
BoolTestStruct goodParam = new BoolTestStruct();
89+
BoolTestStruct badParam = new BoolTestStruct(true, true, true, true);
90+
Assert.assertTrue(BoolTest.testDecode(goodParam));
91+
Assert.assertFalse(BoolTest.testDecode(badParam));
92+
}
93+
94+
}

0 commit comments

Comments
 (0)