Skip to content

Commit cd3de6f

Browse files
committed
Dev: replace @SwiftParam with @swiftblock for PARAMETER
1 parent 129a70d commit cd3de6f

5 files changed

Lines changed: 21 additions & 24 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
161161
}
162162

163163
for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(SwiftBlock.class)) {
164+
// Skip PARAMS
165+
if (annotatedElement.getKind() == ElementKind.PARAMETER) {
166+
continue;
167+
}
168+
164169
// Check if a class has been annotated with @SwiftValue
165170
if (annotatedElement.getKind() != ElementKind.INTERFACE) {
166171
error(annotatedElement, "Only interface can be annotated with @%s", SwiftBlock.class.getSimpleName());

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121

2222
class SwiftBlockDescriptor {
2323

24-
private static final String SUFFIX = "Android.swift";
24+
private static final String SUFFIX = ".swift";
2525
private String swiftFilePath;
2626

2727
private TypeElement annotatedClassElement;
2828
private String javaFullName;
2929
String simpleTypeName;
30+
private String swiftType;
3031
private String[] importPackages;
3132

3233
private String blockSignature;
@@ -57,8 +58,10 @@ class SwiftBlockDescriptor {
5758
javaFullName = classElement.getQualifiedName().toString().replace(".", "/");
5859
}
5960

61+
swiftType = "SwiftBlock" + simpleTypeName;
62+
6063
try {
61-
swiftFilePath = filer.createResource(StandardLocation.SOURCE_OUTPUT, FOLDER, simpleTypeName + SUFFIX, classElement).toUri().getPath();
64+
swiftFilePath = filer.createResource(StandardLocation.SOURCE_OUTPUT, FOLDER, swiftType + SUFFIX, classElement).toUri().getPath();
6265
} catch (IOException e) {
6366
e.printStackTrace();
6467
throw new IllegalArgumentException("Can't create swift file");
@@ -109,8 +112,10 @@ File generateCode() throws IOException {
109112

110113
swiftWriter.emitStatement(String.format("fileprivate let javaClass = JNI.GlobalFindClass(\"%s\")!", javaFullName));
111114

115+
swiftWriter.emitStatement(String.format("public typealias %s = %s", simpleTypeName, blockSignature));
116+
112117
swiftWriter.emitEmptyLine();
113-
swiftWriter.emit(String.format("public class %s", simpleTypeName));
118+
swiftWriter.emit(String.format("public class %s", swiftType));
114119
swiftWriter.emit(" {\n");
115120

116121
swiftWriter.emitEmptyLine();
@@ -129,8 +134,8 @@ File generateCode() throws IOException {
129134

130135
swiftWriter.emitEmptyLine();
131136
swiftWriter.emitStatement("// Create swift object");
132-
swiftWriter.emitStatement(String.format("public static func from(javaObject: jobject) throws -> %s {", blockSignature));
133-
swiftWriter.emitStatement(String.format("return %s(jniObject: javaObject).block", simpleTypeName));
137+
swiftWriter.emitStatement(String.format("public static func from(javaObject: jobject) throws -> %s {", simpleTypeName));
138+
swiftWriter.emitStatement(String.format("return %s(jniObject: javaObject).block", swiftType));
134139
swiftWriter.emitStatement("}");
135140

136141
swiftWriter.emitEmptyLine();
@@ -146,7 +151,7 @@ File generateCode() throws IOException {
146151
sig));
147152

148153
swiftWriter.emitEmptyLine();
149-
swiftWriter.emitStatement(String.format("public lazy var block: %s = {", blockSignature));
154+
swiftWriter.emitStatement(String.format("public lazy var block: %s = {", simpleTypeName));
150155

151156
for (SwiftParamDescriptor param : params) {
152157
swiftWriter.emitStatement(String.format("let java_%s: JNIArgumentProtocol", param.name));
@@ -187,7 +192,7 @@ File generateCode() throws IOException {
187192
jniMethodTemplate = "JNI.CallVoidMethod(self.jniObject, %s.javaMethod%s";
188193
}
189194

190-
swiftWriter.emit(String.format(jniMethodTemplate, simpleTypeName, funcName));
195+
swiftWriter.emit(String.format(jniMethodTemplate, swiftType, funcName));
191196

192197
for (SwiftParamDescriptor param : params) {
193198
swiftWriter.emitStatement(String.format(", java_%s", param.name));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.readdle.codegen;
22

3-
import com.readdle.codegen.anotation.SwiftParam;
3+
import com.readdle.codegen.anotation.SwiftBlock;
44

55
import javax.lang.model.element.VariableElement;
66

@@ -14,9 +14,9 @@ public class SwiftParamDescriptor {
1414
SwiftParamDescriptor(VariableElement variableElement) {
1515
this.name = variableElement.getSimpleName().toString();
1616
this.swiftType = SwiftEnvironment.parseJavaType(variableElement.asType().toString());
17-
SwiftParam swiftParam = variableElement.getAnnotation(SwiftParam.class);
17+
SwiftBlock swiftParam = variableElement.getAnnotation(SwiftBlock.class);
1818
if (swiftParam != null) {
19-
this.swiftType.swiftType = swiftParam.value();
19+
this.swiftType.swiftConstructorType = "SwiftBlock" + this.swiftType.swiftConstructorType;
2020
}
2121
this.isOptional = JavaSwiftProcessor.isNullable(variableElement);
2222
this.description = null;

library/src/main/java/com/readdle/codegen/anotation/SwiftBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
77

8-
@Target(ElementType.TYPE) @Retention(RetentionPolicy.CLASS)
8+
@Target({ElementType.TYPE, ElementType.PARAMETER}) @Retention(RetentionPolicy.CLASS)
99
public @interface SwiftBlock {
1010

1111
String value() default "";

library/src/main/java/com/readdle/codegen/anotation/SwiftParam.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)