sashapolo commented on code in PR #4997: URL: https://github.com/apache/ignite-3/pull/4997#discussion_r1910156534
########## modules/configuration-annotation-processor/src/main/java/org/apache/ignite/internal/configuration/processor/validators/InjectedValueValidator.java: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.configuration.processor.validators; + +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.collectFieldsWithAnnotation; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.simpleName; +import static org.apache.ignite.internal.util.CollectionUtils.concat; + +import java.util.List; +import java.util.UUID; +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; +import javax.lang.model.type.ArrayType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import org.apache.ignite.configuration.annotation.InjectedValue; +import org.apache.ignite.configuration.annotation.Value; +import org.apache.ignite.internal.configuration.processor.ConfigurationProcessorException; + +/** + * Validator class for the {@link InjectedValue} annotation. + */ +public class InjectedValueValidator { + private final ProcessingEnvironment processingEnv; + + public InjectedValueValidator(ProcessingEnvironment processingEnv) { + this.processingEnv = processingEnv; + } + + /** + * Validates invariants of the {@link InjectedValue} annotation. + */ + public void validate(TypeElement clazz, List<VariableElement> fields) { + List<VariableElement> injectedValueFields = collectFieldsWithAnnotation(fields, InjectedValue.class); + + if (injectedValueFields.isEmpty()) { + return; + } + + List<VariableElement> valueFields = collectFieldsWithAnnotation(fields, Value.class); + + if (injectedValueFields.size() > 1 || !valueFields.isEmpty()) { + throw new ConfigurationProcessorException(String.format( + "Field marked as %s must be the only \"value\" field in the schema, found: %s", + simpleName(InjectedValue.class), + concat(injectedValueFields, valueFields) + )); + } + + VariableElement injectedValueField = injectedValueFields.get(0); + + // Must be a primitive or an array of the primitives (including java.lang.String, java.util.UUID). + if (!isValidValueAnnotationFieldType(injectedValueField.asType())) { + throw new ConfigurationProcessorException(String.format( + "%s %s.%s field must have one of the following types: " + + "boolean, int, long, double, String, UUID or an array of aforementioned type.", + simpleName(InjectedValue.class), + clazz.getQualifiedName(), + injectedValueField.getSimpleName() + )); + } + } + + private boolean isValidValueAnnotationFieldType(TypeMirror type) { Review Comment: Note to reviewer: this copy-paste will be refactored in a next PR -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
