Search in sources :

Example 1 with DoPreservedField

use of com.cjsoftware.processor.uistatepreservation.model.DoPreservedField in project ucsFramework by cjsoftware-lib.

the class PreserveUiStateProcessor method addSaveMethod.

private MethodSpec addSaveMethod(TypeName valueOwnerType, List<DoPreservedField> fieldList) {
    MethodSpec.Builder saveMethod = MethodSpec.methodBuilder("savePreservedFields").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(ParameterSpec.builder(valueOwnerType, PARAMETER_FIELD_OWNER_NAME).build());
    for (DoPreservedField field : fieldList) {
        saveMethod.addStatement("$N($N)", String.format(METHOD_SAVE_FIELD_NAME_TEMPLATE, field.getField()), PARAMETER_FIELD_OWNER_NAME);
    }
    saveMethod.addStatement("$N = true", FIELD_HAVE_STATE);
    return saveMethod.build();
}
Also used : DoPreservedField(com.cjsoftware.processor.uistatepreservation.model.DoPreservedField) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 2 with DoPreservedField

use of com.cjsoftware.processor.uistatepreservation.model.DoPreservedField in project ucsFramework by cjsoftware-lib.

the class PreserveUiStateProcessor method processElement.

@Override
public void processElement(VariableElement element) {
    // Group all of the preserved fields by the class they are in
    TypeElement topMostElement = getTopmostEnclosingElement(element);
    List<DoPreservedField> preservedFieldList = preservedFieldGroups.get(topMostElement);
    if (preservedFieldList == null) {
        preservedFieldList = new LinkedList<>();
        preservedFieldGroups.put(topMostElement, preservedFieldList);
    }
    if (element.getModifiers().contains(Modifier.PRIVATE)) {
        warningMessage("Cannot preserve a private field", element);
    } else {
        preservedFieldList.add(buildPreservedField(element.getAnnotation(Preserve.class), element));
    }
}
Also used : DoPreservedField(com.cjsoftware.processor.uistatepreservation.model.DoPreservedField) TypeElement(javax.lang.model.element.TypeElement)

Example 3 with DoPreservedField

use of com.cjsoftware.processor.uistatepreservation.model.DoPreservedField in project ucsFramework by cjsoftware-lib.

the class PreserveUiStateProcessor method buildPreservedField.

private DoPreservedField buildPreservedField(Preserve preserveAnnotation, VariableElement element) {
    DoPreservedField newPreservedField = null;
    String[] ruleGroups = preserveAnnotation.applyRuleGroups();
    Set<String> ruleGroupSet = new HashSet<>();
    for (String groupName : ruleGroups) {
        addRuleGroups(element, groupName, ruleGroupSet);
    }
    Elements elementUtils = getProcessorModel().getElementUtils();
    TypeElement containingType = (TypeElement) element.getEnclosingElement();
    newPreservedField = new DoPreservedField(elementUtils.getPackageOf(containingType), containingType, element);
    newPreservedField.getPreservationStrategyList().addAll(resolveStrategies(ruleGroupSet, element));
    List<? extends TypeMirror> strategies = null;
    try {
        preserveAnnotation.addStrategies();
    } catch (MirroredTypesException mte) {
        strategies = mte.getTypeMirrors();
    }
    if (strategies != null) {
        newPreservedField.getPreservationStrategyList().addAll(strategies);
    }
    try {
        preserveAnnotation.removeStrategies();
    } catch (MirroredTypesException mte) {
        strategies = mte.getTypeMirrors();
    }
    if (strategies != null) {
        newPreservedField.getPreservationStrategyList().removeAll(strategies);
    }
    return newPreservedField;
}
Also used : MirroredTypesException(javax.lang.model.type.MirroredTypesException) DoPreservedField(com.cjsoftware.processor.uistatepreservation.model.DoPreservedField) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) HashSet(java.util.HashSet)

Example 4 with DoPreservedField

use of com.cjsoftware.processor.uistatepreservation.model.DoPreservedField in project ucsFramework by cjsoftware-lib.

the class PreserveUiStateProcessor method completeProcessing.

@Override
protected void completeProcessing() {
    super.completeProcessing();
    if (isFatalErrorEncountered()) {
        // If we hit a fatal error, bail.
        return;
    }
    for (TypeElement preservedFieldContainer : preservedFieldGroups.keySet()) {
        PackageElement packageElement = getProcessorModel().getElementUtils().getPackageOf(preservedFieldContainer);
        ClassName generatedFieldListClassName = ClassName.get(packageElement.getQualifiedName().toString(), String.format(GENERATED_PRESERVED_FIELD_LIST_CLASS_NAME_TEMPLATE, sanitiseClassName(preservedFieldContainer.getSimpleName().toString())));
        TypeSpec.Builder preserveStateClass = TypeSpec.classBuilder(generatedFieldListClassName).addSuperinterface(ParameterizedTypeName.get(ClassName.get(StatePreservationManager.class), ClassName.get(preservedFieldContainer.asType()))).addField(FieldSpec.builder(TypeName.BOOLEAN, FIELD_HAVE_STATE).initializer("false").build());
        List<DoPreservedField> preservedFieldList = preservedFieldGroups.get(preservedFieldContainer);
        addStrategyFields(preserveStateClass, preservedFieldList, preservedFieldContainer.asType());
        preserveStateClass.addMethod(addSaveMethod(TypeName.get(preservedFieldContainer.asType()), preservedFieldList));
        for (DoPreservedField field : preservedFieldList) {
            preserveStateClass.addMethod(addSaveFieldMethod(TypeName.get(preservedFieldContainer.asType()), field));
        }
        preserveStateClass.addMethod(addRestoreMethod(TypeName.get(preservedFieldContainer.asType()), preservedFieldList));
        for (DoPreservedField field : preservedFieldList) {
            preserveStateClass.addMethod(addRestoreFieldMethod(TypeName.get(preservedFieldContainer.asType()), field));
        }
        try {
            JavaFile javaFile = JavaFile.builder(packageElement.getQualifiedName().toString(), preserveStateClass.build()).addFileComment("Generated Code, do not modify.").build();
            javaFile.writeTo(getProcessorModel().getFiler());
        } catch (IOException e) {
            fatalErrorMessage("Exception writing java file: " + e.toString(), preservedFieldContainer);
        }
    }
}
Also used : DoPreservedField(com.cjsoftware.processor.uistatepreservation.model.DoPreservedField) TypeElement(javax.lang.model.element.TypeElement) ClassName(com.squareup.javapoet.ClassName) StatePreservationManager(com.cjsoftware.library.uistatepreservation.StatePreservationManager) JavaFile(com.squareup.javapoet.JavaFile) PackageElement(javax.lang.model.element.PackageElement) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 5 with DoPreservedField

use of com.cjsoftware.processor.uistatepreservation.model.DoPreservedField in project ucsFramework by cjsoftware-lib.

the class PreserveUiStateProcessor method addRestoreMethod.

private MethodSpec addRestoreMethod(TypeName valueOwnerType, List<DoPreservedField> fieldList) {
    MethodSpec.Builder saveMethod = MethodSpec.methodBuilder("restorePreservedFields").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(ParameterSpec.builder(valueOwnerType, PARAMETER_FIELD_OWNER_NAME).build());
    saveMethod.beginControlFlow("if($N)", FIELD_HAVE_STATE).addStatement("$N = false", FIELD_HAVE_STATE);
    for (DoPreservedField field : fieldList) {
        saveMethod.addStatement("$N($N)", String.format(METHOD_RESTORE_FIELD_NAME_TEMPLATE, field.getField()), PARAMETER_FIELD_OWNER_NAME);
    }
    saveMethod.endControlFlow();
    return saveMethod.build();
}
Also used : DoPreservedField(com.cjsoftware.processor.uistatepreservation.model.DoPreservedField) MethodSpec(com.squareup.javapoet.MethodSpec)

Aggregations

DoPreservedField (com.cjsoftware.processor.uistatepreservation.model.DoPreservedField)6 TypeElement (javax.lang.model.element.TypeElement)3 ClassName (com.squareup.javapoet.ClassName)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 Elements (javax.lang.model.util.Elements)2 StatePreservationManager (com.cjsoftware.library.uistatepreservation.StatePreservationManager)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 JavaFile (com.squareup.javapoet.JavaFile)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 PackageElement (javax.lang.model.element.PackageElement)1 MirroredTypesException (javax.lang.model.type.MirroredTypesException)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Types (javax.lang.model.util.Types)1