Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package me.j360.dubbo.modules.util.reflect; import me.j360.dubbo.modules.util.base.ExceptionUtil; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.reflect.ConstructorUtils; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * ??. * * ???modifier????CheckedUnChecked * * 1. ?? * * 2. ??? MethodInvoker ? FastMethodInvoker. * * 3. ??Field?????. */ @SuppressWarnings("unchecked") public class ReflectionUtil { /////////// /////////// /** * Getter, private/protected. */ public static <T> T invokeGetter(Object obj, String propertyName) { Method method = ClassUtil.getGetterMethod(obj.getClass(), propertyName); if (method == null) { throw new IllegalArgumentException( "Could not find getter method [" + propertyName + "] on target [" + obj + ']'); } return (T) invokeMethod(obj, method); } /** * Setter, private/protected, value?. */ public static void invokeSetter(Object obj, String propertyName, Object value) { Method method = ClassUtil.getSetterMethod(obj.getClass(), propertyName, value.getClass()); if (method == null) { throw new IllegalArgumentException( "Could not find getter method [" + propertyName + "] on target [" + obj + ']'); } invokeMethod(obj, method, value); } /** * ?, private/protected, ??getter. */ public static <T> T getFieldValue(final Object obj, final String fieldName) { Field field = ClassUtil.getAccessibleField(obj.getClass(), fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + ']'); } return getFieldValue(obj, field); } /** * ?Field, ?, ??getter. */ public static <T> T getFieldValue(final Object obj, final Field field) { try { return (T) field.get(obj); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } /** * , private/protected, ??setter. */ public static void setFieldValue(final Object obj, final String fieldName, final Object value) { Field field = ClassUtil.getAccessibleField(obj.getClass(), fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + ']'); } setField(obj, field, value); } /** * ?Field, ?, ??setter. */ public static void setField(final Object obj, Field field, final Object value) { try { field.set(obj, value); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } /** * ?Getter?, ????. */ public static <T> T getProperty(Object obj, String propertyName) { Method method = ClassUtil.getGetterMethod(obj.getClass(), propertyName); if (method != null) { try { return (T) method.invoke(obj, ArrayUtils.EMPTY_OBJECT_ARRAY); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } else { return (T) getFieldValue(obj, propertyName); } } /** * ?Setter, ???, value?. */ public static void setProperty(Object obj, String propertyName, final Object value) { Method method = ClassUtil.getSetterMethod(obj.getClass(), propertyName, value.getClass()); if (method != null) { try { method.invoke(obj, value); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } else { setFieldValue(obj, propertyName, value); } } /////////// //////////// /** * , private/protected. * * ??? */ public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return (T) invokeMethod(obj, methodName, theArgs, parameterTypes); } /** * , private/protected. * * ??? */ public static <T> T invokeMethod(final Object obj, final String methodName, final Object[] args, final Class<?>[] parameterTypes) { Method method = ClassUtil.getAccessibleMethod(obj.getClass(), methodName, parameterTypes); if (method == null) { throw new IllegalArgumentException( "Could not find method [" + methodName + "] on target [" + obj + ']'); } return invokeMethod(obj, method, args); } /** * , private/protected * * ???????. ????, ?? */ public static <T> T invokeMethodByName(final Object obj, final String methodName, final Object[] args) { Method method = ClassUtil.getAccessibleMethodByName(obj.getClass(), methodName); if (method == null) { throw new IllegalArgumentException( "Could not find method [" + methodName + "] on target [" + obj + ']'); } return invokeMethod(obj, method, args); } /** * Method */ public static <T> T invokeMethod(final Object obj, Method method, Object... args) { try { return (T) method.invoke(obj, args); } catch (Exception e) { throw ExceptionUtil.uncheckedAndWrap(e); } } ////////// //////// /** * . */ public static <T> T invokeConstructor(final Class<T> cls, Object... args) { try { return ConstructorUtils.invokeConstructor(cls, args); } catch (Exception e) { throw ExceptionUtil.uncheckedAndWrap(e); } } /////// //////// /** * ??checked exception?unchecked exception. */ public static RuntimeException convertReflectionExceptionToUnchecked(Exception e) { if ((e instanceof IllegalAccessException) || (e instanceof NoSuchMethodException)) { return new IllegalArgumentException(e); } else if (e instanceof InvocationTargetException) { return new RuntimeException(((InvocationTargetException) e).getTargetException()); } else if (e instanceof RuntimeException) { return (RuntimeException) e; } return new ExceptionUtil.UncheckedException(e); } }