Skip to content

Commit 6a10934

Browse files
committed
Dev: add support of incremental processing
1 parent 9457a4c commit 6a10934

File tree

16 files changed

+92
-59
lines changed

16 files changed

+92
-59
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ buildscript {
1010

1111
dependencies {
1212
classpath "com.android.tools.build:gradle:3.5.0"
13-
classpath "com.readdle.android.swift:gradle:1.3.0"
13+
classpath "com.readdle.android.swift:gradle:1.3.1"
1414
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1515
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
1616
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"

compiler/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ apply plugin: 'java'
33
archivesBaseName = "compiler"
44

55
dependencies {
6-
compile project(':library')
6+
implementation project(':library')
7+
implementation 'com.google.code.gson:gson:2.8.5'
78
}
89

910
apply from: rootProject.file('bintray-publish.gradle')

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

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

3+
import com.google.gson.Gson;
34
import com.readdle.codegen.anotation.SwiftBlock;
45
import com.readdle.codegen.anotation.SwiftDelegate;
5-
import com.readdle.codegen.anotation.SwiftModule;
66
import com.readdle.codegen.anotation.SwiftReference;
77
import com.readdle.codegen.anotation.SwiftValue;
8-
import com.readdle.codegen.anotation.TypeMapping;
98

109
import java.io.File;
1110
import java.io.IOException;
1211
import java.util.HashMap;
12+
import java.util.HashSet;
1313
import java.util.LinkedHashSet;
1414
import java.util.List;
1515
import java.util.Map;
@@ -27,7 +27,6 @@
2727
import javax.lang.model.element.ElementKind;
2828
import javax.lang.model.element.Name;
2929
import javax.lang.model.element.TypeElement;
30-
import javax.lang.model.type.MirroredTypeException;
3130
import javax.lang.model.util.Elements;
3231
import javax.lang.model.util.Types;
3332
import javax.tools.Diagnostic;
@@ -41,15 +40,14 @@ interface WritableElement {
4140
}
4241

4342
public static final String FOLDER = "SwiftGenerated";
43+
public static final String PACKAGE_OPTION = "com.readdle.codegen.package";
4444

4545
private Types typeUtils;
4646
private Elements elementUtils;
4747
private Filer filer;
4848
private Messager messager;
4949

50-
private String moduleName;
51-
String[] importPackages;
52-
HashMap<String, String> customTypeMappings = new HashMap<>();
50+
SwiftModuleDescriptor moduleDescriptor;
5351

5452
@Override
5553
public synchronized void init(ProcessingEnvironment processingEnv) {
@@ -59,12 +57,38 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
5957
filer = processingEnv.getFiler();
6058
messager = processingEnv.getMessager();
6159

60+
messager.printMessage(Diagnostic.Kind.NOTE, "JavaSwiftProcessor init started");
61+
62+
String packageJson = processingEnv.getOptions().get(PACKAGE_OPTION);
63+
64+
moduleDescriptor = new Gson().fromJson(packageJson, SwiftModuleDescriptor.class);
65+
if (moduleDescriptor == null) {
66+
messager.printMessage(Diagnostic.Kind.ERROR, "No package description with option: com.readdle.codegen.package", null);
67+
return;
68+
}
69+
70+
messager.printMessage(Diagnostic.Kind.NOTE, "Package moduleName: " + moduleDescriptor.moduleName);
71+
72+
if (moduleDescriptor.importPackages != null) {
73+
for (String anImport : moduleDescriptor.importPackages) {
74+
messager.printMessage(Diagnostic.Kind.NOTE, "Package import: " + anImport);
75+
}
76+
}
77+
78+
if (moduleDescriptor.customTypeMappings != null) {
79+
for (String key : moduleDescriptor.customTypeMappings.keySet()) {
80+
messager.printMessage(Diagnostic.Kind.NOTE, "Package custom mapping: " + key + " -> " + moduleDescriptor.customTypeMappings.get(key));
81+
}
82+
}
83+
6284
try {
6385
generateJavaSwift(filer);
6486
} catch (IOException e) {
6587
e.printStackTrace();
6688
error(null, "Can't write to file: " + e.getMessage());
6789
}
90+
91+
messager.printMessage(Diagnostic.Kind.NOTE, "JavaSwiftProcessor init finished successfully");
6892
}
6993

7094
@Override
@@ -82,6 +106,13 @@ public SourceVersion getSupportedSourceVersion() {
82106
return SourceVersion.latestSupported();
83107
}
84108

109+
@Override
110+
public Set<String> getSupportedOptions() {
111+
Set<String> options = new HashSet<>(super.getSupportedOptions());
112+
options.add(PACKAGE_OPTION);
113+
return options;
114+
}
115+
85116
@Override
86117
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
87118
try {
@@ -96,36 +127,23 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
96127

97128
private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
98129
Filer filer = processingEnv.getFiler();
99-
messager.printMessage(Diagnostic.Kind.NOTE, "Start SwiftJava code generation:");
130+
messager.printMessage(Diagnostic.Kind.NOTE, "JavaSwiftProcessor start code generation");
131+
132+
messager.printMessage(Diagnostic.Kind.NOTE, "SwiftValue to process: "
133+
+ roundEnv.getElementsAnnotatedWith(SwiftValue.class).size());
134+
messager.printMessage(Diagnostic.Kind.NOTE, "SwiftReference to process: "
135+
+ roundEnv.getElementsAnnotatedWith(SwiftReference.class).size());
136+
messager.printMessage(Diagnostic.Kind.NOTE, "SwiftDelegate to process: "
137+
+ roundEnv.getElementsAnnotatedWith(SwiftDelegate.class).size());
138+
messager.printMessage(Diagnostic.Kind.NOTE, "SwiftBlock to process: "
139+
+ roundEnv.getElementsAnnotatedWith(SwiftBlock.class).size());
100140

101141
Map<String, SwiftValueDescriptor> swiftValues = new HashMap<>();
102142
Map<String, SwiftReferenceDescriptor> swiftReferences = new HashMap<>();
103143
Map<String, SwiftDelegateDescriptor> swiftDelegates = new HashMap<>();
104144
Map<String, SwiftBlockDescriptor> swiftBlocks = new HashMap<>();
105145

106-
for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(SwiftModule.class)) {
107-
SwiftModule swiftModule = annotatedElement.getAnnotation(SwiftModule.class);
108-
moduleName = swiftModule.moduleName();
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-
}
126-
}
127-
128-
if (moduleName == null || importPackages == null) {
146+
if (moduleDescriptor == null) {
129147
messager.printMessage(Diagnostic.Kind.ERROR, "No package description with SwiftModule.class", null);
130148
}
131149

@@ -279,16 +297,16 @@ private boolean processImpl(Set<? extends TypeElement> annotations, RoundEnviron
279297
}
280298
}
281299

282-
messager.printMessage(Diagnostic.Kind.NOTE, "SwiftJava finished successfully!");
300+
messager.printMessage(Diagnostic.Kind.NOTE, "JavaSwiftProcessor finished successfully!");
283301

284302
return false;
285303
}
286304

287305
private void generateJavaSwift(Filer filer) throws IOException {
288-
String swiftFilePath = filer.createResource(StandardLocation.SOURCE_OUTPUT, FOLDER, "SwiftJava.swift", (Element) null).toUri().getPath();
306+
String swiftFilePath = filer.getResource(StandardLocation.SOURCE_OUTPUT, FOLDER, "SwiftJava.swift").toUri().getPath();
289307
File swiftExtensionFile = new File(swiftFilePath);
290308
swiftExtensionFile.getParentFile().mkdir();
291-
messager.printMessage(Diagnostic.Kind.NOTE, "SwiftJava will generate sources int0: " + swiftExtensionFile.getParent());
309+
messager.printMessage(Diagnostic.Kind.NOTE, "JavaSwiftProcessor will generate sources at: " + swiftExtensionFile.getParent());
292310
SwiftWriter swiftWriter = new SwiftWriter(swiftExtensionFile);
293311
swiftWriter.emitImports(new String[0]);
294312
swiftWriter.emitEmptyLine();
@@ -337,8 +355,8 @@ static String replaceLast(String text, char replace, char replacement) {
337355
}
338356

339357
public SwiftEnvironment.Type parseJavaType(String javaType) {
340-
if (customTypeMappings.containsKey(javaType)) {
341-
return new SwiftEnvironment.Type(customTypeMappings.get(javaType), javaType);
358+
if (moduleDescriptor.customTypeMappings.containsKey(javaType)) {
359+
return new SwiftEnvironment.Type(moduleDescriptor.customTypeMappings.get(javaType), javaType);
342360
}
343361
switch (javaType) {
344362
case "void":

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SwiftBlockDescriptor {
4343

4444
SwiftBlockDescriptor(TypeElement classElement, Filer filer, JavaSwiftProcessor processor) throws IllegalArgumentException {
4545
this.annotatedClassElement = classElement;
46-
this.importPackages = processor.importPackages;
46+
this.importPackages = processor.moduleDescriptor.importPackages;
4747

4848
// Get the full QualifiedTypeName
4949
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class SwiftDelegateDescriptor {
4242
SwiftDelegateDescriptor(TypeElement classElement, Filer filer, JavaSwiftProcessor processor) throws IllegalArgumentException {
4343
this.annotatedClassElement = classElement;
4444
this.isInterface = classElement.getKind() == ElementKind.INTERFACE;
45-
this.importPackages = processor.importPackages;
45+
this.importPackages = processor.moduleDescriptor.importPackages;
4646

4747
// Get the full QualifiedTypeName
4848
try {
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.readdle.codegen;
22

3-
public class SwiftModuleDescriptor {
3+
import java.util.HashMap;
44

5-
private String moduleName;
6-
private String[] imports;
5+
class SwiftModuleDescriptor {
6+
7+
String moduleName;
8+
String[] importPackages;
9+
HashMap<String, String> customTypeMappings;
710

811
}

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

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

33
import com.readdle.codegen.anotation.SwiftBlock;
4-
import com.readdle.codegen.anotation.SwiftModule;
5-
6-
import java.util.HashMap;
7-
84
import javax.lang.model.element.VariableElement;
95

106
import static java.util.Objects.requireNonNull;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SwiftReferenceDescriptor {
3434
List<JavaSwiftProcessor.WritableElement> functions = new LinkedList<>();
3535

3636
SwiftReferenceDescriptor(TypeElement classElement, Filer filer, JavaSwiftProcessor processor) throws IllegalArgumentException {
37-
this.importPackages = processor.importPackages;
37+
this.importPackages = processor.moduleDescriptor.importPackages;
3838

3939
// Get the full QualifiedTypeName
4040
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SwiftValueDescriptor {
3636
private List<JavaSwiftProcessor.WritableElement> functions = new LinkedList<>();
3737

3838
SwiftValueDescriptor(TypeElement classElement, Filer filer, JavaSwiftProcessor processor) throws IllegalArgumentException {
39-
this.importPackages = processor.importPackages;
39+
this.importPackages = processor.moduleDescriptor.importPackages;
4040

4141
// Get the full QualifiedTypeName
4242
try {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.readdle.codegen.JavaSwiftProcessor,isolating

0 commit comments

Comments
 (0)