tkalkirill commented on code in PR #5039: URL: https://github.com/apache/ignite-3/pull/5039#discussion_r1916469485
########## modules/configuration-annotation-processor/src/main/java/org/apache/ignite/internal/configuration/processor/validation/Validator.java: ########## @@ -0,0 +1,160 @@ +/* + * 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.validation; + +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessor.TOP_LEVEL_ANNOTATIONS; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.containsAnyAnnotation; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.isValidValueAnnotationFieldType; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.joinSimpleName; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.sameType; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.simpleName; + +import java.lang.annotation.Annotation; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Name; +import javax.lang.model.element.VariableElement; +import org.apache.ignite.internal.configuration.processor.ClassWrapper; +import org.apache.ignite.internal.configuration.processor.ConfigurationValidationException; + +/** + * Base class for configuration validators. + */ +public abstract class Validator { Review Comment: Maybe it would be better to make an interface out of it? ########## modules/configuration-annotation-processor/src/main/java/org/apache/ignite/internal/configuration/processor/ConfigurationProcessor.java: ########## @@ -163,14 +163,14 @@ private boolean process0(RoundEnvironment roundEnvironment) { return false; } - var injectedValueValidator = new InjectedValueValidator(processingEnv); + List<Validator> validators = validators(); for (TypeElement clazz : annotatedConfigs) { var classWrapper = new ClassWrapper(processingEnv, clazz); - validateConfigurationSchemaClass(classWrapper); + validators.forEach(validator -> validator.validate(classWrapper)); Review Comment: It would be great if it wasn’t linear here, but depending on the class annotations, I’ll leave it to your conscience and honesty. ########## modules/configuration-annotation-processor/src/main/java/org/apache/ignite/internal/configuration/processor/validation/Validator.java: ########## @@ -0,0 +1,160 @@ +/* + * 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.validation; + +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessor.TOP_LEVEL_ANNOTATIONS; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.containsAnyAnnotation; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.isValidValueAnnotationFieldType; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.joinSimpleName; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.sameType; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.simpleName; + +import java.lang.annotation.Annotation; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Name; +import javax.lang.model.element.VariableElement; +import org.apache.ignite.internal.configuration.processor.ClassWrapper; +import org.apache.ignite.internal.configuration.processor.ConfigurationValidationException; + +/** + * Base class for configuration validators. + */ +public abstract class Validator { + final ProcessingEnvironment processingEnvironment; + + Validator(ProcessingEnvironment processingEnvironment) { + this.processingEnvironment = processingEnvironment; + } + + public abstract void validate(ClassWrapper classWrapper) throws ConfigurationValidationException; + + @SafeVarargs + static void assertNotContainsFieldAnnotatedWith(ClassWrapper classWrapper, Class<? extends Annotation>... annotations) { + if (containsAnyAnnotation(classWrapper.fields(), annotations)) { + throw new ConfigurationValidationException(classWrapper, String.format( + "Must not have fields annotated with %s.", joinSimpleName(" or ", annotations) + )); + } + } + + @SafeVarargs + static void assertSuperClassNotContainsFieldAnnotatedWith(ClassWrapper classWrapper, Class<? extends Annotation>... annotations) { + if (containsAnyAnnotation(classWrapper.requiredSuperClass().fields(), annotations)) { + throw new ConfigurationValidationException(classWrapper, String.format( + "Superclass %s must not have fields annotated with %s.", + classWrapper.requiredSuperClass(), joinSimpleName(" or ", annotations) + )); + } + } + + static void assertHasNoSuperClass(ClassWrapper classWrapper) { Review Comment: As far as I remember, we have a similar test in `ClassWrapper`, is it needed here? ########## modules/configuration-annotation-processor/src/main/java/org/apache/ignite/internal/configuration/processor/validation/AbstractConfigurationValidator.java: ########## @@ -0,0 +1,45 @@ +/* + * 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.validation; + +import javax.annotation.processing.ProcessingEnvironment; +import org.apache.ignite.configuration.annotation.AbstractConfiguration; +import org.apache.ignite.configuration.annotation.PolymorphicId; +import org.apache.ignite.internal.configuration.processor.ClassWrapper; + +/** + * Validator for the {@link AbstractConfiguration} annotation. + */ +public class AbstractConfigurationValidator extends Validator { + public AbstractConfigurationValidator(ProcessingEnvironment processingEnvironment) { + super(processingEnvironment); + } + + @Override + public void validate(ClassWrapper classWrapper) { + if (classWrapper.getAnnotation(AbstractConfiguration.class) == null) { Review Comment: Maybe throw an exception if the validator doesn't match? Why does skipping classes look better? ########## modules/configuration-annotation-processor/src/main/java/org/apache/ignite/internal/configuration/processor/validation/ConfigValueValidator.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.validation; + +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.containsAnyAnnotation; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.joinSimpleName; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.simpleName; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; +import org.apache.ignite.configuration.annotation.Config; +import org.apache.ignite.configuration.annotation.ConfigValue; +import org.apache.ignite.configuration.annotation.InjectedName; +import org.apache.ignite.configuration.annotation.Name; +import org.apache.ignite.configuration.annotation.PolymorphicConfig; +import org.apache.ignite.internal.configuration.processor.ClassWrapper; +import org.apache.ignite.internal.configuration.processor.ConfigurationValidationException; + +/** + * Validator for the {@link ConfigValue} annotation. + */ +public class ConfigValueValidator extends Validator { + public ConfigValueValidator(ProcessingEnvironment processingEnvironment) { + super(processingEnvironment); + } + + @Override + public void validate(ClassWrapper classWrapper) { Review Comment: Do you think we should check here that if there is no annotation then we should skip validation? ########## modules/configuration-annotation-processor/src/main/java/org/apache/ignite/internal/configuration/processor/validation/Validator.java: ########## @@ -0,0 +1,160 @@ +/* + * 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.validation; + +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessor.TOP_LEVEL_ANNOTATIONS; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.containsAnyAnnotation; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.isValidValueAnnotationFieldType; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.joinSimpleName; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.sameType; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.simpleName; + +import java.lang.annotation.Annotation; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Name; +import javax.lang.model.element.VariableElement; +import org.apache.ignite.internal.configuration.processor.ClassWrapper; +import org.apache.ignite.internal.configuration.processor.ConfigurationValidationException; + +/** + * Base class for configuration validators. + */ +public abstract class Validator { + final ProcessingEnvironment processingEnvironment; + + Validator(ProcessingEnvironment processingEnvironment) { + this.processingEnvironment = processingEnvironment; + } + + public abstract void validate(ClassWrapper classWrapper) throws ConfigurationValidationException; + + @SafeVarargs + static void assertNotContainsFieldAnnotatedWith(ClassWrapper classWrapper, Class<? extends Annotation>... annotations) { + if (containsAnyAnnotation(classWrapper.fields(), annotations)) { + throw new ConfigurationValidationException(classWrapper, String.format( + "Must not have fields annotated with %s.", joinSimpleName(" or ", annotations) + )); + } + } + + @SafeVarargs + static void assertSuperClassNotContainsFieldAnnotatedWith(ClassWrapper classWrapper, Class<? extends Annotation>... annotations) { + if (containsAnyAnnotation(classWrapper.requiredSuperClass().fields(), annotations)) { + throw new ConfigurationValidationException(classWrapper, String.format( + "Superclass %s must not have fields annotated with %s.", + classWrapper.requiredSuperClass(), joinSimpleName(" or ", annotations) + )); + } + } + + static void assertHasNoSuperClass(ClassWrapper classWrapper) { + if (classWrapper.superClass() != null) { + throw new ConfigurationValidationException(classWrapper, "Must not have a superclass."); + } + } + + @SafeVarargs + static void assertSuperclassHasAnnotations(ClassWrapper classWrapper, Class<? extends Annotation>... annotations) { + if (!containsAnyAnnotation(classWrapper.requiredSuperClass().clazz(), annotations)) { + throw new ConfigurationValidationException( + classWrapper, + String.format("Superclass must be annotated with %s.", joinSimpleName(" or ", annotations)) + ); + } + } + + static void assertNoFieldNameConflictsWithSuperClass(ClassWrapper classWrapper) { + Collection<Name> duplicateFieldNames = findDuplicates(classWrapper.fields(), classWrapper.requiredSuperClass().fields()); + + if (!duplicateFieldNames.isEmpty()) { + throw new ConfigurationValidationException(classWrapper, String.format( + "Duplicate field names detected with the superclass %s: %s.", + classWrapper.requiredSuperClass(), duplicateFieldNames + )); + } + } + + private static Collection<Name> findDuplicates(Collection<VariableElement> fields1, Collection<VariableElement> fields2) { + if (fields1.isEmpty() || fields2.isEmpty()) { + return List.of(); + } + + Set<Name> fieldNames1 = fields1.stream() + .map(VariableElement::getSimpleName) + .collect(toSet()); + + return fields2.stream() + .map(VariableElement::getSimpleName) + .filter(fieldNames1::contains) + .collect(toList()); + } + + void assertFieldType(ClassWrapper classWrapper, VariableElement field, Class<?> expectedType) { + if (!sameType(processingEnvironment, field.asType(), expectedType)) { + throw new ConfigurationValidationException(classWrapper, String.format( + "Field '%s' must be a %s.", + field.getSimpleName(), + expectedType.getSimpleName() + )); + } + } + + void assertValidValueFieldType(ClassWrapper classWrapper, VariableElement field) { + if (!isValidValueAnnotationFieldType(processingEnvironment, field.asType())) { + throw new ConfigurationValidationException(classWrapper, String.format( + "Field '%s' must have one of the following types: " + + "boolean, int, long, double, String, UUID or an array of aforementioned types.", + field.getSimpleName() + )); + } + } + + @SafeVarargs + static void assertHasCompatibleTopLevelAnnotation( + ClassWrapper classWrapper, + Class<? extends Annotation> currentAnnotation, + Class<? extends Annotation>... compatibleAnnotations + ) { + var allCompatibleAnnotations = new HashSet<Class<? extends Annotation>>(); + + allCompatibleAnnotations.add(currentAnnotation); + allCompatibleAnnotations.addAll(Arrays.asList(compatibleAnnotations)); + + assert TOP_LEVEL_ANNOTATIONS.containsAll(allCompatibleAnnotations); Review Comment: Let's output collections in the error message. ########## modules/configuration-annotation-processor/src/main/java/org/apache/ignite/internal/configuration/processor/validation/NamedConfigValueValidator.java: ########## @@ -0,0 +1,59 @@ +/* + * 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.validation; + +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.containsAnyAnnotation; +import static org.apache.ignite.internal.configuration.processor.ConfigurationProcessorUtils.joinSimpleName; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; +import org.apache.ignite.configuration.annotation.Config; +import org.apache.ignite.configuration.annotation.NamedConfigValue; +import org.apache.ignite.configuration.annotation.PolymorphicConfig; +import org.apache.ignite.internal.configuration.processor.ClassWrapper; +import org.apache.ignite.internal.configuration.processor.ConfigurationValidationException; + +/** + * Validator for the {@link NamedConfigValue} annotation. + */ +public class NamedConfigValueValidator extends Validator { + public NamedConfigValueValidator(ProcessingEnvironment processingEnvironment) { + super(processingEnvironment); + } + + @Override + public void validate(ClassWrapper classWrapper) { Review Comment: Do you think we should check here that if there is no annotation then we should skip validation? -- 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]
