use of com.abubusoft.kripton.processor.sharedprefs.model.PrefsEntity in project kripton by xcesco.
the class BindSharedPreferencesSubProcessor method analyzeSharedPreferences.
private String analyzeSharedPreferences(final TypeElement sharedPreference) {
Element beanElement = sharedPreference;
String result = beanElement.getSimpleName().toString();
// create equivalent entity in the domain of bind processor
final BindEntity bindEntity = BindEntityBuilder.parse(null, sharedPreference);
final PrefsEntity currentEntity = new PrefsEntity(beanElement.getSimpleName().toString(), (TypeElement) beanElement, AnnotationUtility.buildAnnotationList((TypeElement) beanElement, classAnnotationFilter));
final boolean bindAllFields = AnnotationUtility.getAnnotationAttributeAsBoolean(currentEntity, BindType.class, AnnotationAttributeType.ALL_FIELDS, Boolean.TRUE);
PropertyUtility.buildProperties(elementUtils, currentEntity, new PropertyFactory<PrefsEntity, PrefsProperty>() {
@Override
public PrefsProperty createProperty(PrefsEntity entity, Element propertyElement) {
return new PrefsProperty(currentEntity, propertyElement, AnnotationUtility.buildAnnotationList(propertyElement));
}
}, propertyAnnotationFilter, new PropertyCreatedListener<PrefsEntity, PrefsProperty>() {
@Override
public boolean onProperty(PrefsEntity entity, PrefsProperty property) {
// if @BindDisabled is present, exit immediately
if (property.hasAnnotation(BindDisabled.class)) {
if (bindAllFields) {
return false;
} else {
throw new InvalidDefinition("@BindDisabled can not be used with @BindType(allField=false)");
}
}
if (property.getPropertyType().isArray() || property.getPropertyType().isList()) {
property.setPreferenceType(PreferenceType.STRING);
} else {
if (property.isType(Boolean.TYPE, Boolean.class)) {
property.setPreferenceType(PreferenceType.BOOL);
} else if (property.isType(Short.TYPE, Short.class)) {
property.setPreferenceType(PreferenceType.INT);
} else if (property.isType(Character.TYPE, Character.class)) {
property.setPreferenceType(PreferenceType.STRING);
} else if (property.isType(Integer.TYPE, Integer.class)) {
property.setPreferenceType(PreferenceType.INT);
} else if (property.isType(Long.TYPE, Long.class)) {
property.setPreferenceType(PreferenceType.LONG);
} else if (property.isType(Float.TYPE, Float.class)) {
property.setPreferenceType(PreferenceType.FLOAT);
} else if (property.isType(Double.TYPE, Double.class)) {
property.setPreferenceType(PreferenceType.STRING);
} else {
property.setPreferenceType(PreferenceType.STRING);
}
}
if (!bindAllFields && !property.hasAnnotation(BindPreference.class)) {
// skip field
return false;
}
// if field disable, skip property definition
ModelAnnotation annotation = property.getAnnotation(BindPreference.class);
if (annotation != null && AnnotationUtility.extractAsBoolean(property, annotation, AnnotationAttributeType.ENABLED) == false) {
return false;
}
if (bindEntity.contains(property.getName())) {
BindProperty bindProperty = bindEntity.get(property.getName());
if (bindProperty.isBindedArray() || bindProperty.isBindedCollection() || bindProperty.isBindedMap() || bindProperty.isBindedObject()) {
property.bindProperty = bindProperty;
}
} else {
throw (new KriptonRuntimeException(String.format("In class '%s' property '%s' has a wrong definition to create SharedPreference", sharedPreference.asType(), property.getName())));
}
return true;
}
});
model.entityAdd(currentEntity);
return result;
}
Aggregations