Skip to content

Commit edafdd5

Browse files
authored
Merge pull request #11 from zayass/feature/renamed-getter-setter
Use proper names for @SwiftGetter/@SwiftSetter
2 parents 0d7b367 + 31f9b56 commit edafdd5

5 files changed

Lines changed: 37 additions & 53 deletions

File tree

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

Lines changed: 4 additions & 8 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.SwiftFunc;
3+
import com.readdle.codegen.anotation.SwiftGetter;
44

55
import java.io.IOException;
66

@@ -17,9 +17,7 @@ class SwiftGetterDescriptor implements JavaSwiftProcessor.WritableElement {
1717
private SwiftEnvironment.Type returnSwiftType;
1818
private boolean isReturnTypeOptional;
1919

20-
private String description;
21-
22-
SwiftGetterDescriptor(ExecutableElement executableElement) {
20+
SwiftGetterDescriptor(ExecutableElement executableElement, SwiftGetter getterAnnotation) {
2321
this.javaName = executableElement.getSimpleName().toString();
2422
this.isStatic = executableElement.getModifiers().contains(Modifier.STATIC);
2523
this.returnSwiftType = SwiftEnvironment.parseJavaType(executableElement.getReturnType().toString());
@@ -33,9 +31,8 @@ class SwiftGetterDescriptor implements JavaSwiftProcessor.WritableElement {
3331
throw new IllegalArgumentException("Getter can't has parameters");
3432
}
3533

36-
SwiftFunc swiftFunc = executableElement.getAnnotation(SwiftFunc.class);
37-
if (swiftFunc != null && !swiftFunc.value().isEmpty()) {
38-
this.swiftName = swiftFunc.value();
34+
if (getterAnnotation != null && !getterAnnotation.value().isEmpty()) {
35+
this.swiftName = getterAnnotation.value();
3936
}
4037
else {
4138
this.swiftName = javaName;
@@ -100,7 +97,6 @@ public String toString() {
10097
", isStatic=" + isStatic +
10198
", returnSwiftType=" + returnSwiftType +
10299
", isReturnTypeOptional=" + isReturnTypeOptional +
103-
", description='" + description + '\'' +
104100
'}';
105101
}
106102
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44

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

7+
import static java.util.Objects.requireNonNull;
8+
79
public class SwiftParamDescriptor {
810

911
final String name;
10-
SwiftEnvironment.Type swiftType;
12+
final SwiftEnvironment.Type swiftType;
1113
final boolean isOptional;
12-
final String description;
1314

1415
SwiftParamDescriptor(VariableElement variableElement) {
1516
this.name = variableElement.getSimpleName().toString();
16-
this.swiftType = SwiftEnvironment.parseJavaType(variableElement.asType().toString());
17+
this.swiftType = requireNonNull(SwiftEnvironment.parseJavaType(variableElement.asType().toString()));
18+
1719
SwiftBlock swiftParam = variableElement.getAnnotation(SwiftBlock.class);
1820
if (swiftParam != null) {
1921
this.swiftType.swiftConstructorType = "SwiftBlock" + this.swiftType.swiftConstructorType;
2022
}
2123
this.isOptional = JavaSwiftProcessor.isNullable(variableElement);
22-
this.description = null;
2324
}
2425

2526
@Override
@@ -28,7 +29,6 @@ public String toString() {
2829
"name='" + name + '\'' +
2930
", swiftType='" + swiftType + '\'' +
3031
", isOptional=" + isOptional +
31-
", description='" + description + '\'' +
3232
'}';
3333
}
3434
}

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44
import com.readdle.codegen.anotation.SwiftReference;
55
import com.readdle.codegen.anotation.SwiftSetter;
66

7-
import java.io.File;
8-
import java.io.IOException;
9-
import java.util.LinkedList;
10-
import java.util.List;
11-
127
import javax.annotation.processing.Filer;
13-
import javax.lang.model.element.Element;
14-
import javax.lang.model.element.ElementKind;
15-
import javax.lang.model.element.ExecutableElement;
16-
import javax.lang.model.element.Modifier;
17-
import javax.lang.model.element.TypeElement;
18-
import javax.lang.model.element.VariableElement;
8+
import javax.lang.model.element.*;
199
import javax.lang.model.type.DeclaredType;
2010
import javax.lang.model.type.MirroredTypeException;
2111
import javax.tools.StandardLocation;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.util.LinkedList;
15+
import java.util.List;
2216

2317
import static com.readdle.codegen.JavaSwiftProcessor.FOLDER;
2418

@@ -27,15 +21,13 @@ class SwiftReferenceDescriptor {
2721
private static final String SUFFIX = "Android.swift";
2822
private String swiftFilePath;
2923

30-
private TypeElement annotatedClassElement;
3124
private String javaFullName;
3225
private String simpleTypeName;
3326
private String[] importPackages;
3427

3528
List<JavaSwiftProcessor.WritableElement> functions = new LinkedList<>();
3629

3730
SwiftReferenceDescriptor(TypeElement classElement, Filer filer, String[] importPackages) throws IllegalArgumentException {
38-
this.annotatedClassElement = classElement;
3931
this.importPackages = importPackages;
4032

4133
// Get the full QualifiedTypeName
@@ -116,13 +108,15 @@ class SwiftReferenceDescriptor {
116108
if (element.getKind() == ElementKind.METHOD) {
117109
ExecutableElement executableElement = (ExecutableElement) element;
118110
if (executableElement.getModifiers().contains(Modifier.NATIVE)) {
119-
if (executableElement.getAnnotation(SwiftGetter.class) != null) {
120-
functions.add(new SwiftGetterDescriptor(executableElement));
121-
}
122-
else if (executableElement.getAnnotation(SwiftSetter.class) != null) {
123-
functions.add(new SwiftSetterDescriptor(executableElement));
111+
SwiftGetter getterAnnotation = executableElement.getAnnotation(SwiftGetter.class);
112+
SwiftSetter setterAnnotation = executableElement.getAnnotation(SwiftSetter.class);
113+
114+
if (getterAnnotation != null) {
115+
functions.add(new SwiftGetterDescriptor(executableElement, getterAnnotation));
124116
}
125-
else {
117+
else if (setterAnnotation != null) {
118+
functions.add(new SwiftSetterDescriptor(executableElement, setterAnnotation));
119+
} else {
126120
functions.add(new SwiftFuncDescriptor(executableElement));
127121
}
128122
}

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

Lines changed: 6 additions & 10 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.SwiftFunc;
3+
import com.readdle.codegen.anotation.SwiftSetter;
44

55
import java.io.IOException;
66

@@ -12,13 +12,11 @@ class SwiftSetterDescriptor implements JavaSwiftProcessor.WritableElement {
1212
private String javaName;
1313
private String swiftName;
1414

15-
boolean isStatic;
16-
17-
private String description;
15+
private boolean isStatic;
1816

1917
private SwiftParamDescriptor param;
2018

21-
SwiftSetterDescriptor(ExecutableElement executableElement) {
19+
SwiftSetterDescriptor(ExecutableElement executableElement, SwiftSetter setterAnnotation) {
2220
this.javaName = executableElement.getSimpleName().toString();
2321
this.isStatic = executableElement.getModifiers().contains(Modifier.STATIC);
2422

@@ -27,14 +25,13 @@ class SwiftSetterDescriptor implements JavaSwiftProcessor.WritableElement {
2725
}
2826

2927
if (executableElement.getParameters().size() != 1) {
30-
throw new IllegalArgumentException("Setter should have at least 1 parameter");
28+
throw new IllegalArgumentException("Setter should have exactly 1 parameter");
3129
}
3230

3331
param = new SwiftParamDescriptor(executableElement.getParameters().get(0));
3432

35-
SwiftFunc swiftFunc = executableElement.getAnnotation(SwiftFunc.class);
36-
if (swiftFunc != null && !swiftFunc.value().isEmpty()) {
37-
this.swiftName = swiftFunc.value();
33+
if (setterAnnotation != null && !setterAnnotation.value().isEmpty()) {
34+
this.swiftName = setterAnnotation.value();
3835
}
3936
else {
4037
this.swiftName = javaName;
@@ -98,7 +95,6 @@ public String toString() {
9895
"javaName='" + javaName + '\'' +
9996
", swiftName='" + swiftName + '\'' +
10097
", isStatic=" + isStatic +
101-
", description='" + description + '\'' +
10298
", param=" + param +
10399
'}';
104100
}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@ class SwiftValueDescriptor {
2626
private static final String SUFFIX = "Android.swift";
2727
private String swiftFilePath;
2828

29-
private TypeElement annotatedClassElement;
3029
private String javaPackage;
3130
private String javaFullName;
3231
private String simpleTypeName;
3332
private String[] importPackages;
3433

3534
private boolean hasSubclasses = false;
3635

37-
List<JavaSwiftProcessor.WritableElement> functions = new LinkedList<>();
36+
private List<JavaSwiftProcessor.WritableElement> functions = new LinkedList<>();
3837

3938
SwiftValueDescriptor(TypeElement classElement, Filer filer, String[] importPackages) throws IllegalArgumentException {
40-
this.annotatedClassElement = classElement;
4139
this.importPackages = importPackages;
4240

4341
// Get the full QualifiedTypeName
@@ -99,13 +97,15 @@ class SwiftValueDescriptor {
9997
if (element.getKind() == ElementKind.METHOD) {
10098
ExecutableElement executableElement = (ExecutableElement) element;
10199
if (executableElement.getModifiers().contains(Modifier.NATIVE)) {
102-
if (executableElement.getAnnotation(SwiftGetter.class) != null) {
103-
functions.add(new SwiftGetterDescriptor(executableElement));
104-
}
105-
else if (executableElement.getAnnotation(SwiftSetter.class) != null) {
106-
functions.add(new SwiftSetterDescriptor(executableElement));
100+
SwiftGetter getterAnnotation = executableElement.getAnnotation(SwiftGetter.class);
101+
SwiftSetter setterAnnotation = executableElement.getAnnotation(SwiftSetter.class);
102+
103+
if (getterAnnotation != null) {
104+
functions.add(new SwiftGetterDescriptor(executableElement, getterAnnotation));
107105
}
108-
else {
106+
else if (setterAnnotation != null) {
107+
functions.add(new SwiftSetterDescriptor(executableElement, setterAnnotation));
108+
} else {
109109
functions.add(new SwiftFuncDescriptor(executableElement));
110110
}
111111
}
@@ -164,6 +164,4 @@ public String getSwiftType() {
164164
return simpleTypeName;
165165
}
166166

167-
168-
169167
}

0 commit comments

Comments
 (0)