diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-09-07 13:36:41 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-09-07 13:52:35 +0100 |
commit | cf282dd15ce391eed832063aea956a8d55521278 (patch) | |
tree | 698c059484fe7215e6842090f1aff4528b947cb6 /common-parameters/src/main | |
parent | 5b0c195d48031684f44289d05a75d22280ff0508 (diff) |
Remove changing of access on par fields
Parameter handling refactored to remove changing of
access on fields in parameters, new implementation requires
getters to be defined for all fields.
Note: This change causes a knock on into distribution
Change-Id: I172f5d9310caf92d6ea825ff93292019c00a47c3
Issue-ID: POLICY-1095
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'common-parameters/src/main')
-rw-r--r-- | common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java | 75 |
1 files changed, 57 insertions, 18 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java index fc2f6ca5..94d9ad49 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java @@ -21,11 +21,16 @@ package org.onap.policy.common.parameters; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; +import org.apache.commons.lang3.StringUtils; + /** * This class holds the result of the validation of a parameter group. */ @@ -60,18 +65,13 @@ public class GroupValidationResult implements ValidationResult { continue; } - // Make the field accessible - boolean savedAccessibilityValue = field.isAccessible(); - field.setAccessible(true); - - try { - // Set the validation result - validationResultMap.put(field.getName(), getValidationResult(field, parameterGroup)); - } catch (IllegalArgumentException | IllegalAccessException e) { - throw new ParameterRuntimeException("could not get value of parameter \"" + field.getName() + "\"", e); - } finally { - field.setAccessible(savedAccessibilityValue); + // Exclude static fields + if (Modifier.isStatic(field.getModifiers())) { + continue; } + + // Set the validation result + validationResultMap.put(field.getName(), getValidationResult(field, parameterGroup)); } } @@ -81,13 +81,12 @@ public class GroupValidationResult implements ValidationResult { * @param field The parameter field * @param ParameterGroup The parameter group containing the field * @return the validation result - * @throws IllegalAccessException on accessing private fields + * @throws Exception on accessing private fields */ - private ValidationResult getValidationResult(final Field field, final ParameterGroup parameterGroup) - throws IllegalAccessException { + private ValidationResult getValidationResult(final Field field, final ParameterGroup parameterGroup) { final String fieldName = field.getName(); final Class<?> fieldType = field.getType(); - final Object fieldObject = field.get(parameterGroup); + final Object fieldObject = getObjectField(parameterGroup, field); // Nested parameter groups are allowed if (ParameterGroup.class.isAssignableFrom(fieldType)) { @@ -111,6 +110,47 @@ public class GroupValidationResult implements ValidationResult { } /** + * Get the value of a field in an object using a getter found with reflection + * + * @param targetObject The object on which to read the field value + * @param fieldName The name of the field + * @return The field value + */ + private Object getObjectField(final Object targetObject, final Field field) { + String getterMethodName; + + // Check for Boolean fields, the convention for boolean getters is that they start with "is" + // If the field name already starts with "is" then the getter has the field name otherwise + // the field name is prepended with "is" + if (boolean.class.equals(field.getType())) { + if (field.getName().startsWith("is")) { + getterMethodName = field.getName(); + } else { + getterMethodName = "is" + StringUtils.capitalize(field.getName()); + } + } else { + getterMethodName = "get" + StringUtils.capitalize(field.getName()); + } + + // Look up the getter method for the field + Method getterMethod; + try { + getterMethod = targetObject.getClass().getMethod(getterMethodName, (Class<?>[]) null); + } catch (NoSuchMethodException | SecurityException e) { + throw new ParameterRuntimeException("could not get getter method for parameter \"" + field.getName() + "\"", + e); + } + + // Invoke the getter + try { + return getterMethod.invoke(targetObject, (Object[]) null); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new ParameterRuntimeException("error calling getter method for parameter \"" + field.getName() + "\"", + e); + } + } + + /** * Check if this field is a map of parameter groups indexed by string keys. * * @param fieldName the name of the collection field. @@ -327,13 +367,12 @@ public class GroupValidationResult implements ValidationResult { validationResultBuilder.append(initialIndentation); validationResultBuilder.append("parameter group \""); - + if (parameterGroup != null) { validationResultBuilder.append(parameterGroup.getName()); validationResultBuilder.append("\" type \""); validationResultBuilder.append(parameterGroup.getClass().getCanonicalName()); - } - else { + } else { validationResultBuilder.append("UNDEFINED"); } validationResultBuilder.append("\" "); |