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;
Class<? extends FrameworkInterface> 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
* @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<? extends FrameworkInterface> 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
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!");
* @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);
*/
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);
* @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
+ 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, value);
+ 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!");
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
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!");
// 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;
+ }
}