📄 propertiesconverter.java
字号:
} } if (PropertiesConverter.warning) Debug.signal(Debug.WARNING, clazz, "\"" + name + "\" not found as a field"); return true; } /** * Recursively parse the properties of an object to save. * @param object the object to save (may be null for class testing). * @param objectClass the class of object to save. * @param properties the object properties to save. * @param prefix the prefix for saving the property. * @return the number of saved properties. **/ private static int toProperties(Object object, Class objectClass, Properties properties, String prefix) { PropertyDescriptor[] descriptors; // Properties of the object to save int nbSaved; // Number of saved properties // Get all the getters of the object. descriptors = getProperties(objectClass); nbSaved = 0; // Save the class of the object properties.setProperty(prefix + "class", objectClass.getName()); if (descriptors != null) { // Among the descriptors, get the one which have a read method // and a write method (it is useless to save if we will not be able // to restore). Method readMethod; for (int i = 0; i < descriptors.length; i++) { if (DEBUG) System.out.println("Candidate property:" + prefix + descriptors[i].getName() + " in class " + objectClass.getName()); if (((readMethod = descriptors[i].getReadMethod()) != null) && (descriptors[i].getWriteMethod() != null)) { // Class property or // Getter and setter are present: check that the property is // not transient if (!isTransient(objectClass, descriptors[i].getName())) { // Not transient: save it if (DEBUG) System.out.println("Storable property:" + prefix + descriptors[i].getName()); nbSaved += saveProperty(object, properties, prefix, descriptors[i], readMethod); } else if (DEBUG) { System.out.println("Property:" + prefix + descriptors[i].getName() + " is transient"); } } else if (DEBUG) { if (readMethod == null) System.out.println("Property:" + prefix + descriptors[i].getName() + " has no read method"); else System.out.println("Property:" + prefix + descriptors[i].getName() + " has no write method"); } } } return nbSaved; } /** * Save a single property of an object. * @param object the object to save (may be null for class testing). * @param properties the object properties to save. * @param prefix the prefix for saving the property. * @param descriptor the property descriptor. * @param readMethod the method for getting the value. * @return the number of saved properties. **/ private static int saveProperty(Object object, Properties properties, String prefix, PropertyDescriptor descriptor, Method readMethod) { Object value; // The value of the object property int nbSaved; // Number of saved properties String propertyName; // Name of the property to save nbSaved = 0; propertyName = prefix + descriptor.getName(); try { if (descriptor instanceof IndexedPropertyDescriptor) { // This is an indexed property: save until array index out of bound int nbCells; nbCells = 0; try { Integer[] arg; arg = new Integer[0]; for (nbCells = 0; true; nbCells++) { if (object == null) { // Test mode (using the class): consider one cell only if (nbCells == 0) value = null; else break; // Only first cell in test mode } else { // Real mode (using the object) arg[0] = new Integer(nbCells); value = readMethod.invoke(object, arg); } // Save the value according to its class nbSaved += saveValue(properties, propertyName + "." + nbCells, value, descriptor.getPropertyType().getComponentType()); } } catch (IndexOutOfBoundsException ex) { // We have reached the end of the indexed properties values } catch (InvocationTargetException ex) { // We suppose that the exception warn of the end of the array, // but we log it anyway if (PropertiesConverter.warning) Debug.signal(Debug.WARNING, object, ex); } // Save the number of values nbSaved += saveValue(properties, propertyName, new Integer(nbCells), (object == null) ? Integer.TYPE : null); } else { // Non-indexed method: save the value according to its class if (object == null) // Test mode (using the class) value = null; else value = readMethod.invoke(object, null); if ((value != null) && (value.getClass().isArray() || readMethod.getReturnType().isArray())) { Object[] values; // Non-indexed, but returns an array: save individual values values = (Object[]) value; // Save the number of values nbSaved += saveValue(properties, propertyName, new Integer(values.length), (object == null) ? Integer.TYPE : null); for (int i = 0; i < values.length; i++) { // Save the value according to its class nbSaved += saveValue(properties, propertyName + "." + i, values[i], (object == null) ? descriptor.getPropertyType().getComponentType() : null); } } else { // Single value nbSaved += saveValue(properties, propertyName, value, (object == null) ? descriptor.getPropertyType() : null); } } } catch (InvocationTargetException ex) { // The read method should be callable Debug.signal(Debug.WARNING, object, ex); } catch (IllegalAccessException ex) { // The read method should be public Debug.signal(Debug.WARNING, object, ex); } catch (IllegalArgumentException ex) { // The read method should be callable with no arguments Debug.signal(Debug.WARNING, object, ex); } return nbSaved; } /** * Save a single value of an object. * @param properties the object properties to save. * @param propertyName the name for saving the property. * @param value the value of the property to save. * @param clazz optional. When present, the class of value (test mode, * value not significant). * @return the number of saved properties. **/ private static int saveValue(Properties properties, String propertyName, Object value, Class clazz) { if (clazz == null) // Actual mode return saveActualValue(properties, propertyName, value); else // Test mode (class without object instance) return saveTestValue(properties, propertyName, clazz); } /** * Save a single value of an object. * @param properties the object properties to save. * @param propertyName the name for saving the property. * @param value the value of the property to save. * @return the number of saved properties. **/ private static int saveActualValue(Properties properties, String propertyName, Object value) { int nbSaved; // Number of saved values nbSaved = 0; if (value != null) { Class valueClass; valueClass = value.getClass(); if (DEBUG) System.out.println("Candidate value:" + propertyName + " (" + valueClass.toString() + ")"); if ((valueClass.isPrimitive()) || (value instanceof Boolean) || (value instanceof Character) || (value instanceof Byte) || (value instanceof Integer) || (value instanceof Short) || (value instanceof Long) || (value instanceof Float) || (value instanceof Double) || (value instanceof StringBuffer)) { // Primitive or string buffer type: // directly register the value as a string properties.setProperty(propertyName, value.toString()); nbSaved = 1; } else if (value instanceof String) { properties.setProperty(propertyName, (String) value); nbSaved = 1; } else // Complex object: recursively add its properties nbSaved = toProperties(value, value.getClass(), properties, propertyName + "."); } return nbSaved; } /** * Save an arbitrary value of an object for testing. * @param properties the object properties to save. * @param propertyName the name for saving the property. * @param clazz The class of value (test mode). * @return the number of saved properties. **/ private static int saveTestValue(Properties properties, String propertyName, Class clazz) { int nbSaved; // Number of saved values nbSaved = 0; String className; className = clazz.getName(); if (DEBUG) System.out.println("Candidate value:" + propertyName + " (" + className + ")"); if (clazz.isPrimitive() || (className.equals("java.lang.Boolean")) || (className.equals("java.lang.Character")) || (className.equals("java.lang.Byte")) || (className.equals("java.lang.Integer")) || (className.equals("java.lang.Short")) || (className.equals("java.lang.Long")) || (className.equals("java.lang.Float")) || (className.equals("java.lang.Double")) || (className.equals("java.lang.StringBuffer")) || (className.equals("java.lang.String"))) { properties.setProperty(propertyName, "value(" + className + ")"); nbSaved = 1; } else // Complex object: recursively add its properties nbSaved = toProperties(null, clazz, properties, propertyName + "."); return nbSaved; } /** * Recursively parse the properties of an object to restore. * @param properties the object properties to restore. * @param prefix the prefix for restoring the property. * @return the object build from properties. * @exception PersistenceException when a problem occurs during loading. **/ private static Object fromProperties(Properties properties, String prefix) throws PersistenceException { PropertyDescriptor[] descriptors; // Properties of the object to restore String className; // Object class Class objectClass; // Class of object Object object; // Restored object // Build the object and gets its properties. className = properties.getProperty(prefix + "class"); if (className == null) return null; try { objectClass = Class.forName(className); } catch (ClassNotFoundException ex) { Debug.signal(Debug.ERROR, className, ex); throw new PersistenceException(ex); } try { object = objectClass.newInstance(); } catch (Exception ex) { Debug.signal(Debug.ERROR, objectClass, ex); throw new PersistenceException(ex); } descriptors = getProperties(objectClass); if (descriptors != null) { // Among the descriptors, get the one which have a read method // and a write method. Method writeMethod;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -