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();
}
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));
}
}
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;
}
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);
}
}
}
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();
}
Aggregations