blob: 8f4d19ed7e1632665ac1a77294401cb3cb56fa7f [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.lang.annotation.Annotation;
18import java.lang.reflect.InvocationHandler;
19import java.lang.reflect.InvocationTargetException;
20import java.lang.reflect.Constructor;
21import java.lang.reflect.Field;
22import java.lang.reflect.Method;
23import java.lang.reflect.Proxy;
24import java.util.Arrays;
Ian Rogers466bb252011-10-14 03:29:56 -070025import java.util.Comparator;
jeffhao5d1ac922011-09-29 17:41:15 -070026
27/**
28 * Do some basic tests.
29 */
30public class BasicTest {
31
32 public static void main(String[] args) {
33 Mix proxyMe = new Mix();
34 Object proxy = createProxy(proxyMe);
35
36 if (!Proxy.isProxyClass(proxy.getClass()))
37 System.err.println("not a proxy class?");
38 if (Proxy.getInvocationHandler(proxy) == null)
39 System.err.println("ERROR: Proxy.getInvocationHandler is null");
40
41 /* take it for a spin; verifies instanceof constraint */
42 Shapes shapes = (Shapes) proxy;
43 shapes.circle(3);
44 shapes.rectangle(10, 20);
45 shapes.blob();
46 Quads quads = (Quads) proxy;
47 quads.rectangle(15, 25);
48 quads.trapezoid(6, 81.18, 4);
49 Colors colors = (Colors) proxy;
50 colors.red(1.0f);
51 colors.blue(777);
52 colors.mauve("sorry");
53 colors.blob();
54
55 try {
56 shapes.upChuck();
57 System.out.println("Didn't get expected exception");
58 } catch (IndexOutOfBoundsException ioobe) {
59 System.out.println("Got expected ioobe");
60 }
61 try {
62 shapes.upCheck();
63 System.out.println("Didn't get expected exception");
64 } catch (InterruptedException ie) {
65 System.out.println("Got expected ie");
66 }
67
68 /*
69 * Exercise annotations on Proxy classes. This is mostly to ensure
70 * that annotation calls work correctly on generated classes.
71 */
72 System.out.println("");
73 Method[] methods = proxy.getClass().getDeclaredMethods();
Ian Rogers466bb252011-10-14 03:29:56 -070074 Arrays.sort(methods, new Comparator<Method>() {
75 public int compare(Method o1, Method o2) {
Jesse Wilsonecbce8f2011-10-21 19:57:36 -040076 int result = o1.getName().compareTo(o2.getName());
77 if (result != 0) {
78 return result;
79 }
80 return o1.getReturnType().getName().compareTo(o2.getReturnType().getName());
Ian Rogers466bb252011-10-14 03:29:56 -070081 }
82 });
jeffhao5d1ac922011-09-29 17:41:15 -070083 System.out.println("Proxy methods: " + Arrays.deepToString(methods));
84 Method meth = methods[methods.length -1];
85 System.out.println("Decl annos: " + Arrays.deepToString(meth.getDeclaredAnnotations()));
86 Annotation[][] paramAnnos = meth.getParameterAnnotations();
87 System.out.println("Param annos (" + paramAnnos.length + ") : "
88 + Arrays.deepToString(paramAnnos));
jeffhao5d1ac922011-09-29 17:41:15 -070089 }
90
91 static Object createProxy(Object proxyMe) {
92 /* declare an object that will handle the method calls */
93 InvocationHandler handler = new MyInvocationHandler(proxyMe);
94
95 /* create the proxy class */
96 Class proxyClass = Proxy.getProxyClass(Shapes.class.getClassLoader(),
97 new Class[] { Quads.class, Colors.class });
98
99 /* create a proxy object, passing the handler object in */
100 Object proxy = null;
101 try {
102 Constructor<Class> cons;
103 cons = proxyClass.getConstructor(
104 new Class[] { InvocationHandler.class });
105 //System.out.println("Constructor is " + cons);
106 proxy = cons.newInstance(new Object[] { handler });
107 } catch (NoSuchMethodException nsme) {
108 System.err.println("failed: " + nsme);
109 } catch (InstantiationException ie) {
110 System.err.println("failed: " + ie);
111 } catch (IllegalAccessException ie) {
112 System.err.println("failed: " + ie);
113 } catch (InvocationTargetException ite) {
114 System.err.println("failed: " + ite);
115 }
116
117 return proxy;
118 }
119}
120
121/*
122 * Some interfaces.
123 */
124interface Shapes {
125 public void circle(int r);
126 public int rectangle(int x, int y);
127
128 public String blob();
129
130 public R0base checkMe();
131 public void upChuck();
132 public void upCheck() throws InterruptedException;
133}
134
135interface Quads extends Shapes {
136 public int rectangle(int x, int y);
137 public int square(int x, int y);
138 public int trapezoid(int x, double off, int y);
139
140 public R0a checkMe();
141}
142
143/*
144 * More interfaces.
145 */
146interface Colors {
147 public int red(float howRed);
148 public int green(double howGreen);
149 public double blue(int howBlue);
150 public int mauve(String apology);
151
152 public String blob();
153
154 public R0aa checkMe();
155}
156
157/*
158 * Some return types.
159 */
160class R0base { int mBlah; }
161class R0a extends R0base { int mBlah_a; }
162class R0aa extends R0a { int mBlah_aa; }
163
164
165/*
166 * A class that implements them all.
167 */
168class Mix implements Quads, Colors {
169 public void circle(int r) {
170 System.out.println("--- circle " + r);
171 }
172 public int rectangle(int x, int y) {
173 System.out.println("--- rectangle " + x + "," + y);
174 return 4;
175 }
176 public int square(int x, int y) {
177 System.out.println("--- square " + x + "," + y);
178 return 4;
179 }
180 public int trapezoid(int x, double off, int y) {
181 System.out.println("--- trap " + x + "," + y + "," + off);
182 return 8;
183 }
184 public String blob() {
185 System.out.println("--- blob");
186 return "mix";
187 }
188
189 public int red(float howRed) {
190 System.out.println("--- red " + howRed);
191 return 0;
192 }
193 public int green(double howGreen) {
194 System.out.println("--- green " + howGreen);
195 return 1;
196 }
197 public double blue(int howBlue) {
198 System.out.println("--- blue " + howBlue);
199 return 2.54;
200 }
201 public int mauve(String apology) {
202 System.out.println("--- mauve " + apology);
203 return 3;
204 }
205
206 public R0aa checkMe() {
207 return null;
208 }
209 public void upChuck() {
210 throw new IndexOutOfBoundsException("upchuck");
211 }
212 public void upCheck() throws InterruptedException {
213 throw new InterruptedException("upcheck");
214 }
215}
216
217/*
218 * Invocation handler, defining the implementation of the proxy functions.
219 */
220class MyInvocationHandler implements InvocationHandler {
221 Object mObj;
222
223 public MyInvocationHandler(Object obj) {
224 mObj = obj;
225 }
226
227 /*
228 * This is called when anything gets invoked in the proxy object.
229 */
230 public Object invoke(Object proxy, Method method, Object[] args)
231 throws Throwable {
232
233 Object result = null;
234
235 // Trap Object calls. This is important here to avoid a recursive
236 // invocation of toString() in the print statements below.
237 if (method.getDeclaringClass() == java.lang.Object.class) {
238 //System.out.println("!!! object " + method.getName());
239 if (method.getName().equals("toString"))
240 return super.toString();
241 else if (method.getName().equals("hashCode"))
242 return Integer.valueOf(super.hashCode());
243 else if (method.getName().equals("equals"))
244 return Boolean.valueOf(super.equals(args[0]));
245 else
246 throw new RuntimeException("huh?");
247 }
248
249 System.out.println("Invoke " + method);
250 if (args == null || args.length == 0) {
251 System.out.println(" (no args)");
252 } else {
253 for (int i = 0; i < args.length; i++)
254 System.out.println(" " + i + ": " + args[i]);
255 }
256
257 try {
258 if (true)
259 result = method.invoke(mObj, args);
260 else
261 result = -1;
262 System.out.println("Success: method " + method.getName()
263 + " res=" + result);
264 } catch (InvocationTargetException ite) {
265 throw ite.getTargetException();
266 } catch (IllegalAccessException iae) {
267 throw new RuntimeException(iae);
268 }
269 return result;
270 }
271}