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