]> git.mxchange.org Git - jcore.git/blobdiff - src/org/mxchange/jcore/BaseFrameworkSystem.java
Sorted imports (NetBeans' choice?)
[jcore.git] / src / org / mxchange / jcore / BaseFrameworkSystem.java
index 6cc490e002e67c8f4aa6666d6cc7b133b9019c67..db3b9ed493a30a629f68c05dca4bc866ee672162 100644 (file)
@@ -33,6 +33,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.ResourceBundle;
 import java.util.StringTokenizer;
+import java.util.logging.Level;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.mxchange.jcore.application.Application;
@@ -219,10 +220,36 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
 
                // Init field instance
-               Method method = null;
+               Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
 
-               // Use reflection to get all attributes
-               method = c.getDeclaredMethod(methodName, new Class<?>[0]);
+               // Assert on field
+               assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
+
+               // Return it
+               return method;
+       }
+
+       /**
+        * Some "getter" for a Method instance from given method name
+        *
+        * @param instance Actual instance to call
+        * @param targetClass Target class name
+        * @param methodName Method name
+        * @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 Class<?> type) throws NoSuchMethodException {
+               // Trace messahe
+               this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N
+
+               // Get target class instance
+               Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
+
+               // Init field instance
+               Method method = c.getDeclaredMethod(methodName, type);
 
                // Assert on field
                assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
@@ -563,10 +590,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!");
@@ -581,17 +605,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);
+               Method method = this.getMethodFromName(instance, targetClass, methodName, type);
 
                // Try to get the value by invoking the method
                method.invoke(instance, value);
@@ -622,11 +650,14 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         */
        protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                // Trace messahe
-               this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
+               this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2}", instance, targetClass, methodName)); //NOI18N
 
                // Get method to call
                Method method = this.getMethodFromName(instance, targetClass, methodName);
 
+               // Debug message
+               this.getLogger().debug("method=" + method + ",instance=" + instance);
+
                // Get value from field
                Object value = method.invoke(instance);
 
@@ -643,20 +674,58 @@ 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 String 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
+               this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2},value={3}", instance, targetClass, methodName, value)); //NOI18N
+
+               // Get field type
+               Class<?> type = this.getType(instance, targetClass, columnName);
+
+               // Debug message
+               this.getLogger().debug("type=" + type);
+
+               // Init object
+               Object object = value;
+
+               // Hard-coded "cast" again ... :-(
+               // @TODO Can't we get rid of this???
+               switch (type.getSimpleName()) {
+                       case "Long": // Long object
+                               object = Long.parseLong((String) value);
+                               break;
+
+                       case "Float": // Float object
+                               object = Float.parseFloat((String) value);
+                               break;
+
+                       case "Boolean": // Boolean object
+                               object = Boolean.parseBoolean((String) value);
+                               break;
+
+                       case "String": // String object
+                               break;
+
+                       default: // Unsupported type
+                               throw new IllegalArgumentException("value " + value + " has unsupported type " + type.getSimpleName());
+               }
+
+               // Debug message
+               this.getLogger().debug("object[" + object.getClass().getSimpleName() + "]=" + object);
 
                // Get method to call
-               Method method = this.getMethodFromName(instance, targetClass, methodName);
+               Method method = this.getMethodFromName(instance, targetClass, methodName, type);
+
+               // Debug message
+               this.getLogger().debug("method=" + method + ",instance=" + instance + ",value[" + value.getClass().getSimpleName() + "]=" + value);
 
                // Get value from field
-               method.invoke(instance, value);
+               method.invoke(instance, object);
 
                // Trace messahe
                this.getLogger().trace("EXIT!");
@@ -973,7 +1042,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * @throws java.lang.IllegalAccessException If the setter cannot be accessed
         * @throws java.lang.reflect.InvocationTargetException Any other problem?
         */
-       protected void setValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName, final String value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+       protected void setValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                // Trace message
                this.getLogger().trace("instance=" + instance + ",targetClass=" + targetClass + ",columnName=" + columnName + ",value=" + value + " - CALLED!");
 
@@ -989,7 +1058,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.parseBoolean(value));
+                       this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), columnName, (Boolean) value);
                }
 
                // Convert column name to field name
@@ -999,7 +1068,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!");
@@ -1050,4 +1119,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;
+       }
 }