@@ -36,6 +36,7 @@ class Codegen {
36
36
add (Vector .class );
37
37
}};
38
38
static volatile Map <String , Decoder > cache = new HashMap <String , Decoder >();
39
+ static List <FieldDecoderFactory > fieldDecoderFactories = new ArrayList <FieldDecoderFactory >();
39
40
static ClassPool pool = ClassPool .getDefault ();
40
41
41
42
static Decoder getDecoder (String cacheKey , Type type , Type ... typeArgs ) {
@@ -160,22 +161,32 @@ public static void addNewDecoder(String cacheKey, Decoder decoder) {
160
161
private static String genObject (Class clazz , String cacheKey ) {
161
162
Map <Integer , Object > map = new HashMap <Integer , Object >();
162
163
for (Field field : clazz .getFields ()) {
163
- byte [] fieldName = field .getName ().getBytes ();
164
- Map <Byte , Object > current = (Map <Byte , Object >) map .get (fieldName .length );
165
- if (current == null ) {
166
- current = new HashMap <Byte , Object >();
167
- map .put (fieldName .length , current );
164
+ Decoder decoder = createFieldDecoder (cacheKey , field );
165
+ String [] alternativeFieldNames = null ;
166
+ if (decoder instanceof FieldDecoder ) {
167
+ alternativeFieldNames = ((FieldDecoder ) decoder ).getAlternativeFieldNames ();
168
168
}
169
- for (int i = 0 ; i < fieldName .length - 1 ; i ++) {
170
- byte b = fieldName [i ];
171
- Map <Byte , Object > next = (Map <Byte , Object >) current .get (b );
172
- if (next == null ) {
173
- next = new HashMap <Byte , Object >();
174
- current .put (b , next );
169
+ if (alternativeFieldNames == null ) {
170
+ alternativeFieldNames = new String []{field .getName ()};
171
+ }
172
+ for (String alternativeFieldName : alternativeFieldNames ) {
173
+ byte [] fieldName = alternativeFieldName .getBytes ();
174
+ Map <Byte , Object > current = (Map <Byte , Object >) map .get (fieldName .length );
175
+ if (current == null ) {
176
+ current = new HashMap <Byte , Object >();
177
+ map .put (fieldName .length , current );
178
+ }
179
+ for (int i = 0 ; i < fieldName .length - 1 ; i ++) {
180
+ byte b = fieldName [i ];
181
+ Map <Byte , Object > next = (Map <Byte , Object >) current .get (b );
182
+ if (next == null ) {
183
+ next = new HashMap <Byte , Object >();
184
+ current .put (b , next );
185
+ }
186
+ current = next ;
175
187
}
176
- current = next ;
188
+ current . put ( fieldName [ fieldName . length - 1 ], field ) ;
177
189
}
178
- current .put (fieldName [fieldName .length - 1 ], field );
179
190
}
180
191
if (map .isEmpty ()) {
181
192
StringBuilder lines = new StringBuilder ();
@@ -206,6 +217,20 @@ private static String genObject(Class clazz, String cacheKey) {
206
217
return lines .toString ().replace ("{{clazz}}" , clazz .getName ());
207
218
}
208
219
220
+ private static Decoder createFieldDecoder (String cacheKey , Field field ) {
221
+ String fieldCacheKey = field .getName () + "@" + cacheKey ;
222
+ for (FieldDecoderFactory fieldDecoderFactory : fieldDecoderFactories ) {
223
+ Decoder decoder = fieldDecoderFactory .createDecoder (field );
224
+ if (decoder != null ) {
225
+ addNewDecoder (fieldCacheKey , decoder );
226
+ break ;
227
+ }
228
+ }
229
+ // the decoder can be just created by the factory
230
+ // or it can be registered directly
231
+ return cache .get (fieldCacheKey );
232
+ }
233
+
209
234
private static void addFieldDispatch (StringBuilder lines , int len , int i , Map <Byte , Object > current , String cacheKey ) {
210
235
for (Map .Entry <Byte , Object > entry : current .entrySet ()) {
211
236
Byte b = entry .getKey ();
@@ -221,8 +246,8 @@ private static void addFieldDispatch(StringBuilder lines, int len, int i, Map<By
221
246
}
222
247
223
248
private static void genField (StringBuilder lines , Field field , String cacheKey ) {
224
- String fieldCacheKey = field .getName () + "@" + cacheKey ;
225
249
String fieldTypeName = field .getType ().getCanonicalName ();
250
+ String fieldCacheKey = field .getName () + "@" + cacheKey ;
226
251
if (cache .containsKey (fieldCacheKey )) {
227
252
append (lines , String .format ("obj.%s = (%s)iter.read(\" %s\" , %s.class);" ,
228
253
field .getName (), fieldTypeName , fieldCacheKey , fieldTypeName ));
@@ -399,4 +424,8 @@ private static void append(StringBuilder lines, String str) {
399
424
lines .append (str );
400
425
lines .append ("\n " );
401
426
}
427
+
428
+ public static void addFieldDecoderFactory (FieldDecoderFactory fieldDecoderFactory ) {
429
+ fieldDecoderFactories .add (fieldDecoderFactory );
430
+ }
402
431
}
0 commit comments