Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 17 | import java.lang.reflect.*; |
| 18 | import java.util.*; |
| 19 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 20 | class ReflectionTest { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 21 | private static boolean z = true; |
| 22 | private static byte b = 8; |
| 23 | private static char c = 'x'; |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 24 | private static double d = Math.PI; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 25 | private static float f = 3.14f; |
| 26 | private static int i = 32; |
| 27 | private static long j = 0x0123456789abcdefL; |
| 28 | private static short s = 16; |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 30 | public static void testFieldReflection() throws Exception { |
| 31 | Field f; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 33 | f = ReflectionTest.class.getDeclaredField("z"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 34 | System.out.println(f.getBoolean(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 35 | f = ReflectionTest.class.getDeclaredField("b"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 36 | System.out.println(f.getByte(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 37 | f = ReflectionTest.class.getDeclaredField("c"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 38 | System.out.println(f.getChar(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 39 | f = ReflectionTest.class.getDeclaredField("d"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 40 | System.out.println(f.getDouble(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 41 | f = ReflectionTest.class.getDeclaredField("f"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 42 | System.out.println(f.getFloat(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 43 | f = ReflectionTest.class.getDeclaredField("i"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 44 | System.out.println(f.getInt(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 45 | f = ReflectionTest.class.getDeclaredField("j"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 46 | System.out.println(f.getLong(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 47 | f = ReflectionTest.class.getDeclaredField("s"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 48 | System.out.println(f.getShort(null)); |
| 49 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 50 | f = ReflectionTest.class.getDeclaredField("z"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 51 | f.setBoolean(null, false); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 52 | f = ReflectionTest.class.getDeclaredField("b"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 53 | f.setByte(null, (byte) 7); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 54 | f = ReflectionTest.class.getDeclaredField("c"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 55 | f.setChar(null, 'y'); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 56 | f = ReflectionTest.class.getDeclaredField("d"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 57 | f.setDouble(null, 2.7); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 58 | f = ReflectionTest.class.getDeclaredField("f"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 59 | f.setFloat(null, 2.7f); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 60 | f = ReflectionTest.class.getDeclaredField("i"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 61 | f.setInt(null, 31); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 62 | f = ReflectionTest.class.getDeclaredField("j"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 63 | f.setLong(null, 63); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 64 | f = ReflectionTest.class.getDeclaredField("s"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 65 | f.setShort(null, (short) 15); |
| 66 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 67 | f = ReflectionTest.class.getDeclaredField("z"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 68 | System.out.println(f.getBoolean(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 69 | f = ReflectionTest.class.getDeclaredField("b"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 70 | System.out.println(f.getByte(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 71 | f = ReflectionTest.class.getDeclaredField("c"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 72 | System.out.println(f.getChar(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 73 | f = ReflectionTest.class.getDeclaredField("d"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 74 | System.out.println(f.getDouble(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 75 | f = ReflectionTest.class.getDeclaredField("f"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 76 | System.out.println(f.getFloat(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 77 | f = ReflectionTest.class.getDeclaredField("i"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 78 | System.out.println(f.getInt(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 79 | f = ReflectionTest.class.getDeclaredField("j"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 80 | System.out.println(f.getLong(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 81 | f = ReflectionTest.class.getDeclaredField("s"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 82 | System.out.println(f.getShort(null)); |
| 83 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 84 | f = ReflectionTest.class.getDeclaredField("z"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 85 | f.set(null, Boolean.valueOf(true)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 86 | f = ReflectionTest.class.getDeclaredField("b"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 87 | f.set(null, Byte.valueOf((byte) 6)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 88 | f = ReflectionTest.class.getDeclaredField("c"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 89 | f.set(null, Character.valueOf('z')); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 90 | f = ReflectionTest.class.getDeclaredField("d"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 91 | f.set(null, Double.valueOf(1.3)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 92 | f = ReflectionTest.class.getDeclaredField("f"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 93 | f.set(null, Float.valueOf(1.3f)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 94 | f = ReflectionTest.class.getDeclaredField("i"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 95 | f.set(null, Integer.valueOf(30)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 96 | f = ReflectionTest.class.getDeclaredField("j"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 97 | f.set(null, Long.valueOf(62)); |
| 98 | f.set(null, Integer.valueOf(62)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 99 | f = ReflectionTest.class.getDeclaredField("s"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 100 | f.set(null, Short.valueOf((short) 14)); |
| 101 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 102 | f = ReflectionTest.class.getDeclaredField("z"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 103 | System.out.println(f.getBoolean(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 104 | f = ReflectionTest.class.getDeclaredField("b"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 105 | System.out.println(f.getByte(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 106 | f = ReflectionTest.class.getDeclaredField("c"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 107 | System.out.println(f.getChar(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 108 | f = ReflectionTest.class.getDeclaredField("d"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 109 | System.out.println(f.getDouble(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 110 | f = ReflectionTest.class.getDeclaredField("f"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 111 | System.out.println(f.getFloat(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 112 | f = ReflectionTest.class.getDeclaredField("i"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 113 | System.out.println(f.getInt(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 114 | f = ReflectionTest.class.getDeclaredField("j"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 115 | System.out.println(f.getLong(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 116 | f = ReflectionTest.class.getDeclaredField("s"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 117 | System.out.println(f.getShort(null)); |
| 118 | |
| 119 | try { |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 120 | f = ReflectionTest.class.getDeclaredField("s"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 121 | f.set(null, Integer.valueOf(14)); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 122 | } catch (IllegalArgumentException expected) { |
| 123 | expected.printStackTrace(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 126 | f = ReflectionTest.class.getDeclaredField("z"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 127 | show(f.get(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 128 | f = ReflectionTest.class.getDeclaredField("b"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 129 | show(f.get(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 130 | f = ReflectionTest.class.getDeclaredField("c"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 131 | show(f.get(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 132 | f = ReflectionTest.class.getDeclaredField("d"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 133 | show(f.get(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 134 | f = ReflectionTest.class.getDeclaredField("f"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 135 | show(f.get(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 136 | f = ReflectionTest.class.getDeclaredField("i"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 137 | show(f.get(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 138 | f = ReflectionTest.class.getDeclaredField("j"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 139 | show(f.get(null)); |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 140 | f = ReflectionTest.class.getDeclaredField("s"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 141 | show(f.get(null)); |
| 142 | |
| 143 | /* |
| 144 | private static boolean z = true; |
| 145 | private static byte b = 8; |
| 146 | private static char c = 'x'; |
| 147 | private static double d = Math.PI; |
| 148 | private static float f = 3.14f; |
| 149 | private static int i = 32; |
| 150 | private static long j = 0x0123456789abcdefL; |
| 151 | private static short s = 16; |
| 152 | */ |
| 153 | } |
| 154 | |
| 155 | private static void show(Object o) { |
| 156 | System.out.println(o + " (" + (o != null ? o.getClass() : "null") + ")"); |
| 157 | } |
| 158 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 159 | public static void testMethodReflection() throws Exception { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 160 | System.out.println(Arrays.toString(String.class.getDeclaredConstructors())); |
| 161 | System.out.println(Arrays.toString(String.class.getDeclaredFields())); |
| 162 | System.out.println(Arrays.toString(String.class.getDeclaredMethods())); |
| 163 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 164 | System.out.println(Arrays.toString(ReflectionTest.class.getInterfaces())); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 165 | System.out.println(Arrays.toString(String.class.getInterfaces())); |
| 166 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 167 | System.out.println(ReflectionTest.class.getModifiers()); |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 168 | System.out.println(String.class.getModifiers()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 169 | |
| 170 | System.out.println(String.class.isAssignableFrom(Object.class)); |
| 171 | System.out.println(Object.class.isAssignableFrom(String.class)); |
| 172 | |
| 173 | System.out.println(String.class.isInstance("hello")); |
| 174 | System.out.println(String.class.isInstance(123)); |
| 175 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 176 | Method m; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 177 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 178 | m = ReflectionTest.class.getDeclaredMethod("IV", int.class); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 179 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 180 | show(m.invoke(null, 4444)); |
| 181 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 182 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 183 | m = ReflectionTest.class.getDeclaredMethod("IIV", int.class, int.class); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 184 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 185 | show(m.invoke(null, 1111, 2222)); |
| 186 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 187 | m = ReflectionTest.class.getDeclaredMethod("III", int.class, int.class); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 188 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 189 | show(m.invoke(null, 1111, 2222)); |
| 190 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 191 | m = ReflectionTest.class.getDeclaredMethod("sumArray", int[].class); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 192 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 193 | show(m.invoke(null, new int[] { 1, 2, 3, 4 })); |
| 194 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 195 | m = ReflectionTest.class.getDeclaredMethod("concat", String[].class); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 196 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 197 | show(m.invoke(null, (Object) new String[] { "h", "e", "l", "l", "o" })); |
| 198 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 199 | m = ReflectionTest.class.getDeclaredMethod("ZBCDFIJSV", boolean.class, byte.class, char.class, double.class, float.class, int.class, long.class, short.class); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 200 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 201 | show(m.invoke(null, true, (byte) 0, '1', 2, 3, 4, 5, (short) 6)); |
| 202 | |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 203 | m = ReflectionTest.class.getDeclaredMethod("ZBCDLFIJSV", boolean.class, byte.class, char.class, double.class, String.class, float.class, int.class, long.class, short.class); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 204 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 205 | show(m.invoke(null, true, (byte) 0, '1', 2, "hello world", 3, 4, 5, (short) 6)); |
| 206 | |
| 207 | try { |
Elliott Hughes | fe9cf7a | 2012-01-09 18:18:25 -0800 | [diff] [blame] | 208 | m = ReflectionTest.class.getDeclaredMethod("thrower"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 209 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 210 | show(m.invoke(null)); |
| 211 | System.out.println("************* should have thrown!"); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 212 | } catch (Exception expected) { |
| 213 | expected.printStackTrace(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | private static void thrower() { |
| 218 | throw new ArithmeticException("surprise!"); |
| 219 | } |
| 220 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 221 | private static int sumArray(int[] xs) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 222 | int result = 0; |
| 223 | for (int x : xs) { |
| 224 | result += x; |
| 225 | } |
| 226 | return result; |
| 227 | } |
| 228 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 229 | private static String concat(String[] strings) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 230 | String result = ""; |
| 231 | for (String s : strings) { |
| 232 | result += s; |
| 233 | } |
| 234 | return result; |
| 235 | } |
| 236 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 237 | private static void IV(int i) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 238 | System.out.println(i); |
| 239 | } |
| 240 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 241 | private static void IIV(int i, int j) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 242 | System.out.println(i + " " + j); |
| 243 | } |
| 244 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 245 | private static int III(int i, int j) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 246 | System.out.println(i + " " + j); |
| 247 | return i + j; |
| 248 | } |
| 249 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 250 | private static void ZBCDFIJSV(boolean z, byte b, char c, double d, float f, int i, long l, short s) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 251 | System.out.println(z + " " + b + " " + c + " " + d + " " + f + " " + i + " " + l + " " + s); |
| 252 | } |
| 253 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 254 | private static void ZBCDLFIJSV(boolean z, byte b, char c, double d, String string, float f, int i, long l, short s) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 255 | System.out.println(z + " " + b + " " + c + " " + d + " " + " " + string + " " + f + " " + i + " " + l + " " + s); |
| 256 | } |
| 257 | |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 258 | public static void testConstructorReflection() throws Exception { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 259 | Constructor<?> ctor; |
| 260 | |
| 261 | ctor = String.class.getConstructor(new Class[0]); |
| 262 | show(ctor.newInstance((Object[]) null)); |
| 263 | |
| 264 | ctor = String.class.getConstructor(char[].class, int.class, int.class); |
| 265 | show(ctor.newInstance(new char[] { 'x', 'y', 'z', '!' }, 1, 2)); |
| 266 | } |
| 267 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 268 | public static void main(String[] args) throws Exception { |
Elliott Hughes | d61ae75 | 2011-10-02 17:47:42 -0700 | [diff] [blame] | 269 | testFieldReflection(); |
| 270 | testMethodReflection(); |
| 271 | testConstructorReflection(); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 272 | } |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 273 | } |