6ac5c48e188d042a2d1f58041f56a3a4b1895a2b
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.editor.rest.handling.bean;
22
23 import java.lang.reflect.Field;
24 import java.lang.reflect.Method;
25
26 /**
27  * The base class for Beans.
28  */
29 public abstract class BeanBase {
30     // Recurring string constants
31     private static final String PROBLEM_RETRIEVING_FIELD_PREFIX = "Problem retrieving field called ('";
32     private static final String JSON_BEAN_SUFFIX = "') from JSON bean ";
33
34     // Magic numbers
35     private static final int GET_LENGTH = 3;
36
37     /**
38      * Gets a named field from the bean.
39      *
40      * @param field the field name
41      * @return the value for the field
42      */
43     public String get(final String field) {
44         // use getter preferably
45         for (final Method method : this.getClass().getMethods()) {
46             if (method.getName().startsWith("get") && method.getName().length() == (field.length() + GET_LENGTH)
47                             && method.getName().toLowerCase().endsWith(field.toLowerCase())) {
48                 return invokeGetterMethod(field, method);
49             }
50         }
51
52         // Use field approach
53         if (field != null) {
54             try {
55                 final Field f = this.getClass().getDeclaredField(field);
56                 if (f != null) {
57                     f.setAccessible(true);
58                     return (String) (f.get(this));
59                 }
60             } catch (final Exception e) {
61                 throw new IllegalArgumentException(PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this,
62                                 e);
63             }
64         }
65         throw new IllegalArgumentException(PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this);
66     }
67
68     /**
69      * Invoke a getter method on a bean.
70      * 
71      * @param field the field that the getter gets a value for
72      * @param method the method to invoke
73      */
74     private String invokeGetterMethod(final String field, final Method method) {
75         try {
76             return (String) method.invoke(this);
77         } catch (final Exception e) {
78             throw new IllegalArgumentException(PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this, e);
79         }
80     }
81 }