From: Roland Haeder Date: Fri, 14 Aug 2015 20:19:35 +0000 (+0200) Subject: Introduced getType() which gets a type reflection from given value X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=83f4aa5236998f4ea42caa26dfcc34e3735f09e9;p=jcore.git Introduced getType() which gets a type reflection from given value Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/jcore/BaseFrameworkSystem.java b/src/org/mxchange/jcore/BaseFrameworkSystem.java index 2dd9dd2..c0cdc9c 100644 --- a/src/org/mxchange/jcore/BaseFrameworkSystem.java +++ b/src/org/mxchange/jcore/BaseFrameworkSystem.java @@ -219,10 +219,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { Class c = this.getClassFromTarget(instance, targetClass); // Init field instance - Method method = null; - - // Use reflection to get all attributes - method = c.getDeclaredMethod(methodName, new Class[0]); + Method method = c.getDeclaredMethod(methodName, new Class[0]); // Assert on field assert (method instanceof Method) : "method is not a Method instance"; //NOI18N @@ -240,21 +237,18 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param instance Actual instance to call * @param targetClass Target class name * @param methodName Method name - * @param value Value to check type from + * @param type Type reflection to check type from * @return A Method instance */ - private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName, final Object value) throws NoSuchMethodException { + private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName, final Class type) throws NoSuchMethodException { // Trace messahe - this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N + this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N // Get target class instance Class c = this.getClassFromTarget(instance, targetClass); // Init field instance - Method method = null; - - // Use reflection to get all attributes - method = c.getDeclaredMethod(methodName, value.getClass()); + Method method = c.getDeclaredMethod(methodName, type); // Assert on field assert (method instanceof Method) : "method is not a Method instance"; //NOI18N @@ -595,10 +589,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { Method method = this.getMethodFromName(instance, targetClass, methodName); // Get value from field - Boolean value = false; - - // Try to get the value by invoking the method - value = (Boolean) method.invoke(instance); + Boolean value = (Boolean) method.invoke(instance); // Trace message this.getLogger().trace("value=" + value + " - EXIT!"); @@ -613,17 +604,21 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param instance The instance to call * @param targetClass Target class to look in * @param methodName Method name to look for + * @param columnName Column name * @param value Boolean value from field * @throws java.lang.NoSuchMethodException If the method was not found * @throws java.lang.IllegalAccessException If the method cannot be accessed * @throws java.lang.reflect.InvocationTargetException Some other problems? */ - protected void setBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName, final Boolean value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + protected void setBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Boolean value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace messahe this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N + // Get field type + Class type = this.getType(instance, targetClass, columnName); + // Get method instance - Method method = this.getMethodFromName(instance, targetClass, methodName, value); + Method method = this.getMethodFromName(instance, targetClass, methodName, type); // Try to get the value by invoking the method method.invoke(instance, value); @@ -675,17 +670,24 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param instance The instance to call * @param targetClass Target class to look in * @param methodName Method name to look for + * @param columnName Column name * @param value Any value from field * @throws java.lang.NoSuchMethodException If the method was not found * @throws java.lang.IllegalAccessException If the method cannot be accessed * @throws java.lang.reflect.InvocationTargetException Some other problems? */ - protected void setField (final FrameworkInterface instance, final String targetClass, final String methodName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + protected void setField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace messahe this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},value={2}", targetClass, methodName, value)); //NOI18N + // Get field type + Class type = this.getType(instance, targetClass, columnName); + + // Debug message + this.getLogger().debug("type=" + type); + // Get method to call - Method method = this.getMethodFromName(instance, targetClass, methodName, value); + Method method = this.getMethodFromName(instance, targetClass, methodName, type); // Get value from field method.invoke(instance, value); @@ -1021,7 +1023,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { this.getLogger().debug("Column " + columnName + " represents a boolean field."); // Yes, then call other method - this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), (Boolean) value); + this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), columnName, (Boolean) value); } // Convert column name to field name @@ -1031,7 +1033,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); // Get field - this.setField(instance, targetClass, methodName, value); + this.setField(instance, targetClass, methodName, columnName, value); // Trace message this.getLogger().trace("EXIT!"); @@ -1082,4 +1084,51 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Return value return value; } + + /** + * Some getter for type reflection of given column name + * + * @param instance The instance to check + * @param targetClass Target class to check + * @param columnName Column name + * @return Type reflection of value + */ + private Class getType (final FrameworkInterface instance, final String targetClass, final String columnName) { + // Trace message + this.getLogger().trace("instance=" + instance + ",targetClass=" + targetClass + ",columnName=" + columnName + " - CALLED!"); + + // Init field tye + Class type = null; + + // Get all attributes from given instance + Field[] fields = this.getClassFromTarget(instance, targetClass).getDeclaredFields(); + + // Debug message + this.getLogger().debug("fields()=" + fields.length); + + // Search for proper field instance + for (final Field field : fields) { + // Debug message + this.getLogger().debug("field=" + field); + + // Does it match? + if (field.getName().equals(columnName)) { + // Found it + type = field.getType(); + break; + } + } + + // type should not be null + if (type == null) { + // No null allowed + throw new NullPointerException("type is null"); + } + + // Trace message + this.getLogger().debug("type=" + type + " - EXIT!"); + + // Return it + return type; + } }