Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: apk/hijack/org/chromium/deconstructed/Reflect.java

Issue 949803002: Deconstructed APK prototyping Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 package org.chromium.deconstructed;
2
3 import java.lang.reflect.Array;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.Field;
6 import java.lang.reflect.InvocationTargetException;
7 import java.lang.reflect.Method;
8 import java.util.Arrays;
9
10 public class Reflect {
11 public static Object invokeMethod(Object instance, String name, Object... pa rams)
12 throws NoSuchMethodException, InvocationTargetException {
13 return invokeMethodInternal(instance.getClass(), instance, name, params) ;
14 }
15
16 public static Object invokeStaticMethod(String className, String name, Objec t... params)
17 throws NoSuchMethodException, InvocationTargetException, ClassNotFou ndException {
18 Class<?> clazz = Class.forName(className);
19 return invokeMethodInternal(clazz, null, name, params);
20 }
21
22 public static Object newInstance(ClassLoader loader, String className, Objec t... params)
23 throws NoSuchMethodException, InstantiationException, InvocationTarg etException {
24 Class clazz = (Class)invokeMethod(loader, "findClass", className);
25 Constructor constructor = findConstructor(clazz, params);
26 try {
27 constructor.setAccessible(true);
28 return constructor.newInstance(params);
29 } catch (IllegalAccessException e) {
30 // This shouldn't happen.
31 }
32 return null;
33 }
34
35 public static void setField(Object instance, String name, Object val)
36 throws NoSuchFieldException {
37 Field field = findField(instance, name);
38 try {
39 field.setAccessible(true);
40 field.set(instance, val);
41 } catch (IllegalAccessException e) {
42 // This shouldn't happen.
43 }
44 }
45
46 public static Object getField(Object instance, String name) throws NoSuchFie ldException {
47 Field field = findField(instance, name);
48 try {
49 field.setAccessible(true);
50 return field.get(instance);
51 } catch (IllegalAccessException e) {
52 // This shouldn't happen.
53 }
54 return null;
55 }
56
57 public static Object[] concatArrays(Object[] left, Object[] right) {
58 Object[] result = (Object[]) (Array.newInstance(
59 left.getClass().getComponentType(), left.length + right.length)) ;
60 System.arraycopy(left, 0, result, 0, left.length);
61 System.arraycopy(right, 0, result, left.length, right.length);
62 return result;
63 }
64
65 private static Object invokeMethodInternal(Class<?> clazz, Object instance, String name, Object... params)
66 throws NoSuchMethodException, InvocationTargetException {
67 Method method = findMethod(clazz, name, params);
68 try {
69 method.setAccessible(true);
70 return method.invoke(instance, params);
71 } catch (IllegalAccessException e) {
72 // This shouldn't happen.
73 }
74 return null;
75 }
76
77 private static Method findMethod(Class<?> clazz, String name, Object... para ms)
78 throws NoSuchMethodException {
79 for (; clazz != null; clazz = clazz.getSuperclass()) {
80 for (Method method : clazz.getDeclaredMethods()) {
81 if (method.getName().equals(name) && isMethodCallableWith(method , params)) {
82 return method;
83 }
84 }
85 }
86 throw new NoSuchMethodException("Method " + name + " with parameters "
87 + Arrays.asList(params) + " not found in " + clazz);
88 }
89
90 private static boolean isMethodCallableWith(Method method, Object... params) {
91 Class<?>[] parameterTypes = method.getParameterTypes();
92 if (params.length != parameterTypes.length) return false;
93 for (int i = 0; i < params.length; i++) {
94 if (!parameterTypes[i].isAssignableFrom(params[i].getClass())) retur n false;
95 }
96 return true;
97 }
98
99 private static Constructor findConstructor(Class clazz, Object... params)
100 throws NoSuchMethodException {
101 for (Constructor constructor : clazz.getDeclaredConstructors()) {
102 if (isConstructorCallableWith(constructor, params)) {
103 return constructor;
104 }
105 }
106 throw new NoSuchMethodException("Constructor with parameters " + Arrays. asList(params)
107 + " not found in " + clazz);
108 }
109
110 private static boolean isConstructorCallableWith(Constructor constructor, Ob ject... params) {
111 Class<?>[] parameterTypes = constructor.getParameterTypes();
112 if (params.length != parameterTypes.length) return false;
113 for (int i = 0; i < params.length; i++) {
114 if (!parameterTypes[i].isAssignableFrom(params[i].getClass())) retur n false;
115 }
116 return true;
117 }
118
119 private static Field findField(Object instance, String name) throws NoSuchFi eldException {
120 for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz. getSuperclass()) {
121 try {
122 return clazz.getDeclaredField(name);
123 } catch (NoSuchFieldException e) {
124 }
125 }
126 throw new NoSuchFieldException("Field " + name + " not found in " + inst ance.getClass());
127 }
128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698