Search in sources :

Example 1 with PrefsEntity

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;
}
Also used : PrefsEntity(com.abubusoft.kripton.processor.sharedprefs.model.PrefsEntity) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException) BindEntity(com.abubusoft.kripton.processor.bind.model.BindEntity) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) InvalidDefinition(com.abubusoft.kripton.processor.exceptions.InvalidDefinition) BindDisabled(com.abubusoft.kripton.annotation.BindDisabled) BindPreference(com.abubusoft.kripton.android.annotation.BindPreference) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Aggregations

BindPreference (com.abubusoft.kripton.android.annotation.BindPreference)1 BindDisabled (com.abubusoft.kripton.annotation.BindDisabled)1 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)1 BindEntity (com.abubusoft.kripton.processor.bind.model.BindEntity)1 BindProperty (com.abubusoft.kripton.processor.bind.model.BindProperty)1 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)1 InvalidDefinition (com.abubusoft.kripton.processor.exceptions.InvalidDefinition)1 PrefsEntity (com.abubusoft.kripton.processor.sharedprefs.model.PrefsEntity)1 PrefsProperty (com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1