use of com.cjsoftware.processor.uistatepreservation.model.DoPreservedField in project ucsFramework by cjsoftware-lib.
the class PreserveUiStateProcessor method addStrategyFields.
private void addStrategyFields(TypeSpec.Builder preservationClass, List<DoPreservedField> fieldList, TypeMirror preservedFieldContainingType) {
Types typeUtils = getProcessorModel().getTypeUtils();
Elements elementUtils = getProcessorModel().getElementUtils();
TypeMirror valueStrategy = elementUtils.getTypeElement(ValuePreservationStrategy.class.getCanonicalName()).asType();
for (DoPreservedField field : fieldList) {
for (TypeMirror strategy : field.getPreservationStrategyList()) {
String fieldName = String.format(FIELD_PRESERVE_STRATEGY_INSTANCE, field.getField().getSimpleName(), typeUtils.asElement(strategy).getSimpleName());
FieldSpec.Builder strategyInstanceField;
if (typeUtils.isAssignable(strategy, valueStrategy)) {
// Value preservation strategy is parametric on preserved value type
TypeMirror safeType = field.getField().asType();
if (safeType.getKind().isPrimitive()) {
safeType = typeUtils.boxedClass((PrimitiveType) safeType).asType();
}
strategyInstanceField = FieldSpec.builder(ParameterizedTypeName.get((ClassName) ClassName.get(strategy), TypeName.get(preservedFieldContainingType), TypeName.get(safeType)), fieldName, Modifier.PRIVATE).initializer("new $T<>()", strategy);
} else {
strategyInstanceField = FieldSpec.builder(TypeName.get(strategy), fieldName, Modifier.PRIVATE).initializer("new $T()", strategy);
}
preservationClass.addField(strategyInstanceField.build());
}
}
}
Aggregations