1f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// Copyright 2006 The Android Open Source Project 2f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 3f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectimport java.lang.reflect.*; 4f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectimport java.io.IOException; 5f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectimport java.util.Collections; 6f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 7f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/** 8f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Reflection test. 9f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 10f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectpublic class Main { 11f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project void printMethodInfo(Method meth) { 12f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Class[] params, exceptions; 13f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int i; 14f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 15f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("Method name is " + meth.getName()); 16f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Declaring class is " 17f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project + meth.getDeclaringClass().getName()); 18f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project params = meth.getParameterTypes(); 19f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project for (i = 0; i < params.length; i++) 20f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Arg " + i + ": " + params[i].getName()); 21f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project exceptions = meth.getExceptionTypes(); 22f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project for (i = 0; i < exceptions.length; i++) 23f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Exc " + i + ": " + exceptions[i].getName()); 24f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Return type is " + meth.getReturnType().getName()); 25f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Access flags are 0x" 26f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project + Integer.toHexString(meth.getModifiers())); 27f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project //System.out.println(" GenericStr is " + meth.toGenericString()); 28f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 29f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 30f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project void printFieldInfo(Field field) { 31f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("Field name is " + field.getName()); 32f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Declaring class is " 33f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project + field.getDeclaringClass().getName()); 34f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Field type is " + field.getType().getName()); 35f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Access flags are 0x" 36f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project + Integer.toHexString(field.getModifiers())); 37f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 38f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 39f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project private void showStrings(Target instance) 40f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project throws NoSuchFieldException, IllegalAccessException { 41f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 42f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Class target = Target.class; 43f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project String one, two, three, four; 44f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Field field = null; 45f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 46f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("string1"); 47f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project one = (String) field.get(instance); 48f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 49f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("string2"); 50f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project two = (String) field.get(instance); 51f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 52f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("string3"); 53f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project three = (String) field.get(instance); 54f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 55f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" ::: " + one + ":" + two + ":" + three); 56f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 57f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 58f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public void run() { 59f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Class target = Target.class; 60f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Method meth = null; 61f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Field field = null; 62f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project boolean excep; 63f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 64f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 65f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project meth = target.getMethod("myMethod", new Class[] { int.class }); 66f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 67f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (meth.getDeclaringClass() != target) 68f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project throw new RuntimeException(); 69f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printMethodInfo(meth); 70f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 71f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project meth = target.getMethod("myMethod", new Class[] { float.class }); 72f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printMethodInfo(meth); 73f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 74f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project meth = target.getMethod("myNoargMethod", (Class[]) null); 75f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printMethodInfo(meth); 76f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 77f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project meth = target.getMethod("myMethod", 78f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project new Class[] { String[].class, float.class, char.class }); 79f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printMethodInfo(meth); 80f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 81f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Target instance = new Target(); 82f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Object[] argList = new Object[] { 83f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project new String[] { "hi there" }, 84f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project new Float(3.1415926f), 85f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project new Character('Q') 86f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project }; 87f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("Before, float is " 88f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project + ((Float)argList[1]).floatValue()); 89f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 90f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Integer boxval; 91f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project boxval = (Integer) meth.invoke(instance, argList); 92f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("Result of invoke: " + boxval.intValue()); 93f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 94f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("Calling no-arg void-return method"); 95f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project meth = target.getMethod("myNoargMethod", (Class[]) null); 96f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project meth.invoke(instance, (Object[]) null); 97f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 98f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* try invoking a method that throws an exception */ 99f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project meth = target.getMethod("throwingMethod", (Class[]) null); 100f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 101f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project meth.invoke(instance, (Object[]) null); 102f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("GLITCH: didn't throw"); 103f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } catch (InvocationTargetException ite) { 104f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("Invoke got expected exception:"); 105f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(ite.getClass().getName()); 106f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(ite.getCause()); 107f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 108f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (Exception ex) { 109f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("GLITCH: invoke got wrong exception:"); 110f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project ex.printStackTrace(); 111f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 112f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(""); 113f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 114f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 115f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("string1"); 116f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (field.getDeclaringClass() != target) 117f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project throw new RuntimeException(); 118f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printFieldInfo(field); 119f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project String strVal = (String) field.get(instance); 120f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" string1 value is '" + strVal + "'"); 121f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 122f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project showStrings(instance); 123f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 124f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.set(instance, new String("a new string")); 125f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project strVal = (String) field.get(instance); 126f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" string1 value is now '" + strVal + "'"); 127f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 128f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project showStrings(instance); 129f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 130f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 131f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.set(instance, new Object()); 132f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("WARNING: able to store Object into String"); 133f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 134f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (IllegalArgumentException iae) { 135f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected illegal obj store exc"); 136f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 137f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 138f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 139f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 140f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project String four; 141f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("string4"); 142f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project four = (String) field.get(instance); 143f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("WARNING: able to access string4: " 144f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project + four); 145f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 146f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (IllegalAccessException iae) { 147f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected access exc"); 148f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 149f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (NoSuchFieldException nsfe) { 150f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got the other expected access exc"); 151f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 152f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 153f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project String three; 154f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("string3"); 155f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project three = (String) field.get(this); 156f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("WARNING: able to get string3 in wrong obj: " 157f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project + three); 158f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 159f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (IllegalArgumentException iae) { 160f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected arg exc"); 161f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 162f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 163f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* 164f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Try setting a field to null. 165f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 166f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project String four; 167f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getDeclaredField("string3"); 168f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.set(instance, null); 169f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 170f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* 171f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Do some stuff with long. 172f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 173f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project long longVal; 174f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("pubLong"); 175f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project longVal = field.getLong(instance); 176f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("pubLong initial value is " + 177f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Long.toHexString(longVal)); 178f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.setLong(instance, 0x9988776655443322L); 179f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project longVal = field.getLong(instance); 180f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("pubLong new value is " + 181f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Long.toHexString(longVal)); 182f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 183f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 184f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("superInt"); 185f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (field.getDeclaringClass() == target) 186f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project throw new RuntimeException(); 187f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printFieldInfo(field); 188f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int intVal = field.getInt(instance); 189f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" superInt value is " + intVal); 190f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Integer boxedIntVal = (Integer) field.get(instance); 191f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" superInt boxed is " + boxedIntVal); 192f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 193f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.set(instance, new Integer(20202)); 194f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project intVal = field.getInt(instance); 195f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" superInt value is now " + intVal); 196f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.setShort(instance, (short)30303); 197f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project intVal = field.getInt(instance); 198f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" superInt value (from short) is now " +intVal); 199f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.setInt(instance, 40404); 200f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project intVal = field.getInt(instance); 201f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" superInt value is now " + intVal); 202f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 203f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.set(instance, new Long(123)); 204f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("FAIL: expected exception not thrown"); 205f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 206f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (IllegalArgumentException iae) { 207f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected long->int failure"); 208f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 209f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 210f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.setLong(instance, 123); 211f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("FAIL: expected exception not thrown"); 212f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 213f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (IllegalArgumentException iae) { 214f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected long->int failure"); 215f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 216f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 217f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.set(instance, new String("abc")); 218f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("FAIL: expected exception not thrown"); 219f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 220f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (IllegalArgumentException iae) { 221f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected string->int failure"); 222f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 223f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 224f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 225f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.getShort(instance); 226f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("FAIL: expected exception not thrown"); 227f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 228f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (IllegalArgumentException iae) { 229f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected int->short failure"); 230f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 231f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 232f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("superClassInt"); 233f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printFieldInfo(field); 234f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int superClassIntVal = field.getInt(instance); 235f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" superClassInt value is " + superClassIntVal); 236f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 237f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("staticDouble"); 238f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printFieldInfo(field); 239f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project double staticDoubleVal = field.getDouble(null); 240f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" staticDoubleVal value is " + staticDoubleVal); 241f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 242f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 243f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.getLong(instance); 244f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("FAIL: expected exception not thrown"); 245f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 246f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (IllegalArgumentException iae) { 247f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected double->long failure"); 248f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 249f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 250f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project excep = false; 251f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 252f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("aPrivateInt"); 253f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printFieldInfo(field); 254f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 255f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (NoSuchFieldException nsfe) { 256f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("as expected: aPrivateInt not found"); 257f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project excep = true; 258f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 259f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (!excep) 260f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("BUG: got aPrivateInt"); 261f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 262f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 263f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("constantString"); 264f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printFieldInfo(field); 265f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project String val = (String) field.get(instance); 266f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" Constant test value is " + val); 267f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 268f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 269f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field = target.getField("cantTouchThis"); 270f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project printFieldInfo(field); 271f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project intVal = field.getInt(instance); 272f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" cantTouchThis is " + intVal); 273f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 274f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project field.setInt(instance, 99); 275f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("ERROR: set-final succeeded\n"); 276f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } catch (IllegalAccessException iae) { 277f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" got expected set-final failure\n"); 278f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 279f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project intVal = field.getInt(instance); 280f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" cantTouchThis is now " + intVal); 281f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 282f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Constructor<Target> cons; 283f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Target targ; 284f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Object[] args; 285f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 286f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project cons = target.getConstructor(new Class[] { int.class,float.class }); 287f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project args = new Object[] { new Integer(7), new Float(3.3333) }; 288f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("cons modifiers=" + cons.getModifiers()); 289f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project targ = cons.newInstance(args); 290f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project targ.myMethod(17); 291f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 292f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 293f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project catch (Exception ex) { 294f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("----- unexpected exception -----"); 295f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project ex.printStackTrace(); 296f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 297f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 298f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("ReflectTest done!"); 299f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 300f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 301f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public static void checkType() { 302f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Method m; 303f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 304f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 305f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project m = Collections.class.getDeclaredMethod("checkType", 306f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Object.class, Class.class); 307f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } catch (NoSuchMethodException nsme) { 308f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project nsme.printStackTrace(); 309f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return; 310f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 311f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 312f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project m.setAccessible(true); 313f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 314f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project m.invoke(null, new Object(), Object.class); 315f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } catch (IllegalAccessException iae) { 316f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project iae.printStackTrace(); 317f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return; 318f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } catch (InvocationTargetException ite) { 319f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project ite.printStackTrace(); 320f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return; 321f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 322f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 323f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project try { 324f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("checkType invoking null"); 325f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project m.invoke(null, new Object(), int.class); 326f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("ERROR: should throw InvocationTargetException"); 327f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } catch (InvocationTargetException ite) { 328f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("checkType got expected exception"); 329f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } catch (IllegalAccessException iae) { 330f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project iae.printStackTrace(); 331f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return; 332f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 333f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 334f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 33557ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden public static void checkInit() { 33657ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden Class niuClass = NoisyInitUser.class; 33757ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden Method[] methods; 33857ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden 33957ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden methods = niuClass.getDeclaredMethods(); 34057ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden System.out.println("got methods"); 34157ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden /* neither NoisyInit nor NoisyInitUser should be initialized yet */ 34257ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden NoisyInitUser niu = new NoisyInitUser(); 34357ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden NoisyInit ni = new NoisyInit(); 34457ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden } 34557ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden 346f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public static void main(String[] args) { 347f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Main test = new Main(); 348f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project test.run(); 349f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 350f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project checkType(); 35157ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden checkInit(); 352f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 353f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 354f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 355f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 356f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectclass SuperTarget { 357f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public SuperTarget() { 358f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("SuperTarget constructor ()V"); 359f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project superInt = 1010101; 360f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project superClassInt = 1010102; 361f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 362f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 363f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public int myMethod(float floatArg) { 364f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("myMethod (F)I " + floatArg); 365f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return 6; 366f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 367f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 368f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public int superInt; 369f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public static int superClassInt; 370f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 371f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 372f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectclass Target extends SuperTarget { 373f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public Target() { 374f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("Target constructor ()V"); 375f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 376f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 377f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public Target(int ii, float ff) { 378f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("Target constructor (IF)V : ii=" 379f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project + ii + " ff=" + ff); 380f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project anInt = ii; 381f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 382f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 383f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public int myMethod(int intarg) throws NullPointerException, IOException { 384f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("myMethod (I)I"); 385f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println(" arg=" + intarg + " anInt=" + anInt); 386f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return 5; 387f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 388f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 389f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public int myMethod(String[] strarg, float f, char c) { 390f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("myMethod: " + strarg[0] + " " + f + " " + c + " !"); 391f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return 7; 392f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 393f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 394f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public static void myNoargMethod() { 395f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("myNoargMethod ()V"); 396f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 397f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 398f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public void throwingMethod() { 399f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("throwingMethod"); 400f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project throw new NullPointerException("gratuitous throw!"); 401f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 402f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 403f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public void misc() { 404f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project System.out.println("misc"); 405f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 406f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 407f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public int anInt; 408f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public String string1 = "hey"; 409f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public String string2 = "yo"; 410f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public String string3 = "there"; 411f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project private String string4 = "naughty"; 412f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public static final String constantString = "a constant string"; 413f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project private int aPrivateInt; 414f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 415f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public final int cantTouchThis = 77; 416f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 417f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public long pubLong = 0x1122334455667788L; 418f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 419f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project public static double staticDouble = 3.3; 420f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 421f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 42257ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFaddenclass NoisyInit { 42357ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden static { 42457ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden System.out.println("NoisyInit is initializing"); 42557ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden //Throwable th = new Throwable(); 42657ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden //th.printStackTrace(); 42757ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden } 42857ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden} 42957ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden 43057ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFaddenclass NoisyInitUser { 43157ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden static { 43257ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden System.out.println("NoisyInitUser is initializing"); 43357ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden } 43457ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden public void createNoisyInit(NoisyInit ni) {} 43557ea16e87e319bf71176dd8e8d12abe84f13d2e6Andy McFadden} 436