/** * checks whether the given class contains a serialVersionUID. * * @param c the class to check * @return true if the class contains a serialVersionUID, otherwise false * (also if the class is not implementing serializable) */ public static boolean hasUID(Class<?> c) { boolean result; result = false; if (isSerializable(c)) { try { c.getDeclaredField(SERIAL_VERSION_UID); result = true; } catch (Exception e) { result = false; } } return result; }
/** * checks whether the given class contains a serialVersionUID. * * @param c the class to check * @return true if the class contains a serialVersionUID, otherwise false * (also if the class is not implementing serializable) */ public static boolean hasUID(Class<?> c) { boolean result; result = false; if (isSerializable(c)) { try { c.getDeclaredField(SERIAL_VERSION_UID); result = true; } catch (Exception e) { result = false; } } return result; }
/** * checks whether a class is serializable. * * @param classname the class to check * @return true if the class or one of its ancestors implements the * Serializable interface, otherwise false (also if the class cannot * be loaded) */ public static boolean isSerializable(String classname) { boolean result; try { // result = isSerializable(Class.forName(classname)); result = isSerializable(WekaPackageClassLoaderManager.forName(classname)); } catch (Exception e) { result = false; } return result; }
/** * checks whether a class needs to declare a serialVersionUID, i.e., it * implements the java.io.Serializable interface but doesn't declare a * serialVersionUID. * * @param c the class to check * @return true if the class needs to declare one, false otherwise */ public static boolean needsUID(Class<?> c) { boolean result; if (isSerializable(c)) { result = !hasUID(c); } else { result = false; } return result; }
/** * checks whether a class needs to declare a serialVersionUID, i.e., it * implements the java.io.Serializable interface but doesn't declare a * serialVersionUID. * * @param c the class to check * @return true if the class needs to declare one, false otherwise */ public static boolean needsUID(Class<?> c) { boolean result; if (isSerializable(c)) { result = !hasUID(c); } else { result = false; } return result; }
/** * checks whether a class is serializable. * * @param classname the class to check * @return true if the class or one of its ancestors implements the * Serializable interface, otherwise false (also if the class cannot * be loaded) */ public static boolean isSerializable(String classname) { boolean result; try { // result = isSerializable(Class.forName(classname)); result = isSerializable(WekaPackageClassLoaderManager.forName(classname)); } catch (Exception e) { result = false; } return result; }
/** * writes the serialized object to the speicified file * * @param filename the file to serialize the object to * @param o the object to serialize * @return true if writing was successful */ public static boolean saveToFile(String filename, Object o) { boolean result; if (SerializationHelper.isSerializable(o.getClass())) { try { SerializationHelper.write(filename, o); result = true; } catch (Exception e) { result = false; } } else { result = false; } return result; }
/** * writes the serialized object to the speicified file * * @param filename the file to serialize the object to * @param o the object to serialize * @return true if writing was successful */ public static boolean saveToFile(String filename, Object o) { boolean result; if (SerializationHelper.isSerializable(o.getClass())) { try { SerializationHelper.write(filename, o); result = true; } catch (Exception e) { result = false; } } else { result = false; } return result; }
/** * Outputs information about a class on the commandline, takes class name as * arguments. * * @param args the classnames to check * @throws Exception if something goes wrong */ public static void main(String[] args) throws Exception { if (args.length == 0) { System.out.println("\nUsage: " + SerializationHelper.class.getName() + " classname [classname [classname [...]]]\n"); System.exit(1); } // check all the classes System.out.println(); for (String arg : args) { System.out.println(arg); System.out.println("- is serializable: " + isSerializable(arg)); System.out.println("- has " + SERIAL_VERSION_UID + ": " + hasUID(arg)); System.out .println("- needs " + SERIAL_VERSION_UID + ": " + needsUID(arg)); System.out.println("- " + SERIAL_VERSION_UID + ": private static final long serialVersionUID = " + getUID(arg) + "L;"); System.out.println(); } } }
/** * Outputs information about a class on the commandline, takes class name as * arguments. * * @param args the classnames to check * @throws Exception if something goes wrong */ public static void main(String[] args) throws Exception { if (args.length == 0) { System.out.println("\nUsage: " + SerializationHelper.class.getName() + " classname [classname [classname [...]]]\n"); System.exit(1); } // check all the classes System.out.println(); for (String arg : args) { System.out.println(arg); System.out.println("- is serializable: " + isSerializable(arg)); System.out.println("- has " + SERIAL_VERSION_UID + ": " + hasUID(arg)); System.out .println("- needs " + SERIAL_VERSION_UID + ": " + needsUID(arg)); System.out.println("- " + SERIAL_VERSION_UID + ": private static final long serialVersionUID = " + getUID(arg) + "L;"); System.out.println(); } } }