summaryrefslogtreecommitdiffstats
path: root/test/044-proxy
diff options
context:
space:
mode:
authorIan Rogers <irogers@google.com>2011-10-14 03:29:56 -0700
committerIan Rogers <irogers@google.com>2011-10-14 21:45:27 -0700
commit466bb25416b88fabd5d4387b7c7e5cc1ece78b8c (patch)
tree8d0952cbd850f1d0ac2e43a8372ce571fb4982d1 /test/044-proxy
parenta10cd94bb77ed66fa0a8b66141c4504045c92d30 (diff)
downloadart-466bb25416b88fabd5d4387b7c7e5cc1ece78b8c.zip
art-466bb25416b88fabd5d4387b7c7e5cc1ece78b8c.tar.gz
art-466bb25416b88fabd5d4387b7c7e5cc1ece78b8c.tar.bz2
Proxy implementation
This rounds out the proxy implementation by adding missing pieces to the class linker, extending tests and fixing issues in the runtime support. There are also some tweaks for performance and to clean up Method/Object a little. A unit test of the functionality is "art/test/run-test 044" Change-Id: Id94102d10b81cd9b12b95ba8618f6187490204c4
Diffstat (limited to 'test/044-proxy')
-rw-r--r--test/044-proxy/expected.txt10
-rw-r--r--test/044-proxy/src/BasicTest.java11
-rw-r--r--test/044-proxy/src/Main.java1
-rw-r--r--test/044-proxy/src/ReturnsAndArgPassing.java206
4 files changed, 225 insertions, 3 deletions
diff --git a/test/044-proxy/expected.txt b/test/044-proxy/expected.txt
index 4be26cf..27771ce 100644
--- a/test/044-proxy/expected.txt
+++ b/test/044-proxy/expected.txt
@@ -1,3 +1,7 @@
+ReturnsAndArgPassing.testProxyReturns RUNNING
+ReturnsAndArgPassing.testProxyReturns PASSED
+ReturnsAndArgPassing.testProxyArgPassing RUNNING
+ReturnsAndArgPassing.testProxyArgPassing PASSED
Invoke public abstract void Shapes.circle(int)
0: 3
--- circle 3
@@ -45,10 +49,10 @@ Invoke public abstract void Shapes.upCheck() throws java.lang.InterruptedExcepti
(no args)
Got expected ie
-Proxy methods: [public native boolean $Proxy0.equals(java.lang.Object), public native int $Proxy0.hashCode(), public native java.lang.String $Proxy0.toString(), public native int $Proxy0.rectangle(int,int), public native int $Proxy0.square(int,int), public native int $Proxy0.trapezoid(int,double,int), public native java.lang.String $Proxy0.blob(), public native void $Proxy0.circle(int), public native void $Proxy0.upCheck(), public native void $Proxy0.upChuck(), public native double $Proxy0.blue(int), public native R0aa $Proxy0.checkMe(), public native int $Proxy0.green(double), public native int $Proxy0.mauve(java.lang.String), public native int $Proxy0.red(float)]
+Proxy methods: [public final java.lang.String $Proxy1.blob(), public final double $Proxy1.blue(int), public final R0a $Proxy1.checkMe(), public final R0base $Proxy1.checkMe(), public final R0aa $Proxy1.checkMe(), public final void $Proxy1.circle(int), public final boolean $Proxy1.equals(java.lang.Object), public final int $Proxy1.green(double), public final int $Proxy1.hashCode(), public final int $Proxy1.mauve(java.lang.String), public final int $Proxy1.rectangle(int,int), public final int $Proxy1.red(float), public final int $Proxy1.square(int,int), public final java.lang.String $Proxy1.toString(), public final int $Proxy1.trapezoid(int,double,int), public final void $Proxy1.upCheck() throws java.lang.InterruptedException, public final void $Proxy1.upChuck()]
Decl annos: []
-Param annos (1) : [[]]
-Proxy fields: [private static java.lang.Throwable[][] $Proxy0.throws]
+Param annos (0) : []
+Proxy fields: [private static java.lang.reflect.Method $Proxy1.m0, private static java.lang.reflect.Method $Proxy1.m1, private static java.lang.reflect.Method $Proxy1.m10, private static java.lang.reflect.Method $Proxy1.m11, private static java.lang.reflect.Method $Proxy1.m12, private static java.lang.reflect.Method $Proxy1.m13, private static java.lang.reflect.Method $Proxy1.m14, private static java.lang.reflect.Method $Proxy1.m15, private static java.lang.reflect.Method $Proxy1.m16, private static java.lang.reflect.Method $Proxy1.m2, private static java.lang.reflect.Method $Proxy1.m3, private static java.lang.reflect.Method $Proxy1.m4, private static java.lang.reflect.Method $Proxy1.m5, private static java.lang.reflect.Method $Proxy1.m6, private static java.lang.reflect.Method $Proxy1.m7, private static java.lang.reflect.Method $Proxy1.m8, private static java.lang.reflect.Method $Proxy1.m9]
Dupe threw expected exception
Clash threw expected exception
Clash2 threw expected exception
diff --git a/test/044-proxy/src/BasicTest.java b/test/044-proxy/src/BasicTest.java
index 2a453c4..fa1896f 100644
--- a/test/044-proxy/src/BasicTest.java
+++ b/test/044-proxy/src/BasicTest.java
@@ -22,6 +22,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
+import java.util.Comparator;
/**
* Do some basic tests.
@@ -70,6 +71,11 @@ public class BasicTest {
*/
System.out.println("");
Method[] methods = proxy.getClass().getDeclaredMethods();
+ Arrays.sort(methods, new Comparator<Method>() {
+ public int compare(Method o1, Method o2) {
+ return o1.getName().compareTo(o2.getName());
+ }
+ });
System.out.println("Proxy methods: " + Arrays.deepToString(methods));
Method meth = methods[methods.length -1];
System.out.println("Decl annos: " + Arrays.deepToString(meth.getDeclaredAnnotations()));
@@ -77,6 +83,11 @@ public class BasicTest {
System.out.println("Param annos (" + paramAnnos.length + ") : "
+ Arrays.deepToString(paramAnnos));
Field[] fields = proxy.getClass().getDeclaredFields();
+ Arrays.sort(fields, new Comparator<Field>() {
+ public int compare(Field o1, Field o2) {
+ return o1.getName().compareTo(o2.getName());
+ }
+ });
System.out.println("Proxy fields: " + Arrays.deepToString(fields));
}
diff --git a/test/044-proxy/src/Main.java b/test/044-proxy/src/Main.java
index 01926af..5396ab8 100644
--- a/test/044-proxy/src/Main.java
+++ b/test/044-proxy/src/Main.java
@@ -19,6 +19,7 @@
*/
public class Main {
public static void main(String[] args) {
+ ReturnsAndArgPassing.main(null);
BasicTest.main(null);
Clash.main(null);
Clash2.main(null);
diff --git a/test/044-proxy/src/ReturnsAndArgPassing.java b/test/044-proxy/src/ReturnsAndArgPassing.java
new file mode 100644
index 0000000..6a706cb
--- /dev/null
+++ b/test/044-proxy/src/ReturnsAndArgPassing.java
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2011 Google Inc. All Rights Reserved.
+ *
+ * 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.
+ */
+import java.lang.reflect.*;
+
+/**
+ * @author irogers@google.com (Ian Rogers)
+ */
+public class ReturnsAndArgPassing {
+
+ public static final String testName = "ReturnsAndArgPassing";
+
+ static void check(boolean x) {
+ if (!x) {
+ throw new AssertionError(testName + " Check failed");
+ }
+ }
+
+ interface MyInterface {
+ void voidFoo();
+ void voidBar();
+ boolean booleanFoo();
+ boolean booleanBar();
+ byte byteFoo();
+ byte byteBar();
+ char charFoo();
+ char charBar();
+ short shortFoo();
+ short shortBar();
+ int intFoo();
+ int intBar();
+ long longFoo();
+ long longBar();
+ float floatFoo();
+ float floatBar();
+ double doubleFoo();
+ double doubleBar();
+ Object selectArg(int select, int a, long b, float c, double d, Object x);
+ }
+
+ static int fooInvocations = 0;
+ static int barInvocations = 0;
+
+ static class MyInvocationHandler implements InvocationHandler {
+ public Object invoke(Object proxy, Method method, Object[] args) {
+ check(proxy instanceof Proxy);
+ check(method.getDeclaringClass() == MyInterface.class);
+ String name = method.getName();
+ if (name.endsWith("Foo")) {
+ check(args == null);
+ fooInvocations++;
+ } else if (name.endsWith("Bar")) {
+ check(args == null);
+ barInvocations++;
+ }
+ if (name.equals("voidFoo")) { return null; }
+ else if (name.equals("voidBar")) { return null; }
+ else if (name.equals("booleanFoo")) { return true; }
+ else if (name.equals("booleanBar")) { return false; }
+ else if (name.equals("byteFoo")) { return Byte.MAX_VALUE; }
+ else if (name.equals("byteBar")) { return Byte.MIN_VALUE; }
+ else if (name.equals("charFoo")) { return Character.MAX_VALUE; }
+ else if (name.equals("charBar")) { return Character.MIN_VALUE; }
+ else if (name.equals("shortFoo")) { return Short.MAX_VALUE; }
+ else if (name.equals("shortBar")) { return Short.MIN_VALUE; }
+ else if (name.equals("intFoo")) { return Integer.MAX_VALUE; }
+ else if (name.equals("intBar")) { return Integer.MIN_VALUE; }
+ else if (name.equals("longFoo")) { return Long.MAX_VALUE; }
+ else if (name.equals("longBar")) { return Long.MIN_VALUE; }
+ else if (name.equals("floatFoo")) { return Float.MAX_VALUE; }
+ else if (name.equals("floatBar")) { return Float.MIN_VALUE; }
+ else if (name.equals("doubleFoo")) { return Double.MAX_VALUE; }
+ else if (name.equals("doubleBar")) { return Double.MIN_VALUE; }
+ else if (name.equals("selectArg")) {
+ check(args.length == 6);
+ int select = (Integer)args[0];
+ return args[select];
+ } else {
+ throw new AssertionError("Unexpect method " + method);
+ }
+ }
+ }
+
+ static void testProxyReturns() {
+ System.out.println(testName + ".testProxyReturns RUNNING");
+ MyInvocationHandler myHandler = new MyInvocationHandler();
+ MyInterface proxyMyInterface =
+ (MyInterface)Proxy.newProxyInstance(ReturnsAndArgPassing.class.getClassLoader(),
+ new Class[] { MyInterface.class },
+ myHandler);
+ check(fooInvocations == 0);
+ proxyMyInterface.voidFoo();
+ check(fooInvocations == 1);
+
+ check(barInvocations == 0);
+ proxyMyInterface.voidBar();
+ check(barInvocations == 1);
+
+ check(fooInvocations == 1);
+ check(proxyMyInterface.booleanFoo() == true);
+ check(fooInvocations == 2);
+
+ check(barInvocations == 1);
+ check(proxyMyInterface.booleanBar() == false);
+ check(barInvocations == 2);
+
+ check(fooInvocations == 2);
+ check(proxyMyInterface.byteFoo() == Byte.MAX_VALUE);
+ check(fooInvocations == 3);
+
+ check(barInvocations == 2);
+ check(proxyMyInterface.byteBar() == Byte.MIN_VALUE);
+ check(barInvocations == 3);
+
+ check(fooInvocations == 3);
+ check(proxyMyInterface.charFoo() == Character.MAX_VALUE);
+ check(fooInvocations == 4);
+
+ check(barInvocations == 3);
+ check(proxyMyInterface.charBar() == Character.MIN_VALUE);
+ check(barInvocations == 4);
+
+ check(fooInvocations == 4);
+ check(proxyMyInterface.shortFoo() == Short.MAX_VALUE);
+ check(fooInvocations == 5);
+
+ check(barInvocations == 4);
+ check(proxyMyInterface.shortBar() == Short.MIN_VALUE);
+ check(barInvocations == 5);
+
+ check(fooInvocations == 5);
+ check(proxyMyInterface.intFoo() == Integer.MAX_VALUE);
+ check(fooInvocations == 6);
+
+ check(barInvocations == 5);
+ check(proxyMyInterface.intBar() == Integer.MIN_VALUE);
+ check(barInvocations == 6);
+
+ check(fooInvocations == 6);
+ check(proxyMyInterface.longFoo() == Long.MAX_VALUE);
+ check(fooInvocations == 7);
+
+ check(barInvocations == 6);
+ check(proxyMyInterface.longBar() == Long.MIN_VALUE);
+ check(barInvocations == 7);
+
+ check(fooInvocations == 7);
+ check(proxyMyInterface.floatFoo() == Float.MAX_VALUE);
+ check(fooInvocations == 8);
+
+ check(barInvocations == 7);
+ check(proxyMyInterface.floatBar() == Float.MIN_VALUE);
+ check(barInvocations == 8);
+
+ check(fooInvocations == 8);
+ check(proxyMyInterface.doubleFoo() == Double.MAX_VALUE);
+ check(fooInvocations == 9);
+
+ check(barInvocations == 8);
+ check(proxyMyInterface.doubleBar() == Double.MIN_VALUE);
+ check(barInvocations == 9);
+
+ System.out.println(testName + ".testProxyReturns PASSED");
+ }
+
+ static void testProxyArgPassing() {
+ System.out.println(testName + ".testProxyArgPassing RUNNING");
+ MyInvocationHandler myHandler = new MyInvocationHandler();
+ MyInterface proxyMyInterface =
+ (MyInterface)Proxy.newProxyInstance(ReturnsAndArgPassing.class.getClassLoader(),
+ new Class[] { MyInterface.class },
+ myHandler);
+
+ check((Integer)proxyMyInterface.selectArg(0, Integer.MAX_VALUE, Long.MAX_VALUE,
+ Float.MAX_VALUE, Double.MAX_VALUE, Object.class) == 0);
+ check((Integer)proxyMyInterface.selectArg(1, Integer.MAX_VALUE, Long.MAX_VALUE,
+ Float.MAX_VALUE, Double.MAX_VALUE, Object.class) == Integer.MAX_VALUE);
+ check((Long)proxyMyInterface.selectArg(2, Integer.MAX_VALUE, Long.MAX_VALUE,
+ Float.MAX_VALUE, Double.MAX_VALUE, Object.class) == Long.MAX_VALUE);
+ check((Float)proxyMyInterface.selectArg(3, Integer.MAX_VALUE, Long.MAX_VALUE,
+ Float.MAX_VALUE, Double.MAX_VALUE, Object.class) == Float.MAX_VALUE);
+ check((Double)proxyMyInterface.selectArg(4, Integer.MAX_VALUE, Long.MAX_VALUE,
+ Float.MAX_VALUE, Double.MAX_VALUE, Object.class) == Double.MAX_VALUE);
+ check(proxyMyInterface.selectArg(5, Integer.MAX_VALUE, Long.MAX_VALUE,
+ Float.MAX_VALUE, Double.MAX_VALUE, Object.class) == Object.class);
+
+ System.out.println(testName + ".testProxyArgPassing PASSED");
+ }
+
+ public static void main(String args[]) {
+ testProxyReturns();
+ testProxyArgPassing();
+ }
+}