Further cleanup of logging.
This commit is contained in:
Fabian Becker 2013-01-31 15:56:53 +00:00
parent d474eebfa2
commit d93c8bf2b0
7 changed files with 1194 additions and 1258 deletions

View File

@ -1,5 +1,6 @@
package eva2.gui; package eva2.gui;
import eva2.client.EvAClient;
import eva2.server.go.populations.Population; import eva2.server.go.populations.Population;
import eva2.server.go.strategies.GeneticAlgorithm; import eva2.server.go.strategies.GeneticAlgorithm;
import eva2.tools.Pair; import eva2.tools.Pair;
@ -15,44 +16,46 @@ import java.lang.reflect.Array;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* Some miscellaneous functions to help with Beans, reflection, conversion and generic display. * Some miscellaneous functions to help with Beans, reflection, conversion and
* generic display.
* *
* @author mkron, Holger Ulmer, Felix Streichert, Hannes Planatscher * @author mkron, Holger Ulmer, Felix Streichert, Hannes Planatscher
* *
*/ */
public class BeanInspector { public class BeanInspector {
public static boolean TRACE = false; private static final Logger LOGGER = Logger.getLogger(BeanInspector.class.getName());
/** /**
* Check for equality based on bean properties of two target objects. * Check for equality based on bean properties of two target objects.
*/ */
public static boolean equalProperties(Object obj_1, Object obj_2) { public static boolean equalProperties(Object obj1, Object obj2) {
if (obj_1 == null || obj_2 == null) { if (obj1 == null || obj2 == null) {
System.out.println(""); System.out.println("");
return false; return false;
} }
System.out.println("equalProperties: " + obj_1.getClass().getName() + " " + obj_2.getClass().getName()); System.out.println("equalProperties: " + obj1.getClass().getName() + " " + obj2.getClass().getName());
if (obj_1.getClass().getName().equals(obj_2.getClass().getName()) == false) { if (obj1.getClass().getName().equals(obj2.getClass().getName()) == false) {
System.out.println(""); System.out.println("");
return false; return false;
} }
// compare each of the properties !! // compare each of the properties !!
BeanInfo Info_1 = null; BeanInfo beanInfo1;
BeanInfo Info_2 = null; BeanInfo beanInfo2;
PropertyDescriptor[] Properties_1 = null; PropertyDescriptor[] Properties_1 = null;
PropertyDescriptor[] Properties_2 = null; PropertyDescriptor[] Properties_2 = null;
try { try {
Info_1 = Introspector.getBeanInfo(obj_1.getClass()); beanInfo1 = Introspector.getBeanInfo(obj1.getClass());
Info_2 = Introspector.getBeanInfo(obj_2.getClass()); beanInfo2 = Introspector.getBeanInfo(obj2.getClass());
Properties_1 = Info_1.getPropertyDescriptors(); Properties_1 = beanInfo1.getPropertyDescriptors();
Properties_2 = Info_2.getPropertyDescriptors(); Properties_2 = beanInfo2.getPropertyDescriptors();
Info_1.getMethodDescriptors(); beanInfo1.getMethodDescriptors();
} catch (IntrospectionException ex) { } catch (IntrospectionException ex) {
System.out.println("BeanTest: Couldn't introspect !!!!!!!!!"); LOGGER.log(Level.FINEST, "Could not introspect object.", ex);
return false; return false;
} }
boolean BeansInside = false; boolean BeansInside = false;
@ -76,8 +79,8 @@ public class BeanInspector {
Object args_2[] = {}; Object args_2[] = {};
//System.out.println("m_Target"+m_Target.toString()); //System.out.println("m_Target"+m_Target.toString());
try { try {
Object value_1 = getter_1.invoke(obj_1, args_1); Object value_1 = getter_1.invoke(obj1, args_1);
Object value_2 = getter_2.invoke(obj_2, args_2); Object value_2 = getter_2.invoke(obj2, args_2);
BeansInside = true; BeansInside = true;
if (BeanInspector.equalProperties(value_1, value_2) == false) { if (BeanInspector.equalProperties(value_1, value_2) == false) {
BeansEqual = false; BeansEqual = false;
@ -90,13 +93,13 @@ public class BeanInspector {
return BeansEqual; return BeansEqual;
} }
// here we have Integer or Double ... // here we have Integer or Double ...
if (obj_1 instanceof Integer || if (obj1 instanceof Integer
obj_1 instanceof Boolean || || obj1 instanceof Boolean
obj_1 instanceof Float || || obj1 instanceof Float
obj_1 instanceof Double || || obj1 instanceof Double
obj_1 instanceof Long || || obj1 instanceof Long
obj_1 instanceof String) { || obj1 instanceof String) {
return obj_1.equals(obj_2); return obj1.equals(obj2);
} }
System.err.println(" Attention no match !!!"); System.err.println(" Attention no match !!!");
@ -115,7 +118,9 @@ public class BeanInspector {
} }
/** /**
* Produce a string with newlines and indentation (easier readable for if an object has many properties). * Produce a string with newlines and indentation (easier readable for if an
* object has many properties).
*
* @param obj * @param obj
* @return * @return
*/ */
@ -124,10 +129,11 @@ public class BeanInspector {
} }
/** /**
* Collect the accessible properties of an object and their values in a string with indentations. * Collect the accessible properties of an object and their values in a
* Special cases: Arrays and Lists are concatenations of their elements, Population is excepted from lists. * string with indentations. Special cases: Arrays and Lists are
* If the object has its own toString method, this one is preferred. Hidden or expert properties are not * concatenations of their elements, Population is excepted from lists. If
* shown. * the object has its own toString method, this one is preferred. Hidden or
* expert properties are not shown.
* *
* @param obj an arbitrary object * @param obj an arbitrary object
* @return a String description of the object * @return a String description of the object
@ -137,10 +143,11 @@ public class BeanInspector {
} }
/** /**
* Collect the accessible properties of an object and their values in a string. * Collect the accessible properties of an object and their values in a
* Special cases: Arrays and Lists are concatenations of their elements, Population is excepted from lists. * string. Special cases: Arrays and Lists are concatenations of their
* If the object has its own toString method, this one is preferred. Hidden or expert properties are not * elements, Population is excepted from lists. If the object has its own
* shown. * toString method, this one is preferred. Hidden or expert properties are
* not shown.
* *
* @param obj Description of the Parameter * @param obj Description of the Parameter
* @return Description of the Return Value * @return Description of the Return Value
@ -151,7 +158,7 @@ public class BeanInspector {
} }
// try the object itself // try the object itself
if (obj instanceof String) { if (obj instanceof String) {
return (String)obj; return (String) obj;
} // directly return a string object } // directly return a string object
Class<? extends Object> type = obj.getClass(); Class<? extends Object> type = obj.getClass();
@ -162,13 +169,13 @@ public class BeanInspector {
sbuf.append(" "); sbuf.append(" ");
} }
int len = Array.getLength(obj); int len = Array.getLength(obj);
for (int i=0; i<len; i++) { for (int i = 0; i < len; i++) {
// sbuf.append(toString(Array.get(obj, i))); // sbuf.append(toString(Array.get(obj, i)));
if (withNewlines) { if (withNewlines) {
sbuf.append('\n'); sbuf.append('\n');
} }
sbuf.append(toString(Array.get(obj, i), delim, tight, indentStr, indentDepth, withNewlines)); sbuf.append(toString(Array.get(obj, i), delim, tight, indentStr, indentDepth, withNewlines));
if (i<len-1) { if (i < len - 1) {
// sbuf.append(delim); // sbuf.append(delim);
if (!tight) { if (!tight) {
sbuf.append(" "); sbuf.append(" ");
@ -196,7 +203,7 @@ public class BeanInspector {
if (!tight) { if (!tight) {
sbuf.append(" "); sbuf.append(" ");
} }
List<?> lst = (List<?>)obj; List<?> lst = (List<?>) obj;
for (Object o : lst) { for (Object o : lst) {
sbuf.append(o.toString()); sbuf.append(o.toString());
sbuf.append(delim); sbuf.append(delim);
@ -204,10 +211,10 @@ public class BeanInspector {
sbuf.append(" "); sbuf.append(" ");
} }
} }
if (!tight && (sbuf.charAt(sbuf.length()-2) == delim)) { if (!tight && (sbuf.charAt(sbuf.length() - 2) == delim)) {
sbuf.setCharAt(sbuf.length()-2, ' '); sbuf.setCharAt(sbuf.length() - 2, ' ');
} // delete the delim } // delete the delim
sbuf.setCharAt(sbuf.length()-1, ']'); sbuf.setCharAt(sbuf.length() - 1, ']');
return sbuf.toString(); return sbuf.toString();
} }
@ -218,19 +225,16 @@ public class BeanInspector {
//args[0] = obj; //args[0] = obj;
try { try {
String ret = (String) methods[ii].invoke(obj, args); String ret = (String) methods[ii].invoke(obj, args);
if (TRACE) {
System.out.println("toString on "+ obj.getClass() + " gave me " + ret);
}
return makeIndent(indentStr, indentDepth) + ret; return makeIndent(indentStr, indentDepth) + ret;
} catch (Exception e) { } catch (Exception e) {
System.err.println(" ERROR +"+ e.getMessage()); System.err.println(" ERROR +" + e.getMessage());
} }
} }
} }
// otherwise try introspection and collect all public properties as strings // otherwise try introspection and collect all public properties as strings
Pair<String[],Object[]> nameVals = getPublicPropertiesOf(obj, true, true); Pair<String[], Object[]> nameVals = getPublicPropertiesOf(obj, true, true);
StringBuffer sbuf = new StringBuffer(); StringBuffer sbuf = new StringBuffer();
if (withNewlines) { if (withNewlines) {
@ -239,15 +243,15 @@ public class BeanInspector {
addIndent(sbuf, indentStr, indentDepth); addIndent(sbuf, indentStr, indentDepth);
sbuf.append(type.getName()); sbuf.append(type.getName());
sbuf.append("{"); sbuf.append("{");
for (int i=0; i<nameVals.head.length; i++) { for (int i = 0; i < nameVals.head.length; i++) {
if (nameVals.head[i]!=null) { if (nameVals.head[i] != null) {
if (withNewlines) { if (withNewlines) {
sbuf.append('\n'); sbuf.append('\n');
} }
addIndent(sbuf, indentStr, indentDepth); addIndent(sbuf, indentStr, indentDepth);
sbuf.append(nameVals.head[i]); sbuf.append(nameVals.head[i]);
sbuf.append("="); sbuf.append("=");
sbuf.append(toString(nameVals.tail[i], delim, tight, indentStr, indentDepth+1, withNewlines)); sbuf.append(toString(nameVals.tail[i], delim, tight, indentStr, indentDepth + 1, withNewlines));
sbuf.append(delim); sbuf.append(delim);
if (!tight) { if (!tight) {
sbuf.append(" "); sbuf.append(" ");
@ -260,21 +264,20 @@ public class BeanInspector {
} }
private static void addIndent(StringBuffer sbuf, String indentStr, int indentDepth) { private static void addIndent(StringBuffer sbuf, String indentStr, int indentDepth) {
if (indentStr!=null && (indentDepth>0)) { if (indentStr != null && (indentDepth > 0)) {
for (int i=0; i<indentDepth; i++) { for (int i = 0; i < indentDepth; i++) {
sbuf.append(indentStr); sbuf.append(indentStr);
} }
} }
} }
private static String makeIndent(String indentStr, int indentDepth) { private static String makeIndent(String indentStr, int indentDepth) {
if (indentStr!=null) { if (indentStr != null) {
if (indentDepth<1) { if (indentDepth < 1) {
return ""; return "";
} } else {
else {
StringBuffer sbuf = new StringBuffer(indentStr); StringBuffer sbuf = new StringBuffer(indentStr);
for (int i=2; i<=indentDepth; i++) { for (int i = 2; i <= indentDepth; i++) {
sbuf.append(indentStr); sbuf.append(indentStr);
} }
return sbuf.toString(); return sbuf.toString();
@ -300,14 +303,15 @@ public class BeanInspector {
} }
/** /**
* Retrieve names and values of instance fields which are accessible by getter method, optionally * Retrieve names and values of instance fields which are accessible by
* by both getter and setter method. The returned arrays may contain null entries. * getter method, optionally by both getter and setter method. The returned
* Properties marked as hidden or expert are skipped. * arrays may contain null entries. Properties marked as hidden or expert
* are skipped.
* *
* @param target * @param target
* @return * @return
*/ */
public static Pair<String[],Object[]> getPublicPropertiesOf(Object target, boolean requireSetter, boolean showHidden) { public static Pair<String[], Object[]> getPublicPropertiesOf(Object target, boolean requireSetter, boolean showHidden) {
BeanInfo Info = null; BeanInfo Info = null;
PropertyDescriptor[] Properties = null; PropertyDescriptor[] Properties = null;
// MethodDescriptor[] Methods = null; // MethodDescriptor[] Methods = null;
@ -343,19 +347,18 @@ public class BeanInspector {
//System.out.println("m_obj"+m_obj.toString()); //System.out.println("m_obj"+m_obj.toString());
try { try {
nameArray[i]=name; nameArray[i] = name;
valArray[i] = getter.invoke(target, args); valArray[i] = getter.invoke(target, args);
} catch (Exception e) { } catch (Exception e) {
System.err.println("BeanTest ERROR +"+ e.getMessage()); System.err.println("BeanTest ERROR +" + e.getMessage());
} }
} }
Pair<String[],Object[]> nameVals = new Pair<String[],Object[]>(nameArray, valArray); Pair<String[], Object[]> nameVals = new Pair<String[], Object[]>(nameArray, valArray);
return nameVals; return nameVals;
} }
/** /**
*@param obj Description of the Parameter * @param obj Description of the Parameter
*/ */
public static void showInfo(Object obj) { public static void showInfo(Object obj) {
System.out.println("Inspecting " + obj.getClass().getName()); System.out.println("Inspecting " + obj.getClass().getName());
@ -422,10 +425,10 @@ public class BeanInspector {
} }
/** /**
* Call a method by a given name with given arguments, if the method is available. * Call a method by a given name with given arguments, if the method is
* Returns the return values of the call or null if it isnt found. * available. Returns the return values of the call or null if it isnt
* This of course means that the caller is unable to distinguish between "method not found" * found. This of course means that the caller is unable to distinguish
* and "method found and it returned null". * between "method not found" and "method found and it returned null".
* *
* @param obj * @param obj
* @param mName * @param mName
@ -437,8 +440,8 @@ public class BeanInspector {
if (meth != null) { if (meth != null) {
try { try {
return meth.invoke(obj, args); return meth.invoke(obj, args);
} catch(Exception e) { } catch (Exception e) {
System.err.println("Error on calling method "+mName + " on " + obj.getClass().getName()); System.err.println("Error on calling method " + mName + " on " + obj.getClass().getName());
System.err.println("Object: " + obj.toString() + ", method name: " + mName); System.err.println("Object: " + obj.toString() + ", method name: " + mName);
System.err.println("Arguments were " + BeanInspector.toString(args)); System.err.println("Arguments were " + BeanInspector.toString(args));
e.printStackTrace(); e.printStackTrace();
@ -450,27 +453,26 @@ public class BeanInspector {
} }
/** /**
* Produce an array of Class instances matching the types of * Produce an array of Class instances matching the types of the given
* the given object array. * object array.
* *
* @param o * @param o
* @return * @return
*/ */
public static Class[] toClassArray(Object[] o) { public static Class[] toClassArray(Object[] o) {
if (o==null) { if (o == null) {
return null; return null;
} }
Class[] clz = new Class[o.length]; Class[] clz = new Class[o.length];
for (int i=0; i<o.length; i++) { for (int i = 0; i < o.length; i++) {
clz[i]=o[i].getClass(); clz[i] = o[i].getClass();
} }
return clz; return clz;
} }
/** /**
* Check whether an object has a method by the given name and with * Check whether an object has a method by the given name and with matching
* matching signature considering the arguments. Return * signature considering the arguments. Return it if found, or null if not.
* it if found, or null if not.
* *
* @param obj * @param obj
* @param mName the method name * @param mName the method name
@ -482,13 +484,13 @@ public class BeanInspector {
} }
/** /**
* Check whether an object has a method by the given name and * Check whether an object has a method by the given name and with the given
* with the given parameter signature. Return * parameter signature. Return it if found, or null if not.
* it if found, or null if not.
* *
* @param obj * @param obj
* @param mName the method name * @param mName the method name
* @param paramTypes the parameter types, null allowed if no parameters are expected * @param paramTypes the parameter types, null allowed if no parameters are
* expected
* @return the method or null if it isn't found * @return the method or null if it isn't found
*/ */
public static Method hasMethod(Object obj, String mName, Class[] paramTypes) { public static Method hasMethod(Object obj, String mName, Class[] paramTypes) {
@ -497,15 +499,16 @@ public class BeanInspector {
for (Method method : meths) { for (Method method : meths) {
if (method.getName().equals(mName)) { // name match if (method.getName().equals(mName)) { // name match
Class[] methParamTypes = method.getParameterTypes(); Class[] methParamTypes = method.getParameterTypes();
if (paramTypes==null && methParamTypes.length==0) { if (paramTypes == null && methParamTypes.length == 0) {
return method; return method;
} // full match } // full match
else { else {
if (paramTypes!=null && (methParamTypes.length==paramTypes.length)) { if (paramTypes != null && (methParamTypes.length == paramTypes.length)) {
boolean mismatch = false; int i=0; boolean mismatch = false;
while ((i<methParamTypes.length) && (!mismatch)) { int i = 0;
while ((i < methParamTypes.length) && (!mismatch)) {
if (!methParamTypes[i].isAssignableFrom(paramTypes[i]) && !isBoxableFrom(methParamTypes[i], paramTypes[i])) { if (!methParamTypes[i].isAssignableFrom(paramTypes[i]) && !isBoxableFrom(methParamTypes[i], paramTypes[i])) {
mismatch=true; mismatch = true;
} }
i++; i++;
} }
@ -520,7 +523,8 @@ public class BeanInspector {
} }
/** /**
* Check if the given first class is a primitive type and can be boxed to match the second class. * Check if the given first class is a primitive type and can be boxed to
* match the second class.
* *
* @param clz1 * @param clz1
* @param clz2 * @param clz2
@ -528,7 +532,7 @@ public class BeanInspector {
*/ */
private static boolean isBoxableFrom(Class clz1, Class clz2) { private static boolean isBoxableFrom(Class clz1, Class clz2) {
Class box = getBoxedType(clz1); Class box = getBoxedType(clz1);
if (box!=null && (clz2.isAssignableFrom(box))) { if (box != null && (clz2.isAssignableFrom(box))) {
return true; return true;
} else { } else {
return false; return false;
@ -536,8 +540,8 @@ public class BeanInspector {
} }
/** /**
* For a primitive type, return the boxed referenced type. * For a primitive type, return the boxed referenced type. Return null for
* Return null for any non-primitive type. * any non-primitive type.
* *
* @param clz1 * @param clz1
* @return * @return
@ -546,29 +550,21 @@ public class BeanInspector {
if (cls.isPrimitive()) { if (cls.isPrimitive()) {
if (cls == double.class) { if (cls == double.class) {
return Double.class; return Double.class;
} } else if (cls == char.class) {
else if (cls == char.class) {
return Character.class; return Character.class;
} } else if (cls == int.class) {
else if (cls == int.class) {
return Integer.class; return Integer.class;
} } else if (cls == boolean.class) {
else if (cls == boolean.class) {
return Boolean.class; return Boolean.class;
} } else if (cls == byte.class) {
else if (cls == byte.class) {
return Byte.class; return Byte.class;
} } else if (cls == short.class) {
else if (cls == short.class) {
return Short.class; return Short.class;
} } else if (cls == long.class) {
else if (cls == long.class) {
return Long.class; return Long.class;
} } else if (cls == float.class) {
else if (cls == float.class) {
return Float.class; return Float.class;
} } else {
else {
return Void.class; return Void.class;
} }
} else { } else {
@ -577,44 +573,37 @@ public class BeanInspector {
} }
/** /**
* For a Java primitive wrapper class return the corresponding * For a Java primitive wrapper class return the corresponding primitive
* primitive class. * class.
**/ *
*/
public static Class getUnboxedType(Class cls) { public static Class getUnboxedType(Class cls) {
if (cls == Double.class) { if (cls == Double.class) {
return double.class; return double.class;
} } else if (cls == Character.class) {
else if (cls == Character.class) {
return char.class; return char.class;
} } else if (cls == Integer.class) {
else if (cls == Integer.class) {
return int.class; return int.class;
} } else if (cls == Boolean.class) {
else if (cls == Boolean.class) {
return boolean.class; return boolean.class;
} } else if (cls == Byte.class) {
else if (cls == Byte.class) {
return byte.class; return byte.class;
} } else if (cls == Short.class) {
else if (cls == Short.class) {
return short.class; return short.class;
} } else if (cls == Long.class) {
else if (cls == Long.class) {
return long.class; return long.class;
} } else if (cls == Float.class) {
else if (cls == Float.class) {
return float.class; return float.class;
} } else if (cls == Void.class) {
else if (cls == Void.class) {
return void.class; return void.class;
} } else {
else {
return null; return null;
} }
} }
/** /**
* Just concatenates getClassDescription(obj) and getMemberDescriptions(obj, withValues). * Just concatenates getClassDescription(obj) and getMemberDescriptions(obj,
* withValues).
* *
* @param obj target object * @param obj target object
* @param withValues if true, member values are displayed as well * @param withValues if true, member values are displayed as well
@ -631,8 +620,8 @@ public class BeanInspector {
} }
/** /**
* Check for info methods on the object to be provided by the developer * Check for info methods on the object to be provided by the developer and
* and return their text as String. * return their text as String.
* *
* @param obj * @param obj
* @return String information about the object's class * @return String information about the object's class
@ -642,14 +631,14 @@ public class BeanInspector {
infoBf.append(obj.getClass().getName()); infoBf.append(obj.getClass().getName());
infoBf.append("\t"); infoBf.append("\t");
Object args[] = { }; Object args[] = {};
Object ret; Object ret;
for (String meth : new String[]{"getName", "globalInfo"}) { for (String meth : new String[]{"getName", "globalInfo"}) {
ret = callIfAvailable(obj, meth, args); ret = callIfAvailable(obj, meth, args);
if (ret != null) { if (ret != null) {
infoBf.append("\t"); infoBf.append("\t");
infoBf.append((String)ret); infoBf.append((String) ret);
} }
} }
@ -657,9 +646,10 @@ public class BeanInspector {
} }
/** /**
* Return an info string on the members of the object class, containing name, type, optional * Return an info string on the members of the object class, containing
* value and tool tip text if available. The type is accompanied by a tag "common" or "restricted", * name, type, optional value and tool tip text if available. The type is
* indicating whether the member property is normal or hidden, meaning it may have effect depending * accompanied by a tag "common" or "restricted", indicating whether the
* member property is normal or hidden, meaning it may have effect depending
* on settings of other members only, for instance. * on settings of other members only, for instance.
* *
* @param obj target object * @param obj target object
@ -670,7 +660,7 @@ public class BeanInspector {
BeanInfo bi; BeanInfo bi;
try { try {
bi = Introspector.getBeanInfo(obj.getClass()); bi = Introspector.getBeanInfo(obj.getClass());
} catch(IntrospectionException e) { } catch (IntrospectionException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
@ -683,9 +673,6 @@ public class BeanInspector {
} }
String name = m_Properties[i].getDisplayName(); String name = m_Properties[i].getDisplayName();
if (TRACE) {
System.out.println("PSP looking at "+ name);
}
Method getter = m_Properties[i].getReadMethod(); Method getter = m_Properties[i].getReadMethod();
Method setter = m_Properties[i].getWriteMethod(); Method setter = m_Properties[i].getWriteMethod();
@ -695,7 +682,7 @@ public class BeanInspector {
} }
try { try {
Object args[] = { }; Object args[] = {};
Object value = getter.invoke(obj, args); Object value = getter.invoke(obj, args);
// Don't try to set null values: // Don't try to set null values:
@ -703,7 +690,7 @@ public class BeanInspector {
// If it's a user-defined property we give a warning. // If it's a user-defined property we give a warning.
String getterClass = m_Properties[i].getReadMethod().getDeclaringClass().getName(); String getterClass = m_Properties[i].getReadMethod().getDeclaringClass().getName();
if (getterClass.indexOf("java.") != 0) { if (getterClass.indexOf("java.") != 0) {
System.err.println("Warning: Property \"" + name+ "\" has null initial value. Skipping."); System.err.println("Warning: Property \"" + name + "\" has null initial value. Skipping.");
} }
continue; continue;
} }
@ -721,11 +708,11 @@ public class BeanInspector {
} }
String typeName = value.getClass().getName(); String typeName = value.getClass().getName();
if (value instanceof SelectedTag) { if (value instanceof SelectedTag) {
Tag[] tags = ((SelectedTag)value).getTags(); Tag[] tags = ((SelectedTag) value).getTags();
memberInfoBf.append("String in {"); memberInfoBf.append("String in {");
for (int k=0; k<tags.length; k++) { for (int k = 0; k < tags.length; k++) {
memberInfoBf.append(tags[k].getString()); memberInfoBf.append(tags[k].getString());
if (k+1<tags.length) { if (k + 1 < tags.length) {
memberInfoBf.append(", "); memberInfoBf.append(", ");
} }
} }
@ -752,7 +739,7 @@ public class BeanInspector {
memberInfoBf.append('\n'); memberInfoBf.append('\n');
memberInfoList.add(memberInfoBf.toString()); memberInfoList.add(memberInfoBf.toString());
} catch (Exception ex) { } catch (Exception ex) {
System.err.println("Skipping property "+name+" ; exception: " + ex.getMessage()); System.err.println("Skipping property " + name + " ; exception: " + ex.getMessage());
ex.printStackTrace(); ex.printStackTrace();
} // end try } // end try
} // end for } // end for
@ -760,7 +747,8 @@ public class BeanInspector {
} }
/** /**
* Take an object of primitive type (like int, Integer etc) and convert it to double. * Take an object of primitive type (like int, Integer etc) and convert it
* to double.
* *
* @param val * @param val
* @return * @return
@ -768,37 +756,30 @@ public class BeanInspector {
*/ */
public static double toDouble(Object val) throws IllegalArgumentException { public static double toDouble(Object val) throws IllegalArgumentException {
if (val instanceof Integer) { if (val instanceof Integer) {
return ((Integer)val).doubleValue(); return ((Integer) val).doubleValue();
} } else if (val instanceof Double) {
else if (val instanceof Double) { return ((Double) val).doubleValue();
return ((Double)val).doubleValue(); } else if (val instanceof Boolean) {
} return (((Boolean) val) ? 1. : 0.);
else if (val instanceof Boolean) { } else if (val instanceof Character) {
return (((Boolean)val) ? 1. : 0.); return ((Character) val).charValue();
} } else if (val instanceof Byte) {
else if (val instanceof Character) { return ((Byte) val).doubleValue();
return ((Character)val).charValue(); } else if (val instanceof Short) {
} return ((Short) val).doubleValue();
else if (val instanceof Byte) { } else if (val instanceof Long) {
return ((Byte)val).doubleValue(); return ((Long) val).doubleValue();
} } else if (val instanceof Float) {
else if (val instanceof Short) { return ((Float) val).doubleValue();
return ((Short)val).doubleValue(); } else if (val instanceof Void) {
}
else if (val instanceof Long) {
return ((Long)val).doubleValue();
}
else if (val instanceof Float) {
return ((Float)val).doubleValue();
}
else if (val instanceof Void) {
return 0; return 0;
} }
throw new IllegalArgumentException("Illegal type, cant convert " + val.getClass() + " to double."); throw new IllegalArgumentException("Illegal type, cant convert " + val.getClass() + " to double.");
} }
/** /**
* Take a String and convert it to a destined data type using the appropriate function. * Take a String and convert it to a destined data type using the
* appropriate function.
* *
* @param str * @param str
* @param destType * @param destType
@ -807,29 +788,21 @@ public class BeanInspector {
public static Object stringToPrimitive(String str, Class<?> destType) throws NumberFormatException { public static Object stringToPrimitive(String str, Class<?> destType) throws NumberFormatException {
if ((destType == Integer.class) || (destType == int.class)) { if ((destType == Integer.class) || (destType == int.class)) {
return Integer.valueOf(str); return Integer.valueOf(str);
} } else if ((destType == Double.class) || (destType == double.class)) {
else if ((destType == Double.class) || (destType == double.class)) {
return Double.valueOf(str); return Double.valueOf(str);
} } else if ((destType == Boolean.class) || (destType == boolean.class)) {
else if ((destType == Boolean.class) || (destType == boolean.class)) {
return Boolean.valueOf(str); return Boolean.valueOf(str);
} } else if ((destType == Byte.class) || (destType == byte.class)) {
else if ((destType == Byte.class) || (destType == byte.class)) {
return Byte.valueOf(str); return Byte.valueOf(str);
} } else if ((destType == Short.class) || (destType == short.class)) {
else if ((destType == Short.class) || (destType == short.class)) {
return Short.valueOf(str); return Short.valueOf(str);
} } else if ((destType == Long.class) || (destType == long.class)) {
else if ((destType == Long.class) || (destType == long.class)) {
return Long.valueOf(str); return Long.valueOf(str);
} } else if ((destType == Float.class) || (destType == float.class)) {
else if ((destType == Float.class) || (destType == float.class)) {
return Float.valueOf(str); return Float.valueOf(str);
} } else if ((destType == Character.class) || (destType == char.class)) {
else if ((destType == Character.class) || (destType == char.class)) {
return str.charAt(0); return str.charAt(0);
} } else {
else {
// if (destType == Void.class) // if (destType == Void.class)
System.err.println("warning, value interpreted as void type"); System.err.println("warning, value interpreted as void type");
return 0; return 0;
@ -849,28 +822,21 @@ public class BeanInspector {
} }
if ((destType == Integer.class) || (destType == int.class)) { if ((destType == Integer.class) || (destType == int.class)) {
return new Integer(d.intValue()); return new Integer(d.intValue());
} } else if ((destType == Boolean.class) || (destType == boolean.class)) {
else if ((destType == Boolean.class) || (destType == boolean.class)) { return (d != 0) ? Boolean.TRUE : Boolean.FALSE;
return (d!=0) ? Boolean.TRUE : Boolean.FALSE; } else if ((destType == Byte.class) || (destType == byte.class)) {
}
else if ((destType == Byte.class) || (destType == byte.class)) {
return new Byte(d.byteValue()); return new Byte(d.byteValue());
} } else if ((destType == Short.class) || (destType == short.class)) {
else if ((destType == Short.class) || (destType == short.class)) {
return new Short(d.shortValue()); return new Short(d.shortValue());
} } else if ((destType == Long.class) || (destType == long.class)) {
else if ((destType == Long.class) || (destType == long.class)) {
return new Long(d.longValue()); return new Long(d.longValue());
} } else if ((destType == Float.class) || (destType == float.class)) {
else if ((destType == Float.class) || (destType == float.class)) {
return new Float(d.floatValue()); return new Float(d.floatValue());
} } else { // this makes hardly sense...
else { // this makes hardly sense...
System.err.println("warning: converting from double to character or void..."); System.err.println("warning: converting from double to character or void...");
if ((destType == Character.class) || (destType == char.class)) { if ((destType == Character.class) || (destType == char.class)) {
return new Character(d.toString().charAt(0)); return new Character(d.toString().charAt(0));
} } else {
else {
return 0; return 0;
} }
} }
@ -895,8 +861,9 @@ public class BeanInspector {
} }
/** /**
* Get the primitive class of a Java primitive encapsulation, or null if not applicable. * Get the primitive class of a Java primitive encapsulation, or null if not
* E.g., returns int for Integer, long for Long, Boolean for Boolean etc. * applicable. E.g., returns int for Integer, long for Long, Boolean for
* Boolean etc.
* *
* @param cls * @param cls
* @return * @return
@ -907,30 +874,25 @@ public class BeanInspector {
} }
if (cls == Double.class) { if (cls == Double.class) {
return double.class; return double.class;
} } else if (cls == Integer.class) {
else if (cls == Integer.class) {
return int.class; return int.class;
} } else if (cls == Boolean.class) {
else if (cls == Boolean.class) {
return Boolean.class; return Boolean.class;
} } else if (cls == Byte.class) {
else if (cls == Byte.class) {
return byte.class; return byte.class;
} } else if (cls == Short.class) {
else if (cls == Short.class) {
return short.class; return short.class;
} } else if (cls == Long.class) {
else if (cls == Long.class) {
return long.class; return long.class;
} } else if (cls == Float.class) {
else if (cls == Float.class) {
return float.class; return float.class;
} }
return null; return null;
} }
/** /**
* Try to convert an object to a destination type, especially for primitive types (int, double etc. * Try to convert an object to a destination type, especially for primitive
* but also Integer, Double etc.). * types (int, double etc. but also Integer, Double etc.).
* *
* @param destType * @param destType
* @param value * @param value
@ -945,18 +907,17 @@ public class BeanInspector {
if (destType == String.class || destType == SelectedTag.class) { if (destType == String.class || destType == SelectedTag.class) {
if (value.getClass() == String.class) { if (value.getClass() == String.class) {
return value; return value;
} } else {
else {
return value.toString(); return value.toString();
} }
} else if (isJavaPrimitive(destType)) { } else if (isJavaPrimitive(destType)) {
try { try {
if (value.getClass() == String.class) { if (value.getClass() == String.class) {
return stringToPrimitive((String)value, destType); return stringToPrimitive((String) value, destType);
} else { } else {
return doubleToPrimitive(toDouble(value), destType); return doubleToPrimitive(toDouble(value), destType);
} }
} catch(Exception e) { } catch (Exception e) {
System.err.println("Error in converting type of " + value + " to " + destType.getName() + ": " + e.getMessage()); System.err.println("Error in converting type of " + value + " to " + destType.getName() + ": " + e.getMessage());
return null; return null;
} }
@ -965,10 +926,9 @@ public class BeanInspector {
return value; return value;
} }
/** /**
* Try to get an object member value using the default getter. * Try to get an object member value using the default getter. Returns null
* Returns null if not successful. * if not successful.
* *
* @param obj * @param obj
* @param mem * @param mem
@ -978,7 +938,7 @@ public class BeanInspector {
BeanInfo bi; BeanInfo bi;
try { try {
bi = Introspector.getBeanInfo(obj.getClass()); bi = Introspector.getBeanInfo(obj.getClass());
} catch(IntrospectionException e) { } catch (IntrospectionException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
@ -994,7 +954,7 @@ public class BeanInspector {
try { try {
return getter.invoke(obj, (Object[]) null); return getter.invoke(obj, (Object[]) null);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Exception in invoking setter: "+e.getMessage()); System.err.println("Exception in invoking setter: " + e.getMessage());
return null; return null;
} }
} else { } else {
@ -1004,9 +964,9 @@ public class BeanInspector {
} }
/** /**
* Try to set an object member to a given value. * Try to set an object member to a given value. Returns true if successful,
* Returns true if successful, else false. The types are adapted as generally as possible, * else false. The types are adapted as generally as possible, converting
* converting using the decodeType() method. * using the decodeType() method.
* *
* @param obj * @param obj
* @param mem * @param mem
@ -1017,7 +977,7 @@ public class BeanInspector {
BeanInfo bi; BeanInfo bi;
try { try {
bi = Introspector.getBeanInfo(obj.getClass()); bi = Introspector.getBeanInfo(obj.getClass());
} catch(IntrospectionException e) { } catch (IntrospectionException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
@ -1038,7 +998,7 @@ public class BeanInspector {
if (setter != null) { if (setter != null) {
try { try {
// System.out.println("setting value..."); // System.out.println("setting value...");
Object[] args = new Object[]{ decodeType(type, val) }; Object[] args = new Object[]{decodeType(type, val)};
if (args[0] != null) { if (args[0] != null) {
setter.invoke(obj, args); setter.invoke(obj, args);
return true; return true;
@ -1047,8 +1007,7 @@ public class BeanInspector {
return false; return false;
} }
} catch (Exception e) { } catch (Exception e) {
System.err.println("Exception in invoking setter: "+e.getMessage()); System.err.println("Exception in invoking setter: " + e.getMessage());
// e.printStackTrace();
return false; return false;
} }
} else { } else {
@ -1057,11 +1016,13 @@ public class BeanInspector {
} }
} }
/** This method simply looks for an appropriate tiptext /**
* This method simply looks for an appropriate tiptext
*
* @param name The name of the property * @param name The name of the property
* @param methods A list of methods to search. * @param methods A list of methods to search.
* @param target The target object * @param target The target object
* @return String for the tooltip. * @return String for the ToolTip.
*/ */
public static String getToolTipText(String name, MethodDescriptor[] methods, Object target, boolean stripToolTipToFirstPoint, int toHTMLLen) { public static String getToolTipText(String name, MethodDescriptor[] methods, Object target, boolean stripToolTipToFirstPoint, int toHTMLLen) {
String result = ""; String result = "";
@ -1100,7 +1061,7 @@ public class BeanInspector {
* @param name The name of the property * @param name The name of the property
* @param methods A list of methods to search. * @param methods A list of methods to search.
* @param target The target object * @param target The target object
* @return String for the tooltip. * @return String for the ToolTip.
*/ */
public static String getToolTipText(String name, MethodDescriptor[] methods, Object target) { public static String getToolTipText(String name, MethodDescriptor[] methods, Object target) {
return getToolTipText(name, methods, target, false, 0); return getToolTipText(name, methods, target, false, 0);

View File

@ -29,7 +29,6 @@ public class GOEPanel extends JPanel implements ItemListener {
private Object backupObject; private Object backupObject;
private PropertyChangeSupport propChangeSupport; private PropertyChangeSupport propChangeSupport;
private static boolean TRACE = false;
/** /**
* The chooser component * The chooser component
*/ */

View File

@ -264,7 +264,6 @@ public class GenericObjectEditor implements PropertyEditor {
* @param type a value of type 'Class' * @param type a value of type 'Class'
*/ */
public void setClassType(Class<?> type) { public void setClassType(Class<?> type) {
//if (TRACE) System.out.println("GOE setClassType("+ (type == null? "<null>" : type.getName()) + ")");
classType = type; classType = type;
if (editorComponent != null) { if (editorComponent != null) {
editorComponent.updateClassType(); editorComponent.updateClassType();
@ -337,7 +336,6 @@ public class GenericObjectEditor implements PropertyEditor {
*/ */
private void setObject(Object c) { private void setObject(Object c) {
// This should really call equals() for comparison. // This should really call equals() for comparison.
//if (TRACE) System.out.println("setObject "+ c.getClass().getName() + " " + c);
boolean trueChange = (c != getValue()); boolean trueChange = (c != getValue());
m_Backup = m_Object; m_Backup = m_Object;

View File

@ -6,13 +6,14 @@ package eva2.gui;
*/ */
import java.util.ArrayList; import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* It represents one plot window in the client GUI. * It represents one plot window in the client GUI.
*/ */
public class GraphWindow { public class GraphWindow {
private static final Logger LOGGER = Logger.getLogger(GraphWindow.class.getName());
public static boolean TRACE = false;
static private int graphCounter = -1; static private int graphCounter = -1;
static private PlotContainer plotContainer; static private PlotContainer plotContainer;
private PlotInterface plotter; private PlotInterface plotter;
@ -57,9 +58,6 @@ public class GraphWindow {
* *
*/ */
private GraphWindow(String plotName, String strx, String stry) { private GraphWindow(String plotName, String strx, String stry) {
if (TRACE) {
System.out.println("Constructor GraphWindow");
}
name = plotName; name = plotName;
plotter = new Plot(plotName, strx, stry, true); plotter = new Plot(plotName, strx, stry, true);
} }
@ -76,9 +74,7 @@ public class GraphWindow {
*/ */
public Graph getNewGraph(String infoString) { public Graph getNewGraph(String infoString) {
graphCounter++; graphCounter++;
if (TRACE) { LOGGER.log(Level.FINEST, "Graph.getNewGraph No:{0} - {1} created.", new Object[]{graphCounter, infoString});
System.out.println("Graph.getNewGraph No:" + graphCounter + " - " + infoString);
}
return new Graph(infoString, plotter, graphCounter); return new Graph(infoString, plotter, graphCounter);
} }
} }
@ -104,7 +100,7 @@ class PlotContainer extends ArrayList<GraphWindow> {
* *
*/ */
public boolean containsName(String name) { public boolean containsName(String name) {
GraphWindow temp = null; GraphWindow temp;
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); i++) {
temp = (GraphWindow) (get(i)); temp = (GraphWindow) (get(i));
if (name.equals(temp.getName())) { if (name.equals(temp.getName())) {
@ -130,7 +126,7 @@ class PlotContainer extends ArrayList<GraphWindow> {
return actualPlot; return actualPlot;
} }
} }
GraphWindow temp = null; GraphWindow temp;
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); i++) {
temp = (GraphWindow) (get(i)); temp = (GraphWindow) (get(i));
if (name.equals(temp.getName())) { if (name.equals(temp.getName())) {

View File

@ -9,92 +9,91 @@ package eva2.gui;
* $Date: 2006-01-18 11:02:22 +0100 (Wed, 18 Jan 2006) $ * $Date: 2006-01-18 11:02:22 +0100 (Wed, 18 Jan 2006) $
* $Author: streiche $ * $Author: streiche $
*/ */
/*==========================================================================*
* IMPORTS
*==========================================================================*/
import java.beans.*; import java.beans.*;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
/** /**
* *
*/ */
public class JExtMenu extends JMenu{ public class JExtMenu extends JMenu {
public final static String ACTION = "Action"; public final static String ACTION = "Action";
/** /**
* *
*/ */
public JExtMenu(){ public JExtMenu() {
//super(); //super();
//Mnemonic m = new Mnemonic(s); //Mnemonic m = new Mnemonic(s);
//setText(m.getText()); //setText(m.getText());
//setMnemonic(m.getMnemonic()); //setMnemonic(m.getMnemonic());
} }
/** /**
* *
*/ */
public JExtMenu(String s){ public JExtMenu(String s) {
super(); super();
Mnemonic m = new Mnemonic(s); Mnemonic m = new Mnemonic(s);
setText(m.getText()); setText(m.getText());
setMnemonic(m.getMnemonic()); setMnemonic(m.getMnemonic());
} }
/** /**
* *
*/ */
@Override @Override
public JMenuItem add(Action a){ public JMenuItem add(Action a) {
JMenuItem item = super.add(a); JMenuItem item = super.add(a);
Object o; Object o;
o = a.getValue(ExtAction.MNEMONIC); o = a.getValue(ExtAction.MNEMONIC);
if(o != null) { if (o != null) {
item.setMnemonic(((Character)o).charValue()); item.setMnemonic(((Character) o).charValue());
} }
o = a.getValue(ExtAction.TOOLTIP); o = a.getValue(ExtAction.TOOLTIP);
if(o != null) { if (o != null) {
item.setToolTipText((String)o); item.setToolTipText((String) o);
} }
o = a.getValue(ExtAction.KEYSTROKE); o = a.getValue(ExtAction.KEYSTROKE);
if(o != null) { if (o != null) {
item.setAccelerator((KeyStroke)o); item.setAccelerator((KeyStroke) o);
} }
return item; return item;
} }
/** /**
* *
*/ */
@Override @Override
protected PropertyChangeListener createActionChangeListener(JMenuItem b){ protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
return new ExtActionChangedListener(b){ return new ExtActionChangedListener(b) {
@Override @Override
public void propertyChange(PropertyChangeEvent e) { public void propertyChange(PropertyChangeEvent e) {
JMenuItem menuItem = (JMenuItem)component; JMenuItem menuItem = (JMenuItem) component;
if(menuItem == null) { if (menuItem == null) {
return; return;
} }
String propertyName = e.getPropertyName(); String propertyName = e.getPropertyName();
if(propertyName.equals(Action.NAME)) { if (propertyName.equals(Action.NAME)) {
menuItem.setText((String)e.getNewValue()); menuItem.setText((String) e.getNewValue());
} } else if (propertyName.equals("enabled")) {
else if(propertyName.equals("enabled")) { menuItem.setEnabled(((Boolean) e.getNewValue()).booleanValue());
menuItem.setEnabled(((Boolean)e.getNewValue()).booleanValue()); } else if (propertyName.equals(Action.SMALL_ICON)) {
} Icon icon = (Icon) e.getNewValue();
else if(propertyName.equals(Action.SMALL_ICON)){
Icon icon = (Icon)e.getNewValue();
menuItem.setIcon(icon); menuItem.setIcon(icon);
menuItem.invalidate(); menuItem.invalidate();
menuItem.repaint(); menuItem.repaint();
} } else if (propertyName.equals(ExtAction.MNEMONIC)) {
else if(propertyName.equals(ExtAction.MNEMONIC)) { menuItem.setMnemonic(((Character) e.getNewValue()).charValue());
menuItem.setMnemonic(((Character)e.getNewValue()).charValue()); } else if (propertyName.equals(ExtAction.TOOLTIP)) {
} menuItem.setToolTipText((String) e.getNewValue());
else if(propertyName.equals(ExtAction.TOOLTIP)) { } else if (propertyName.equals(ExtAction.KEYSTROKE)) {
menuItem.setToolTipText((String)e.getNewValue()); menuItem.setAccelerator((KeyStroke) e.getNewValue());
}
else if(propertyName.equals(ExtAction.KEYSTROKE)) {
menuItem.setAccelerator((KeyStroke)e.getNewValue());
} }
} }
}; };

View File

@ -92,19 +92,6 @@ public class ModuleServer {
} }
} }
// and the running modules
// @todo running modules sind abgeschaltet
// for (int i = 0; i < m_RunnungModules.size(); i++) {
// String AdapterName = null;
// try {
// AdapterName = ( (ModuleAdapter) m_RunnungModules.get(i)).getAdapterName();
// }
// catch (Exception ee) {
// System.err.println("Error: GetAdapterName" + ee.getMessage());
// }
// ModuleNameList.add(AdapterName);
// }
String[] x = new String[moduleNameList.size()]; String[] x = new String[moduleNameList.size()];
moduleNameList.toArray(x); moduleNameList.toArray(x);
@ -176,21 +163,6 @@ public class ModuleServer {
return (ModuleAdapter) moduleAdapter; return (ModuleAdapter) moduleAdapter;
} }
} }
// // @todo running modules sind gerade noch abgeschaltet
// for (int i = 0; i < m_RunnungModules.size(); i++) {
// try {
// adapterName = ( (ModuleAdapter) m_RunnungModules.get(i)).getAdapterName();
// }
// catch (Exception e) {
// System.out.println("Error : GetAdapterName" + e);
// }
// if (adapterName.equals(selectedModuleName)) {
// if (TRACE)
// System.out.println(" Choose a running Module!!! " + adapterName);
// m_ModuleAdapter = ( (ModuleAdapter) m_RunnungModules.get(i));
// return (ModuleAdapter) m_ModuleAdapter;
// }
// }
LOGGER.log(Level.SEVERE, "No valid module defined: {0}", selectedModuleName); LOGGER.log(Level.SEVERE, "No valid module defined: {0}", selectedModuleName);
return null; return null;

View File

@ -6,12 +6,14 @@ import java.util.logging.Logger;
/** /**
* This class defines utility routines that use Java serialization. Any * This class defines utility routines that use Java serialization. Any
* serializable object can be stored to a file, loaded, and cloned (returning * serializable object can be stored to a file, loaded, and cloned (returning a
* a deep copy). * deep copy).
* *
* @author Holger Ulmer, Felix Streichert, Hannes Planatscher, Marcel Kronfeld * @author Holger Ulmer, Felix Streichert, Hannes Planatscher, Marcel Kronfeld
**/ *
*/
public class Serializer { public class Serializer {
/** /**
* The logging instance for this class. * The logging instance for this class.
*/ */
@ -20,19 +22,22 @@ public class Serializer {
/** /**
* Private constructor to prevent instantiating module class. * Private constructor to prevent instantiating module class.
*/ */
private Serializer() { } private Serializer() {
}
/** /**
* Serialize the object o (and any Serializable objects it refers to) and * Serialize the object o (and any Serializable objects it refers to) and
* store its serialized state in File f. If serializeInMem is true, the object * store its serialized state in File f. If serializeInMem is true, the
* is wrapped in a SerializedObject first, which seems to be more efficient than * object is wrapped in a SerializedObject first, which seems to be more
* writing a nested object directly to a file. * efficient than writing a nested object directly to a file.
* *
* @param o the object to write * @param o the object to write
* @param outStream The stream to write to * @param outStream The stream to write to
* @param serializeInMem flag whether to wrap the object in a SerializedObject * @param serializeInMem flag whether to wrap the object in a
* SerializedObject
* @throws IOException * @throws IOException
**/ *
*/
private static void store(Serializable o, OutputStream outStream, boolean serializeInMem) throws IOException { private static void store(Serializable o, OutputStream outStream, boolean serializeInMem) throws IOException {
ObjectOutputStream out = new ObjectOutputStream(outStream); ObjectOutputStream out = new ObjectOutputStream(outStream);
try { try {
@ -49,14 +54,15 @@ public class Serializer {
} }
/** /**
* Deserialize the contents of File f and return the resulting object. * Deserialize the contents of File f and return the resulting object. A
* A SerializedObject is unwrapped once. * SerializedObject is unwrapped once.
* *
* @param inputStream The Input stream to read from * @param inputStream The Input stream to read from
* @throws ClassNotFoundException * @throws ClassNotFoundException
* @throws IOException * @throws IOException
* @return The deserialized Object from the file * @return The deserialized Object from the file
**/ *
*/
private static Object load(final InputStream inputStream) throws IOException, ClassNotFoundException { private static Object load(final InputStream inputStream) throws IOException, ClassNotFoundException {
ObjectInputStream objInputStream = new ObjectInputStream(inputStream); ObjectInputStream objInputStream = new ObjectInputStream(inputStream);
Object ret = objInputStream.readObject(); Object ret = objInputStream.readObject();
@ -68,13 +74,14 @@ public class Serializer {
} }
/** /**
* Use object serialization to make a "deep clone" of the object o. * Use object serialization to make a "deep clone" of the object o. This
* This method serializes o and all of its member objects, and then * method serializes o and all of its member objects, and then deserializes
* deserializes that graph of objects, which means that everything is * that graph of objects, which means that everything is copied. This
* copied. This differs from the clone() method of an object which is * differs from the clone() method of an object which is usually implemented
* usually implemented to produce a "shallow" clone that copies references * to produce a "shallow" clone that copies references to other objects,
* to other objects, instead of copying all referenced objects. * instead of copying all referenced objects.
**/ *
*/
public static Object deepClone(Object o) { public static Object deepClone(Object o) {
Object obj = null; Object obj = null;
try { try {
@ -103,7 +110,8 @@ public class Serializer {
* *
* @param outStream The output stream * @param outStream The output stream
* @param data The string data * @param data The string data
**/ *
*/
public static void storeString(final OutputStream outStream, final String data) { public static void storeString(final OutputStream outStream, final String data) {
try { try {
outStream.write(data.getBytes()); outStream.write(data.getBytes());
@ -113,12 +121,13 @@ public class Serializer {
} }
/** /**
* Deserialize the contents of the InputStream containing * Deserialize the contents of the InputStream containing a string and
* a string and return the resulting string. * return the resulting string.
* *
* @param inputStream The input stream to read from * @param inputStream The input stream to read from
* @return The deserialized data from the stream * @return The deserialized data from the stream
**/ *
*/
public static String loadString(final InputStream inputStream) { public static String loadString(final InputStream inputStream) {
StringBuilder sBuilder = new StringBuilder(); StringBuilder sBuilder = new StringBuilder();
try { try {
@ -134,9 +143,9 @@ public class Serializer {
} }
/** /**
* Serialize the string s and * Serialize the string s and store its serialized state in File with name
* store its serialized state in File with name Filename. * Filename.
**/ */
public static void storeObject(OutputStream outStream, Serializable s) { public static void storeObject(OutputStream outStream, Serializable s) {
try { try {
store(s, outStream, true); store(s, outStream, true);
@ -146,22 +155,24 @@ public class Serializer {
} }
/** /**
* Deserialize the contents of File with given name containing * Deserialize the contents of File with given name containing a string and
* a string and return the resulting string. If the indicated file * return the resulting string. If the indicated file doesn't exist or an
* doesnt exist or an error occurs, null is returned. * error occurs, null is returned.
**/ *
public static Object loadObject (InputStream inputStream) { */
public static Object loadObject(InputStream inputStream) {
return loadObject(inputStream, true); return loadObject(inputStream, true);
} }
/** /**
* Deserialize the contents of File with given name containing * Deserialize the contents of File with given name containing a string and
* a string and return the resulting string. If the indicated file * return the resulting string. If the indicated file doesn't exist or an
* doesnt exist or an error occurs, null is returned. * error occurs, null is returned. If casually is false, an error message is
* If casually is false, an error message is printed and an exception * printed and an exception is raised if the file was not found or an error
* is raised if the file was not found or an error occured on loading. * occured on loading.
**/ *
public static Object loadObject (InputStream inputStream, boolean casually) { */
public static Object loadObject(InputStream inputStream, boolean casually) {
Object serializedObject = null; Object serializedObject = null;
try { try {