use of org.apache.ignite.configuration.annotation.Value in project ignite-3 by apache.
the class Processor method createPojoBindings.
/**
* Create VIEW and CHANGE classes and methods.
*
* @param fields Collection of configuration fields.
* @param schemaClassName Class name of schema.
* @param configurationInterfaceBuilder Configuration interface builder.
* @param extendBaseSchema {@code true} if extending base schema interfaces.
* @param realSchemaClass Class descriptor.
* @param isPolymorphicConfig Is a polymorphic configuration.
* @param isPolymorphicInstanceConfig Is an instance of polymorphic configuration.
*/
private void createPojoBindings(Collection<VariableElement> fields, ClassName schemaClassName, TypeSpec.Builder configurationInterfaceBuilder, boolean extendBaseSchema, TypeElement realSchemaClass, boolean isPolymorphicConfig, boolean isPolymorphicInstanceConfig) {
ClassName viewClsName = Utils.getViewName(schemaClassName);
ClassName changeClsName = Utils.getChangeName(schemaClassName);
TypeName configInterfaceType;
@Nullable TypeName viewBaseSchemaInterfaceType;
@Nullable TypeName changeBaseSchemaInterfaceType;
if (extendBaseSchema) {
DeclaredType superClassType = (DeclaredType) realSchemaClass.getSuperclass();
ClassName superClassSchemaClassName = ClassName.get((TypeElement) superClassType.asElement());
configInterfaceType = Utils.getConfigurationInterfaceName(superClassSchemaClassName);
viewBaseSchemaInterfaceType = Utils.getViewName(superClassSchemaClassName);
changeBaseSchemaInterfaceType = Utils.getChangeName(superClassSchemaClassName);
} else {
ClassName confTreeInterface = ClassName.get("org.apache.ignite.configuration", "ConfigurationTree");
configInterfaceType = ParameterizedTypeName.get(confTreeInterface, viewClsName, changeClsName);
viewBaseSchemaInterfaceType = null;
changeBaseSchemaInterfaceType = null;
}
configurationInterfaceBuilder.addSuperinterface(configInterfaceType);
// This code will be refactored in the future. Right now I don't want to entangle it with existing code
// generation. It has only a few considerable problems - hardcode and a lack of proper arrays handling.
// Clone method should be used to guarantee data integrity.
TypeSpec.Builder viewClsBuilder = TypeSpec.interfaceBuilder(viewClsName).addModifiers(PUBLIC);
if (viewBaseSchemaInterfaceType != null) {
viewClsBuilder.addSuperinterface(viewBaseSchemaInterfaceType);
}
TypeSpec.Builder changeClsBuilder = TypeSpec.interfaceBuilder(changeClsName).addSuperinterface(viewClsName).addModifiers(PUBLIC);
if (changeBaseSchemaInterfaceType != null) {
changeClsBuilder.addSuperinterface(changeBaseSchemaInterfaceType);
}
if (isPolymorphicInstanceConfig) {
changeClsBuilder.addSuperinterface(POLYMORPHIC_CHANGE_CLASSNAME);
}
ClassName consumerClsName = ClassName.get(Consumer.class);
for (VariableElement field : fields) {
Value valAnnotation = field.getAnnotation(Value.class);
String fieldName = field.getSimpleName().toString();
TypeMirror schemaFieldType = field.asType();
TypeName schemaFieldTypeName = TypeName.get(schemaFieldType);
boolean leafField = isPrimitiveOrArray(schemaFieldType) || !((ClassName) schemaFieldTypeName).simpleName().contains(CONFIGURATION_SCHEMA_POSTFIX);
boolean namedListField = field.getAnnotation(NamedConfigValue.class) != null;
TypeName viewFieldType = leafField ? schemaFieldTypeName : Utils.getViewName((ClassName) schemaFieldTypeName);
TypeName changeFieldType = leafField ? schemaFieldTypeName : Utils.getChangeName((ClassName) schemaFieldTypeName);
if (namedListField) {
changeFieldType = ParameterizedTypeName.get(ClassName.get(NamedListChange.class), viewFieldType, changeFieldType);
viewFieldType = ParameterizedTypeName.get(ClassName.get(NamedListView.class), WildcardTypeName.subtypeOf(viewFieldType));
}
MethodSpec.Builder getMtdBuilder = MethodSpec.methodBuilder(fieldName).addModifiers(PUBLIC, ABSTRACT).returns(viewFieldType);
viewClsBuilder.addMethod(getMtdBuilder.build());
// Read only.
if (field.getAnnotation(PolymorphicId.class) != null || field.getAnnotation(InjectedName.class) != null || field.getAnnotation(InternalId.class) != null) {
continue;
}
String changeMtdName = "change" + capitalize(fieldName);
MethodSpec.Builder changeMtdBuilder = MethodSpec.methodBuilder(changeMtdName).addModifiers(PUBLIC, ABSTRACT).returns(changeClsName);
if (valAnnotation != null) {
if (schemaFieldType.getKind() == TypeKind.ARRAY) {
changeMtdBuilder.varargs(true);
}
changeMtdBuilder.addParameter(changeFieldType, fieldName);
} else {
changeMtdBuilder.addParameter(ParameterizedTypeName.get(consumerClsName, changeFieldType), fieldName);
}
changeClsBuilder.addMethod(changeMtdBuilder.build());
}
if (isPolymorphicConfig) {
// Parameter type: Class<T>.
ParameterizedTypeName parameterType = ParameterizedTypeName.get(ClassName.get(Class.class), TypeVariableName.get("T"));
// Variable type, for example: <T extends SimpleChange & PolymorphicInstance>.
TypeVariableName typeVariable = TypeVariableName.get("T", changeClsName, POLYMORPHIC_CHANGE_CLASSNAME);
// Method like: <T extends SimpleChange> T convert(Class<T> changeClass);
MethodSpec.Builder convertMtdBuilder = MethodSpec.methodBuilder("convert").addModifiers(PUBLIC, ABSTRACT).addTypeVariable(typeVariable).addParameter(parameterType, "changeClass").returns(TypeVariableName.get("T"));
changeClsBuilder.addMethod(convertMtdBuilder.build());
}
TypeSpec viewCls = viewClsBuilder.build();
TypeSpec changeCls = changeClsBuilder.build();
buildClass(viewClsName.packageName(), viewCls);
buildClass(changeClsName.packageName(), changeCls);
}
Aggregations