OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.incrementalinstall; | |
6 | |
7 import java.lang.reflect.Array; | |
8 import java.lang.reflect.Constructor; | |
9 import java.lang.reflect.Field; | |
10 import java.lang.reflect.InvocationTargetException; | |
11 import java.lang.reflect.Method; | |
12 import java.util.Arrays; | |
13 | |
14 /** | |
15 * Reflection helper methods. | |
16 */ | |
17 final class Reflect { | |
18 /** | |
19 * Sets the value of an object's field (even if it's not visible). | |
20 * | |
21 * @param instance The object containing the field to set. | |
22 * @param name The name of the field to set. | |
23 * @param value The new value for the field. | |
24 */ | |
25 static void setField(Object instance, String name, Object value) throws NoSu chFieldException { | |
26 Field field = findField(instance, name); | |
27 try { | |
28 field.setAccessible(true); | |
29 field.set(instance, value); | |
30 } catch (IllegalAccessException e) { | |
31 // This shouldn't happen. | |
32 } | |
33 } | |
34 | |
35 /** | |
36 * Retrieves the value of an object's field (even if it's not visible). | |
37 * | |
38 * @param instance The object containing the field to set. | |
39 * @param name The name of the field to set. | |
40 * @return The field's value. Primitive values are returned as their boxed | |
41 * type. | |
42 */ | |
43 static Object getField(Object instance, String name) throws NoSuchFieldExcep tion { | |
44 Field field = findField(instance, name); | |
45 try { | |
46 field.setAccessible(true); | |
47 return field.get(instance); | |
48 } catch (IllegalAccessException e) { | |
49 // This shouldn't happen. | |
50 } | |
51 return null; | |
52 } | |
53 | |
54 /** | |
55 * Concatenates two arrays into a new array. The arrays must be of the same | |
56 * type. | |
nyquist
2015/09/16 06:30:46
Nit: How do you feel about adding an assert (or ju
agrieve
2015/09/16 14:59:59
It will already throw if the types are incompatibl
| |
57 */ | |
58 static Object[] concatArrays(Object[] left, Object[] right) { | |
59 Object[] result = (Object[]) (Array.newInstance( | |
nyquist
2015/09/16 06:30:46
Nit: paranthesis around Array.newInstance(...) cal
agrieve
2015/09/16 14:59:59
Done.
| |
60 left.getClass().getComponentType(), left.length + right.length)) ; | |
61 System.arraycopy(left, 0, result, 0, left.length); | |
62 System.arraycopy(right, 0, result, left.length, right.length); | |
63 return result; | |
64 } | |
65 | |
66 static Object invokeMethod(Object instance, String name, Object... params) | |
67 throws NoSuchMethodException, InvocationTargetException { | |
68 Class<?> clazz; | |
69 if (instance instanceof Class) { | |
70 clazz = (Class<?>) instance; | |
71 instance = null; | |
nyquist
2015/09/16 06:30:46
Nit: This reassigns a parameter value to null whic
agrieve
2015/09/16 14:59:59
Done.
| |
72 } else { | |
73 clazz = instance.getClass(); | |
74 } | |
75 Method method = findMethod(clazz, name, params); | |
76 try { | |
77 method.setAccessible(true); | |
78 return method.invoke(instance, params); | |
79 } catch (IllegalAccessException e) { | |
80 // This shouldn't happen. | |
81 } | |
82 return null; | |
83 } | |
84 | |
85 static Object newInstance(Class<?> clazz, Object... params) | |
nyquist
2015/09/16 06:30:46
Nit: Could you add a comment about whether this wo
agrieve
2015/09/16 14:59:59
Done.
| |
86 throws NoSuchMethodException, InstantiationException, InvocationTarg etException { | |
87 Constructor<?> constructor = findConstructor(clazz, params); | |
88 try { | |
89 constructor.setAccessible(true); | |
90 return constructor.newInstance(params); | |
91 } catch (IllegalAccessException e) { | |
92 // This shouldn't happen. | |
93 } | |
94 return null; | |
95 } | |
96 | |
97 private static Field findField(Object instance, String name) throws NoSuchFi eldException { | |
98 Class<?> clazz; | |
99 if (instance instanceof Class) { | |
100 clazz = (Class<?>) instance; | |
101 } else { | |
102 clazz = instance.getClass(); | |
103 } | |
104 for (; clazz != null; clazz = clazz.getSuperclass()) { | |
105 try { | |
106 return clazz.getDeclaredField(name); | |
107 } catch (NoSuchFieldException e) { | |
nyquist
2015/09/16 06:30:46
Nit: Could you add the boilerplate-like '// This s
agrieve
2015/09/16 14:59:59
Done.
| |
108 } | |
109 } | |
110 throw new NoSuchFieldException("Field " + name + " not found in " + inst ance.getClass()); | |
111 } | |
112 | |
113 private static Method findMethod(Class<?> clazz, String name, Object... para ms) | |
114 throws NoSuchMethodException { | |
115 for (; clazz != null; clazz = clazz.getSuperclass()) { | |
116 for (Method method : clazz.getDeclaredMethods()) { | |
117 if (method.getName().equals(name) | |
118 && areParametersCompatible(method.getParameterTypes(), p arams)) { | |
119 return method; | |
120 } | |
121 } | |
122 } | |
123 throw new NoSuchMethodException("Method " + name + " with parameters " | |
124 + Arrays.asList(params) + " not found in " + clazz); | |
125 } | |
126 | |
127 private static Constructor<?> findConstructor(Class<?> clazz, Object... para ms) | |
128 throws NoSuchMethodException { | |
129 for (Constructor<?> constructor : clazz.getDeclaredConstructors()) { | |
130 if (areParametersCompatible(constructor.getParameterTypes(), params) ) { | |
131 return constructor; | |
132 } | |
133 } | |
134 throw new NoSuchMethodException("Constructor with parameters " + Arrays. asList(params) | |
135 + " not found in " + clazz); | |
136 } | |
137 | |
138 private static boolean areParametersCompatible(Class<?>[] paramTypes, Object ... params) { | |
139 if (params.length != paramTypes.length) { | |
140 return false; | |
141 } | |
142 for (int i = 0; i < params.length; i++) { | |
143 if (!isAssignableFrom(paramTypes[i], params[i])) { | |
144 return false; | |
145 } | |
146 } | |
147 return true; | |
148 } | |
149 | |
150 private static boolean isAssignableFrom(Class<?> left, Object right) { | |
151 if (right == null) { | |
152 return !left.isPrimitive(); | |
153 } | |
154 Class<?> rightClazz = right.getClass(); | |
155 if (left.isPrimitive()) { | |
156 // TODO(agrieve): Fill in the rest as needed. | |
157 return left == boolean.class && rightClazz == Boolean.class | |
158 || left == int.class && rightClazz == Integer.class; | |
159 } | |
160 return left.isAssignableFrom(rightClazz); | |
161 } | |
162 } | |
OLD | NEW |