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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: apk/hijack/org/chromium/deconstructed/Reflect.java
diff --git a/apk/hijack/org/chromium/deconstructed/Reflect.java b/apk/hijack/org/chromium/deconstructed/Reflect.java
new file mode 100644
index 0000000000000000000000000000000000000000..07d60dff447288b8457e8ff9d03571d67a0a80bc
--- /dev/null
+++ b/apk/hijack/org/chromium/deconstructed/Reflect.java
@@ -0,0 +1,128 @@
+package org.chromium.deconstructed;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+public class Reflect {
+ public static Object invokeMethod(Object instance, String name, Object... params)
+ throws NoSuchMethodException, InvocationTargetException {
+ return invokeMethodInternal(instance.getClass(), instance, name, params);
+ }
+
+ public static Object invokeStaticMethod(String className, String name, Object... params)
+ throws NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
+ Class<?> clazz = Class.forName(className);
+ return invokeMethodInternal(clazz, null, name, params);
+ }
+
+ public static Object newInstance(ClassLoader loader, String className, Object... params)
+ throws NoSuchMethodException, InstantiationException, InvocationTargetException {
+ Class clazz = (Class)invokeMethod(loader, "findClass", className);
+ Constructor constructor = findConstructor(clazz, params);
+ try {
+ constructor.setAccessible(true);
+ return constructor.newInstance(params);
+ } catch (IllegalAccessException e) {
+ // This shouldn't happen.
+ }
+ return null;
+ }
+
+ public static void setField(Object instance, String name, Object val)
+ throws NoSuchFieldException {
+ Field field = findField(instance, name);
+ try {
+ field.setAccessible(true);
+ field.set(instance, val);
+ } catch (IllegalAccessException e) {
+ // This shouldn't happen.
+ }
+ }
+
+ public static Object getField(Object instance, String name) throws NoSuchFieldException {
+ Field field = findField(instance, name);
+ try {
+ field.setAccessible(true);
+ return field.get(instance);
+ } catch (IllegalAccessException e) {
+ // This shouldn't happen.
+ }
+ return null;
+ }
+
+ public static Object[] concatArrays(Object[] left, Object[] right) {
+ Object[] result = (Object[]) (Array.newInstance(
+ left.getClass().getComponentType(), left.length + right.length));
+ System.arraycopy(left, 0, result, 0, left.length);
+ System.arraycopy(right, 0, result, left.length, right.length);
+ return result;
+ }
+
+ private static Object invokeMethodInternal(Class<?> clazz, Object instance, String name, Object... params)
+ throws NoSuchMethodException, InvocationTargetException {
+ Method method = findMethod(clazz, name, params);
+ try {
+ method.setAccessible(true);
+ return method.invoke(instance, params);
+ } catch (IllegalAccessException e) {
+ // This shouldn't happen.
+ }
+ return null;
+ }
+
+ private static Method findMethod(Class<?> clazz, String name, Object... params)
+ throws NoSuchMethodException {
+ for (; clazz != null; clazz = clazz.getSuperclass()) {
+ for (Method method : clazz.getDeclaredMethods()) {
+ if (method.getName().equals(name) && isMethodCallableWith(method, params)) {
+ return method;
+ }
+ }
+ }
+ throw new NoSuchMethodException("Method " + name + " with parameters "
+ + Arrays.asList(params) + " not found in " + clazz);
+ }
+
+ private static boolean isMethodCallableWith(Method method, Object... params) {
+ Class<?>[] parameterTypes = method.getParameterTypes();
+ if (params.length != parameterTypes.length) return false;
+ for (int i = 0; i < params.length; i++) {
+ if (!parameterTypes[i].isAssignableFrom(params[i].getClass())) return false;
+ }
+ return true;
+ }
+
+ private static Constructor findConstructor(Class clazz, Object... params)
+ throws NoSuchMethodException {
+ for (Constructor constructor : clazz.getDeclaredConstructors()) {
+ if (isConstructorCallableWith(constructor, params)) {
+ return constructor;
+ }
+ }
+ throw new NoSuchMethodException("Constructor with parameters " + Arrays.asList(params)
+ + " not found in " + clazz);
+ }
+
+ private static boolean isConstructorCallableWith(Constructor constructor, Object... params) {
+ Class<?>[] parameterTypes = constructor.getParameterTypes();
+ if (params.length != parameterTypes.length) return false;
+ for (int i = 0; i < params.length; i++) {
+ if (!parameterTypes[i].isAssignableFrom(params[i].getClass())) return false;
+ }
+ return true;
+ }
+
+ private static Field findField(Object instance, String name) throws NoSuchFieldException {
+ for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
+ try {
+ return clazz.getDeclaredField(name);
+ } catch (NoSuchFieldException e) {
+ }
+ }
+ throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698