Search in sources :

Example 1 with PolymorphicId

use of org.apache.ignite.configuration.annotation.PolymorphicId in project ignite-3 by apache.

the class Processor method getInterfaceGetMethodType.

/**
 * Get types for configuration classes generation.
 *
 * @param field Field.
 * @return Bundle with all types for configuration
 */
private static TypeName getInterfaceGetMethodType(VariableElement field) {
    TypeName interfaceGetMethodType = null;
    TypeName baseType = TypeName.get(field.asType());
    ConfigValue confAnnotation = field.getAnnotation(ConfigValue.class);
    if (confAnnotation != null) {
        interfaceGetMethodType = Utils.getConfigurationInterfaceName((ClassName) baseType);
    }
    NamedConfigValue namedConfigAnnotation = field.getAnnotation(NamedConfigValue.class);
    if (namedConfigAnnotation != null) {
        ClassName interfaceGetType = Utils.getConfigurationInterfaceName((ClassName) baseType);
        TypeName viewClassType = Utils.getViewName((ClassName) baseType);
        TypeName changeClassType = Utils.getChangeName((ClassName) baseType);
        interfaceGetMethodType = ParameterizedTypeName.get(ClassName.get(NamedConfigurationTree.class), interfaceGetType, viewClassType, changeClassType);
    }
    Value valueAnnotation = field.getAnnotation(Value.class);
    PolymorphicId polymorphicIdAnnotation = field.getAnnotation(PolymorphicId.class);
    InjectedName injectedNameAnnotation = field.getAnnotation(InjectedName.class);
    InternalId internalIdAnnotation = field.getAnnotation(InternalId.class);
    if (valueAnnotation != null || polymorphicIdAnnotation != null || injectedNameAnnotation != null || internalIdAnnotation != null) {
        // It is necessary to use class names without loading classes so that we won't
        // accidentally get NoClassDefFoundError
        ClassName confValueClass = ClassName.get("org.apache.ignite.configuration", "ConfigurationValue");
        TypeName genericType = baseType;
        if (genericType.isPrimitive()) {
            genericType = genericType.box();
        }
        interfaceGetMethodType = ParameterizedTypeName.get(confValueClass, genericType);
    }
    return interfaceGetMethodType;
}
Also used : NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) PolymorphicId(org.apache.ignite.configuration.annotation.PolymorphicId) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) InternalId(org.apache.ignite.configuration.annotation.InternalId) InjectedName(org.apache.ignite.configuration.annotation.InjectedName) 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)

Example 2 with PolymorphicId

use of org.apache.ignite.configuration.annotation.PolymorphicId in project ignite-3 by apache.

the class Processor method process0.

/**
 * Processes a set of annotation types on type elements.
 *
 * @param roundEnvironment Processing environment.
 * @return Whether the set of annotation types are claimed by this processor.
 */
private boolean process0(RoundEnvironment roundEnvironment) {
    Elements elementUtils = processingEnv.getElementUtils();
    // All classes annotated with {@link #supportedAnnotationTypes}.
    List<TypeElement> annotatedConfigs = roundEnvironment.getElementsAnnotatedWithAny(supportedAnnotationTypes()).stream().filter(element -> element.getKind() == ElementKind.CLASS).map(TypeElement.class::cast).collect(toList());
    if (annotatedConfigs.isEmpty()) {
        return false;
    }
    for (TypeElement clazz : annotatedConfigs) {
        // Find all the fields of the schema.
        List<VariableElement> fields = fields(clazz);
        validate(clazz, fields);
        // Get package name of the schema class
        String packageName = elementUtils.getPackageOf(clazz).getQualifiedName().toString();
        ClassName schemaClassName = ClassName.get(packageName, clazz.getSimpleName().toString());
        // Get name for generated configuration interface.
        ClassName configInterface = Utils.getConfigurationInterfaceName(schemaClassName);
        TypeSpec.Builder configurationInterfaceBuilder = TypeSpec.interfaceBuilder(configInterface).addModifiers(PUBLIC);
        for (VariableElement field : fields) {
            if (!field.getModifiers().contains(PUBLIC)) {
                throw new ProcessorException("Field " + clazz.getQualifiedName() + "." + field + " must be public");
            }
            final String fieldName = field.getSimpleName().toString();
            // Get configuration types (VIEW, CHANGE and so on)
            final TypeName interfaceGetMethodType = getInterfaceGetMethodType(field);
            if (field.getAnnotation(ConfigValue.class) != null) {
                checkConfigField(field, ConfigValue.class);
                checkMissingNameForInjectedName(field);
            }
            if (field.getAnnotation(NamedConfigValue.class) != null) {
                checkConfigField(field, NamedConfigValue.class);
            }
            Value valueAnnotation = field.getAnnotation(Value.class);
            if (valueAnnotation != null) {
                // Must be a primitive or an array of the primitives (including java.lang.String)
                if (!isPrimitiveOrArray(field.asType())) {
                    throw new ProcessorException("@Value " + clazz.getQualifiedName() + "." + field.getSimpleName() + " field must" + " have one of the following types: boolean, int, long, double, String or an array of " + "aforementioned type.");
                }
            }
            PolymorphicId polymorphicId = field.getAnnotation(PolymorphicId.class);
            if (polymorphicId != null) {
                if (!isClass(field.asType(), String.class)) {
                    throw new ProcessorException(String.format(FIELD_MUST_BE_SPECIFIC_CLASS_ERROR_FORMAT, simpleName(PolymorphicId.class), clazz.getQualifiedName(), field.getSimpleName(), String.class.getSimpleName()));
                }
            }
            if (field.getAnnotation(InternalId.class) != null) {
                if (!isClass(field.asType(), UUID.class)) {
                    throw new ProcessorException(String.format(FIELD_MUST_BE_SPECIFIC_CLASS_ERROR_FORMAT, simpleName(InternalId.class), clazz.getQualifiedName(), field.getSimpleName(), UUID.class.getSimpleName()));
                }
            }
            createGetters(configurationInterfaceBuilder, fieldName, interfaceGetMethodType);
        }
        // Is root of the configuration.
        boolean isRootConfig = clazz.getAnnotation(ConfigurationRoot.class) != null;
        // Is the internal configuration.
        boolean isInternalConfig = clazz.getAnnotation(InternalConfiguration.class) != null;
        // Is a polymorphic configuration.
        boolean isPolymorphicConfig = clazz.getAnnotation(PolymorphicConfig.class) != null;
        // Is an instance of a polymorphic configuration.
        boolean isPolymorphicInstance = clazz.getAnnotation(PolymorphicConfigInstance.class) != null;
        // Create VIEW and CHANGE classes.
        createPojoBindings(fields, schemaClassName, configurationInterfaceBuilder, (isInternalConfig && !isRootConfig) || isPolymorphicInstance, clazz, isPolymorphicConfig, isPolymorphicInstance);
        if (isRootConfig) {
            createRootKeyField(configInterface, configurationInterfaceBuilder, schemaClassName, clazz);
        }
        // Write configuration interface
        buildClass(packageName, configurationInterfaceBuilder.build());
    }
    return true;
}
Also used : PolymorphicConfig(org.apache.ignite.configuration.annotation.PolymorphicConfig) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) InternalId(org.apache.ignite.configuration.annotation.InternalId) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) PolymorphicId(org.apache.ignite.configuration.annotation.PolymorphicId) PolymorphicConfigInstance(org.apache.ignite.configuration.annotation.PolymorphicConfigInstance) ConfigurationRoot(org.apache.ignite.configuration.annotation.ConfigurationRoot) 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) InternalConfiguration(org.apache.ignite.configuration.annotation.InternalConfiguration) UUID(java.util.UUID) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ClassName (com.squareup.javapoet.ClassName)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeName (com.squareup.javapoet.TypeName)2 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)2 ConfigValue (org.apache.ignite.configuration.annotation.ConfigValue)2 InternalId (org.apache.ignite.configuration.annotation.InternalId)2 NamedConfigValue (org.apache.ignite.configuration.annotation.NamedConfigValue)2 PolymorphicId (org.apache.ignite.configuration.annotation.PolymorphicId)2 Value (org.apache.ignite.configuration.annotation.Value)2 TypeSpec (com.squareup.javapoet.TypeSpec)1 UUID (java.util.UUID)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 Elements (javax.lang.model.util.Elements)1 ConfigurationRoot (org.apache.ignite.configuration.annotation.ConfigurationRoot)1 InjectedName (org.apache.ignite.configuration.annotation.InjectedName)1 InternalConfiguration (org.apache.ignite.configuration.annotation.InternalConfiguration)1 PolymorphicConfig (org.apache.ignite.configuration.annotation.PolymorphicConfig)1 PolymorphicConfigInstance (org.apache.ignite.configuration.annotation.PolymorphicConfigInstance)1