jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | import java.lang.annotation.Annotation; |
| 18 | import java.lang.reflect.InvocationHandler; |
| 19 | import java.lang.reflect.InvocationTargetException; |
| 20 | import java.lang.reflect.Constructor; |
| 21 | import java.lang.reflect.Field; |
| 22 | import java.lang.reflect.Method; |
| 23 | import java.lang.reflect.Proxy; |
| 24 | import java.util.Arrays; |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 25 | import java.util.Comparator; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Do some basic tests. |
| 29 | */ |
| 30 | public 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 Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 74 | Arrays.sort(methods, new Comparator<Method>() { |
| 75 | public int compare(Method o1, Method o2) { |
| 76 | return o1.getName().compareTo(o2.getName()); |
| 77 | } |
| 78 | }); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 79 | System.out.println("Proxy methods: " + Arrays.deepToString(methods)); |
| 80 | Method meth = methods[methods.length -1]; |
| 81 | System.out.println("Decl annos: " + Arrays.deepToString(meth.getDeclaredAnnotations())); |
| 82 | Annotation[][] paramAnnos = meth.getParameterAnnotations(); |
| 83 | System.out.println("Param annos (" + paramAnnos.length + ") : " |
| 84 | + Arrays.deepToString(paramAnnos)); |
| 85 | Field[] fields = proxy.getClass().getDeclaredFields(); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 86 | Arrays.sort(fields, new Comparator<Field>() { |
| 87 | public int compare(Field o1, Field o2) { |
| 88 | return o1.getName().compareTo(o2.getName()); |
| 89 | } |
| 90 | }); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 91 | System.out.println("Proxy fields: " + Arrays.deepToString(fields)); |
| 92 | } |
| 93 | |
| 94 | static Object createProxy(Object proxyMe) { |
| 95 | /* declare an object that will handle the method calls */ |
| 96 | InvocationHandler handler = new MyInvocationHandler(proxyMe); |
| 97 | |
| 98 | /* create the proxy class */ |
| 99 | Class proxyClass = Proxy.getProxyClass(Shapes.class.getClassLoader(), |
| 100 | new Class[] { Quads.class, Colors.class }); |
| 101 | |
| 102 | /* create a proxy object, passing the handler object in */ |
| 103 | Object proxy = null; |
| 104 | try { |
| 105 | Constructor<Class> cons; |
| 106 | cons = proxyClass.getConstructor( |
| 107 | new Class[] { InvocationHandler.class }); |
| 108 | //System.out.println("Constructor is " + cons); |
| 109 | proxy = cons.newInstance(new Object[] { handler }); |
| 110 | } catch (NoSuchMethodException nsme) { |
| 111 | System.err.println("failed: " + nsme); |
| 112 | } catch (InstantiationException ie) { |
| 113 | System.err.println("failed: " + ie); |
| 114 | } catch (IllegalAccessException ie) { |
| 115 | System.err.println("failed: " + ie); |
| 116 | } catch (InvocationTargetException ite) { |
| 117 | System.err.println("failed: " + ite); |
| 118 | } |
| 119 | |
| 120 | return proxy; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Some interfaces. |
| 126 | */ |
| 127 | interface Shapes { |
| 128 | public void circle(int r); |
| 129 | public int rectangle(int x, int y); |
| 130 | |
| 131 | public String blob(); |
| 132 | |
| 133 | public R0base checkMe(); |
| 134 | public void upChuck(); |
| 135 | public void upCheck() throws InterruptedException; |
| 136 | } |
| 137 | |
| 138 | interface Quads extends Shapes { |
| 139 | public int rectangle(int x, int y); |
| 140 | public int square(int x, int y); |
| 141 | public int trapezoid(int x, double off, int y); |
| 142 | |
| 143 | public R0a checkMe(); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * More interfaces. |
| 148 | */ |
| 149 | interface Colors { |
| 150 | public int red(float howRed); |
| 151 | public int green(double howGreen); |
| 152 | public double blue(int howBlue); |
| 153 | public int mauve(String apology); |
| 154 | |
| 155 | public String blob(); |
| 156 | |
| 157 | public R0aa checkMe(); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Some return types. |
| 162 | */ |
| 163 | class R0base { int mBlah; } |
| 164 | class R0a extends R0base { int mBlah_a; } |
| 165 | class R0aa extends R0a { int mBlah_aa; } |
| 166 | |
| 167 | |
| 168 | /* |
| 169 | * A class that implements them all. |
| 170 | */ |
| 171 | class Mix implements Quads, Colors { |
| 172 | public void circle(int r) { |
| 173 | System.out.println("--- circle " + r); |
| 174 | } |
| 175 | public int rectangle(int x, int y) { |
| 176 | System.out.println("--- rectangle " + x + "," + y); |
| 177 | return 4; |
| 178 | } |
| 179 | public int square(int x, int y) { |
| 180 | System.out.println("--- square " + x + "," + y); |
| 181 | return 4; |
| 182 | } |
| 183 | public int trapezoid(int x, double off, int y) { |
| 184 | System.out.println("--- trap " + x + "," + y + "," + off); |
| 185 | return 8; |
| 186 | } |
| 187 | public String blob() { |
| 188 | System.out.println("--- blob"); |
| 189 | return "mix"; |
| 190 | } |
| 191 | |
| 192 | public int red(float howRed) { |
| 193 | System.out.println("--- red " + howRed); |
| 194 | return 0; |
| 195 | } |
| 196 | public int green(double howGreen) { |
| 197 | System.out.println("--- green " + howGreen); |
| 198 | return 1; |
| 199 | } |
| 200 | public double blue(int howBlue) { |
| 201 | System.out.println("--- blue " + howBlue); |
| 202 | return 2.54; |
| 203 | } |
| 204 | public int mauve(String apology) { |
| 205 | System.out.println("--- mauve " + apology); |
| 206 | return 3; |
| 207 | } |
| 208 | |
| 209 | public R0aa checkMe() { |
| 210 | return null; |
| 211 | } |
| 212 | public void upChuck() { |
| 213 | throw new IndexOutOfBoundsException("upchuck"); |
| 214 | } |
| 215 | public void upCheck() throws InterruptedException { |
| 216 | throw new InterruptedException("upcheck"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Invocation handler, defining the implementation of the proxy functions. |
| 222 | */ |
| 223 | class MyInvocationHandler implements InvocationHandler { |
| 224 | Object mObj; |
| 225 | |
| 226 | public MyInvocationHandler(Object obj) { |
| 227 | mObj = obj; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * This is called when anything gets invoked in the proxy object. |
| 232 | */ |
| 233 | public Object invoke(Object proxy, Method method, Object[] args) |
| 234 | throws Throwable { |
| 235 | |
| 236 | Object result = null; |
| 237 | |
| 238 | // Trap Object calls. This is important here to avoid a recursive |
| 239 | // invocation of toString() in the print statements below. |
| 240 | if (method.getDeclaringClass() == java.lang.Object.class) { |
| 241 | //System.out.println("!!! object " + method.getName()); |
| 242 | if (method.getName().equals("toString")) |
| 243 | return super.toString(); |
| 244 | else if (method.getName().equals("hashCode")) |
| 245 | return Integer.valueOf(super.hashCode()); |
| 246 | else if (method.getName().equals("equals")) |
| 247 | return Boolean.valueOf(super.equals(args[0])); |
| 248 | else |
| 249 | throw new RuntimeException("huh?"); |
| 250 | } |
| 251 | |
| 252 | System.out.println("Invoke " + method); |
| 253 | if (args == null || args.length == 0) { |
| 254 | System.out.println(" (no args)"); |
| 255 | } else { |
| 256 | for (int i = 0; i < args.length; i++) |
| 257 | System.out.println(" " + i + ": " + args[i]); |
| 258 | } |
| 259 | |
| 260 | try { |
| 261 | if (true) |
| 262 | result = method.invoke(mObj, args); |
| 263 | else |
| 264 | result = -1; |
| 265 | System.out.println("Success: method " + method.getName() |
| 266 | + " res=" + result); |
| 267 | } catch (InvocationTargetException ite) { |
| 268 | throw ite.getTargetException(); |
| 269 | } catch (IllegalAccessException iae) { |
| 270 | throw new RuntimeException(iae); |
| 271 | } |
| 272 | return result; |
| 273 | } |
| 274 | } |