Skip to content

Commit 1bec723

Browse files
committed
Dev: move generated source to default annotation processor output folder
1 parent acd32df commit 1bec723

4 files changed

Lines changed: 50 additions & 39 deletions

File tree

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

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,13 @@ public class JavaSwiftProcessor extends AbstractProcessor {
3636
private Filer filer;
3737
private Messager messager;
3838

39-
private File sourcePath;
40-
4139
@Override
4240
public synchronized void init(ProcessingEnvironment processingEnv) {
4341
super.init(processingEnv);
4442
typeUtils = processingEnv.getTypeUtils();
4543
elementUtils = processingEnv.getElementUtils();
4644
filer = processingEnv.getFiler();
4745
messager = processingEnv.getMessager();
48-
49-
// TODO: try to find better way to get GeneratedSources path
50-
try {
51-
Filer filer = processingEnv.getFiler();
52-
FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "tmp", (Element[]) null);
53-
String projectPath = resource.toUri().getPath();
54-
projectPath = projectPath.substring(0, projectPath.lastIndexOf("/build/"));
55-
resource.delete();
56-
sourcePath = new File(projectPath, "/src/main/swift/.build/generated");
57-
58-
if (sourcePath.mkdirs()) {
59-
messager.printMessage(Diagnostic.Kind.NOTE, "GeneratedSources was created");
60-
}
61-
}
62-
catch (Exception e) {
63-
messager.printMessage(Diagnostic.Kind.ERROR, "Can't get source dir: " + e.getMessage());
64-
}
6546
}
6647

6748
@Override
@@ -80,7 +61,7 @@ public SourceVersion getSupportedSourceVersion() {
8061

8162
@Override
8263
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
83-
64+
Filer filer = processingEnv.getFiler();
8465
messager.printMessage(Diagnostic.Kind.NOTE, "Start SwiftJava code generation:");
8566

8667
Map<String, SwiftValueDescriptor> swiftValues = new HashMap<>();
@@ -98,7 +79,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
9879
TypeElement typeElement = (TypeElement) annotatedElement;
9980

10081
try {
101-
SwiftValueDescriptor swiftValueDescriptor = new SwiftValueDescriptor(typeElement);
82+
SwiftValueDescriptor swiftValueDescriptor = new SwiftValueDescriptor(typeElement, filer);
10283
swiftValues.put(swiftValueDescriptor.getSwiftType(), swiftValueDescriptor);
10384
}
10485
catch (IllegalArgumentException e) {
@@ -118,7 +99,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
11899
TypeElement typeElement = (TypeElement) annotatedElement;
119100

120101
try {
121-
SwiftReferenceDescriptor swiftReferenceDescriptor = new SwiftReferenceDescriptor(typeElement);
102+
SwiftReferenceDescriptor swiftReferenceDescriptor = new SwiftReferenceDescriptor(typeElement, filer);
122103
swiftReferences.put(swiftReferenceDescriptor.getSwiftType(), swiftReferenceDescriptor);
123104
}
124105
catch (IllegalArgumentException e) {
@@ -138,7 +119,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
138119
TypeElement typeElement = (TypeElement) annotatedElement;
139120

140121
try {
141-
SwiftDelegateDescriptor delegateDescriptor = new SwiftDelegateDescriptor(typeElement);
122+
SwiftDelegateDescriptor delegateDescriptor = new SwiftDelegateDescriptor(typeElement, filer);
142123
swiftDelegates.put(delegateDescriptor.simpleTypeName, delegateDescriptor);
143124
}
144125
catch (IllegalArgumentException e) {
@@ -154,7 +135,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
154135
}
155136

156137
try {
157-
File file = valueDescriptor.generateCode(sourcePath);
138+
File file = valueDescriptor.generateCode();
158139
messager.printMessage(Diagnostic.Kind.NOTE, file.getName() + " generated");
159140
} catch (IOException e) {
160141
error(null, "Can't write to file: " + e.getMessage());
@@ -169,7 +150,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
169150
}
170151

171152
try {
172-
File file = referenceDescriptor.generateCode(sourcePath);
153+
File file = referenceDescriptor.generateCode();
173154
messager.printMessage(Diagnostic.Kind.NOTE, file.getName() + " generated");
174155
} catch (IOException e) {
175156
error(null, "Can't write to file: " + e.getMessage());
@@ -184,7 +165,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
184165
}
185166

186167
try {
187-
File file = delegateDescriptor.generateCode(sourcePath);
168+
File file = delegateDescriptor.generateCode();
188169
messager.printMessage(Diagnostic.Kind.NOTE, file.getName() + " generated");
189170
} catch (IOException e) {
190171
error(null, "Can't write to file: " + e.getMessage());
@@ -193,7 +174,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
193174
}
194175

195176
try {
196-
generateJavaSwift(sourcePath);
177+
generateJavaSwift(filer);
197178
} catch (IOException e) {
198179
error(null, "Can't write to file: " + e.getMessage());
199180
return true; // Exit processing
@@ -204,9 +185,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
204185
return false;
205186
}
206187

207-
void generateJavaSwift(File dirPath) throws IOException {
208-
// TODO: check if already generated
209-
File swiftExtensionFile = new File(dirPath, "JavaSwift.swift");
188+
private void generateJavaSwift(Filer filer) throws IOException {
189+
String swiftFilePath = filer.createResource(StandardLocation.SOURCE_OUTPUT, "SwiftGenerated", "SwiftJava.swift", (Element[]) null).toUri().getPath();
190+
File swiftExtensionFile = new File(swiftFilePath);
210191
SwiftWriter swiftWriter = new SwiftWriter(swiftExtensionFile);
211192
swiftWriter.emitImports(new String[0]);
212193
swiftWriter.emitEmptyLine();

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.LinkedList;
99
import java.util.List;
1010

11+
import javax.annotation.processing.Filer;
1112
import javax.lang.model.element.Element;
1213
import javax.lang.model.element.ElementKind;
1314
import javax.lang.model.element.ExecutableElement;
@@ -16,10 +17,12 @@
1617
import javax.lang.model.element.VariableElement;
1718
import javax.lang.model.type.DeclaredType;
1819
import javax.lang.model.type.MirroredTypeException;
20+
import javax.tools.StandardLocation;
1921

2022
class SwiftDelegateDescriptor {
2123

2224
private static final String SUFFIX = "Android.swift";
25+
private String swiftFilePath;
2326

2427
private TypeElement annotatedClassElement;
2528
private String javaFullName;
@@ -33,7 +36,7 @@ class SwiftDelegateDescriptor {
3336

3437
private boolean isInterface;
3538

36-
SwiftDelegateDescriptor(TypeElement classElement) throws IllegalArgumentException {
39+
SwiftDelegateDescriptor(TypeElement classElement, Filer filer) throws IllegalArgumentException {
3740
this.annotatedClassElement = classElement;
3841
this.isInterface = classElement.getKind() == ElementKind.INTERFACE;
3942

@@ -52,6 +55,13 @@ class SwiftDelegateDescriptor {
5255
javaFullName = classElement.getQualifiedName().toString().replace(".", "/");
5356
}
5457

58+
try {
59+
swiftFilePath = filer.createResource(StandardLocation.SOURCE_OUTPUT, "SwiftGenerated", simpleTypeName + SUFFIX, classElement).toUri().getPath();
60+
} catch (IOException e) {
61+
e.printStackTrace();
62+
throw new IllegalArgumentException("Can't create swift file");
63+
}
64+
5565
Element enclosingElement = classElement.getEnclosingElement();
5666
while (enclosingElement != null && enclosingElement.getKind() == ElementKind.CLASS) {
5767
javaFullName = JavaSwiftProcessor.replaceLast(javaFullName, '/', '$');
@@ -144,8 +154,8 @@ class SwiftDelegateDescriptor {
144154
}
145155
}
146156

147-
File generateCode(File dirPath) throws IOException {
148-
File swiftExtensionFile = new File(dirPath, simpleTypeName + SUFFIX);
157+
File generateCode() throws IOException {
158+
File swiftExtensionFile = new File(swiftFilePath);
149159
SwiftWriter swiftWriter = new SwiftWriter(swiftExtensionFile);
150160

151161
// Write imports

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.LinkedList;
88
import java.util.List;
99

10+
import javax.annotation.processing.Filer;
1011
import javax.lang.model.element.Element;
1112
import javax.lang.model.element.ElementKind;
1213
import javax.lang.model.element.ExecutableElement;
@@ -15,10 +16,12 @@
1516
import javax.lang.model.element.VariableElement;
1617
import javax.lang.model.type.DeclaredType;
1718
import javax.lang.model.type.MirroredTypeException;
19+
import javax.tools.StandardLocation;
1820

1921
class SwiftReferenceDescriptor {
2022

2123
private static final String SUFFIX = "Android.swift";
24+
private String swiftFilePath;
2225

2326
private TypeElement annotatedClassElement;
2427
private String javaFullName;
@@ -28,7 +31,7 @@ class SwiftReferenceDescriptor {
2831

2932
List<SwiftFuncDescriptor> functions = new LinkedList<>();
3033

31-
SwiftReferenceDescriptor(TypeElement classElement) throws IllegalArgumentException {
34+
SwiftReferenceDescriptor(TypeElement classElement, Filer filer) throws IllegalArgumentException {
3235
this.annotatedClassElement = classElement;
3336

3437
// Get the full QualifiedTypeName
@@ -44,6 +47,13 @@ class SwiftReferenceDescriptor {
4447
javaFullName = classElement.getQualifiedName().toString().replace(".", "/");
4548
}
4649

50+
try {
51+
swiftFilePath = filer.createResource(StandardLocation.SOURCE_OUTPUT, "SwiftGenerated", simpleTypeName + SUFFIX, classElement).toUri().getPath();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
throw new IllegalArgumentException("Can't create swift file");
55+
}
56+
4757
Element enclosingElement = classElement.getEnclosingElement();
4858
while (enclosingElement != null && enclosingElement.getKind() == ElementKind.CLASS) {
4959
javaFullName = JavaSwiftProcessor.replaceLast(javaFullName, '/', '$');
@@ -128,8 +138,8 @@ class SwiftReferenceDescriptor {
128138
}
129139
}
130140

131-
File generateCode(File dirPath) throws IOException {
132-
File swiftExtensionFile = new File(dirPath, simpleTypeName + SUFFIX);
141+
File generateCode() throws IOException {
142+
File swiftExtensionFile = new File(swiftFilePath);
133143
SwiftWriter swiftWriter = new SwiftWriter(swiftExtensionFile);
134144

135145
// Write imports

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77
import java.util.LinkedList;
88
import java.util.List;
99

10+
import javax.annotation.processing.Filer;
1011
import javax.lang.model.element.Element;
1112
import javax.lang.model.element.ElementKind;
1213
import javax.lang.model.element.ExecutableElement;
1314
import javax.lang.model.element.Modifier;
1415
import javax.lang.model.element.TypeElement;
1516
import javax.lang.model.type.DeclaredType;
1617
import javax.lang.model.type.MirroredTypeException;
18+
import javax.tools.StandardLocation;
1719

1820
class SwiftValueDescriptor {
1921

2022
private static final String SUFFIX = "Android.swift";
23+
private String swiftFilePath;
2124

2225
private TypeElement annotatedClassElement;
2326
private String javaPackage;
@@ -27,7 +30,7 @@ class SwiftValueDescriptor {
2730

2831
List<SwiftFuncDescriptor> functions = new LinkedList<>();
2932

30-
SwiftValueDescriptor(TypeElement classElement) throws IllegalArgumentException {
33+
SwiftValueDescriptor(TypeElement classElement, Filer filer) throws IllegalArgumentException {
3134
this.annotatedClassElement = classElement;
3235

3336
// Get the full QualifiedTypeName
@@ -45,6 +48,13 @@ class SwiftValueDescriptor {
4548
javaFullName = classElement.getQualifiedName().toString().replace(".", "/");
4649
}
4750

51+
try {
52+
swiftFilePath = filer.createResource(StandardLocation.SOURCE_OUTPUT, "SwiftGenerated", simpleTypeName + SUFFIX, classElement).toUri().getPath();
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
throw new IllegalArgumentException("Can't create swift file");
56+
}
57+
4858
Element enclosingElement = classElement.getEnclosingElement();
4959
while (enclosingElement != null && enclosingElement.getKind() == ElementKind.CLASS) {
5060
javaFullName = JavaSwiftProcessor.replaceLast(javaFullName, '/', '$');
@@ -89,8 +99,8 @@ class SwiftValueDescriptor {
8999

90100
}
91101

92-
File generateCode(File dirPath) throws IOException {
93-
File swiftExtensionFile = new File(dirPath, simpleTypeName + SUFFIX);
102+
File generateCode() throws IOException {
103+
File swiftExtensionFile = new File(swiftFilePath);
94104
SwiftWriter swiftWriter = new SwiftWriter(swiftExtensionFile);
95105

96106
// Write imports

0 commit comments

Comments
 (0)