Search in sources :

Example 1 with PrefsProperty

use of com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty 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)

Example 2 with PrefsProperty

use of com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty in project kripton by xcesco.

the class BindSharedPreferencesBuilder method generateWriteMethod.

/**
 * @param entity
 */
private static void generateWriteMethod(PrefsEntity entity) {
    // write method
    MethodSpec.Builder method = MethodSpec.methodBuilder("write").addJavadoc("write bean entirely\n\n").addJavadoc("@param bean bean to entirely write\n").addModifiers(Modifier.PUBLIC).addParameter(typeName(entity.getName()), "bean").returns(Void.TYPE);
    method.addStatement("$T editor=prefs.edit()", SharedPreferences.Editor.class);
    PrefsTransform transform;
    for (PrefsProperty item : entity.getCollection()) {
        if (item.hasTypeAdapter()) {
            transform = PrefsTransformer.lookup(item.typeAdapter.getDataTypeTypename());
        } else {
            transform = PrefsTransformer.lookup(item);
        }
        transform.generateWriteProperty(method, "editor", typeName(entity.getElement()), "bean", item);
        method.addCode("\n");
    }
    method.addCode("\n");
    method.addStatement("editor.commit()");
    builder.addMethod(method.build());
}
Also used : PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) MethodSpec(com.squareup.javapoet.MethodSpec) BindSharedPreferences(com.abubusoft.kripton.android.annotation.BindSharedPreferences) SharedPreferences(android.content.SharedPreferences) PrefsTransform(com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)

Example 3 with PrefsProperty

use of com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty in project kripton by xcesco.

the class BindSharedPreferencesBuilder method generateReadMethod.

private static void generateReadMethod(PrefsEntity entity) {
    // read method
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("read").addModifiers(Modifier.PUBLIC).addJavadoc("read bean entirely\n\n").addJavadoc("@return read bean\n").returns(typeName(entity.getName()));
    methodBuilder.addStatement("$T bean=new $T()", typeName(entity.getName()), typeName(entity.getName()));
    PrefsTransform transform;
    for (PrefsProperty item : entity.getCollection()) {
        if (item.hasTypeAdapter()) {
            transform = PrefsTransformer.lookup(item.typeAdapter.getDataTypeTypename());
        } else {
            transform = PrefsTransformer.lookup(item);
        }
        transform.generateReadProperty(methodBuilder, "prefs", typeName(item.getElement().asType()), "bean", item, true);
        methodBuilder.addCode("\n");
    }
    methodBuilder.addCode("\n");
    methodBuilder.addStatement("return bean");
    builder.addMethod(methodBuilder.build());
}
Also used : PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) MethodSpec(com.squareup.javapoet.MethodSpec) PrefsTransform(com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)

Example 4 with PrefsProperty

use of com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty in project kripton by xcesco.

the class BindSharedPreferencesBuilder method generateSingleReadMethod.

private static void generateSingleReadMethod(PrefsEntity entity) {
    // read method
    PrefsTransform transform;
    for (PrefsProperty item : entity.getCollection()) {
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(item.getName()).addModifiers(Modifier.PUBLIC).addJavadoc("read property $L\n\n", item.getName()).addJavadoc("@return property $L value\n", item.getName()).returns(item.getPropertyType().getTypeName());
        if (item.hasTypeAdapter()) {
            transform = PrefsTransformer.lookup(item.typeAdapter.getDataTypeTypename());
        } else {
            transform = PrefsTransformer.lookup(item);
        }
        transform.generateReadProperty(methodBuilder, "prefs", typeName(item.getElement().asType()), "defaultBean", item, false);
        methodBuilder.addCode("\n");
        builder.addMethod(methodBuilder.build());
    }
}
Also used : PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) MethodSpec(com.squareup.javapoet.MethodSpec) PrefsTransform(com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)

Example 5 with PrefsProperty

use of com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty in project kripton by xcesco.

the class BindSharedPreferencesBuilder method generate.

/**
 * Generate shared preference manager
 *
 * @return typeName of generated class
 *
 * @throws IOException
 */
public static String generate(Elements elementUtils, Filer filer, PrefsEntity entity) throws IOException {
    com.abubusoft.kripton.common.Converter<String, String> converter = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL);
    String beanClassName = entity.getSimpleName().toString();
    String suffix;
    if (beanClassName.endsWith(SUFFIX_SHARED_PREFERENCE)) {
        suffix = "";
    } else if (beanClassName.endsWith(SUFFIX_PREFERENCE)) {
        suffix = "";
    } else {
        suffix = SUFFIX_SHARED_PREFERENCE;
    }
    String className = PREFIX + beanClassName + suffix;
    ModelAnnotation annotation = entity.getAnnotation(BindSharedPreferences.class);
    String sharedPreferenceName = annotation.getAttribute(AnnotationAttributeType.VALUE);
    PackageElement pkg = elementUtils.getPackageOf(entity.getElement());
    String packageName = pkg.isUnnamed() ? "" : pkg.getQualifiedName().toString();
    AnnotationProcessorUtilis.infoOnGeneratedClasses(BindSharedPreferences.class, packageName, className);
    // @formatter:off
    builder = TypeSpec.classBuilder(className).addModifiers(Modifier.PUBLIC).superclass(AbstractSharedPreference.class);
    BindTypeContext context = new BindTypeContext(builder, TypeUtility.typeName(entity.getElement()), Modifier.PRIVATE);
    // @formatter:on
    builder.addJavadoc("This class is the shared preference binder defined for $T\n\n", entity.getElement());
    JavadocUtility.generateJavadocGeneratedBy(builder);
    builder.addJavadoc("@see $T\n", entity.getElement());
    if (StringUtils.hasText(sharedPreferenceName)) {
        builder.addField(FieldSpec.builder(String.class, "SHARED_PREFERENCE_NAME", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("$S", converter.convert(entity.getSimpleName().toString())).addJavadoc("shared preferences typeName for $T\n", entity.getElement()).build());
    }
    builder.addField(FieldSpec.builder(className(beanClassName), "defaultBean", Modifier.PRIVATE, Modifier.FINAL).addJavadoc("working instance of bean\n").build());
    {
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("edit").addJavadoc("create an editor to modify shared preferences\n").returns(typeName("BindEditor")).addModifiers(Modifier.PUBLIC);
        methodBuilder.addStatement("return new $T()", typeName("BindEditor"));
        builder.addMethod(methodBuilder.build());
    }
    generateEditor(entity);
    generateConstructor(sharedPreferenceName, beanClassName);
    generateRefresh(sharedPreferenceName, className);
    generateResetMethod(entity);
    generateReadMethod(entity);
    generateWriteMethod(entity);
    generateSingleReadMethod(entity);
    // generate all needed writer and reader
    List<PrefsProperty> fields = entity.getCollection();
    List<PrefsProperty> filteredFields = new ArrayList<>();
    // we need to avoid generation of persists values
    for (PrefsProperty item : fields) {
        if (SetPrefsTransformation.isStringSet(item)) {
            continue;
        } else {
            filteredFields.add(item);
        }
    }
    // avoid to consider StringSet
    ManagedPropertyPersistenceHelper.generateFieldPersistance(context, filteredFields, PersistType.STRING, false, Modifier.PROTECTED);
    generateInstance(className);
    TypeSpec typeSpec = builder.build();
    JavaWriterHelper.writeJava2File(filer, packageName, typeSpec);
    return className;
}
Also used : Builder(com.squareup.javapoet.TypeSpec.Builder) ArrayList(java.util.ArrayList) BindTypeContext(com.abubusoft.kripton.processor.bind.BindTypeContext) AbstractSharedPreference(com.abubusoft.kripton.android.sharedprefs.AbstractSharedPreference) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) PackageElement(javax.lang.model.element.PackageElement) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

PrefsProperty (com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty)6 PrefsTransform (com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)4 MethodSpec (com.squareup.javapoet.MethodSpec)4 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)2 Builder (com.squareup.javapoet.TypeSpec.Builder)2 SharedPreferences (android.content.SharedPreferences)1 BindPreference (com.abubusoft.kripton.android.annotation.BindPreference)1 BindSharedPreferences (com.abubusoft.kripton.android.annotation.BindSharedPreferences)1 AbstractSharedPreference (com.abubusoft.kripton.android.sharedprefs.AbstractSharedPreference)1 BindDisabled (com.abubusoft.kripton.annotation.BindDisabled)1 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)1 BindTypeContext (com.abubusoft.kripton.processor.bind.BindTypeContext)1 BindEntity (com.abubusoft.kripton.processor.bind.model.BindEntity)1 BindProperty (com.abubusoft.kripton.processor.bind.model.BindProperty)1 InvalidDefinition (com.abubusoft.kripton.processor.exceptions.InvalidDefinition)1 PrefsEntity (com.abubusoft.kripton.processor.sharedprefs.model.PrefsEntity)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 ArrayList (java.util.ArrayList)1 Element (javax.lang.model.element.Element)1