Skip to content

Commit 944de15

Browse files
committed
remove javapoet and replace it by homemade small code gen classes
1 parent 12adacb commit 944de15

9 files changed

Lines changed: 478 additions & 495 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ repositories {
1818
}
1919

2020
dependencies {
21-
implementation("com.palantir.javapoet:javapoet:0.12.0")
2221
testImplementation(platform("org.junit:junit-bom:6.0.3"))
2322
testImplementation("org.junit.jupiter:junit-jupiter")
2423
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
@@ -259,6 +258,7 @@ val compileProcessor by tasks.registering(JavaCompile::class) {
259258
source = fileTree("src/main/java") {
260259
include("**/processor/**")
261260
include("**/annotation/**")
261+
include("**/util/internal/codegen/**")
262262
}
263263
classpath = configurations.compileClasspath.get()
264264
destinationDirectory.set(layout.buildDirectory.dir("classes/java/processor"))

src/main/java/com/github/elebras1/flecs/processor/ComponentGenerator.java

Lines changed: 110 additions & 141 deletions
Large diffs are not rendered by default.

src/main/java/com/github/elebras1/flecs/processor/ComponentMapGenerator.java

Lines changed: 86 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,214 +1,126 @@
11
package com.github.elebras1.flecs.processor;
22

3-
import com.palantir.javapoet.*;
3+
import com.github.elebras1.flecs.util.internal.codegen.CodeBuilder;
4+
import com.github.elebras1.flecs.util.internal.codegen.SourceFile;
45

5-
import javax.lang.model.element.Modifier;
6+
import javax.lang.model.element.Element;
67
import javax.lang.model.element.PackageElement;
78
import javax.lang.model.element.TypeElement;
8-
import javax.lang.model.element.Element;
99
import java.util.List;
10-
import java.util.function.Supplier;
1110

1211
public class ComponentMapGenerator {
1312

14-
private static final ClassName COMPONENT_INTERFACE = ClassName.get("com.github.elebras1.flecs", "Component");
15-
private static final ClassName COMPONENT_VIEW_INTERFACE = ClassName.get("com.github.elebras1.flecs", "ComponentView");
16-
private static final ClassName COMPONENT_ROW_VIEW_INTERFACE = ClassName.get("com.github.elebras1.flecs", "ComponentRowView");
1713
private static final String MAP_PACKAGE = "com.github.elebras1.flecs";
1814
private static final String MAP_COMPONENT_CLASS_NAME = "ComponentMap";
1915

20-
public JavaFile generateComponentMap(List<TypeElement> components) {
21-
TypeSpec.Builder mapClass = TypeSpec.classBuilder(MAP_COMPONENT_CLASS_NAME)
22-
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build())
23-
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
24-
.addField(this.createComponentSizeField(components))
25-
.addField(this.createComponentsField())
26-
.addField(this.createComponentRowViewsField())
27-
.addField(this.createComponentViewsField())
28-
.addField(this.createClassValueField(components))
29-
.addStaticBlock(this.createStaticInitializer(components))
30-
.addMethod(this.createConstructor())
31-
.addMethod(this.createGetIndexMethod())
32-
.addMethod(this.createSizeMethod())
33-
.addMethod(this.createGetInstanceMethod())
34-
.addMethod(this.createGetViewMethod())
35-
.addMethod(this.createGetRowViewMethod());
36-
37-
return JavaFile.builder(MAP_PACKAGE, mapClass.build())
38-
.addFileComment("Generated by ComponentMapGenerator.")
39-
.indent(" ")
40-
.build();
41-
}
16+
public SourceFile generateComponentMap(List<TypeElement> components) {
17+
CodeBuilder body = new CodeBuilder();
4218

43-
private FieldSpec createComponentSizeField(List<TypeElement> components) {
44-
return FieldSpec.builder(int.class, "COMPONENT_COUNT",
45-
Modifier.PRIVATE,
46-
Modifier.STATIC,
47-
Modifier.FINAL)
48-
.initializer("$L", components.size())
49-
.build();
50-
}
19+
body.append("@SuppressWarnings(\"unchecked\")").newline();
20+
body.append("public final class ComponentMap {").newline();
5121

52-
private FieldSpec createComponentsField() {
53-
return FieldSpec.builder(ArrayTypeName.of(ParameterizedTypeName.get(
54-
COMPONENT_INTERFACE, WildcardTypeName.subtypeOf(Object.class))),
55-
"COMPONENTS",
56-
Modifier.PRIVATE,
57-
Modifier.STATIC,
58-
Modifier.FINAL
59-
).build();
60-
}
22+
body.indent4().append("private static final int COMPONENT_COUNT = ").append(components.size()).append(";").newline();
6123

62-
private FieldSpec createComponentViewsField() {
63-
ParameterizedTypeName supplierType = ParameterizedTypeName.get(ClassName.get(Supplier.class), COMPONENT_VIEW_INTERFACE);
64-
return FieldSpec.builder(
65-
ArrayTypeName.of(supplierType),
66-
"VIEWS",
67-
Modifier.PRIVATE,
68-
Modifier.STATIC,
69-
Modifier.FINAL
70-
).build();
71-
}
24+
body.newline();
25+
body.indent4().append("private static final Component<?>[] COMPONENTS;").newline();
7226

73-
private FieldSpec createComponentRowViewsField() {
74-
ParameterizedTypeName supplierType = ParameterizedTypeName.get(ClassName.get(Supplier.class), COMPONENT_ROW_VIEW_INTERFACE);
75-
return FieldSpec.builder(
76-
ArrayTypeName.of(supplierType),
77-
"ROW_VIEWS",
78-
Modifier.PRIVATE,
79-
Modifier.STATIC,
80-
Modifier.FINAL
81-
).build();
82-
}
27+
body.newline();
28+
body.indent4().append("private static final Supplier<ComponentRowView>[] ROW_VIEWS;").newline();
8329

84-
private FieldSpec createClassValueField(List<TypeElement> components) {
85-
ParameterizedTypeName classValueType = ParameterizedTypeName.get(
86-
ClassName.get(ClassValue.class), ClassName.get(Integer.class));
87-
88-
TypeSpec anonymousClassValue = TypeSpec.anonymousClassBuilder("")
89-
.superclass(classValueType)
90-
.addMethod(MethodSpec.methodBuilder("computeValue")
91-
.addAnnotation(Override.class)
92-
.addModifiers(Modifier.PROTECTED)
93-
.returns(ClassName.get(Integer.class))
94-
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(Object.class)), "clazz")
95-
.addCode(this.createComputeValueBody(components))
96-
.build())
97-
.build();
98-
99-
return FieldSpec.builder(classValueType, "COMPONENT_INDEX",
100-
Modifier.PRIVATE,
101-
Modifier.STATIC,
102-
Modifier.FINAL)
103-
.initializer("$L", anonymousClassValue)
104-
.build();
105-
}
30+
body.newline();
31+
body.indent4().append("private static final Supplier<ComponentView>[] VIEWS;").newline();
10632

107-
private CodeBlock createComputeValueBody(List<TypeElement> components) {
108-
CodeBlock.Builder builder = CodeBlock.builder();
109-
int index = 0;
110-
for (TypeElement component : components) {
33+
body.newline();
34+
body.indent4().append("private static final ClassValue<Integer> COMPONENT_INDEX = new ClassValue<Integer>() {").newline();
35+
body.indent8().append("@Override").newline();
36+
body.indent8().append("protected Integer computeValue(Class<?> clazz) {").newline();
37+
for (int i = 0; i < components.size(); i++) {
38+
TypeElement component = components.get(i);
11139
String packageName = this.getPackageName(component);
11240
String recordName = component.getSimpleName().toString();
113-
ClassName recordClass = ClassName.get(packageName, recordName);
114-
builder.addStatement("if (clazz == $T.class) return $L", recordClass, index++);
41+
String fqn = packageName.isEmpty() ? recordName : packageName + "." + recordName;
42+
body.indent12().append("if (clazz == ").append(fqn).append(".class) return ").append(i).append(";").newline();
11543
}
116-
builder.addStatement("return -1");
117-
return builder.build();
118-
}
44+
body.indent12().append("return -1;").newline();
45+
body.indent8().append("}").newline();
46+
body.indent4().append("};").newline();
11947

120-
private CodeBlock createStaticInitializer(List<TypeElement> components) {
121-
CodeBlock.Builder builder = CodeBlock.builder();
48+
body.newline();
49+
body.indent4().append("static {").newline();
12250

123-
builder.add("COMPONENTS = new $T[] {\n", ParameterizedTypeName.get(COMPONENT_INTERFACE, WildcardTypeName.subtypeOf(Object.class)));
51+
body.indent8().append("COMPONENTS = new Component<?>[] {").newline();
12452
for (TypeElement component : components) {
12553
String packageName = this.getPackageName(component);
12654
String recordName = component.getSimpleName().toString();
127-
ClassName componentClass = ClassName.get(packageName, recordName + "Component");
128-
builder.add(" $T.getInstance(),\n", componentClass);
55+
String fqn = packageName.isEmpty() ? recordName + "Component" : packageName + "." + recordName + "Component";
56+
body.indent12().append(fqn).append(".getInstance(),").newline();
12957
}
130-
builder.add("};\n\n");
58+
body.indent8().append("};").newline();
59+
body.newline();
13160

132-
builder.add("VIEWS = new $T[] {\n", ClassName.get(Supplier.class));
61+
body.indent8().append("VIEWS = new Supplier[] {").newline();
13362
for (TypeElement component : components) {
13463
String packageName = this.getPackageName(component);
13564
String recordName = component.getSimpleName().toString();
136-
ClassName viewClass = ClassName.get(packageName, recordName + "View");
137-
builder.add(" $T::new,\n", viewClass);
65+
String fqn = packageName.isEmpty() ? recordName + "View" : packageName + "." + recordName + "View";
66+
body.indent12().append(fqn).append("::new,").newline();
13867
}
139-
builder.add("};\n");
68+
body.indent8().append("};").newline();
14069

141-
builder.add("ROW_VIEWS = new $T[] {\n", ClassName.get(Supplier.class));
70+
body.indent8().append("ROW_VIEWS = new Supplier[] {").newline();
14271
for (TypeElement component : components) {
14372
String packageName = this.getPackageName(component);
14473
String recordName = component.getSimpleName().toString();
145-
ClassName rowViewClass = ClassName.get(packageName, recordName + "RowView");
146-
builder.add(" $T::new,\n", rowViewClass);
74+
String fqn = packageName.isEmpty() ? recordName + "RowView" : packageName + "." + recordName + "RowView";
75+
body.indent12().append(fqn).append("::new,").newline();
14776
}
148-
builder.add("};\n");
149-
150-
return builder.build();
151-
}
152-
153-
private MethodSpec createConstructor() {
154-
return MethodSpec.constructorBuilder()
155-
.addModifiers(Modifier.PRIVATE)
156-
.build();
157-
}
158-
159-
private MethodSpec createGetIndexMethod() {
160-
return MethodSpec.methodBuilder("getIndex")
161-
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
162-
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(Object.class)), "componentClass")
163-
.returns(TypeName.INT)
164-
.addStatement("return COMPONENT_INDEX.get(componentClass)")
165-
.build();
166-
}
167-
168-
private MethodSpec createSizeMethod() {
169-
return MethodSpec.methodBuilder("size")
170-
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
171-
.returns(TypeName.INT)
172-
.addStatement("return COMPONENT_COUNT")
173-
.build();
174-
}
175-
176-
private MethodSpec createGetInstanceMethod() {
177-
return MethodSpec.methodBuilder("getInstance")
178-
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
179-
.addTypeVariable(TypeVariableName.get("T"))
180-
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), TypeVariableName.get("T")), "componentClass")
181-
.returns(ParameterizedTypeName.get(COMPONENT_INTERFACE, TypeVariableName.get("T")))
182-
.addStatement("int index = COMPONENT_INDEX.get(componentClass)")
183-
.addStatement("return index >= 0 ? ($T<T>) COMPONENTS[index] : null", COMPONENT_INTERFACE)
184-
.build();
185-
}
186-
187-
private MethodSpec createGetViewMethod() {
188-
ParameterizedTypeName supplierType = ParameterizedTypeName.get(ClassName.get(Supplier.class), COMPONENT_VIEW_INTERFACE);
189-
190-
return MethodSpec.methodBuilder("getView")
191-
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
192-
.addTypeVariable(TypeVariableName.get("T"))
193-
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), TypeVariableName.get("T")), "componentClass")
194-
.returns(COMPONENT_VIEW_INTERFACE)
195-
.addStatement("int index = COMPONENT_INDEX.get(componentClass)")
196-
.addStatement("$T supplier = index >= 0 ? VIEWS[index] : null", supplierType)
197-
.addStatement("return supplier != null ? supplier.get() : null")
198-
.build();
199-
}
200-
201-
private MethodSpec createGetRowViewMethod() {
202-
ParameterizedTypeName supplierType = ParameterizedTypeName.get(ClassName.get(Supplier.class), COMPONENT_ROW_VIEW_INTERFACE);
203-
204-
return MethodSpec.methodBuilder("getRowView")
205-
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
206-
.addTypeVariable(TypeVariableName.get("T"))
207-
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), TypeVariableName.get("T")), "componentClass")
208-
.returns(COMPONENT_ROW_VIEW_INTERFACE)
209-
.addStatement("int index = COMPONENT_INDEX.get(componentClass)")
210-
.addStatement("$T supplier = index >= 0 ? ROW_VIEWS[index] : null", supplierType)
211-
.addStatement("return supplier != null ? supplier.get() : null")
77+
body.indent8().append("};").newline();
78+
79+
body.indent4().append("}").newline();
80+
81+
body.newline();
82+
body.indent4().append("private ComponentMap() {").newline();
83+
body.indent4().append("}").newline();
84+
85+
body.newline();
86+
body.indent4().append("public static int getIndex(Class<?> componentClass) {").newline();
87+
body.indent8().append("return COMPONENT_INDEX.get(componentClass);").newline();
88+
body.indent4().append("}").newline();
89+
90+
body.newline();
91+
body.indent4().append("public static int size() {").newline();
92+
body.indent8().append("return COMPONENT_COUNT;").newline();
93+
body.indent4().append("}").newline();
94+
95+
body.newline();
96+
body.indent4().append("public static <T> Component<T> getInstance(Class<T> componentClass) {").newline();
97+
body.indent8().append("int index = COMPONENT_INDEX.get(componentClass);").newline();
98+
body.indent8().append("return index >= 0 ? (Component<T>) COMPONENTS[index] : null;").newline();
99+
body.indent4().append("}").newline();
100+
101+
body.newline();
102+
body.indent4().append("public static <T> ComponentView getView(Class<T> componentClass) {").newline();
103+
body.indent8().append("int index = COMPONENT_INDEX.get(componentClass);").newline();
104+
body.indent8().append("Supplier<ComponentView> supplier = index >= 0 ? VIEWS[index] : null;").newline();
105+
body.indent8().append("return supplier != null ? supplier.get() : null;").newline();
106+
body.indent4().append("}").newline();
107+
108+
body.newline();
109+
body.indent4().append("public static <T> ComponentRowView getRowView(Class<T> componentClass) {").newline();
110+
body.indent8().append("int index = COMPONENT_INDEX.get(componentClass);").newline();
111+
body.indent8().append("Supplier<ComponentRowView> supplier = index >= 0 ? ROW_VIEWS[index] : null;").newline();
112+
body.indent8().append("return supplier != null ? supplier.get() : null;").newline();
113+
body.indent4().append("}").newline();
114+
115+
body.append("}").newline();
116+
117+
return SourceFile.builder(MAP_PACKAGE, MAP_COMPONENT_CLASS_NAME)
118+
.fileComment("Generated by ComponentMapGenerator.")
119+
.addImport("com.github.elebras1.flecs.Component")
120+
.addImport("com.github.elebras1.flecs.ComponentRowView")
121+
.addImport("com.github.elebras1.flecs.ComponentView")
122+
.addImport("java.util.function.Supplier")
123+
.classBody(body.toString())
212124
.build();
213125
}
214126

src/main/java/com/github/elebras1/flecs/processor/ComponentProcessor.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.elebras1.flecs.processor;
22

3-
import com.palantir.javapoet.JavaFile;
3+
import com.github.elebras1.flecs.util.internal.codegen.SourceFile;
44

55
import javax.annotation.processing.*;
66
import javax.lang.model.SourceVersion;
@@ -68,7 +68,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
6868

6969
if (!this.mapGenerated && !this.processedComponents.isEmpty() && annotations.isEmpty()) {
7070
try {
71-
JavaFile mapFile = this.mapGenerator.generateComponentMap(this.processedComponents);
71+
SourceFile mapFile = this.mapGenerator.generateComponentMap(this.processedComponents);
7272
mapFile.writeTo(this.filer);
7373
this.mapGenerated = true;
7474
} catch (IOException e) {
@@ -90,13 +90,13 @@ private void processRecord(TypeElement recordElement) throws IOException {
9090
}
9191
}
9292

93-
JavaFile javaComponentFile = this.componentGenerator.generate(recordElement, fields);
93+
SourceFile javaComponentFile = this.componentGenerator.generate(recordElement, fields);
9494
javaComponentFile.writeTo(this.filer);
9595

96-
JavaFile javaComponentViewFile = this.componentViewGenerator.generate(recordElement, fields);
96+
SourceFile javaComponentViewFile = this.componentViewGenerator.generate(recordElement, fields);
9797
javaComponentViewFile.writeTo(this.filer);
9898

99-
JavaFile javaComponentRowFile = this.componentRowViewGenerator.generate(recordElement, fields);
99+
SourceFile javaComponentRowFile = this.componentRowViewGenerator.generate(recordElement, fields);
100100
javaComponentRowFile.writeTo(this.filer);
101101
}
102102

@@ -113,6 +113,4 @@ private List<VariableElement> extractRecordComponents(TypeElement recordElement)
113113
private boolean isSupportedType(TypeMirror type) {
114114
return SUPPORTED_TYPES.contains(type.toString());
115115
}
116-
}
117-
118-
116+
}

0 commit comments

Comments
 (0)