Search in sources :

Example 11 with Value

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);
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) InjectedName(org.apache.ignite.configuration.annotation.InjectedName) VariableElement(javax.lang.model.element.VariableElement) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) PolymorphicId(org.apache.ignite.configuration.annotation.PolymorphicId) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) Value(org.apache.ignite.configuration.annotation.Value) TypeVariableName(com.squareup.javapoet.TypeVariableName) Nullable(org.jetbrains.annotations.Nullable) DeclaredType(javax.lang.model.type.DeclaredType) TypeSpec(com.squareup.javapoet.TypeSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Aggregations

ConfigValue (org.apache.ignite.configuration.annotation.ConfigValue)11 Value (org.apache.ignite.configuration.annotation.Value)11 NamedConfigValue (org.apache.ignite.configuration.annotation.NamedConfigValue)10 UUID (java.util.UUID)8 ConfigurationRoot (org.apache.ignite.configuration.annotation.ConfigurationRoot)8 TestConfigurationStorage (org.apache.ignite.internal.configuration.storage.TestConfigurationStorage)8 Matchers.nullValue (org.hamcrest.Matchers.nullValue)8 List (java.util.List)7 Map (java.util.Map)7 NoSuchElementException (java.util.NoSuchElementException)7 Config (org.apache.ignite.configuration.annotation.Config)7 LOCAL (org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL)7 InjectedName (org.apache.ignite.configuration.annotation.InjectedName)7 InternalId (org.apache.ignite.configuration.annotation.InternalId)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Matchers.is (org.hamcrest.Matchers.is)7 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)7 Test (org.junit.jupiter.api.Test)7 ConfigurationRegistry (org.apache.ignite.internal.configuration.ConfigurationRegistry)6 CompletableFuture (java.util.concurrent.CompletableFuture)5