import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
+import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
// Trace message
this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
-
+
// Instance reflaction of this class
Class<? extends FrameworkInterface> c = instance.getClass();
-
+
// Analyze class
while (!targetClass.equals(c.getSimpleName())) {
// Debug message
this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
-
+
// Get super class (causes unchecked warning)
c = (Class<? extends FrameworkInterface>) c.getSuperclass();
}
-
+
// Trace message
this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
-
+
// Return it
return c;
}
private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) {
// Trace messahe
this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //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
try {
method = c.getDeclaredMethod(methodName, new Class<?>[0]);
// Method not found
this.abortProgramWithException(ex);
}
-
+
// 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;
}
protected final void abortProgramWithException (final Throwable throwable) {
// Log exception ...
this.getLogger().catching(throwable);
-
+
// .. and exit
System.exit(1);
* @param delimiter Delimiter
* @param size Size of array
* @return Array from tokenized string
+ * @todo Get rid of size parameter
*/
protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
// Trace message
* @return Propety value
*/
protected final String getProperty (final String key) {
- return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.addressbook.%s", key)); //NOI18N
+ return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.%s", key)); //NOI18N
}
/**
// Is already set
throw new IllegalStateException("called twice");
}
-
+
// Set instance
bundle = ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE); // NOI18N
}
// Return result
return isBool;
}
+
+ /**
+ * Creates an iterator from given instance and class name. The class name
+ * is required in getValueFromColumn() to make a proper call.
+ *
+ * @param instance Instance to run getter calls on
+ * @param className Class name to iterate over
+ * @return An iterator over all object's fields
+ */
+ protected Iterator<Object> fieldIterator (final FrameworkInterface instance, final String className) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className));
+
+ // Get all attributes from given instance
+ Field[] fields = instance.getClass().getDeclaredFields();
+
+ // A list is fine
+ List<Object> list = new ArrayList<>(fields.length);
+
+ // Walk through all
+ for (final Field field : fields) {
+ // Get value from it
+ Object value = this.getValueFromColumn(field.getName());
+
+ // Add it to list
+ assert(list.add(value)) : MessageFormat.format("value {0} has not been added", value);
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", list.size()));
+
+ // Return list iterator
+ return list.iterator();
+ }
}