You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/09/12 11:12:59 UTC
svn commit: r442538 - in
/incubator/harmony/enhanced/classlib/trunk/modules/beans: ./
src/main/java/java/beans/ src/main/java/org/apache/harmony/beans/
src/test/java/org/apache/harmony/beans/tests/java/beans/
Author: mloenko
Date: Tue Sep 12 02:12:58 2006
New Revision: 442538
URL: http://svn.apache.org/viewvc?view=rev&rev=442538
Log:
applied patch for HARMONY-1408
[classlib][beans] implementation of ProxyPersistenceDelegate
Added:
incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_reflect_ProxyPersistenceDelegate.java
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/beans/build.xml
incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/DefaultPersistenceDelegatesFactory.java
incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/CustomizedPersistenceDelegateTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/build.xml
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/build.xml?view=diff&rev=442538&r1=442537&r2=442538
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/build.xml (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/build.xml Tue Sep 12 02:12:58 2006
@@ -223,7 +223,6 @@
<fileset dir="${hy.beans.src.test.java}">
<include name="**/*Test.java"/>
<exclude name="org/apache/harmony/beans/tests/java/beans/EventHandlerTest.java" />
- <exclude name="org/apache/harmony/beans/tests/java/beans/CustomizedPersistenceDelegateTest.java" />
<exclude name="org/apache/harmony/beans/tests/java/beans/DefaultPersistenceDelegateTest.java" />
<exclude name="org/apache/harmony/beans/tests/java/beans/EncoderTest.java" />
<exclude name="org/apache/harmony/beans/tests/java/beans/EventHandlerTest.java" />
Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java?view=diff&rev=442538&r1=442537&r2=442538
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java Tue Sep 12 02:12:58 2006
@@ -133,9 +133,7 @@
result = method.invoke(null, ama);
} else if (methodName.equals("newInstance") //$NON-NLS-1$
- && target instanceof Class
- && ((Class) target).getName().equals(
- "java.lang.reflect.Array")) { //$NON-NLS-1$
+ && target instanceof Class && target == Array.class) {
Class<?> componentType = (Class) arguments[0];
int length = ((Integer) arguments[1]).intValue();
@@ -154,31 +152,56 @@
}
} else if (target instanceof Class) {
Method method = null;
+ boolean found = false;
try {
- // try to look for static method at first
- method = findMethod((Class) target, methodName, arguments,
- true);
- result = method.invoke(null, arguments);
- } catch (NoSuchMethodException e) {
+ // 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 (target != Class.class) {
+ method = findMethod((Class) target, methodName,
+ arguments, true);
+ result = method.invoke(null, arguments);
+ found = true;
+ }
+ } catch (NoSuchMethodException e) {}
+
+ if (!found) {
// static method was not found
- // try to invoke non-static method of Class object
- method = findMethod(target.getClass(), methodName,
- arguments, false);
- result = method.invoke(target, arguments);
+ // try to invoke method of Class object
+
+ if (methodName.equals("forName") //$NON-NLS-1$
+ && arguments.length == 1
+ && arguments[0] instanceof String) {
+ // special handling of Class.forName(String)
+
+ try {
+ result = Class.forName((String) arguments[0]);
+ } catch (ClassNotFoundException e2) {
+ result = Class.forName((String) arguments[0], true,
+ Thread.currentThread()
+ .getContextClassLoader());
+ }
+ } else {
+ method = findMethod(target.getClass(), methodName,
+ arguments, false);
+ result = method.invoke(target, arguments);
+ }
}
} else {
Method method = findMethod(target.getClass(), methodName,
arguments, false);
// XXX investigate: do we really need this?
-// AccessController.doPrivileged(new PrivilegedAction<Object>() {
-//
-// public Object run() {
-// mtd.setAccessible(true);
-// return null;
-// }
-// });
+ // AccessController.doPrivileged(new PrivilegedAction<Object>()
+ // {
+ //
+ // public Object run() {
+ // mtd.setAccessible(true);
+ // return null;
+ // }
+ // });
result = method.invoke(target, arguments);
}
@@ -191,19 +214,18 @@
}
private Method findArrayMethod() throws NoSuchMethodException {
- // the code below reproduces exact RI exception throwing behavior
+ // the code below reproduces exact RI exception throwing behavior
if (!methodName.equals("set") && !methodName.equals("get")) { //$NON-NLS-1$ //$NON-NLS-2$
throw new NoSuchMethodException(Messages.getString("beans.3C")); //$NON-NLS-1$
} else if (arguments.length > 0
&& arguments[0].getClass() != Integer.class) {
- throw new ClassCastException(
- Messages.getString("beans.3D")); //$NON-NLS-1$
+ throw new ClassCastException(Messages.getString("beans.3D")); //$NON-NLS-1$
} else if (methodName.equals("get") && (arguments.length != 1)) { //$NON-NLS-1$
- throw new ArrayIndexOutOfBoundsException(
- Messages.getString("beans.3E")); //$NON-NLS-1$
+ throw new ArrayIndexOutOfBoundsException(Messages
+ .getString("beans.3E")); //$NON-NLS-1$
} else if (methodName.equals("set") && (arguments.length != 2)) { //$NON-NLS-1$
- throw new ArrayIndexOutOfBoundsException(
- Messages.getString("beans.3F")); //$NON-NLS-1$
+ throw new ArrayIndexOutOfBoundsException(Messages
+ .getString("beans.3F")); //$NON-NLS-1$
}
if (methodName.equals("get")) { //$NON-NLS-1$
@@ -262,7 +284,8 @@
}
if (result == null) {
- throw new NoSuchMethodException(Messages.getString("beans.40", targetClass.getName())); //$NON-NLS-1$
+ throw new NoSuchMethodException(Messages.getString(
+ "beans.40", targetClass.getName())); //$NON-NLS-1$
}
return result;
@@ -318,7 +341,8 @@
}
if (foundMethods.size() == 0) {
- throw new NoSuchMethodException(Messages.getString("beans.41", methodName)); //$NON-NLS-1$
+ throw new NoSuchMethodException(Messages.getString(
+ "beans.41", methodName)); //$NON-NLS-1$
}
foundMethodsArr = foundMethods.toArray(new Method[foundMethods.size()]);
@@ -337,8 +361,8 @@
}
try {
- Statement.findMethod((Class) target, mName,
- stmt.getArguments(), true);
+ Statement.findMethod((Class) target, mName, stmt.getArguments(),
+ true);
return true;
} catch (NoSuchMethodException e) {
return false;
@@ -350,31 +374,30 @@
* objects. Not necessary reflects to real methods.
*/
private static String[][] pdConstructorSignatures = {
- {"java.lang.Class", "new", "java.lang.Boolean", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "new", "java.lang.Byte", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "new", "java.lang.Character", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "new", "java.lang.Double", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "new", "java.lang.Float", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "new", "java.lang.Integer", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "new", "java.lang.Long", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "new", "java.lang.Short", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "new", "java.lang.String", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "forName", "java.lang.String", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "newInstance", "java.lang.Class", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- "java.lang.Integer", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- {"java.lang.reflect.Field", "get", "null", "", "", ""}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
- {"java.lang.Class", "forName", "java.lang.String", "", "", ""} //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.Boolean", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.Byte", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.Character", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.Double", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.Float", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.Integer", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.Long", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.Short", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "new", "java.lang.String", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "forName", "java.lang.String", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "newInstance", "java.lang.Class", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ "java.lang.Integer", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ { "java.lang.reflect.Field", "get", "null", "", "", "" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ { "java.lang.Class", "forName", "java.lang.String", "", "", "" } //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
};
-
+
static boolean isPDConstructor(Statement stmt) {
Object target = stmt.getTarget();
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;
}
@@ -384,23 +407,23 @@
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$
}
}
-
+
for (int i = 0; i < pdConstructorSignatures.length; i++) {
if (Arrays.equals(sig, pdConstructorSignatures[i])) {
return true;
}
}
-
+
return false;
}
-
+
private static boolean isPrimitiveWrapper(Class<?> wrapper, Class<?> base) {
return (base == boolean.class) && (wrapper == Boolean.class)
|| (base == byte.class) && (wrapper == Byte.class)
@@ -490,7 +513,7 @@
/**
* Comparator to determine which of two methods is "closer" to the reference
- * method.
+ * method.
*/
static class MethodComparator implements Comparator<Method> {
@@ -526,6 +549,7 @@
/**
* Returns the norm for given method. The norm is the "distance" from
* the reference method to the given method.
+ *
* @param m the method to calculate the norm for
* @return norm of given method
*/
@@ -566,11 +590,12 @@
}
/**
- * Returns a "hierarchy distance" between two classes.
+ * Returns a "hierarchy distance" between two classes.
+ *
* @param clz1
* @param clz2 should be superclass or superinterface of clz1
* @return hierarchy distance from clz1 to clz2, Integer.MAX_VALUE if
- * clz2 is not assignable from clz1.
+ * clz2 is not assignable from clz1.
*/
private static int getDistance(Class<?> clz1, Class<?> clz2) {
Class superClz;
Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/DefaultPersistenceDelegatesFactory.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/DefaultPersistenceDelegatesFactory.java?view=diff&rev=442538&r1=442537&r2=442538
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/DefaultPersistenceDelegatesFactory.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/DefaultPersistenceDelegatesFactory.java Tue Sep 12 02:12:58 2006
@@ -36,16 +36,19 @@
private static PersistenceDelegate createPersistenceDelegate(Class type) {
PersistenceDelegate pd = null;
+
try {
String className = createDefaultNameForPersistenceDelegateClass(type);
+
pd = (PersistenceDelegate) Class.forName(className, true,
type.getClassLoader()).newInstance();
} catch (Exception e) {
Class ancestor = type.getSuperclass();
- while (ancestor != null) {
+ while (pd == null && ancestor != null) {
try {
String className = createDefaultNameForPersistenceDelegateClass(ancestor);
+
pd = (PersistenceDelegate) Class.forName(className, true,
type.getClassLoader()).newInstance();
} catch (Exception e2) {
Added: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_reflect_ProxyPersistenceDelegate.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_reflect_ProxyPersistenceDelegate.java?view=auto&rev=442538
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_reflect_ProxyPersistenceDelegate.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_reflect_ProxyPersistenceDelegate.java Tue Sep 12 02:12:58 2006
@@ -0,0 +1,49 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.beans;
+
+import java.beans.*;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.InvocationHandler;
+
+public class java_lang_reflect_ProxyPersistenceDelegate extends
+ PersistenceDelegate {
+
+ protected Expression instantiate(Object oldInstance, Encoder out) {
+ assert oldInstance instanceof Proxy : oldInstance;
+
+ Class[] interfaces = oldInstance.getClass().getInterfaces();
+ InvocationHandler handler = Proxy.getInvocationHandler(oldInstance);
+
+ return new Expression(oldInstance, Proxy.class, "newProxyInstance", //$NON-NLS-1$
+ new Object[] { oldInstance.getClass().getClassLoader(),
+ interfaces, handler });
+ }
+
+ protected void initialize(Class<?> type, Object oldInstance,
+ Object newInstance, Encoder out) {
+ // check for consistency
+ assert oldInstance instanceof Proxy : oldInstance;
+ assert newInstance instanceof Proxy : newInstance;
+ assert newInstance == oldInstance;
+ }
+
+ protected boolean mutatesTo(Object oldInstance, Object newInstance) {
+ assert oldInstance instanceof Proxy : oldInstance;
+
+ return oldInstance == newInstance;
+ }
+
+}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/CustomizedPersistenceDelegateTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/CustomizedPersistenceDelegateTest.java?view=diff&rev=442538&r1=442537&r2=442538
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/CustomizedPersistenceDelegateTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/CustomizedPersistenceDelegateTest.java Tue Sep 12 02:12:58 2006
@@ -33,211 +33,220 @@
*/
public class CustomizedPersistenceDelegateTest extends TestCase {
- private MockEncoder enc = null;
+ private MockEncoder enc = null;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ enc = new MockEncoder();
+ }
+
+ public void testStringPD() {
+ enc.writeObject("");
+ assertEquals("", enc.get(""));
+ }
+
+ public void testArrayPD() {
+ Integer[] ia = new Integer[] { new Integer(1), new Integer(11100) };
+ enc.writeObject(ia);
+ assertTrue(Arrays.equals(ia, (Integer[]) enc.get(ia)));
+ }
+
+ public void testClassPD() {
+ enc.writeObject(Integer.class);
+ assertSame(Integer.class, enc.get(Integer.class));
+ enc.writeObject(String.class);
+ assertSame(String.class, enc.get(String.class));
+ enc.writeObject(Class.class);
+ assertSame(Class.class, enc.get(Class.class));
+ }
+
+ /*
+ * Test the PD for proxy classes. Please note that this testcase has the
+ * side effect that a PD for the system classloader is registered in the
+ * encoder and may affect other testcases because the registration is
+ * static.
+ */
+ public void testProxyPD() throws Exception {
+ MyInterface o = (MyInterface) Proxy.newProxyInstance(ClassLoader
+ .getSystemClassLoader(), new Class[] { MyInterface.class },
+ new MyHandler(1));
+ PersistenceDelegate pd = new PersistenceDelegate() {
+
+ public Expression instantiate(Object o, Encoder e) {
+ return new Expression(o, ClassLoader.class,
+ "getSystemClassLoader", null);
+ }
+ };
+ enc.setPersistenceDelegate(ClassLoader.getSystemClassLoader()
+ .getClass(), pd);
+ enc.writeObject(o);
+ MyInterface o2 = (MyInterface) enc.get(o);
+ assertEquals(o.getProp(), o2.getProp());
+ }
+
+ public void testPrimitivePD() {
+ // int
+ int[] ia = new int[] { 1, 2 };
+ enc.writeObject(ia);
+ assertTrue(Arrays.equals(ia, (int[]) enc.get(ia)));
+ ia = new int[0];
+ enc.writeObject(ia);
+ assertTrue(Arrays.equals(ia, (int[]) enc.get(ia)));
+
+ // short
+ short[] sa = new short[] { 1, 2 };
+ enc.writeObject(sa);
+ assertTrue(Arrays.equals(sa, (short[]) enc.get(sa)));
+ sa = new short[0];
+ enc.writeObject(sa);
+ assertTrue(Arrays.equals(sa, (short[]) enc.get(sa)));
+
+ // long
+ long[] la = new long[] { 1, 2 };
+ enc.writeObject(la);
+ assertTrue(Arrays.equals(la, (long[]) enc.get(la)));
+ la = new long[0];
+ enc.writeObject(la);
+ assertTrue(Arrays.equals(la, (long[]) enc.get(la)));
+
+ // float
+ float[] fa = new float[] { 1, 2 };
+ enc.writeObject(fa);
+ assertTrue(Arrays.equals(fa, (float[]) enc.get(fa)));
+ fa = new float[0];
+ enc.writeObject(fa);
+ assertTrue(Arrays.equals(fa, (float[]) enc.get(fa)));
+
+ // double
+ double[] da = new double[] { -1, 2.0 };
+ enc.writeObject(da);
+ assertTrue(Arrays.equals(da, (double[]) enc.get(da)));
+ da = new double[0];
+ enc.writeObject(da);
+ assertTrue(Arrays.equals(da, (double[]) enc.get(da)));
+
+ // boolean
+ boolean[] ba = new boolean[] { true, false };
+ enc.writeObject(ba);
+ assertTrue(Arrays.equals(ba, (boolean[]) enc.get(ba)));
+ ba = new boolean[0];
+ enc.writeObject(ba);
+ assertTrue(Arrays.equals(ba, (boolean[]) enc.get(ba)));
+
+ // char
+ char[] ca = new char[] { 'c', 'b' };
+ enc.writeObject(ca);
+ assertTrue(Arrays.equals(ca, (char[]) enc.get(ca)));
+ ca = new char[0];
+ enc.writeObject(ca);
+ assertTrue(Arrays.equals(ca, (char[]) enc.get(ca)));
+
+ // byte
+ byte[] bba = new byte[] { 112, 12 };
+ enc.writeObject(bba);
+ assertTrue(Arrays.equals(bba, (byte[]) enc.get(bba)));
+ bba = new byte[0];
+ enc.writeObject(bba);
+ assertTrue(Arrays.equals(bba, (byte[]) enc.get(bba)));
+ }
+
+ public void testMethodPD() throws Exception {
+ Method m = Object.class.getMethod("getClass", (Class[]) null);
+ enc.writeObject(m);
+ assertEquals(m, enc.get(m));
+ }
+
+ public void testFieldPD() throws Exception {
+ Field f = MyHandler.class.getField("i");
+ enc.writeObject(f);
+ assertEquals(f, enc.get(f));
+ }
+
+ static class MockEncoder extends Encoder {
+
+ public void writeObject(Object o) {
+ // System.out.println("write object: " + o);
+ super.writeObject(o);
+ }
+
+ public PersistenceDelegate getPersistenceDelegate(Class<?> type) {
+ // System.out.println("getPersistenceDelegate for " + type);
+ return super.getPersistenceDelegate(type);
+ }
+
+ public void writeExpression(Expression oldExp) {
+ // System.out.println("Expression: " + oldExp);
+ super.writeExpression(oldExp);
+ }
+
+ public void writeStatement(Statement oldStat) {
+ // System.out.println("Statement: " + oldStat);
+ if (oldStat.getMethodName().equals("add")) {
+ new Throwable().printStackTrace();
+ }
+ super.writeStatement(oldStat);
+ }
+
+ public Object get(Object o) {
+ // StackTraceElement[] stack = new Throwable().getStackTrace();
+ // new Throwable().printStackTrace();
+ // System.out.println("get object: " + o);
+ return super.get(o);
+ }
+ }
+
+ /*
+ * Interface for the proxy class.
+ */
+ public static interface MyInterface {
+
+ void setProp(int i);
+
+ int getProp();
+ }
+
+ /*
+ * Handler for the proxy class.
+ */
+ public static class MyHandler implements InvocationHandler {
+
+ public int i;
+
+ public MyHandler() {
+ this.i = 0;
+ }
+
+ public MyHandler(int i) {
+ this.i = i;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+ if (method.getName().equals("setProp")) {
+ this.i = ((Integer) args[0]).intValue();
+ return null;
+ } else if (method.getName().equals("getProp")) {
+ return new Integer(i);
+ } else if (method.getName().equals("hashCode")) {
+ return this.hashCode() + 113;
+ } else if (method.getName().equals("equals") && args.length == 1) {
+ return proxy.getClass().getName().equals(
+ args[0].getClass().getName());
+ }
+ return null;
+ }
+
+ public void setI(int i) {
+ this.i = i;
+ }
+
+ public int getI() {
+ return this.i;
+ }
+ }
- protected void setUp() throws Exception {
- super.setUp();
- enc = new MockEncoder();
- }
-
- public void testStringPD() {
- enc.writeObject("");
- assertEquals("", enc.get(""));
- }
-
- public void testArrayPD() {
- Integer[] ia = new Integer[] { new Integer(1), new Integer(11100) };
- enc.writeObject(ia);
- assertTrue(Arrays.equals(ia, (Integer[]) enc.get(ia)));
- }
-
- public void testClassPD() {
- enc.writeObject(Integer.class);
- assertSame(Integer.class, enc.get(Integer.class));
- enc.writeObject(String.class);
- assertSame(String.class, enc.get(String.class));
- enc.writeObject(Class.class);
- assertSame(Class.class, enc.get(Class.class));
- }
-
- /*
- * Test the PD for proxy classes. Please note that this testcase has the
- * side effect that a PD for the system classloader is registered in the
- * encoder and may affect other testcases because the registration is
- * static.
- */
- public void testProxyPD() throws Exception {
- MyInterface o = (MyInterface) Proxy.newProxyInstance(ClassLoader
- .getSystemClassLoader(), new Class[] { MyInterface.class },
- new MyHandler(1));
- PersistenceDelegate pd = new PersistenceDelegate() {
- public Expression instantiate(Object o, Encoder e) {
- return new Expression(o, ClassLoader.class,
- "getSystemClassLoader", null);
- }
- };
- enc.setPersistenceDelegate(ClassLoader.getSystemClassLoader()
- .getClass(), pd);
- enc.writeObject(o);
- MyInterface o2 = (MyInterface) enc.get(o);
- assertEquals(o.getProp(), o2.getProp());
- }
-
- public void testPrimitivePD() {
- // int
- int[] ia = new int[] { 1, 2 };
- enc.writeObject(ia);
- assertTrue(Arrays.equals(ia, (int[]) enc.get(ia)));
- ia = new int[0];
- enc.writeObject(ia);
- assertTrue(Arrays.equals(ia, (int[]) enc.get(ia)));
-
- // short
- short[] sa = new short[] { 1, 2 };
- enc.writeObject(sa);
- assertTrue(Arrays.equals(sa, (short[]) enc.get(sa)));
- sa = new short[0];
- enc.writeObject(sa);
- assertTrue(Arrays.equals(sa, (short[]) enc.get(sa)));
-
- // long
- long[] la = new long[] { 1, 2 };
- enc.writeObject(la);
- assertTrue(Arrays.equals(la, (long[]) enc.get(la)));
- la = new long[0];
- enc.writeObject(la);
- assertTrue(Arrays.equals(la, (long[]) enc.get(la)));
-
- // float
- float[] fa = new float[] { 1, 2 };
- enc.writeObject(fa);
- assertTrue(Arrays.equals(fa, (float[]) enc.get(fa)));
- fa = new float[0];
- enc.writeObject(fa);
- assertTrue(Arrays.equals(fa, (float[]) enc.get(fa)));
-
- // double
- double[] da = new double[] { -1, 2.0 };
- enc.writeObject(da);
- assertTrue(Arrays.equals(da, (double[]) enc.get(da)));
- da = new double[0];
- enc.writeObject(da);
- assertTrue(Arrays.equals(da, (double[]) enc.get(da)));
-
- // boolean
- boolean[] ba = new boolean[] { true, false };
- enc.writeObject(ba);
- assertTrue(Arrays.equals(ba, (boolean[]) enc.get(ba)));
- ba = new boolean[0];
- enc.writeObject(ba);
- assertTrue(Arrays.equals(ba, (boolean[]) enc.get(ba)));
-
- // char
- char[] ca = new char[] { 'c', 'b' };
- enc.writeObject(ca);
- assertTrue(Arrays.equals(ca, (char[]) enc.get(ca)));
- ca = new char[0];
- enc.writeObject(ca);
- assertTrue(Arrays.equals(ca, (char[]) enc.get(ca)));
-
- // byte
- byte[] bba = new byte[] { 112, 12 };
- enc.writeObject(bba);
- assertTrue(Arrays.equals(bba, (byte[]) enc.get(bba)));
- bba = new byte[0];
- enc.writeObject(bba);
- assertTrue(Arrays.equals(bba, (byte[]) enc.get(bba)));
- }
-
- public void testMethodPD() throws Exception {
- Method m = Object.class.getMethod("getClass", (Class[])null);
- enc.writeObject(m);
- assertEquals(m, enc.get(m));
- }
-
- public void testFieldPD() throws Exception {
- Field f = MyHandler.class.getField("i");
- enc.writeObject(f);
- assertEquals(f, enc.get(f));
- }
-
- static class MockEncoder extends Encoder {
- public void writeObject(Object o) {
- // System.out.println("write object: " + o);
- super.writeObject(o);
- }
-
- public PersistenceDelegate getPersistenceDelegate(Class type) {
- // System.out.println("getPersistenceDelegate for " + type);
- return super.getPersistenceDelegate(type);
- }
-
- public void writeExpression(Expression oldExp) {
- // System.out.println("Expression: " + oldExp);
- super.writeExpression(oldExp);
- }
-
- public void writeStatement(Statement oldStat) {
- // System.out.println("Statement: " + oldStat);
- if (oldStat.getMethodName().equals("add")) {
- new Throwable().printStackTrace();
- }
- super.writeStatement(oldStat);
- }
-
- public Object get(Object o) {
- // StackTraceElement[] stack = new Throwable().getStackTrace();
- // new Throwable().printStackTrace();
- // System.out.println("get object: " + o);
- return super.get(o);
- }
- }
-
- /*
- * Interface for the proxy class.
- */
- public static interface MyInterface {
- void setProp(int i);
-
- int getProp();
- }
-
- /*
- * Handler for the proxy class.
- */
- public static class MyHandler implements InvocationHandler {
-
- public int i;
-
- public MyHandler() {
- this.i = 0;
- }
-
- public MyHandler(int i) {
- this.i = i;
- }
-
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- if (method.getName().equals("setProp")) {
- this.i = ((Integer) args[0]).intValue();
- return null;
- } else if (method.getName().equals("getProp")) {
- return new Integer(i);
- }
- return null;
- }
-
- public void setI(int i) {
- this.i = i;
- }
-
- public int getI() {
- return this.i;
- }
- }
-
public static class MockBean {
+
String str;
public MockBean() {
@@ -247,7 +256,7 @@
public String getStr() {
return str;
}
-
+
public void addStr(String s) {
str += s;
}
@@ -261,7 +270,7 @@
str += "side2";
return new MockBean();
}
-
+
public String toString() {
return str;
}