aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
blob: dbd3c7ce018eb15c605990f90a0beb571bd7eeee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*-
 * ============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.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.onap.policy.common.parameters.annotations.Max;
import org.onap.policy.common.parameters.annotations.Min;
import org.onap.policy.common.parameters.annotations.NotBlank;
import org.onap.policy.common.parameters.annotations.NotNull;

/**
 * Bean validator, supporting the parameter annotations.
 * <p/>
 * Note: this currently does not support Min/Max validation of "short" or "byte"; these
 * annotations are simply ignored for these types.
 */
public class BeanValidator {

    /**
     * {@code True} if there is a field-level annotation, {@code false} otherwise.
     */
    private boolean fieldIsAnnotated;

    /**
     * Validates top level fields within an object. For each annotated field, it retrieves
     * the value using the public "getter" method for the field. If there is no public
     * "getter" method, then it throws an exception. Otherwise, it validates the retrieved
     * value based on the annotations. This recurses through super classes looking for
     * fields to be verified, but it does not examine any interfaces.
     *
     * @param name name of the object being validated
     * @param object object to be validated. If {@code null}, then an empty result is
     *        returned
     * @return the validation result
     */
    public BeanValidationResult validateTop(String name, Object object) {
        BeanValidationResult result = new BeanValidationResult(name, object);
        if (object == null) {
            return result;
        }

        // check class hierarchy - don't need to check interfaces
        for (Class<?> clazz = object.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
            validateFields(result, object, clazz);
        }

        return result;
    }

    /**
     * Performs validation of all annotated fields found within the class.
     *
     * @param result validation results are added here
     * @param object object whose fields are to be validated
     * @param clazz class, within the object's hierarchy, to be examined for fields to be
     *        verified
     */
    private void validateFields(BeanValidationResult result, Object object, Class<?> clazz) {
        for (Field field : clazz.getDeclaredFields()) {
            validateField(result, object, clazz, field);
        }
    }

    /**
     * Performs validation of a single field.
     *
     * @param result validation results are added here
     * @param object object whose fields are to be validated
     * @param clazz class, within the object's hierarchy, containing the field
     * @param field field whose value is to be validated
     */
    private void validateField(BeanValidationResult result, Object object, Class<?> clazz, Field field) {
        final String fieldName = field.getName();
        if (fieldName.contains("$")) {
            return;
        }

        /*
         * Identify the annotations. NotNull MUST be first so the check is run before the
         * others.
         */
        fieldIsAnnotated = false;
        List<Predicate<Object>> checkers = new ArrayList<>(10);
        addAnnotation(clazz, field, checkers, NotNull.class, (annot, value) -> verNotNull(result, fieldName, value));
        addAnnotation(clazz, field, checkers, NotBlank.class, (annot, value) -> verNotBlank(result, fieldName, value));
        addAnnotation(clazz, field, checkers, Max.class, (annot, value) -> verMax(result, fieldName, annot, value));
        addAnnotation(clazz, field, checkers, Min.class, (annot, value) -> verMin(result, fieldName, annot, value));

        if (checkers.isEmpty()) {
            // has no annotations - nothing to check
            return;
        }

        // verify the field type is of interest
        int mod = field.getModifiers();
        if (Modifier.isStatic(mod)) {
            classOnly(clazz.getName() + "." + fieldName + " is annotated but the field is static");
            return;
        }

        // get the field's "getter" method
        Method accessor = getAccessor(object.getClass(), fieldName);
        if (accessor == null) {
            classOnly(clazz.getName() + "." + fieldName + " is annotated but has no \"get\" method");
            return;
        }

        // get the value
        Object value = getValue(object, clazz, fieldName, accessor);

        // perform the checks
        if (value == null && field.getAnnotation(NotNull.class) == null && clazz.getAnnotation(NotNull.class) == null) {
            // value is null and there's no null check - just return
            return;
        }

        for (Predicate<Object> checker : checkers) {
            if (!checker.test(value)) {
                // invalid - don't bother with additional checks
                return;
            }
        }
    }

    /**
     * Gets the value from the object using the accessor function.
     *
     * @param object object whose value is to be retrieved
     * @param clazz class containing the field
     * @param fieldName name of the field
     * @param accessor "getter" method
     * @return the object's value
     */
    private Object getValue(Object object, Class<?> clazz, final String fieldName, Method accessor) {
        try {
            return accessor.invoke(object);

        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new IllegalArgumentException(clazz.getName() + "." + fieldName + " accessor threw an exception", e);
        }
    }

    /**
     * Throws an exception if there are field-level annotations.
     *
     * @param exceptionMessage exception message
     */
    private void classOnly(String exceptionMessage) {
        if (fieldIsAnnotated) {
            throw new IllegalArgumentException(exceptionMessage);
        }
    }

    /**
     * Looks for an annotation at the class or field level. If an annotation is found at
     * either the field or class level, then it adds a verifier to the list of checkers.
     *
     * @param clazz class to be searched
     * @param field field to be searched
     * @param checkers where to place the new field verifier
     * @param annotClass class of annotation to find
     * @param check verification function to be added to the list, if the annotation is
     *        found
     */
    private <T extends Annotation> void addAnnotation(Class<?> clazz, Field field, List<Predicate<Object>> checkers,
                    Class<T> annotClass, BiPredicate<T, Object> check) {

        // field annotation takes precedence over class annotation
        T annot = field.getAnnotation(annotClass);
        if (annot != null) {
            fieldIsAnnotated = true;

        } else if ((annot = clazz.getAnnotation(annotClass)) == null) {
            return;
        }

        T annot2 = annot;
        checkers.add(value -> check.test(annot2, value));
    }

    /**
     * Verifies that the value is not null.
     *
     * @param result where to add the validation result
     * @param fieldName field whose value is being verified
     * @param value value to be verified, assumed to be non-null
     * @return {@code true} if the value is valid, {@code false} otherwise
     */
    private boolean verNotNull(BeanValidationResult result, String fieldName, Object value) {
        return result.validateNotNull(fieldName, value);
    }

    /**
     * Verifies that the value is not blank.
     *
     * @param result where to add the validation result
     * @param fieldName field whose value is being verified
     * @param value value to be verified, assumed to be non-null
     * @return {@code true} if the value is valid, {@code false} otherwise
     */
    private boolean verNotBlank(BeanValidationResult result, String fieldName, Object value) {
        if (value instanceof String && StringUtils.isBlank(value.toString())) {
            ObjectValidationResult fieldResult =
                            new ObjectValidationResult(fieldName, value, ValidationStatus.INVALID, "is blank");
            result.addResult(fieldResult);
            return false;
        }

        return true;
    }

    /**
     * Verifies that the value is <= the minimum value.
     *
     * @param result where to add the validation result
     * @param fieldName field whose value is being verified
     * @param annot annotation against which the value is being verified
     * @param value value to be verified, assumed to be non-null
     * @return {@code true} if the value is valid, {@code false} otherwise
     */
    private boolean verMax(BeanValidationResult result, String fieldName, Max annot, Object value) {
        if (!(value instanceof Number)) {
            return true;
        }

        Number num = (Number) value;
        if (num instanceof Integer || num instanceof Long) {
            if (num.longValue() <= annot.value()) {
                return true;
            }

        } else if (num instanceof Float || num instanceof Double) {
            if (num.doubleValue() <= annot.value()) {
                return true;
            }

        } else {
            return true;
        }

        ObjectValidationResult fieldResult = new ObjectValidationResult(fieldName, value, ValidationStatus.INVALID,
                        "exceeds the maximum value: " + annot.value());
        result.addResult(fieldResult);
        return false;
    }

    /**
     * Verifies that the value is >= the minimum value.
     *
     * @param result where to add the validation result
     * @param fieldName field whose value is being verified
     * @param annot annotation against which the value is being verified
     * @param value value to be verified, assumed to be non-null
     * @return {@code true} if the value is valid, {@code false} otherwise
     */
    private boolean verMin(BeanValidationResult result, String fieldName, Min annot, Object value) {
        if (!(value instanceof Number)) {
            return true;
        }

        Number num = (Number) value;
        if (num instanceof Integer || num instanceof Long) {
            if (num.longValue() >= annot.value()) {
                return true;
            }

        } else if (num instanceof Float || num instanceof Double) {
            if (num.doubleValue() >= annot.value()) {
                return true;
            }

        } else {
            return true;
        }

        ObjectValidationResult fieldResult = new ObjectValidationResult(fieldName, value, ValidationStatus.INVALID,
                        "is below the minimum value: " + annot.value());
        result.addResult(fieldResult);
        return false;
    }

    /**
     * Gets an accessor method for the given field.
     *
     * @param clazz class whose methods are to be searched
     * @param fieldName field whose "getter" is to be identified
     * @return the field's "getter" method, or {@code null} if it is not found
     */
    private Method getAccessor(Class<?> clazz, String fieldName) {
        String capname = StringUtils.capitalize(fieldName);
        Method accessor = getMethod(clazz, "get" + capname);
        if (accessor != null) {
            return accessor;
        }

        return getMethod(clazz, "is" + capname);
    }

    /**
     * Gets the "getter" method having the specified name.
     *
     * @param clazz class whose methods are to be searched
     * @param methodName name of the method of interest
     * @return the method, or {@code null} if it is not found
     */
    private Method getMethod(Class<?> clazz, String methodName) {
        for (Method method : clazz.getMethods()) {
            if (methodName.equals(method.getName()) && validMethod(method)) {
                return method;
            }
        }

        return null;
    }

    /**
     * Determines if a method is a valid "getter".
     *
     * @param method method to be checked
     * @return {@code true} if the method is a valid "getter", {@code false} otherwise
     */
    private boolean validMethod(Method method) {
        int mod = method.getModifiers();
        return !(Modifier.isStatic(mod) || method.getReturnType() == void.class || method.getParameterCount() != 0);
    }
}