/** * OpenAtlasForAndroid Project * The MIT License (MIT) Copyright (OpenAtlasForAndroid) 2015 Bunny Blue,achellies * <p/> * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to the following conditions: * <p/> * The above copyright notice and this permission notice shall be included in all copies * or substantial portions of the Software. * <p/> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @author BunnyBlue **/ package com.openatlas.hack; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * OpenAtlas internal reflect tool ,used for system inject * ****/ public class Reflect { public static boolean sIsReflectAvailable; static { sIsReflectAvailable = true; } /** * Returns a {@code Method} object which represents the method matching the * specified name and parameter types that is declared by the class * represented by this {@code Class}. * * @param name * the requested method's name. * @param parameterTypes * the parameter types of the requested method. * {@code (Class[]) null} is equivalent to the empty array. * @return the method described by {@code name} and {@code parameterTypes}. */ public static Method getMethod(Class<?> cls, String name, Class<?>... parameterTypes) { try { Method method = cls.getDeclaredMethod(name, parameterTypes); method.setAccessible(true); return method; } catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } /** * Returns the result of dynamically invoking this method. Equivalent to * {@code receiver.methodName(arg1, arg2, ... , argN)}. * * <p>If the method is static, the receiver argument is ignored (and may be null). * * <p>If the method takes no arguments, you can pass {@code (Object[]) null} instead of * allocating an empty array. * * <p>If you're calling a varargs method, you need to pass an {@code Object[]} for the * varargs parameter: that conversion is usually done in {@code javac}, not the VM, and * the reflection machinery does not do this for you. (It couldn't, because it would be * ambiguous.) * * <p>Reflective method invocation follows the usual process for method lookup. * * <p>If an exception is thrown during the invocation it is caught and * wrapped in an InvocationTargetException. This exception is then thrown. * * <p>If the invocation completes normally, the return value itself is * returned. If the method is declared to return a primitive type, the * return value is boxed. If the return type is void, null is returned. * * @param receiver * the object on which to call this method (or null for static methods) * @param args * the arguments to the method * @return the result **/ public static Object invokeMethod(Method method, Object receiver, Object... args) { try { method.setAccessible(true); return method.invoke(receiver, args); } catch (IllegalArgumentException e) { e.printStackTrace(); return null; } catch (IllegalAccessException e) { e.printStackTrace(); return null; } catch (InvocationTargetException e) { e.printStackTrace(); return null; } } /** * Returns the value of the field in the specified object. * * @param cls class * @param object the object to access * @param fieldName filed name * @return the field value, possibly boxed **/ public static Object fieldGet(Class<?> cls, Object object, String fieldName) { try { Field declaredField = cls.getDeclaredField(fieldName); declaredField.setAccessible(true); return declaredField.get(object); } catch (SecurityException e) { e.printStackTrace(); return null; } catch (NoSuchFieldException e) { e.printStackTrace(); return null; } catch (IllegalArgumentException e) { e.printStackTrace(); return null; } catch (IllegalAccessException e) { e.printStackTrace(); return null; } } /** * Returns the value of the field in the specified object. * * @param object the object to access * @return the field value, possibly boxed **/ public static Object fieldGet(Field field, Object object) { try { field.setAccessible(true); return field.get(object); } catch (SecurityException e) { e.printStackTrace(); return null; } catch (IllegalArgumentException e) { e.printStackTrace(); return null; } catch (IllegalAccessException e) { e.printStackTrace(); return null; } } /** * Sets the value of the field in the specified object to the value. * @param object the object to access * @param value the new value */ public static boolean fieldSet(Field field, Object object, Object value) { field.setAccessible(true); try { field.set(object, value); return true; } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } return false; } /** * Sets the value of the field in the specified object to the value. * * @param object the object to access * @param value the new value * @param fieldName field name */ public static boolean fieldSet(Class<?> cls, Object object, String fieldName, Object value) { try { Field field = cls.getDeclaredField(fieldName); field.setAccessible(true); field.set(object, value); return true; } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } return false; } /** * Returns a Fieldobject for the field with the given name * which is declared in the class represented by this {@code Class}. * @param clazz class * @param fieldName field name */ public static Field getField(Class<?> clazz, String fieldName) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch (NoSuchFieldException e) { e.printStackTrace(); } return null; } }