This patch refines some code in beans module, move some common properties and methods into o.a.h.b.BeansUtils class. It's a non-functional patch. git-svn-id: https://svn.apache.org/repos/asf/harmony/enhanced/java/trunk@1001187 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java index 3120e01..6b3b06d 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java
@@ -19,13 +19,14 @@ import java.awt.Color; +import org.apache.harmony.beans.BeansUtils; + class AwtColorPersistenceDelegate extends DefaultPersistenceDelegate { @Override - @SuppressWarnings("boxing") protected Expression instantiate(Object oldInstance, Encoder enc) { - Color color = (Color) oldInstance; - return new Expression(oldInstance, oldInstance.getClass(), - Statement.CONSTRUCTOR_NAME, new Object[] { color.getRed(), - color.getGreen(), color.getBlue(), color.getAlpha() }); - } + Color color = (Color) oldInstance; + return new Expression(oldInstance, oldInstance.getClass(), + BeansUtils.NEW, new Object[] { color.getRed(), + color.getGreen(), color.getBlue(), color.getAlpha() }); + } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtCursorPersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtCursorPersistenceDelegate.java index 9fcfa33..8b46cad 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtCursorPersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtCursorPersistenceDelegate.java
@@ -19,12 +19,13 @@ import java.awt.Cursor; +import org.apache.harmony.beans.BeansUtils; + class AwtCursorPersistenceDelegate extends DefaultPersistenceDelegate { @Override - @SuppressWarnings("boxing") protected Expression instantiate(Object oldInstance, Encoder enc) { - Cursor cursor = (Cursor) oldInstance; - return new Expression(oldInstance, oldInstance.getClass(), - Statement.CONSTRUCTOR_NAME, new Object[] { cursor.getType() }); - } + Cursor cursor = (Cursor) oldInstance; + return new Expression(oldInstance, oldInstance.getClass(), + BeansUtils.NEW, new Object[] { cursor.getType() }); + } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtDimensionPersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtDimensionPersistenceDelegate.java index edb9827..e3b007d 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtDimensionPersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtDimensionPersistenceDelegate.java
@@ -19,14 +19,13 @@ import java.awt.Dimension; -class AwtDimensionPersistenceDelegate extends DefaultPersistenceDelegate { - - @Override - @SuppressWarnings("boxing") - protected Expression instantiate(Object oldInstance, Encoder enc) { - Dimension dimension = (Dimension) oldInstance; +import org.apache.harmony.beans.BeansUtils; - return new Expression(dimension, dimension.getClass(), Statement.CONSTRUCTOR_NAME, +class AwtDimensionPersistenceDelegate extends DefaultPersistenceDelegate { + @Override + protected Expression instantiate(Object oldInstance, Encoder enc) { + Dimension dimension = (Dimension) oldInstance; + return new Expression(dimension, dimension.getClass(), BeansUtils.NEW, new Object[] { dimension.width, dimension.height }); } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtFontPersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtFontPersistenceDelegate.java index 57087e8..5428c3d 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtFontPersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtFontPersistenceDelegate.java
@@ -19,13 +19,14 @@ import java.awt.Font; +import org.apache.harmony.beans.BeansUtils; + class AwtFontPersistenceDelegate extends DefaultPersistenceDelegate { @Override - @SuppressWarnings("boxing") protected Expression instantiate(Object oldInstance, Encoder enc) { - Font font = (Font) oldInstance; - return new Expression(oldInstance, oldInstance.getClass(), - Statement.CONSTRUCTOR_NAME, new Object[] { font.getFontName(), - font.getStyle(), font.getSize() }); - } + Font font = (Font) oldInstance; + return new Expression(oldInstance, oldInstance.getClass(), + BeansUtils.NEW, new Object[] { font.getFontName(), + font.getStyle(), font.getSize() }); + } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtInsetsPersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtInsetsPersistenceDelegate.java index 5d03ba5..e0f42dc 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtInsetsPersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtInsetsPersistenceDelegate.java
@@ -19,13 +19,14 @@ import java.awt.Insets; +import org.apache.harmony.beans.BeansUtils; + class AwtInsetsPersistenceDelegate extends DefaultPersistenceDelegate { @Override - @SuppressWarnings("boxing") protected Expression instantiate(Object oldInstance, Encoder enc) { - Insets inset = (Insets) oldInstance; - return new Expression(oldInstance, oldInstance.getClass(), - Statement.CONSTRUCTOR_NAME, new Object[] { inset.top, inset.left, inset.bottom, - inset.right, }); - } + Insets inset = (Insets) oldInstance; + return new Expression(oldInstance, oldInstance.getClass(), + BeansUtils.NEW, new Object[] { inset.top, inset.left, + inset.bottom, inset.right, }); + } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtMenuShortcutPersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtMenuShortcutPersistenceDelegate.java index 731ca3e..da946bf 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtMenuShortcutPersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtMenuShortcutPersistenceDelegate.java
@@ -19,16 +19,15 @@ import java.awt.MenuShortcut; +import org.apache.harmony.beans.BeansUtils; + class AwtMenuShortcutPersistenceDelegate extends PersistenceDelegate { - @Override - @SuppressWarnings("boxing") - protected Expression instantiate(Object oldInstance, Encoder enc) { - MenuShortcut shortcut = (MenuShortcut) oldInstance; - int keyCode = shortcut.getKey(); - boolean useShiftModifier = shortcut.usesShiftModifier(); - return new Expression(shortcut, shortcut.getClass(), - Statement.CONSTRUCTOR_NAME, new Object[] {keyCode, useShiftModifier}); - } - + protected Expression instantiate(Object oldInstance, Encoder enc) { + MenuShortcut shortcut = (MenuShortcut) oldInstance; + int keyCode = shortcut.getKey(); + boolean useShiftModifier = shortcut.usesShiftModifier(); + return new Expression(shortcut, shortcut.getClass(), BeansUtils.NEW, + new Object[] { keyCode, useShiftModifier }); + } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtPointPersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtPointPersistenceDelegate.java index b6b8621..34cc94b 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtPointPersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtPointPersistenceDelegate.java
@@ -14,16 +14,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package java.beans; import java.awt.Point; +import org.apache.harmony.beans.BeansUtils; + class AwtPointPersistenceDelegate extends DefaultPersistenceDelegate { @Override - @SuppressWarnings("boxing") protected Expression instantiate(Object oldInstance, Encoder enc) { - Point point = (Point) oldInstance; - return new Expression(oldInstance, oldInstance.getClass(), - Statement.CONSTRUCTOR_NAME, new Object[] { point.x, point.y }); - } + Point point = (Point) oldInstance; + return new Expression(oldInstance, oldInstance.getClass(), + BeansUtils.NEW, new Object[] { point.x, point.y }); + } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java index 359d036..ff0a886 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java
@@ -19,25 +19,25 @@ import java.awt.Rectangle; +import org.apache.harmony.beans.BeansUtils; + class AwtRectanglePersistenceDelegate extends DefaultPersistenceDelegate { - @Override + @Override protected boolean mutatesTo(Object o1, Object o2) { - return o1.equals(o2); - } + return o1.equals(o2); + } - @Override + @Override protected void initialize(Class<?> type, Object oldInstance, - Object newInstance, Encoder enc) { - return; - } - - @Override - @SuppressWarnings("boxing") - protected Expression instantiate(Object oldInstance, Encoder enc) { - Rectangle rect = (Rectangle) oldInstance; + Object newInstance, Encoder enc) { + return; + } - return new Expression(rect, rect.getClass(), Statement.CONSTRUCTOR_NAME, + @Override + protected Expression instantiate(Object oldInstance, Encoder enc) { + Rectangle rect = (Rectangle) oldInstance; + return new Expression(rect, rect.getClass(), BeansUtils.NEW, new Object[] { rect.x, rect.y, rect.width, rect.height }); } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/AwtScrollPanePersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/AwtScrollPanePersistenceDelegate.java index 289d28c..b9df058 100644 --- a/classlib/modules/beans/src/main/java/java/beans/AwtScrollPanePersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/AwtScrollPanePersistenceDelegate.java
@@ -18,14 +18,14 @@ import java.awt.ScrollPane; -class AwtScrollPanePersistenceDelegate extends - DefaultPersistenceDelegate { - @Override - @SuppressWarnings("boxing") +import org.apache.harmony.beans.BeansUtils; + +class AwtScrollPanePersistenceDelegate extends DefaultPersistenceDelegate { + @Override protected Expression instantiate(Object oldInstance, Encoder enc) { - return new Expression(oldInstance, oldInstance.getClass(), - Statement.CONSTRUCTOR_NAME, - new Object[] { ((ScrollPane) oldInstance) - .getScrollbarDisplayPolicy() }); - } + return new Expression(oldInstance, oldInstance.getClass(), + BeansUtils.NEW, + new Object[] { ((ScrollPane) oldInstance) + .getScrollbarDisplayPolicy() }); + } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java index 4b9a4be..d066ef4 100644 --- a/classlib/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java
@@ -23,6 +23,8 @@ import java.security.PrivilegedAction; import java.util.HashMap; +import org.apache.harmony.beans.BeansUtils; + /** * Default PersistenceDelegate for normal classes. The instances of this class * are used when other customized PersistenceDelegate is not set in the encoders @@ -186,13 +188,14 @@ /* * Get the value for the specified property of the given bean instance. */ - private Object getPropertyValue(HashMap<String, PropertyDescriptor> proDscMap, Object oldInstance, + private Object getPropertyValue( + HashMap<String, PropertyDescriptor> proDscMap, Object oldInstance, String propName) throws Exception { // Try to get the read method for the property Method getter = null; if (null != proDscMap) { - PropertyDescriptor pd = proDscMap - .get(Introspector.decapitalize(propName)); + PropertyDescriptor pd = proDscMap.get(Introspector + .decapitalize(propName)); if (null != pd) { getter = pd.getReadMethod(); } @@ -212,7 +215,6 @@ "The getter method for the property " //$NON-NLS-1$ + propName + " can't be found."); //$NON-NLS-1$ } - } /** @@ -264,7 +266,7 @@ } return new Expression(oldInstance, oldInstance.getClass(), - Statement.CONSTRUCTOR_NAME, args); + BeansUtils.NEW, args); } private static HashMap<String, PropertyDescriptor> internalAsMap( @@ -280,9 +282,9 @@ * Determines whether one object mutates to the other object. If this * <code>DefaultPersistenceDelegate</code> is constructed with one or more * property names, and the class of <code>o1</code> overrides the - * "equals(Object)" method, then <code>o2</code> is considered to mutate - * to <code>o1</code> if <code>o1</code> equals to <code>o2</code>. - * Otherwise, the result is the same as the definition in + * "equals(Object)" method, then <code>o2</code> is considered to mutate to + * <code>o1</code> if <code>o1</code> equals to <code>o2</code>. Otherwise, + * the result is the same as the definition in * <code>PersistenceDelegate</code>. * * @param o1 @@ -295,17 +297,7 @@ @Override protected boolean mutatesTo(Object o1, Object o2) { if (this.propertyNames.length > 0) { - Class<?> clazz = o1.getClass(); - // Check the "equals" method has been declared - Method equalMethod = null; - try { - equalMethod = clazz.getDeclaredMethod("equals", //$NON-NLS-1$ - new Class[] { Object.class }); - } catch (NoSuchMethodException e) { - // Ignored - } - - if (null != equalMethod) { + if (BeansUtils.declaredEquals(o1.getClass())) { return o1.equals(o2); } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/Expression.java b/classlib/modules/beans/src/main/java/java/beans/Expression.java index 450f22e..33aa543 100644 --- a/classlib/modules/beans/src/main/java/java/beans/Expression.java +++ b/classlib/modules/beans/src/main/java/java/beans/Expression.java
@@ -17,7 +17,7 @@ package java.beans; -import org.apache.harmony.beans.internal.nls.Messages; +import org.apache.harmony.beans.BeansUtils; public class Expression extends Statement { @@ -40,25 +40,21 @@ @Override public String toString() { - try { - StringBuilder sb = new StringBuilder(); - - if (!valueIsDefined) { - sb.append("<unbound>"); //$NON-NLS-1$ + StringBuilder sb = new StringBuilder(); + if (!valueIsDefined) { + sb.append("<unbound>"); //$NON-NLS-1$ + } else { + if (value == null) { + sb.append(BeansUtils.NULL); } else { - if (value == null) { - sb.append("null"); //$NON-NLS-1$ - } else { - sb.append(convertClassName(value.getClass())); - } + Class<?> clazz = value.getClass(); + sb.append(clazz == String.class ? BeansUtils.QUOTE : BeansUtils + .idOfClass(clazz)); } - sb.append('='); - sb.append(super.toString()); - - return sb.toString(); - } catch (Exception e) { - return new String(Messages.getString("beans.0D", e.getClass())); //$NON-NLS-1$ } + sb.append('='); + sb.append(super.toString()); + return sb.toString(); } public void setValue(Object value) { @@ -73,4 +69,4 @@ } return value; } -} +} \ No newline at end of file
diff --git a/classlib/modules/beans/src/main/java/java/beans/Statement.java b/classlib/modules/beans/src/main/java/java/beans/Statement.java index 3d7d2ff..ac29ab8 100644 --- a/classlib/modules/beans/src/main/java/java/beans/Statement.java +++ b/classlib/modules/beans/src/main/java/java/beans/Statement.java
@@ -31,63 +31,50 @@ import java.util.Map; import java.util.WeakHashMap; +import org.apache.harmony.beans.BeansUtils; import org.apache.harmony.beans.internal.nls.Messages; public class Statement { - private static final Object[] EMPTY_ARRAY = new Object[0]; private Object target; private String methodName; private Object[] arguments; - + // cache used methods of specified target class to accelerate method search - private static WeakHashMap<Class<?>, Method[]> cache = new WeakHashMap<Class<?>, Method[]>(); - - // the special method name donating constructors - static final String CONSTRUCTOR_NAME = "new"; //$NON-NLS-1$ - - // the special method name donating array "get" - static final String ARRAY_GET = "get"; //$NON-NLS-1$ - - // the special method name donating array "set" - static final String ARRAY_SET = "set"; //$NON-NLS-1$ + private static WeakHashMap<Class<?>, Method[]> classMethodsCache = new WeakHashMap<Class<?>, Method[]>(); public Statement(Object target, String methodName, Object[] arguments) { this.target = target; this.methodName = methodName; - if (arguments != null) { - this.arguments = arguments; - } else { - this.arguments = EMPTY_ARRAY; - } + this.arguments = arguments == null ? BeansUtils.EMPTY_OBJECT_ARRAY + : arguments; } @Override public String toString() { StringBuilder sb = new StringBuilder(); - Object theTarget = getTarget(); - String theMethodName = getMethodName(); - Object[] theArguments = getArguments(); - String targetVar = theTarget != null ? convertClassName(theTarget.getClass()) : "null"; //$NON-NLS-1$ - sb.append(targetVar); - sb.append('.'); - sb.append(theMethodName); - sb.append('('); - if (theArguments != null) { - for (int i = 0; i < theArguments.length; ++i) { - if (i > 0) { + if (target == null) { + sb.append(BeansUtils.NULL); + } else { + Class<?> clazz = target.getClass(); + sb.append(clazz == String.class ? BeansUtils.QUOTE : BeansUtils + .idOfClass(clazz)); + } + sb.append('.' + methodName + '('); + if (arguments != null) { + Class<?> clazz; + for (int index = 0; index < arguments.length; index++) { + if (index > 0) { sb.append(", "); //$NON-NLS-1$ } - if (theArguments[i] == null) { - sb.append("null"); //$NON-NLS-1$ - } else if (theArguments[i] instanceof String) { - sb.append('"'); - sb.append(theArguments[i].toString()); - sb.append('"'); + if (arguments[index] == null) { + sb.append(BeansUtils.NULL); } else { - sb.append(convertClassName(theArguments[i].getClass())); + clazz = arguments[index].getClass(); + sb.append(clazz == String.class ? '"' + (String) arguments[index] + '"' + : BeansUtils.idOfClass(clazz)); } } } @@ -115,120 +102,117 @@ Object invokeMethod() throws Exception { Object result = null; try { - Object theTarget = getTarget(); - String theMethodName = getMethodName(); - Object[] theArguments = getArguments(); - if (theTarget.getClass().isArray()) { - Method method = findArrayMethod(theMethodName, theArguments); - Object[] args = new Object[theArguments.length + 1]; - args[0] = theTarget; - System.arraycopy(theArguments, 0, args, 1, theArguments.length); - result = method.invoke(null, args); - } else if (theMethodName.equals("newInstance") //$NON-NLS-1$ - && theTarget == Array.class) { - Class<?> componentType = (Class<?>) theArguments[0]; - int length = ((Integer) theArguments[1]).intValue(); - result = Array.newInstance(componentType, length); - } else if (theMethodName.equals("new") //$NON-NLS-1$ - || theMethodName.equals("newInstance")) { //$NON-NLS-1$ - if (theTarget instanceof Class<?>) { - Constructor<?> constructor = findConstructor((Class<?>)theTarget, theArguments); - result = constructor.newInstance(theArguments); + Object target = getTarget(); + String methodName = getMethodName(); + Object[] arguments = getArguments(); + Class<?> targetClass = target.getClass(); + if (targetClass.isArray()) { + Method method = findArrayMethod(methodName, arguments); + Object[] copy = new Object[arguments.length + 1]; + copy[0] = target; + System.arraycopy(arguments, 0, copy, 1, arguments.length); + result = method.invoke(null, copy); + } else if (BeansUtils.NEWINSTANCE.equals(methodName) + && target == Array.class) { + result = Array.newInstance((Class<?>) arguments[0], + ((Integer) arguments[1]).intValue()); + } else if (BeansUtils.NEW.equals(methodName) + || BeansUtils.NEWINSTANCE.equals(methodName)) { + if (target instanceof Class<?>) { + Constructor<?> constructor = findConstructor( + (Class<?>) target, arguments); + result = constructor.newInstance(arguments); } else { - if ("new".equals(theMethodName)) { //$NON-NLS-1$ + if (BeansUtils.NEW.equals(methodName)) { throw new NoSuchMethodException(this.toString()); } // target class declares a public named "newInstance" method - Method method = findMethod(theTarget.getClass(), - theMethodName, theArguments, false); - result = method.invoke(theTarget, theArguments); + Method method = findMethod(targetClass, methodName, + arguments, false); + result = method.invoke(target, arguments); } - } else if (theMethodName.equals("newArray")) {//$NON-NLS-1$ + } else if (methodName.equals(BeansUtils.NEWARRAY)) { // create a new array instance without length attribute - int length = theArguments.length; - Class<?> clazz = (Class<?>) theTarget; + Class<?> clazz = (Class<?>) target, argClass; // check the element types of array - for (int i = 0; i < length; i++) { - boolean isNull = theArguments[i] == null; - boolean isPrimitiveWrapper = isNull ? false - : isPrimitiveWrapper(theArguments[i].getClass(), - clazz); - boolean isAssignable = isNull ? false : clazz - .isAssignableFrom(theArguments[i].getClass()); - if (!isNull && !isPrimitiveWrapper && !isAssignable) { - throw new IllegalArgumentException(Messages - .getString("beans.63")); //$NON-NLS-1$ + for (int index = 0; index < arguments.length; index++) { + argClass = arguments[index] == null ? null + : arguments[index].getClass(); + if (argClass != null && !clazz.isAssignableFrom(argClass) + && !BeansUtils.isPrimitiveWrapper(argClass, clazz)) { + throw new IllegalArgumentException( + Messages.getString("beans.63")); //$NON-NLS-1$ } } - result = Array.newInstance(clazz, length); + result = Array.newInstance(clazz, arguments.length); if (clazz.isPrimitive()) { // Copy element according to primitive types - arrayCopy(clazz, theArguments, result, length); + arrayCopy(clazz, arguments, result, arguments.length); } else { // Copy element of Objects - System.arraycopy(theArguments, 0, result, 0, length); + System.arraycopy(arguments, 0, result, 0, arguments.length); } return result; - } else if (theTarget instanceof Class<?>) { + } else if (target instanceof Class<?>) { Method method = null; - boolean found = false; try { /* * Try to look for a static method of class described by the * given Class object at first process only if the class * differs from Class itself */ - if (theTarget != Class.class) { - method = findMethod((Class<?>) theTarget, theMethodName, theArguments, true); - result = method.invoke(null, theArguments); - found = true; + if (target != Class.class) { + method = findMethod((Class<?>) target, methodName, + arguments, true); + result = method.invoke(null, arguments); } } catch (NoSuchMethodException e) { // expected } - if (!found) { + if (method == null) { // static method was not found // try to invoke method of Class object - if (theMethodName.equals("forName") //$NON-NLS-1$ - && theArguments.length == 1 && theArguments[0] instanceof String) { + if (BeansUtils.FORNAME.equals(methodName) + && arguments.length == 1 + && arguments[0] instanceof String) { // special handling of Class.forName(String) try { - result = Class.forName((String) theArguments[0]); + result = Class.forName((String) arguments[0]); } catch (ClassNotFoundException e2) { - result = Class.forName((String) theArguments[0], true, Thread - .currentThread().getContextClassLoader()); + result = Class.forName((String) arguments[0], true, + Thread.currentThread() + .getContextClassLoader()); } } else { - method = findMethod(theTarget.getClass(), theMethodName, theArguments, false); - result = method.invoke(theTarget, theArguments); + method = findMethod(targetClass, methodName, arguments, + false); + result = method.invoke(target, arguments); } } - } else if (theTarget instanceof Iterator<?>){ - final Iterator<?> iterator = (Iterator<?>) theTarget; - final Method method = findMethod(theTarget.getClass(), theMethodName, - theArguments, false); - if (iterator.hasNext()) { - PrivilegedAction<Object> action = new PrivilegedAction<Object>() { + } else if (target instanceof Iterator<?>) { + final Iterator<?> iterator = (Iterator<?>) target; + final Method method = findMethod(targetClass, methodName, + arguments, false); + if (iterator.hasNext()) { + result = new PrivilegedAction<Object>() { + public Object run() { + try { + method.setAccessible(true); + return (method.invoke(iterator, new Object[0])); + } catch (Exception e) { + // ignore + } + return null; + } - public Object run() { - try { - method.setAccessible(true); - return (method.invoke(iterator, new Object[0])); - } catch (Exception e) { - // ignore - } - return null; - } - - }; - result = action.run(); - } + }.run(); + } } else { - Method method = findMethod(theTarget.getClass(), theMethodName, - theArguments, false); + Method method = findMethod(targetClass, methodName, arguments, + false); method.setAccessible(true); - result = method.invoke(theTarget, theArguments); + result = method.invoke(target, arguments); } } catch (InvocationTargetException ite) { Throwable t = ite.getCause(); @@ -236,122 +220,109 @@ } return result; } - + private void arrayCopy(Class<?> type, Object[] src, Object dest, int length) { if (type == boolean.class) { boolean[] destination = (boolean[]) dest; - for (int i = 0; i < length; i++) { - destination[i] = ((Boolean) src[i]).booleanValue(); + for (int index = 0; index < length; index++) { + destination[index] = ((Boolean) src[index]).booleanValue(); } } else if (type == short.class) { short[] destination = (short[]) dest; - for (int i = 0; i < length; i++) { - destination[i] = ((Short) src[i]).shortValue(); + for (int index = 0; index < length; index++) { + destination[index] = ((Short) src[index]).shortValue(); } } else if (type == byte.class) { byte[] destination = (byte[]) dest; - for (int i = 0; i < length; i++) { - destination[i] = ((Byte) src[i]).byteValue(); + for (int index = 0; index < length; index++) { + destination[index] = ((Byte) src[index]).byteValue(); } } else if (type == char.class) { char[] destination = (char[]) dest; - for (int i = 0; i < length; i++) { - destination[i] = ((Character) src[i]).charValue(); + for (int index = 0; index < length; index++) { + destination[index] = ((Character) src[index]).charValue(); } } else if (type == int.class) { int[] destination = (int[]) dest; - for (int i = 0; i < length; i++) { - destination[i] = ((Integer) src[i]).intValue(); + for (int index = 0; index < length; index++) { + destination[index] = ((Integer) src[index]).intValue(); } } else if (type == long.class) { long[] destination = (long[]) dest; - for (int i = 0; i < length; i++) { - destination[i] = ((Long) src[i]).longValue(); + for (int index = 0; index < length; index++) { + destination[index] = ((Long) src[index]).longValue(); } } else if (type == float.class) { float[] destination = (float[]) dest; - for (int i = 0; i < length; i++) { - destination[i] = ((Float) src[i]).floatValue(); + for (int index = 0; index < length; index++) { + destination[index] = ((Float) src[index]).floatValue(); } } else if (type == double.class) { double[] destination = (double[]) dest; - for (int i = 0; i < length; i++) { - destination[i] = ((Double) src[i]).doubleValue(); + for (int index = 0; index < length; index++) { + destination[index] = ((Double) src[index]).doubleValue(); } } } - private Method findArrayMethod(String theMethodName, Object[] theArguments) throws NoSuchMethodException { + private Method findArrayMethod(String methodName, Object[] args) + throws NoSuchMethodException { // the code below reproduces exact RI exception throwing behavior - if (!theMethodName.equals("set") && !theMethodName.equals("get")) { //$NON-NLS-1$ //$NON-NLS-2$ + boolean isGet = BeansUtils.GET.equals(methodName); //$NON-NLS-1$ + boolean isSet = BeansUtils.SET.equals(methodName); //$NON-NLS-1$ + if (!isGet && !isSet) { throw new NoSuchMethodException(Messages.getString("beans.3C")); //$NON-NLS-1$ - } else if (theArguments.length > 0 && theArguments[0].getClass() != Integer.class) { + } else if (args.length > 0 && args[0].getClass() != Integer.class) { throw new ClassCastException(Messages.getString("beans.3D")); //$NON-NLS-1$ - } else if (theMethodName.equals("get") && (theArguments.length != 1)) { //$NON-NLS-1$ - throw new ArrayIndexOutOfBoundsException(Messages.getString("beans.3E")); //$NON-NLS-1$ - } else if (theMethodName.equals("set") && (theArguments.length != 2)) { //$NON-NLS-1$ - throw new ArrayIndexOutOfBoundsException(Messages.getString("beans.3F")); //$NON-NLS-1$ + } else if (isGet && args.length != 1) { + throw new ArrayIndexOutOfBoundsException( + Messages.getString("beans.3E")); //$NON-NLS-1$ + } else if (isSet && args.length != 2) { + throw new ArrayIndexOutOfBoundsException( + Messages.getString("beans.3F")); //$NON-NLS-1$ } - if (theMethodName.equals("get")) { //$NON-NLS-1$ - return Array.class.getMethod("get", new Class[] { Object.class, //$NON-NLS-1$ - int.class }); - } - return Array.class.getMethod("set", new Class[] { Object.class, //$NON-NLS-1$ - int.class, Object.class }); + + Class<?>[] paraTypes = isGet ? new Class<?>[] { Object.class, int.class } + : new Class<?>[] { Object.class, int.class, Object.class }; + return Array.class.getMethod(methodName, paraTypes); } - private Constructor<?> findConstructor(Class<?> targetClass, Object[] theArguments) throws NoSuchMethodException { - Class<?>[] argClasses = getClasses(theArguments); + private Constructor<?> findConstructor(Class<?> clazz, Object[] args) + throws NoSuchMethodException { + Class<?>[] argTypes = getTypes(args), paraTypes, resultParaTypes; Constructor<?> result = null; - Constructor<?>[] constructors = targetClass.getConstructors(); - for (Constructor<?> constructor : constructors) { - Class<?>[] parameterTypes = constructor.getParameterTypes(); - if (parameterTypes.length == argClasses.length) { - boolean found = true; - for (int j = 0; j < parameterTypes.length; ++j) { - boolean argIsNull = argClasses[j] == null; - boolean argIsPrimitiveWrapper = isPrimitiveWrapper(argClasses[j], - parameterTypes[j]); - boolean paramIsPrimitive = parameterTypes[j].isPrimitive(); - boolean paramIsAssignable = argIsNull ? false : parameterTypes[j] - .isAssignableFrom(argClasses[j]); - if (!argIsNull && !paramIsAssignable && !argIsPrimitiveWrapper || argIsNull - && paramIsPrimitive) { - found = false; + boolean isAssignable; + for (Constructor<?> constructor : clazz.getConstructors()) { + paraTypes = constructor.getParameterTypes(); + if (match(argTypes, paraTypes)) { + if (result == null) { + // first time, set constructor + result = constructor; + continue; + } + // find out more suitable constructor + resultParaTypes = result.getParameterTypes(); + isAssignable = true; + for (int index = 0; index < paraTypes.length; index++) { + if (argTypes[index] != null + && !(isAssignable &= resultParaTypes[index] + .isAssignableFrom(paraTypes[index]))) { + break; + } + if (argTypes[index] == null + && !(isAssignable &= paraTypes[index] + .isAssignableFrom(resultParaTypes[index]))) { break; } } - if (found) { - if (result == null) { - // first time, set constructor - result = constructor; - continue; - } - // find out more suitable constructor - Class<?>[] resultParameterTypes = result - .getParameterTypes(); - boolean isAssignable = true; - for (int j = 0; j < parameterTypes.length; ++j) { - if (theArguments[j] != null - && !(isAssignable &= resultParameterTypes[j] - .isAssignableFrom(parameterTypes[j]))) { - break; - } - if (theArguments[j] == null - && !(isAssignable &= parameterTypes[j] - .isAssignableFrom(resultParameterTypes[j]))) { - break; - } - } - if (isAssignable) { - result = constructor; - } + if (isAssignable) { + result = constructor; } } } if (result == null) { throw new NoSuchMethodException(Messages.getString( - "beans.40", targetClass.getName())); //$NON-NLS-1$ + "beans.40", clazz.getName())); //$NON-NLS-1$ } return result; } @@ -359,90 +330,91 @@ /** * Searches for best matching method for given name and argument types. */ - static Method findMethod(Class<?> targetClass, String methodName, Object[] arguments, - boolean methodIsStatic) throws NoSuchMethodException { - Class<?>[] argClasses = getClasses(arguments); + static Method findMethod(Class<?> clazz, String methodName, Object[] args, + boolean isStatic) throws NoSuchMethodException { + Class<?>[] argTypes = getTypes(args); + Method[] methods = null; - - if(cache.containsKey(targetClass)){ - methods = cache.get(targetClass); - }else{ - methods = targetClass.getMethods(); - cache.put(targetClass, methods); + if (classMethodsCache.containsKey(clazz)) { + methods = classMethodsCache.get(clazz); + } else { + methods = clazz.getMethods(); + classMethodsCache.put(clazz, methods); } - - ArrayList<Method> foundMethods = new ArrayList<Method>(); - Method[] foundMethodsArr; + + ArrayList<Method> fitMethods = new ArrayList<Method>(); for (Method method : methods) { - int mods = method.getModifiers(); - if (method.getName().equals(methodName) - && (methodIsStatic ? Modifier.isStatic(mods) : true)) { - Class<?>[] parameterTypes = method.getParameterTypes(); - if (parameterTypes.length == argClasses.length) { - boolean found = true; - for (int j = 0; j < parameterTypes.length; ++j) { - boolean argIsNull = (argClasses[j] == null); - boolean argIsPrimitiveWrapper = isPrimitiveWrapper(argClasses[j], - parameterTypes[j]); - boolean paramIsAssignable = argIsNull ? false : parameterTypes[j] - .isAssignableFrom(argClasses[j]); - if (!argIsNull && !paramIsAssignable && !argIsPrimitiveWrapper){ - found = false; - break; - } - } - if (found) { - foundMethods.add(method); + if (methodName.equals(method.getName())) { + if (!isStatic || Modifier.isStatic(method.getModifiers())) { + if (match(argTypes, method.getParameterTypes())) { + fitMethods.add(method); } } } } - if (foundMethods.size() == 0) { - throw new NoSuchMethodException(Messages.getString("beans.41", methodName)); //$NON-NLS-1$ + int fitSize = fitMethods.size(); + if (fitSize == 0) { + throw new NoSuchMethodException(Messages.getString( + "beans.41", methodName)); //$NON-NLS-1$ } - if(foundMethods.size() == 1){ - return foundMethods.get(0); + if (fitSize == 1) { + return fitMethods.get(0); } - foundMethodsArr = foundMethods.toArray(new Method[foundMethods.size()]); - //find the most relevant one - MethodComparator comparator = new MethodComparator(methodName, argClasses); - Method chosenOne = foundMethodsArr[0]; - for (int i = 1; i < foundMethodsArr.length; i++) { - int difference = comparator.compare(chosenOne, foundMethodsArr[i]); - //if 2 methods have same relevance, throw exception - if (difference == 0) { + // find the most relevant one + MethodComparator comparator = new MethodComparator(methodName, argTypes); + Method[] fitMethodArray = fitMethods.toArray(new Method[fitSize]); + Method onlyMethod = fitMethodArray[0]; + Class<?> onlyReturnType, fitReturnType; + int difference; + for (int i = 1; i < fitMethodArray.length; i++) { + // if 2 methods have same relevance, check their return type + if ((difference = comparator.compare(onlyMethod, fitMethodArray[i])) == 0) { // if 2 methods have the same signature, check their return type - Class<?> oneReturnType = chosenOne.getReturnType(); - Class<?> foundMethodReturnType = foundMethodsArr[i] - .getReturnType(); - if (oneReturnType.equals(foundMethodReturnType)) { - // if 2 methods have the same signature and return type, - // throw NoSuchMethodException + onlyReturnType = onlyMethod.getReturnType(); + fitReturnType = fitMethodArray[i].getReturnType(); + if (onlyReturnType == fitReturnType) { + // if 2 methods have the same relevance and return type throw new NoSuchMethodException(Messages.getString( "beans.62", methodName)); //$NON-NLS-1$ } - if (oneReturnType.isAssignableFrom(foundMethodReturnType)) { - // if chosenOne is super class or interface of - // foundMethodReturnType, set chosenOne to foundMethodArr[i] - chosenOne = foundMethodsArr[i]; + if (onlyReturnType.isAssignableFrom(fitReturnType)) { + // if onlyReturnType is super class or interface of + // fitReturnType, set onlyMethod to fitMethodArray[i] + onlyMethod = fitMethodArray[i]; } } - if(difference > 0){ - chosenOne = foundMethodsArr[i]; + if (difference > 0) { + onlyMethod = fitMethodArray[i]; } } - return chosenOne; + return onlyMethod; + } + + private static boolean match(Class<?>[] argTypes, Class<?>[] paraTypes) { + if (paraTypes.length != argTypes.length) { + return false; + } + for (int index = 0; index < paraTypes.length; index++) { + if (argTypes[index] != null + && !paraTypes[index].isAssignableFrom(argTypes[index]) + && !BeansUtils.isPrimitiveWrapper(argTypes[index], + paraTypes[index])) { + return false; + } + } + return true; } static boolean isStaticMethodCall(Statement stmt) { Object target = stmt.getTarget(); - String mName = stmt.getMethodName(); + String methodName = stmt.getMethodName(); if (!(target instanceof Class<?>)) { return false; } try { - Statement.findMethod((Class<?>) target, mName, stmt.getArguments(), true); + Statement.findMethod((Class<?>) target, methodName, + stmt.getArguments(), true); return true; } catch (NoSuchMethodException e) { return false; @@ -475,7 +447,8 @@ String methodName = stmt.getMethodName(); Object[] args = stmt.getArguments(); String[] sig = new String[pdConstructorSignatures[0].length]; - if (target == null || methodName == null || args == null || args.length == 0) { + if (target == null || methodName == null || args == null + || args.length == 0) { // not a constructor for sure return false; } @@ -483,7 +456,8 @@ sig[1] = methodName; for (int i = 2; i < sig.length; i++) { if (args.length > i - 2) { - sig[i] = args[i - 2] != null ? args[i - 2].getClass().getName() : "null"; //$NON-NLS-1$ + sig[i] = args[i - 2] != null ? args[i - 2].getClass().getName() + : "null"; //$NON-NLS-1$ } else { sig[i] = ""; //$NON-NLS-1$ } @@ -496,16 +470,6 @@ return false; } - private static boolean isPrimitiveWrapper(Class<?> wrapper, Class<?> base) { - return (base == boolean.class) && (wrapper == Boolean.class) || (base == byte.class) - && (wrapper == Byte.class) || (base == char.class) - && (wrapper == Character.class) || (base == short.class) - && (wrapper == Short.class) || (base == int.class) - && (wrapper == Integer.class) || (base == long.class) - && (wrapper == Long.class) || (base == float.class) && (wrapper == Float.class) - || (base == double.class) && (wrapper == Double.class); - } - private static Class<?> getPrimitiveWrapper(Class<?> base) { Class<?> res = null; if (base == boolean.class) { @@ -528,31 +492,13 @@ return res; } - static String convertClassName(Class<?> type) { - StringBuilder clazzNameSuffix = new StringBuilder(); - Class<?> componentType = null; - Class<?> clazzType = type; - while ((componentType = clazzType.getComponentType()) != null) { - clazzNameSuffix.append("Array"); //$NON-NLS-1$ - clazzType = componentType; + private static Class<?>[] getTypes(Object[] arguments) { + Class<?>[] types = new Class[arguments.length]; + for (int index = 0; index < arguments.length; ++index) { + types[index] = (arguments[index] == null) ? null : arguments[index] + .getClass(); } - String clazzName = clazzType.getName(); - int k = clazzName.lastIndexOf('.'); - if (k != -1 && k < clazzName.length()) { - clazzName = clazzName.substring(k + 1); - } - if (clazzNameSuffix.length() == 0 && "String".equals(clazzName)) { //$NON-NLS-1$ - return "\"\""; //$NON-NLS-1$ - } - return clazzName + clazzNameSuffix.toString(); - } - - private static Class<?>[] getClasses(Object[] arguments) { - Class<?>[] result = new Class[arguments.length]; - for (int i = 0; i < arguments.length; ++i) { - result[i] = (arguments[i] == null) ? null : arguments[i].getClass(); - } - return result; + return types; } /** @@ -568,7 +514,8 @@ private final Map<Method, Integer> cache; - public MethodComparator(String refMethodName, Class<?>[] refArgumentTypes) { + public MethodComparator(String refMethodName, + Class<?>[] refArgumentTypes) { this.referenceMethodName = refMethodName; this.referenceMethodArgumentTypes = refArgumentTypes; cache = new HashMap<Method, Integer>(); @@ -615,7 +562,8 @@ if (argumentTypes[i].isPrimitive()) { argumentTypes[i] = getPrimitiveWrapper(argumentTypes[i]); } - totalNorm += getDistance(referenceMethodArgumentTypes[i], argumentTypes[i]); + totalNorm += getDistance(referenceMethodArgumentTypes[i], + argumentTypes[i]); } return totalNorm; } @@ -659,4 +607,4 @@ return (superDist != INFINITY ? superDist + 2 : INFINITY); } } -} +} \ No newline at end of file
diff --git a/classlib/modules/beans/src/main/java/java/beans/UtilDatePersistenceDelegate.java b/classlib/modules/beans/src/main/java/java/beans/UtilDatePersistenceDelegate.java index ce43aef..605c0c5 100644 --- a/classlib/modules/beans/src/main/java/java/beans/UtilDatePersistenceDelegate.java +++ b/classlib/modules/beans/src/main/java/java/beans/UtilDatePersistenceDelegate.java
@@ -19,12 +19,13 @@ import java.util.Date; +import org.apache.harmony.beans.BeansUtils; + class UtilDatePersistenceDelegate extends DefaultPersistenceDelegate { @Override - @SuppressWarnings("boxing") protected Expression instantiate(Object oldInstance, Encoder enc) { Date date = (Date) oldInstance; return new Expression(oldInstance, oldInstance.getClass(), - Statement.CONSTRUCTOR_NAME, new Object[] { date.getTime() }); + BeansUtils.NEW, new Object[] { date.getTime() }); } }
diff --git a/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java b/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java index 9c65533..53d2c3b 100644 --- a/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java +++ b/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
@@ -30,11 +30,12 @@ import java.util.Iterator; import java.util.List; +import org.apache.harmony.beans.BeansUtils; import org.apache.harmony.beans.internal.nls.Messages; /** - * <code>XMLEncoder</code> extends <code>Encoder</code> to write out the - * encoded statements and expressions in xml format. The xml can be read by + * <code>XMLEncoder</code> extends <code>Encoder</code> to write out the encoded + * statements and expressions in XML format. The XML can be read by * <code>XMLDecoder</code> later to restore objects and their states. * <p> * The API is similar to <code>ObjectOutputStream</code>. @@ -46,67 +47,67 @@ private static int DEADLOCK_THRESHOLD = 7; /* - * Every object written by the encoder has a record. - */ - private static class Record { - // The expression by which the object is created or obtained. - Expression exp = null; + * Every object written by the encoder has a record. + */ + private static class Record { + // The expression by which the object is created or obtained. + Expression exp = null; - // Id of the object, if it is referenced more than once. - String id = null; + // Id of the object, if it is referenced more than once. + String id = null; - // Count of the references of the object. - int refCount = 0; + // Count of the references of the object. + int refCount = 0; - // A list of statements that execute on the object. - ArrayList<Statement> stats = new ArrayList<Statement>(); - } + // A list of statements that execute on the object. + ArrayList<Statement> stats = new ArrayList<Statement>(); + } - private static final int INDENT_UNIT = 1; + private static final int INDENT_UNIT = 1; - private static final boolean isStaticConstantsSupported = true; + private static final boolean isStaticConstantsSupported = true; - // the main record of all root objects - private ArrayList<Object> flushPending = new ArrayList<Object>(); + // the main record of all root objects + private ArrayList<Object> flushPending = new ArrayList<Object>(); - // the record of root objects with a void tag - private ArrayList<Object> flushPendingStat = new ArrayList<Object>(); + // the record of root objects with a void tag + private ArrayList<Object> flushPendingStat = new ArrayList<Object>(); - // keep the pre-required objects for each root object - private ArrayList<Object> flushPrePending = new ArrayList<Object>(); + // keep the pre-required objects for each root object + private ArrayList<Object> flushPrePending = new ArrayList<Object>(); - private boolean hasXmlHeader = false; + private boolean hasXmlHeader = false; - /* - * if any expression or statement references owner, it is set true in method - * recordStatement() or recordExpressions(), and, at the first time - * flushObject() meets an owner object, it calls the flushOwner() method and - * then set needOwner to false, so that all succeeding flushing of owner - * will call flushExpression() or flushStatement() normally, which will get - * a reference of the owner property. - */ - private boolean needOwner = false; + /* + * if any expression or statement references owner, it is set true in method + * recordStatement() or recordExpressions(), and, at the first time + * flushObject() meets an owner object, it calls the flushOwner() method and + * then set needOwner to false, so that all succeeding flushing of owner + * will call flushExpression() or flushStatement() normally, which will get + * a reference of the owner property. + */ + private boolean needOwner = false; - private PrintWriter out; + private PrintWriter out; - private Object owner = null; + private Object owner = null; - private ReferenceMap records = new ReferenceMap(); + private ReferenceMap records = new ReferenceMap(); private ReferenceMap objPrePendingCache = new ReferenceMap(); private ReferenceMap clazzCounterMap = new ReferenceMap(); - private boolean writingObject = false; + private boolean writingObject = false; - /** - * Construct a <code>XMLEncoder</code>. - * - * @param out - * the output stream where xml is written to - */ - public XMLEncoder(OutputStream out) { - if (null != out) { + /** + * Construct a <code>XMLEncoder</code>. + * + * @param out + * the output stream where XML is written to + */ + public XMLEncoder(OutputStream out) { + if (null != out) { try { this.out = new PrintWriter( new OutputStreamWriter(out, "UTF-8"), true); //$NON-NLS-1$ @@ -115,144 +116,146 @@ e.printStackTrace(); } } - } + } - /** - * Call <code>flush()</code> first, then write out xml footer and close - * the underlying output stream. - */ - public void close() { - flush(); - out.println("</java>"); //$NON-NLS-1$ - out.close(); - } + /** + * Call <code>flush()</code> first, then write out XML footer and close the + * underlying output stream. + */ + public void close() { + flush(); + out.println("</java>"); //$NON-NLS-1$ + out.close(); + } - private StringBuffer decapitalize(String s) { - StringBuffer buf = new StringBuffer(s); - buf.setCharAt(0, Character.toLowerCase(buf.charAt(0))); - return buf; - } + private StringBuffer decapitalize(String s) { + StringBuffer buf = new StringBuffer(s); + buf.setCharAt(0, Character.toLowerCase(buf.charAt(0))); + return buf; + } private String idSerialNoOfObject(Object obj) { Class<?> clazz = obj.getClass(); Integer serialNo = (Integer) clazzCounterMap.get(clazz); serialNo = serialNo == null ? 0 : serialNo; - String id = nameForClass(obj.getClass()) + serialNo; + String id = BeansUtils.idOfClass(obj.getClass()) + serialNo; clazzCounterMap.put(clazz, ++serialNo); return id; } - /** - * Writes out all objects since last flush to the output stream. - * <p> - * The implementation write the xml header first if it has not been - * written. Then all pending objects since last flush are written. - * </p> - */ - @SuppressWarnings("nls") + /** + * Writes out all objects since last flush to the output stream. + * <p> + * The implementation write the XML header first if it has not been written. + * Then all pending objects since last flush are written. + * </p> + */ + @SuppressWarnings("nls") public void flush() { - synchronized (this) { - // write xml header - if (!hasXmlHeader) { - out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); - out.println("<java version=\"" - + System.getProperty("java.version") - + "\" class=\"java.beans.XMLDecoder\">"); - hasXmlHeader = true; - } + synchronized (this) { + // write xml header + if (!hasXmlHeader) { + out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); + out.println("<java version=\"" + + System.getProperty("java.version") + + "\" class=\"java.beans.XMLDecoder\">"); + hasXmlHeader = true; + } - // preprocess pending objects - for (Iterator<Object> iter = flushPending.iterator(); iter.hasNext();) { - Object o = iter.next(); - Record rec = (Record) records.get(o); - if (rec != null) { - preprocess(o, rec); - } - } + // preprocess pending objects + for (Iterator<Object> iter = flushPending.iterator(); iter + .hasNext();) { + Object o = iter.next(); + Record rec = (Record) records.get(o); + if (rec != null) { + preprocess(o, rec); + } + } - // flush pending objects - for (Iterator<Object> iter = flushPending.iterator(); iter.hasNext();) { - Object o = iter.next(); - flushObject(o, INDENT_UNIT); - // remove flushed obj - iter.remove(); - } + // flush pending objects + for (Iterator<Object> iter = flushPending.iterator(); iter + .hasNext();) { + Object o = iter.next(); + flushObject(o, INDENT_UNIT); + // remove flushed obj + iter.remove(); + } - // clear statement records - records.clear(); + // clear statement records + records.clear(); flushPendingStat.clear(); objPrePendingCache.clear(); clazzCounterMap.clear(); - // remove all old->new mappings - super.clear(); - } - } + // remove all old->new mappings + super.clear(); + } + } - @SuppressWarnings("nls") + @SuppressWarnings("nls") private void flushBasicObject(Object obj, int indent) { - if( obj instanceof Proxy) { - return; - } - flushIndent(indent); - if (obj == null) { - out.println("<null />"); - } else if (obj instanceof String) { - Record rec = (Record) records.get(obj); - if( null != rec) { - if (flushPendingStat.contains(obj)) { - flushExpression(obj, rec, indent - 3, true); - } else { - flushExpression(obj, rec, indent - 3, false); - } - return; - } - out.print("<string>"); - flushString((String) obj); - out.println("</string>"); - } else if (obj instanceof Class<?>) { - out.print("<class>"); - out.print(((Class<?>) obj).getName()); - out.println("</class>"); - } else if (obj instanceof Boolean) { - out.print("<boolean>"); - out.print(obj); - out.println("</boolean>"); - } else if (obj instanceof Byte) { - out.print("<byte>"); - out.print(obj); - out.println("</byte>"); - } else if (obj instanceof Character) { - out.print("<char>"); - out.print(obj); - out.println("</char>"); - } else if (obj instanceof Double) { - out.print("<double>"); - out.print(obj); - out.println("</double>"); - } else if (obj instanceof Float) { - out.print("<float>"); - out.print(obj); - out.println("</float>"); - } else if (obj instanceof Integer) { - out.print("<int>"); - out.print(obj); - out.println("</int>"); - } else if (obj instanceof Long) { - out.print("<long>"); - out.print(obj); - out.println("</long>"); - } else if (obj instanceof Short) { - out.print("<short>"); - out.print(obj); - out.println("</short>"); - } else { - getExceptionListener().exceptionThrown( - new Exception(Messages.getString("beans.73", obj))); - } - } + if (obj instanceof Proxy) { + return; + } + flushIndent(indent); + if (obj == null) { + out.println("<null />"); + } else if (obj instanceof String) { + Record rec = (Record) records.get(obj); + if (null != rec) { + if (flushPendingStat.contains(obj)) { + flushExpression(obj, rec, indent - 3, true); + } else { + flushExpression(obj, rec, indent - 3, false); + } + return; + } + out.print("<string>"); + flushString((String) obj); + out.println("</string>"); + } else if (obj instanceof Class<?>) { + out.print("<class>"); + out.print(((Class<?>) obj).getName()); + out.println("</class>"); + } else if (obj instanceof Boolean) { + out.print("<boolean>"); + out.print(obj); + out.println("</boolean>"); + } else if (obj instanceof Byte) { + out.print("<byte>"); + out.print(obj); + out.println("</byte>"); + } else if (obj instanceof Character) { + out.print("<char>"); + out.print(obj); + out.println("</char>"); + } else if (obj instanceof Double) { + out.print("<double>"); + out.print(obj); + out.println("</double>"); + } else if (obj instanceof Float) { + out.print("<float>"); + out.print(obj); + out.println("</float>"); + } else if (obj instanceof Integer) { + out.print("<int>"); + out.print(obj); + out.println("</int>"); + } else if (obj instanceof Long) { + out.print("<long>"); + out.print(obj); + out.println("</long>"); + } else if (obj instanceof Short) { + out.print("<short>"); + out.print(obj); + out.println("</short>"); + } else { + getExceptionListener().exceptionThrown( + new Exception(Messages.getString("beans.73", obj))); + } + } - @SuppressWarnings("nls") + @SuppressWarnings("nls") private void flushExpression(Object obj, Record rec, int indent, boolean asStatement) { // flush @@ -282,197 +285,197 @@ flushStatement(stat, rec.id, rec.stats, indent); } - private void flushIndent(int indent) { - for (int i = 0; i < indent; i++) { - out.print(" "); //$NON-NLS-1$ - } - } + private void flushIndent(int indent) { + for (int i = 0; i < indent; i++) { + out.print(" "); //$NON-NLS-1$ + } + } - private void flushObject(Object obj, int indent) { - Record rec = (Record) records.get(obj); - if (rec == null && !isBasicType(obj)) - return; + private void flushObject(Object obj, int indent) { + Record rec = (Record) records.get(obj); + if (rec == null && !isBasicType(obj)) + return; - if (obj == owner && this.needOwner) { - flushOwner(obj, rec, indent); - this.needOwner = false; - return; - } + if (obj == owner && this.needOwner) { + flushOwner(obj, rec, indent); + this.needOwner = false; + return; + } - if (isBasicType(obj)) { - flushBasicObject(obj, indent); - } else { - if (flushPendingStat.contains(obj)) { - flushExpression(obj, rec, indent, true); - } else { - flushExpression(obj, rec, indent, false); - } - } - } + if (isBasicType(obj)) { + flushBasicObject(obj, indent); + } else { + if (flushPendingStat.contains(obj)) { + flushExpression(obj, rec, indent, true); + } else { + flushExpression(obj, rec, indent, false); + } + } + } - @SuppressWarnings("nls") + @SuppressWarnings("nls") private void flushOwner(Object obj, Record rec, int indent) { if (rec.refCount > 1 && rec.id == null) { rec.id = idSerialNoOfObject(obj); } - flushIndent(indent); - String tagName = "void"; - out.print("<"); - out.print(tagName); + flushIndent(indent); + String tagName = "void"; + out.print("<"); + out.print(tagName); - // id attribute - if (rec.id != null) { - out.print(" id=\""); - out.print(rec.id); - out.print("\""); - } + // id attribute + if (rec.id != null) { + out.print(" id=\""); + out.print(rec.id); + out.print("\""); + } - out.print(" property=\"owner\""); + out.print(" property=\"owner\""); - // open tag, end - if (rec.exp.getArguments().length == 0 && rec.stats.isEmpty()) { - out.println("/>"); - return; - } - out.println(">"); + // open tag, end + if (rec.exp.getArguments().length == 0 && rec.stats.isEmpty()) { + out.println("/>"); + return; + } + out.println(">"); - // arguments - for (int i = 0; i < rec.exp.getArguments().length; i++) { - flushObject(rec.exp.getArguments()[i], indent + INDENT_UNIT); - } + // arguments + for (int i = 0; i < rec.exp.getArguments().length; i++) { + flushObject(rec.exp.getArguments()[i], indent + INDENT_UNIT); + } - // sub statements - flushSubStatements(rec.stats, indent); + // sub statements + flushSubStatements(rec.stats, indent); - // close tag - flushIndent(indent); - out.print("</"); - out.print(tagName); - out.println(">"); - } + // close tag + flushIndent(indent); + out.print("</"); + out.print(tagName); + out.println(">"); + } - @SuppressWarnings("nls") + @SuppressWarnings("nls") private void flushStatArray(Statement stat, String id, List<?> subStats, - int indent) { - // open tag, begin - flushIndent(indent); - out.print("<array"); + int indent) { + // open tag, begin + flushIndent(indent); + out.print("<array"); - // id attribute - if (id != null) { - out.print(" id=\""); - out.print(id); - out.print("\""); - } + // id attribute + if (id != null) { + out.print(" id=\""); + out.print(id); + out.print("\""); + } - // class & length - out.print(" class=\""); - out.print(((Class<?>) stat.getArguments()[0]).getName()); - out.print("\" length=\""); - out.print(stat.getArguments()[1]); - out.print("\""); + // class & length + out.print(" class=\""); + out.print(((Class<?>) stat.getArguments()[0]).getName()); + out.print("\" length=\""); + out.print(stat.getArguments()[1]); + out.print("\""); - // open tag, end - if (subStats.isEmpty()) { - out.println("/>"); - return; - } - out.println(">"); + // open tag, end + if (subStats.isEmpty()) { + out.println("/>"); + return; + } + out.println(">"); - // sub statements - flushSubStatements(subStats, indent); + // sub statements + flushSubStatements(subStats, indent); - // close tag - flushIndent(indent); - out.println("</array>"); - } + // close tag + flushIndent(indent); + out.println("</array>"); + } - @SuppressWarnings("nls") + @SuppressWarnings("nls") private void flushStatCommon(Statement stat, String id, List<?> subStats, - int indent) { - // open tag, begin - flushIndent(indent); - String tagName = stat instanceof Expression ? "object" : "void"; - out.print("<"); - out.print(tagName); + int indent) { + // open tag, begin + flushIndent(indent); + String tagName = stat instanceof Expression ? "object" : "void"; + out.print("<"); + out.print(tagName); - // id attribute - if (id != null) { - out.print(" id=\""); - out.print(id); - out.print("\""); - } + // id attribute + if (id != null) { + out.print(" id=\""); + out.print(id); + out.print("\""); + } - // special class attribute - if (stat.getTarget() instanceof Class<?>) { - out.print(" class=\""); - out.print(((Class<?>) stat.getTarget()).getName()); - out.print("\""); - } + // special class attribute + if (stat.getTarget() instanceof Class<?>) { + out.print(" class=\""); + out.print(((Class<?>) stat.getTarget()).getName()); + out.print("\""); + } - // method attribute - if (!"new".equals(stat.getMethodName())) { - out.print(" method=\""); - out.print(stat.getMethodName()); - out.print("\""); - } + // method attribute + if (!"new".equals(stat.getMethodName())) { + out.print(" method=\""); + out.print(stat.getMethodName()); + out.print("\""); + } - // open tag, end - if (stat.getArguments().length == 0 && subStats.isEmpty()) { - out.println("/>"); - return; - } - out.println(">"); + // open tag, end + if (stat.getArguments().length == 0 && subStats.isEmpty()) { + out.println("/>"); + return; + } + out.println(">"); - // arguments - for (int i = 0; i < stat.getArguments().length; i++) { - flushObject(stat.getArguments()[i], indent + INDENT_UNIT); - } + // arguments + for (int i = 0; i < stat.getArguments().length; i++) { + flushObject(stat.getArguments()[i], indent + INDENT_UNIT); + } - // sub statements - flushSubStatements(subStats, indent); + // sub statements + flushSubStatements(subStats, indent); - // close tag - flushIndent(indent); - out.print("</"); - out.print(tagName); - out.println(">"); - } + // close tag + flushIndent(indent); + out.print("</"); + out.print(tagName); + out.println(">"); + } - @SuppressWarnings("nls") + @SuppressWarnings("nls") private void flushStatement(Statement stat, String id, List<?> subStats, - int indent) { - Object target = stat.getTarget(); - String method = stat.getMethodName(); - Object args[] = stat.getArguments(); + int indent) { + Object target = stat.getTarget(); + String method = stat.getMethodName(); + Object args[] = stat.getArguments(); - // special case for array - if (Array.class == target && "newInstance".equals(method)) { - flushStatArray(stat, id, subStats, indent); - return; - } - // special case for get(int) and set(int, Object) - if (isGetArrayStat(target, method, args) - || isSetArrayStat(target, method, args)) { - flushStatIndexed(stat, id, subStats, indent); - return; - } - // special case for getProperty() and setProperty(Object) - if (isGetPropertyStat(method, args) || isSetPropertyStat(method, args)) { - flushStatGetterSetter(stat, id, subStats, indent); - return; - } + // special case for array + if (Array.class == target && "newInstance".equals(method)) { + flushStatArray(stat, id, subStats, indent); + return; + } + // special case for get(int) and set(int, Object) + if (isGetArrayStat(target, method, args) + || isSetArrayStat(target, method, args)) { + flushStatIndexed(stat, id, subStats, indent); + return; + } + // special case for getProperty() and setProperty(Object) + if (isGetPropertyStat(method, args) || isSetPropertyStat(method, args)) { + flushStatGetterSetter(stat, id, subStats, indent); + return; + } - if (isStaticConstantsSupported - && "getField".equals(stat.getMethodName())) { + if (isStaticConstantsSupported + && "getField".equals(stat.getMethodName())) { flushStatField(stat, indent); - return; - } + return; + } - // common case - flushStatCommon(stat, id, subStats, indent); - } + // common case + flushStatCommon(stat, id, subStats, indent); + } @SuppressWarnings("nls") private void flushStatField(Statement stat, int indent) { @@ -515,194 +518,182 @@ } } - @SuppressWarnings("nls") + @SuppressWarnings("nls") private void flushStatGetterSetter(Statement stat, String id, - List<?> subStats, int indent) { - // open tag, begin - flushIndent(indent); - String tagName = stat instanceof Expression ? "object" : "void"; - out.print("<"); - out.print(tagName); + List<?> subStats, int indent) { + // open tag, begin + flushIndent(indent); + String tagName = stat instanceof Expression ? "object" : "void"; + out.print("<"); + out.print(tagName); - // id attribute - if (id != null) { - out.print(" id=\""); - out.print(id); - out.print("\""); - } - - // special class attribute - if (stat.getTarget() instanceof Class<?>) { - out.print(" class=\""); - out.print(((Class<?>) stat.getTarget()).getName()); - out.print("\""); - } - - // property attribute - out.print(" property=\""); - out.print(decapitalize(stat.getMethodName().substring(3))); - out.print("\""); - - // open tag, end - if (stat.getArguments().length == 0 && subStats.isEmpty()) { - out.println("/>"); - return; - } - out.println(">"); - - // arguments - for (int i = 0; i < stat.getArguments().length; i++) { - flushObject(stat.getArguments()[i], indent + INDENT_UNIT); - } - - // sub statements - flushSubStatements(subStats, indent); - - // close tag - flushIndent(indent); - out.print("</"); - out.print(tagName); - out.println(">"); - } - - @SuppressWarnings("nls") - private void flushStatIndexed(Statement stat, String id, List<?> subStats, - int indent) { - // open tag, begin - flushIndent(indent); - String tagName = stat instanceof Expression ? "object" : "void"; - out.print("<"); - out.print(tagName); - - // id attribute - if (id != null) { - out.print(" id=\""); - out.print(id); - out.print("\""); - } - - // special class attribute - if (stat.getTarget() instanceof Class<?>) { - out.print(" class=\""); - out.print(((Class<?>) stat.getTarget()).getName()); - out.print("\""); - } - - // index attribute - out.print(" index=\""); - out.print(stat.getArguments()[0]); - out.print("\""); - - // open tag, end - if (stat.getArguments().length == 1 && subStats.isEmpty()) { - out.println("/>"); - return; - } - out.println(">"); - - // arguments - for (int i = 1; i < stat.getArguments().length; i++) { - flushObject(stat.getArguments()[i], indent + INDENT_UNIT); - } - - // sub statements - flushSubStatements(subStats, indent); - - // close tag - flushIndent(indent); - out.print("</"); - out.print(tagName); - out.println(">"); - } - - @SuppressWarnings("nls") - private void flushString(String s) { - char c; - for (int i = 0; i < s.length(); i++) { - c = s.charAt(i); - if (c == '<') { - out.print("<"); - } else if (c == '>') { - out.print(">"); - } else if (c == '&') { - out.print("&"); - } else if (c == '\'') { - out.print("'"); - } else if (c == '"') { - out.print("""); - } else { - out.print(c); - } - } - } - - private void flushSubStatements(List<?> subStats, int indent) { - for (int i = 0; i < subStats.size(); i++) { - Statement subStat = (Statement) subStats.get(i); - try { - if (subStat instanceof Expression) { - Expression subExp = (Expression) subStat; - Object obj = subExp.getValue(); - Record rec = (Record) records.get(obj); - flushExpression(obj, rec, indent + INDENT_UNIT, true); - } else { - flushStatement(subStat, null, Collections.EMPTY_LIST, - indent + INDENT_UNIT); - } - } catch (Exception e) { - // should not happen - getExceptionListener().exceptionThrown(e); - } - } - } - - /** - * Returns the owner of this encoder. - * - * @return the owner of this encoder - */ - public Object getOwner() { - return owner; - } - - private boolean isBasicType(Object value) { - return value == null || value instanceof Boolean - || value instanceof Byte || value instanceof Character - || value instanceof Class<?> || value instanceof Double - || value instanceof Float || value instanceof Integer - || value instanceof Long || value instanceof Short - || value instanceof String || value instanceof Proxy; - } - - private boolean isGetArrayStat(Object target, String method, Object[] args) { - return ("get".equals(method) && args.length == 1 //$NON-NLS-1$ - && args[0] instanceof Integer && target.getClass().isArray()); - } - - private boolean isGetPropertyStat(String method, Object[] args) { - return (method.startsWith("get") && method.length() > 3 && args.length == 0); //$NON-NLS-1$ - } - - private boolean isSetArrayStat(Object target, String method, Object[] args) { - return ("set".equals(method) && args.length == 2 //$NON-NLS-1$ - && args[0] instanceof Integer && target.getClass().isArray()); - } - - private boolean isSetPropertyStat(String method, Object[] args) { - return (method.startsWith("set") && method.length() > 3 && args.length == 1); //$NON-NLS-1$ - } - - private String nameForClass(Class<?> c) { - if (c.isArray()) { - return nameForClass(c.getComponentType()) + "Array"; //$NON-NLS-1$ - } - String name = c.getName(); - int i = name.lastIndexOf('.'); - if (-1 == i) { - return name; + // id attribute + if (id != null) { + out.print(" id=\""); + out.print(id); + out.print("\""); } - return name.substring(i + 1); - } + + // special class attribute + if (stat.getTarget() instanceof Class<?>) { + out.print(" class=\""); + out.print(((Class<?>) stat.getTarget()).getName()); + out.print("\""); + } + + // property attribute + out.print(" property=\""); + out.print(decapitalize(stat.getMethodName().substring(3))); + out.print("\""); + + // open tag, end + if (stat.getArguments().length == 0 && subStats.isEmpty()) { + out.println("/>"); + return; + } + out.println(">"); + + // arguments + for (int i = 0; i < stat.getArguments().length; i++) { + flushObject(stat.getArguments()[i], indent + INDENT_UNIT); + } + + // sub statements + flushSubStatements(subStats, indent); + + // close tag + flushIndent(indent); + out.print("</"); + out.print(tagName); + out.println(">"); + } + + @SuppressWarnings("nls") + private void flushStatIndexed(Statement stat, String id, List<?> subStats, + int indent) { + // open tag, begin + flushIndent(indent); + String tagName = stat instanceof Expression ? "object" : "void"; + out.print("<"); + out.print(tagName); + + // id attribute + if (id != null) { + out.print(" id=\""); + out.print(id); + out.print("\""); + } + + // special class attribute + if (stat.getTarget() instanceof Class<?>) { + out.print(" class=\""); + out.print(((Class<?>) stat.getTarget()).getName()); + out.print("\""); + } + + // index attribute + out.print(" index=\""); + out.print(stat.getArguments()[0]); + out.print("\""); + + // open tag, end + if (stat.getArguments().length == 1 && subStats.isEmpty()) { + out.println("/>"); + return; + } + out.println(">"); + + // arguments + for (int i = 1; i < stat.getArguments().length; i++) { + flushObject(stat.getArguments()[i], indent + INDENT_UNIT); + } + + // sub statements + flushSubStatements(subStats, indent); + + // close tag + flushIndent(indent); + out.print("</"); + out.print(tagName); + out.println(">"); + } + + @SuppressWarnings("nls") + private void flushString(String s) { + char c; + for (int i = 0; i < s.length(); i++) { + c = s.charAt(i); + if (c == '<') { + out.print("<"); + } else if (c == '>') { + out.print(">"); + } else if (c == '&') { + out.print("&"); + } else if (c == '\'') { + out.print("'"); + } else if (c == '"') { + out.print("""); + } else { + out.print(c); + } + } + } + + private void flushSubStatements(List<?> subStats, int indent) { + for (int i = 0; i < subStats.size(); i++) { + Statement subStat = (Statement) subStats.get(i); + try { + if (subStat instanceof Expression) { + Expression subExp = (Expression) subStat; + Object obj = subExp.getValue(); + Record rec = (Record) records.get(obj); + flushExpression(obj, rec, indent + INDENT_UNIT, true); + } else { + flushStatement(subStat, null, Collections.EMPTY_LIST, + indent + INDENT_UNIT); + } + } catch (Exception e) { + // should not happen + getExceptionListener().exceptionThrown(e); + } + } + } + + /** + * Returns the owner of this encoder. + * + * @return the owner of this encoder + */ + public Object getOwner() { + return owner; + } + + private boolean isBasicType(Object value) { + return value == null || value instanceof Boolean + || value instanceof Byte || value instanceof Character + || value instanceof Class<?> || value instanceof Double + || value instanceof Float || value instanceof Integer + || value instanceof Long || value instanceof Short + || value instanceof String || value instanceof Proxy; + } + + private boolean isGetArrayStat(Object target, String method, Object[] args) { + return ("get".equals(method) && args.length == 1 //$NON-NLS-1$ + && args[0] instanceof Integer && target.getClass().isArray()); + } + + private boolean isGetPropertyStat(String method, Object[] args) { + return (method.startsWith("get") && method.length() > 3 && args.length == 0); //$NON-NLS-1$ + } + + private boolean isSetArrayStat(Object target, String method, Object[] args) { + return ("set".equals(method) && args.length == 2 //$NON-NLS-1$ + && args[0] instanceof Integer && target.getClass().isArray()); + } + + private boolean isSetPropertyStat(String method, Object[] args) { + return (method.startsWith("set") && method.length() > 3 && args.length == 1); //$NON-NLS-1$ + } /* * The preprocess removes unused statements and counts references of every @@ -743,102 +734,103 @@ } } - for (Iterator<?> iter = rec.stats.iterator(); iter.hasNext();) { - Statement subStat = (Statement) iter.next(); - if (subStat instanceof Expression) { - try { - Expression subExp = (Expression) subStat; - Record subRec = (Record) records.get(subExp.getValue()); - if (subRec == null || subRec.exp == null - || subRec.exp != subExp) { - iter.remove(); - continue; - } - preprocess(subExp.getValue(), subRec); - if (subRec.stats.isEmpty()) { - if (isGetArrayStat(subExp.getTarget(), subExp - .getMethodName(), subExp.getArguments()) - || isGetPropertyStat(subExp.getMethodName(), - subExp.getArguments())) { - iter.remove(); - continue; - } - } - } catch (Exception e) { - getExceptionListener().exceptionThrown(e); - iter.remove(); - } - continue; - } + for (Iterator<?> iter = rec.stats.iterator(); iter.hasNext();) { + Statement subStat = (Statement) iter.next(); + if (subStat instanceof Expression) { + try { + Expression subExp = (Expression) subStat; + Record subRec = (Record) records.get(subExp.getValue()); + if (subRec == null || subRec.exp == null + || subRec.exp != subExp) { + iter.remove(); + continue; + } + preprocess(subExp.getValue(), subRec); + if (subRec.stats.isEmpty()) { + if (isGetArrayStat(subExp.getTarget(), + subExp.getMethodName(), subExp.getArguments()) + || isGetPropertyStat(subExp.getMethodName(), + subExp.getArguments())) { + iter.remove(); + continue; + } + } + } catch (Exception e) { + getExceptionListener().exceptionThrown(e); + iter.remove(); + } + continue; + } - Object subStatArgs[] = subStat.getArguments(); - for (int i = 0; i < subStatArgs.length; i++) { - Record argRec = (Record) records.get(subStatArgs[i]); - if (argRec != null) { - preprocess(subStatArgs[i], argRec); - } - } - } - } + Object subStatArgs[] = subStat.getArguments(); + for (int i = 0; i < subStatArgs.length; i++) { + Record argRec = (Record) records.get(subStatArgs[i]); + if (argRec != null) { + preprocess(subStatArgs[i], argRec); + } + } + } + } - private void recordExpression(Object value, Expression exp) { - // record how a new object is created or obtained - Record rec = (Record) records.get(value); - if (rec == null) { - rec = new Record(); - records.put(value, rec); - } + private void recordExpression(Object value, Expression exp) { + // record how a new object is created or obtained + Record rec = (Record) records.get(value); + if (rec == null) { + rec = new Record(); + records.put(value, rec); + } - if (rec.exp == null) { - // it is generated by its sub stats - for (Iterator<?> iter = rec.stats.iterator(); iter.hasNext();) { - Statement stat = (Statement) iter.next(); - try { - if (stat instanceof Expression) { - flushPrePending.add(value); - } - } catch (Exception e) { - e.printStackTrace(); - } + if (rec.exp == null) { + // it is generated by its sub stats + for (Iterator<?> iter = rec.stats.iterator(); iter.hasNext();) { + Statement stat = (Statement) iter.next(); + try { + if (stat instanceof Expression) { + flushPrePending.add(value); + } + } catch (Exception e) { + e.printStackTrace(); + } - } - } + } + } - rec.exp = exp; + rec.exp = exp; - // deal with 'owner' property - if (value == owner && owner != null) { - needOwner = true; - } + // deal with 'owner' property + if (value == owner && owner != null) { + needOwner = true; + } - // also record as a statement - recordStatement(exp); - } + // also record as a statement + recordStatement(exp); + } - private void recordStatement(Statement stat) { - if (null == stat) return; - // deal with 'owner' property - if (stat.getTarget() == owner && owner != null) { - needOwner = true; - } + private void recordStatement(Statement stat) { + if (null == stat) + return; + // deal with 'owner' property + if (stat.getTarget() == owner && owner != null) { + needOwner = true; + } - // record how a statement affects the target object - Record rec = (Record) records.get(stat.getTarget()); - if (rec == null) { - rec = new Record(); - records.put(stat.getTarget(), rec); - } - rec.stats.add(stat); - } + // record how a statement affects the target object + Record rec = (Record) records.get(stat.getTarget()); + if (rec == null) { + rec = new Record(); + records.put(stat.getTarget(), rec); + } + rec.stats.add(stat); + } /** * Imperfect attempt to detect a dead loop. This works with specific - * patterns that can be found in our AWT implementation. - * See HARMONY-5707 for details. - * - * @param value the object to check dupes for - * @return true if a dead loop detected; false otherwise - * FIXME + * patterns that can be found in our AWT implementation. See HARMONY-5707 + * for details. + * + * @param value + * the object to check dupes for + * @return true if a dead loop detected; false otherwise FIXME */ private boolean checkDeadLoop(Object value) { int n = 0; @@ -852,14 +844,14 @@ } else { break; } - + if (obj != null && (obj.getClass().isAssignableFrom(value.getClass())) && obj.equals(value)) { n++; if (n >= DEADLOCK_THRESHOLD) { - //System.out.println("Dead loop hit!"); + // System.out.println("Dead loop hit!"); return true; } } @@ -868,48 +860,49 @@ } /** - * Sets the owner of this encoder. - * - * @param owner - * the owner to set - */ - public void setOwner(Object owner) { - this.owner = owner; - } + * Sets the owner of this encoder. + * + * @param owner + * the owner to set + */ + public void setOwner(Object owner) { + this.owner = owner; + } - /** - * Records the expression so that it can be written out later, then calls - * super implementation. - */ - @Override + /** + * Records the expression so that it can be written out later, then calls + * super implementation. + */ + @Override public void writeExpression(Expression oldExp) { if (null == oldExp) { throw new NullPointerException(); } - boolean oldWritingObject = writingObject; - writingObject = true; - // get expression value - Object oldValue = null; + boolean oldWritingObject = writingObject; + writingObject = true; + // get expression value + Object oldValue = null; try { - oldValue = oldExp.getValue(); - } catch (Exception e) { - getExceptionListener() - .exceptionThrown( - new Exception("failed to execute expression: " //$NON-NLS-1$ - + oldExp, e)); - return; - } + oldValue = oldExp.getValue(); + } catch (Exception e) { + getExceptionListener().exceptionThrown( + new Exception("failed to execute expression: " //$NON-NLS-1$ + + oldExp, e)); + return; + } - // check existence - if (get(oldValue) != null && (!(oldValue instanceof String) || oldWritingObject)) { - return; - } + // check existence + if (get(oldValue) != null + && (!(oldValue instanceof String) || oldWritingObject)) { + return; + } - // record how the object is obtained - if (!isBasicType(oldValue) || (oldValue instanceof String && !oldWritingObject)) { - recordExpression(oldValue, oldExp); - } + // record how the object is obtained + if (!isBasicType(oldValue) + || (oldValue instanceof String && !oldWritingObject)) { + recordExpression(oldValue, oldExp); + } // try to detect if we run into a dead loop if (checkDeadLoop(oldValue)) { @@ -917,13 +910,13 @@ } super.writeExpression(oldExp); - writingObject = oldWritingObject; - } + writingObject = oldWritingObject; + } - /** - * Records the object so that it can be written out later, then calls super - * implementation. - */ + /** + * Records the object so that it can be written out later, then calls super + * implementation. + */ @SuppressWarnings("unchecked") @Override public void writeObject(Object o) { @@ -971,23 +964,21 @@ } } - /** - * Records the statement so that it can be written out later, then calls - * super implementation. - */ - @Override + /** + * Records the statement so that it can be written out later, then calls + * super implementation. + */ + @Override public void writeStatement(Statement oldStat) { - if(null == oldStat) { - System.err.println("java.lang.Exception: XMLEncoder: discarding statement null"); + if (null == oldStat) { + System.err + .println("java.lang.Exception: XMLEncoder: discarding statement null"); System.err.println("Continuing..."); return; } - // record how the object is changed - recordStatement(oldStat); + // record how the object is changed + recordStatement(oldStat); - super.writeStatement(oldStat); - } - -} - - + super.writeStatement(oldStat); + } +} \ No newline at end of file
diff --git a/classlib/modules/beans/src/main/java/org/apache/harmony/beans/BeansUtils.java b/classlib/modules/beans/src/main/java/org/apache/harmony/beans/BeansUtils.java index a526696..3447a7d 100644 --- a/classlib/modules/beans/src/main/java/org/apache/harmony/beans/BeansUtils.java +++ b/classlib/modules/beans/src/main/java/org/apache/harmony/beans/BeansUtils.java
@@ -17,8 +17,35 @@ package org.apache.harmony.beans; +import java.lang.reflect.Method; +import java.util.Arrays; + public class BeansUtils { + public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; + + public static final String NEW = "new"; //$NON-NLS-1$ + + public static final String NEWINSTANCE = "newInstance"; //$NON-NLS-1$ + + public static final String NEWARRAY = "newArray"; //$NON-NLS-1$ + + public static final String FORNAME = "forName"; //$NON-NLS-1$ + + public static final String GET = "get"; //$NON-NLS-1$ + + public static final String IS = "is"; //$NON-NLS-1$ + + public static final String SET = "set"; //$NON-NLS-1$ + + public static final String ADD = "add"; //$NON-NLS-1$ + + public static final String PUT = "put"; //$NON-NLS-1$ + + public static final String NULL = "null"; //$NON-NLS-1$ + + public static final String QUOTE = "\"\""; //$NON-NLS-1$ + public static final int getHashCode(Object obj) { return obj != null ? obj.hashCode() : 0; } @@ -63,4 +90,33 @@ || (base == float.class) && (wrapper == Float.class) || (base == double.class) && (wrapper == Double.class); } + + private static final String EQUALS_METHOD = "equals"; + + private static final Class<?>[] EQUALS_PARAMETERS = new Class<?>[] { Object.class }; + + public static boolean declaredEquals(Class<?> clazz) { + for (Method declaredMethod : clazz.getDeclaredMethods()) { + if (EQUALS_METHOD.equals(declaredMethod.getName()) + && Arrays.equals(declaredMethod.getParameterTypes(), + EQUALS_PARAMETERS)) { + return true; + } + } + return false; + } + + public static String idOfClass(Class<?> clazz) { + Class<?> theClass = clazz; + StringBuilder sb = new StringBuilder(); + if (theClass.isArray()) { + do { + sb.append("Array"); //$NON-NLS-1$ + theClass = theClass.getComponentType(); + } while (theClass.isArray()); + } + String clazzName = theClass.getName(); + clazzName = clazzName.substring(clazzName.lastIndexOf('.') + 1); + return clazzName + sb.toString(); + } }