11package com .github .elebras1 .flecs .processor ;
22
3-
4- import com .github .elebras1 .flecs .annotation .FixedArray ;
5- import com .github .elebras1 .flecs .annotation .FixedString ;
63import com .palantir .javapoet .*;
74
85import javax .lang .model .element .*;
96import java .lang .foreign .MemoryLayout ;
107import java .lang .foreign .MemorySegment ;
118import java .util .List ;
129
13- public class ComponentCodeGenerator {
10+ public class ComponentAbstractGenerator extends AbstractGenerator {
1411
15- private static final String LAYOUT_FIELD_CLASS = "com.github.elebras1.flecs.util.LayoutField" ;
16- private static final String COMPONENT_INTERFACE = "com.github.elebras1.flecs.Component" ;
17- private static final int DEFAULT_STRING_SIZE = 32 ;
12+ protected static final String LAYOUT_FIELD_CLASS = "com.github.elebras1.flecs.util.LayoutField" ;
13+ protected static final String COMPONENT_INTERFACE = "com.github.elebras1.flecs.Component" ;
14+ protected static final int DEFAULT_STRING_SIZE = 32 ;
1815
19- public JavaFile generateComponentClass (TypeElement recordElement , List <VariableElement > fields ) {
16+ public JavaFile generate (TypeElement recordElement , List <VariableElement > fields ) {
2017 String packageName = this .getPackageName (recordElement );
2118 String recordName = recordElement .getSimpleName ().toString ();
2219 String componentClassName = recordName + "Component" ;
@@ -37,42 +34,7 @@ public JavaFile generateComponentClass(TypeElement recordElement, List<VariableE
3734 .addMethod (this .createGetInstanceMethod (packageName , recordName ))
3835 .build ();
3936
40- return JavaFile .builder (packageName , componentClass )
41- .addFileComment ("Generated by ComponentProcessor" )
42- .indent (" " )
43- .build ();
44- }
45-
46- private String getPackageName (TypeElement element ) {
47- Element current = element .getEnclosingElement ();
48- while (current != null && !(current instanceof PackageElement )) {
49- current = current .getEnclosingElement ();
50- }
51- return current != null ? ((PackageElement ) current ).getQualifiedName ().toString () : "" ;
52- }
53-
54- private int getStringSize (VariableElement field ) {
55- FixedString annotation = field .getAnnotation (FixedString .class );
56- if (annotation != null ) {
57- int size = annotation .size ();
58- if ((size & (size - 1 )) != 0 ) {
59- throw new IllegalArgumentException ("Field '" + field .getSimpleName () + "': @FixedString size must be a power of 2. Got: " + size );
60- }
61- return size ;
62- }
63- return DEFAULT_STRING_SIZE ;
64- }
65-
66- private int getArrayLength (VariableElement field ) {
67- FixedArray annotation = field .getAnnotation (FixedArray .class );
68- if (annotation != null ) {
69- int length = annotation .length ();
70- if (length <= 0 ) {
71- throw new IllegalArgumentException ("Field '" + field .getSimpleName () + "': @FixedArray length must be greater than 0. Got: " + length );
72- }
73- return length ;
74- }
75- throw new IllegalArgumentException ("Field '" + field .getSimpleName () + "': Missing @FixedArray annotation for array type." );
37+ return JavaFile .builder (packageName , componentClass ).addFileComment ("Generated by ComponentProcessor" ).indent (" " ).build ();
7638 }
7739
7840 private FieldSpec createLayoutField (String recordName , List <VariableElement > fields ) {
@@ -102,15 +64,15 @@ private FieldSpec createLayoutField(String recordName, List<VariableElement> fie
10264 layoutBuilder .unindent ();
10365 }
10466 layoutBuilder .add (")" );
105- return FieldSpec .builder (MemoryLayout .class , "LAYOUT" , Modifier .PRIVATE , Modifier .STATIC , Modifier .FINAL ).initializer (layoutBuilder .build ()).build ();
67+ return FieldSpec .builder (MemoryLayout .class , "LAYOUT" , Modifier .PROTECTED , Modifier .STATIC , Modifier .FINAL ).initializer (layoutBuilder .build ()).build ();
10668 }
10769
10870 private List <FieldSpec > createOffsetFields (List <VariableElement > fields ) {
10971 return fields .stream ()
11072 .map (field -> {
11173 String fieldName = field .getSimpleName ().toString ();
11274 String constantName = "OFFSET_" + fieldName .toUpperCase ();
113- return FieldSpec .builder (long .class , constantName , Modifier .PRIVATE , Modifier .STATIC , Modifier .FINAL )
75+ return FieldSpec .builder (long .class , constantName , Modifier .PROTECTED , Modifier .STATIC , Modifier .FINAL )
11476 .initializer ("$L.offsetOf(LAYOUT, $S)" , LAYOUT_FIELD_CLASS , fieldName )
11577 .build ();
11678 })
@@ -254,27 +216,6 @@ private String getLayoutMethod(String type) {
254216 };
255217 }
256218
257- private String getGetterMethod (String type ) {
258- return switch (type ) {
259- case "byte" -> "getByte" ;
260- case "short" -> "getShort" ;
261- case "int" -> "getInt" ;
262- case "long" -> "getLong" ;
263- case "float" -> "getFloat" ;
264- case "double" -> "getDouble" ;
265- case "boolean" -> "getBoolean" ;
266- case "byte[]" -> "getByteArray" ;
267- case "short[]" -> "getShortArray" ;
268- case "int[]" -> "getIntArray" ;
269- case "long[]" -> "getLongArray" ;
270- case "float[]" -> "getFloatArray" ;
271- case "double[]" -> "getDoubleArray" ;
272- case "boolean[]" -> "getBooleanArray" ;
273- case "java.lang.String" -> "getFixedString" ;
274- default -> throw new IllegalArgumentException ("Unsupported type: " + type );
275- };
276- }
277-
278219 private MethodSpec createOffsetOfMethod (List <VariableElement > fields ) {
279220 MethodSpec .Builder method = MethodSpec .methodBuilder ("offsetOf" )
280221 .addAnnotation (Override .class )
0 commit comments