From 4fd3271364741062cfc3b84b79425e93e3e3d864 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 7 Jan 2021 10:03:30 -0500 Subject: Deprecate old validation annotations Removed @Items and @Entries validation annotations, as they are no longer needed. Issue-ID: POLICY-2648 Change-Id: I68b78738d520ad96175567572e3c2f4a845dae44 Signed-off-by: Jim Hahn --- .../policy/common/parameters/BeanValidator.java | 100 --------------------- .../policy/common/parameters/EntryValidator.java | 84 ----------------- .../policy/common/parameters/FieldValidator.java | 6 +- .../policy/common/parameters/Item2Validator.java | 71 --------------- .../policy/common/parameters/ItemValidator.java | 65 +++----------- .../common/parameters/annotations/Entries.java | 45 ---------- .../common/parameters/annotations/Items.java | 66 -------------- 7 files changed, 14 insertions(+), 423 deletions(-) delete mode 100644 common-parameters/src/main/java/org/onap/policy/common/parameters/EntryValidator.java delete mode 100644 common-parameters/src/main/java/org/onap/policy/common/parameters/Item2Validator.java delete mode 100644 common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Entries.java delete mode 100644 common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Items.java (limited to 'common-parameters/src/main') diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java index 6791c616..68455ac3 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java @@ -20,14 +20,11 @@ package org.onap.policy.common.parameters; -import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Collection; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang3.StringUtils; -import org.onap.policy.common.parameters.annotations.Entries; -import org.onap.policy.common.parameters.annotations.Items; import org.onap.policy.common.parameters.annotations.Max; import org.onap.policy.common.parameters.annotations.Min; import org.onap.policy.common.parameters.annotations.NotBlank; @@ -81,8 +78,6 @@ public class BeanValidator { validator.addAnnotation(Min.class, this::verMin); validator.addAnnotation(Pattern.class, this::verRegex); validator.addAnnotation(Valid.class, this::verCascade); - validator.addAnnotation(Items.class, this::verCollection); - validator.addAnnotation(Entries.class, this::verMap); } /** @@ -272,29 +267,6 @@ public class BeanValidator { return result2.isValid(); } - /** - * Validates the items in a collection. - * - * @param result where to add the validation result - * @param fieldName name of the field containing the collection - * @param annot validation annotations for individual items - * @param value value to be verified - * @return {@code true} if the next check should be performed, {@code false} otherwise - */ - public boolean verCollection(BeanValidationResult result, String fieldName, Annotation annot, Object value) { - - if (!(value instanceof Collection)) { - return true; - } - - ItemValidator itemValidator = makeItemValidator(annot); - if (itemValidator.isEmpty()) { - return true; - } - - return verCollection(result, fieldName, itemValidator, value); - } - /** * Validates the items in a collection. * @@ -327,57 +299,6 @@ public class BeanValidator { return false; } - /** - * Validates the items in a Map. - * - * @param result where to add the validation result - * @param fieldName name of the field containing the map - * @param annot validation annotations for individual entries - * @param value value to be verified - * @return {@code true} if the next check should be performed, {@code false} otherwise - */ - public boolean verMap(BeanValidationResult result, String fieldName, Entries annot, Object value) { - - if (!(value instanceof Map)) { - return true; - } - - EntryValidator entryValidator = makeEntryValidator(annot.key(), annot.value()); - - return verMap(result, fieldName, entryValidator, value); - } - - /** - * Validates the items in a Map. - * - * @param result where to add the validation result - * @param fieldName name of the field containing the map - * @param entryValidator validator for individual entries within the Map - * @param value value to be verified - * @return {@code true} if the next check should be performed, {@code false} otherwise - */ - public boolean verMap(BeanValidationResult result, String fieldName, EntryValidator entryValidator, Object value) { - - if (!(value instanceof Map) || entryValidator.isEmpty()) { - return true; - } - - Map map = (Map) value; - - BeanValidationResult result2 = new BeanValidationResult(fieldName, value); - - for (Entry entry : map.entrySet()) { - entryValidator.validateEntry(result2, entry); - } - - if (result2.isClean()) { - return true; - } - - result.addResult(result2); - return false; - } - /** * Validates the items in a Map. * @@ -445,27 +366,6 @@ public class BeanValidator { return new FieldValidator(this, clazz, field); } - /** - * Makes an item validator. - * - * @param annot container for the item annotations - * @return a new item validator - */ - protected ItemValidator makeItemValidator(Annotation annot) { - return new ItemValidator(this, annot); - } - - /** - * Makes an entry validator. - * - * @param keyAnnot container for the annotations associated with the entry key - * @param valueAnnot container for the annotations associated with the entry value - * @return a new entry validator - */ - protected EntryValidator makeEntryValidator(Annotation keyAnnot, Annotation valueAnnot) { - return new EntryValidator(this, keyAnnot, valueAnnot); - } - /** * Translates a value to something printable, for use by * {@link ObjectValidationResult}. This default method simply returns the original diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/EntryValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/EntryValidator.java deleted file mode 100644 index 965c95e5..00000000 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/EntryValidator.java +++ /dev/null @@ -1,84 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.parameters; - -import java.lang.annotation.Annotation; -import java.util.Map; - -/** - * Validator of an entry within a Map. - */ -public class EntryValidator { - private final ItemValidator keyValidator; - private final ItemValidator valueValidator; - - /** - * Constructs the object. - * - * @param validator provider of validation methods - * @param keyAnnotationContainer an annotation containing validation annotations to be - * applied to the entry key - * @param valueAnnotationContainer an annotation containing validation annotations to - * be applied to the entry value - */ - public EntryValidator(BeanValidator validator, Annotation keyAnnotationContainer, - Annotation valueAnnotationContainer) { - keyValidator = new ItemValidator(validator, keyAnnotationContainer); - valueValidator = new ItemValidator(validator, valueAnnotationContainer); - } - - public boolean isEmpty() { - return (keyValidator.isEmpty() && valueValidator.isEmpty()); - } - - /** - * Performs validation of a single entry. - * - * @param result validation results are added here - * @param entry value to be validated - */ - public void validateEntry(BeanValidationResult result, Map.Entry entry) { - String name = getName(entry); - - BeanValidationResult result2 = new BeanValidationResult(name, entry); - keyValidator.validateValue(result2, "key", entry.getKey()); - valueValidator.validateValue(result2, "value", entry.getValue()); - - if (!result2.isClean()) { - result.addResult(result2); - } - } - - /** - * Gets a name for the entry. - * - * @param entry entry whose name is to be determined - * @return a name for the entry - */ - protected String getName(Map.Entry entry) { - K key = entry.getKey(); - if (key == null) { - return ""; - } - - return key.toString(); - } -} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java index 58f3e833..efe48bb3 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java @@ -131,7 +131,7 @@ public class FieldValidator extends ValueValidator { return; } - Item2Validator itemValidator = new Item2Validator(validator, targs[0]); + ItemValidator itemValidator = new ItemValidator(validator, targs[0]); if (itemValidator.isEmpty()) { return; } @@ -159,8 +159,8 @@ public class FieldValidator extends ValueValidator { return; } - Item2Validator keyValidator = new Item2Validator(validator, targs[0]); - Item2Validator valueValidator = new Item2Validator(validator, targs[1]); + ItemValidator keyValidator = new ItemValidator(validator, targs[0]); + ItemValidator valueValidator = new ItemValidator(validator, targs[1]); if (keyValidator.isEmpty() && valueValidator.isEmpty()) { return; } diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/Item2Validator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/Item2Validator.java deleted file mode 100644 index c82244d6..00000000 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/Item2Validator.java +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.parameters; - -import java.lang.annotation.Annotation; -import java.lang.reflect.AnnotatedType; - -/** - * Validator of an "item", which is typically found in a collection, or the key or value - * components of an entry in a Map. - */ -public class Item2Validator extends ValueValidator { - private final AnnotatedType annotatedType; - - /** - * Constructs the object. - * - * @param validator provider of validation methods - * @param annotatedType a type having validation annotations to be - * applied to the item - */ - public Item2Validator(BeanValidator validator, AnnotatedType annotatedType) { - this(validator, annotatedType, true); - } - - /** - * Constructs the object. - * - * @param validator provider of validation methods - * @param annotatedType a type having validation annotations to be - * applied to the item - * @param addValidators {@code true} if to add validators - */ - public Item2Validator(BeanValidator validator, AnnotatedType annotatedType, boolean addValidators) { - this.annotatedType = annotatedType; - - if (addValidators) { - validator.addValidators(this); - } - } - - /** - * Gets an annotation from the field or the class. - * - * @param annotClass annotation class of interest - * @return the annotation, or {@code null} if the {@link #annotatedType} does - * not contain the desired annotation - */ - @Override - public T getAnnotation(Class annotClass) { - return annotatedType.getAnnotation(annotClass); - } -} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java index 07efebbe..44b70cdf 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,37 +21,36 @@ package org.onap.policy.common.parameters; import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; +import java.lang.reflect.AnnotatedType; /** * Validator of an "item", which is typically found in a collection, or the key or value * components of an entry in a Map. */ public class ItemValidator extends ValueValidator { - private final Annotation annotationContainer; + private final AnnotatedType annotatedType; /** * Constructs the object. * * @param validator provider of validation methods - * @param annotationContainer an annotation containing validation annotations to be + * @param annotatedType a type having validation annotations to be * applied to the item */ - public ItemValidator(BeanValidator validator, Annotation annotationContainer) { - this(validator, annotationContainer, true); + public ItemValidator(BeanValidator validator, AnnotatedType annotatedType) { + this(validator, annotatedType, true); } /** * Constructs the object. * * @param validator provider of validation methods - * @param annotationContainer an annotation containing validation annotations to be + * @param annotatedType a type having validation annotations to be * applied to the item * @param addValidators {@code true} if to add validators */ - public ItemValidator(BeanValidator validator, Annotation annotationContainer, boolean addValidators) { - this.annotationContainer = annotationContainer; + public ItemValidator(BeanValidator validator, AnnotatedType annotatedType, boolean addValidators) { + this.annotatedType = annotatedType; if (addValidators) { validator.addValidators(this); @@ -62,53 +61,11 @@ public class ItemValidator extends ValueValidator { * Gets an annotation from the field or the class. * * @param annotClass annotation class of interest - * @return the annotation, or {@code null} if the {@link #annotationContainer} does + * @return the annotation, or {@code null} if the {@link #annotatedType} does * not contain the desired annotation */ @Override public T getAnnotation(Class annotClass) { - try { - for (Method meth : annotationContainer.getClass().getDeclaredMethods()) { - T annot = getAnnotation2(annotClass, meth); - if (annot != null) { - return annot; - } - } - } catch (RuntimeException | IllegalAccessException | InvocationTargetException e) { - throw new IllegalArgumentException("cannot determine " + annotClass.getName(), e); - } - - return null; - } - - /** - * Note: this is only marked "protected" so it can be overridden for junit testing. - */ - protected T getAnnotation2(Class annotClass, Method method) - throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { - - Class ret = method.getReturnType(); - if (!ret.isArray()) { - return null; - } - - Class comp = ret.getComponentType(); - if (comp != annotClass) { - return null; - } - - // get the array for this type of annotation - @SuppressWarnings("unchecked") - T[] arrobj = (T[]) method.invoke(annotationContainer); - - if (arrobj.length == 0) { - return null; - } - - if (arrobj.length > 1) { - throw new IllegalArgumentException("extra item annotations of type: " + annotClass.getName()); - } - - return arrobj[0]; + return annotatedType.getAnnotation(annotClass); } } diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Entries.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Entries.java deleted file mode 100644 index 89c9ce26..00000000 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Entries.java +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.parameters.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -/** - * Validations on entries within a Map. - */ -@Retention(RUNTIME) -@Target(FIELD) -public @interface Entries { - - /** - * Validations to perform on each entry's key. - */ - Items key(); - - /** - * Validations to perform on each entry's value. - */ - Items value(); -} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Items.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Items.java deleted file mode 100644 index d022d95e..00000000 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Items.java +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.parameters.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -/** - * Validations on individual items, typically within a collection. - */ -@Retention(RUNTIME) -@Target(FIELD) -public @interface Items { - - /** - * Validates the item is not {@code null}. - */ - NotNull[] notNull() default {}; - - /** - * Validates the item is not blank. - */ - NotBlank[] notBlank() default {}; - - /** - * Validates the item matches a regular expression. - */ - Pattern[] pattern() default {}; - - /** - * Validates the item is not greater than a certain value. - */ - Max[] max() default {}; - - /** - * Validates the item is not less than a certain value. - */ - Min[] min() default {}; - - /** - * Validates the item is valid, using a {@link BeanValidator}. - */ - Valid[] valid() default {}; - -} -- cgit 1.2.3-korg