Last active
January 15, 2016 17:20
-
-
Save rickyngk/61a852db375d0a4b762a to your computer and use it in GitHub Desktop.
EntityX annotations mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class EntityX { | |
static class EntityMetaData { | |
String key; | |
Class entityType; | |
Class<?> collectionType; | |
boolean isArray = false; | |
boolean isDerivedFromEntity; | |
EntityMetaData(String key, Class entityType, Class collectionType) { | |
this.key = key; | |
this.entityType = entityType; | |
this.collectionType = collectionType; | |
isArray = true; | |
isDerivedFromEntity = EntityX.class.isAssignableFrom(entityType); | |
} | |
EntityMetaData(String key, Class entityType) { | |
this.key = key; | |
this.entityType = entityType; | |
this.collectionType = void.class; | |
isDerivedFromEntity = EntityX.class.isAssignableFrom(entityType); | |
} | |
public Class getEntityType() { | |
return entityType; | |
} | |
} | |
protected HashMap<String, EntityMetaData>fields; | |
void bind(EntityX clzz) { | |
try { | |
fields = new HashMap<>(); | |
for (Field field : clzz.getClass().getDeclaredFields()) { | |
final BindField bf = field.getAnnotation(BindField.class); | |
if (bf != null) { | |
String field_name = bf.value(); | |
Class clz = field.getType(); | |
if (Iterable.class.isAssignableFrom(clz)) { | |
Type type = field.getGenericType(); | |
ParameterizedType pt = (ParameterizedType) type; | |
Type innerType = pt.getActualTypeArguments()[0]; | |
Class<?> innerClass = (Class<?>) innerType; | |
fields.put(field.getName(), new EntityMetaData(field_name, innerClass, clz)); | |
} else { | |
fields.put(field.getName(), new EntityMetaData(field_name, clz)); | |
} | |
} | |
} | |
} catch (Exception E) { | |
E.printStackTrace(); | |
} | |
} | |
protected EntityX() { | |
bind(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment