一、前言
关于log4j2中log4j-core源码包org.apache.logging.log4j.core.util.ReflectionUtil类映射工具类,对属性访问作用域类型isAccessible判断、扩展访问访问makeAccessible方法、获取对象指定属性getFieldValue、获取静态属性方法getStaticFieldValue、获取默认容器getDefaultConstructor方法等、,具体参见源码说明。
二、源码说明
package org.apache.logging.log4j.core.util;@b@@b@import java.lang.reflect.AccessibleObject;@b@import java.lang.reflect.Constructor;@b@import java.lang.reflect.Field;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Member;@b@import java.lang.reflect.Modifier;@b@import java.util.Objects;@b@@b@/**@b@ * Utility class for performing common reflective operations. @b@ * @since 2.1@b@ */@b@public final class ReflectionUtil {@b@ private ReflectionUtil() {@b@ }@b@@b@ /**@b@ * Indicates whether or not a {@link Member} is both public and is contained in a public class.@b@ *@b@ * @param <T> type of the object whose accessibility to test@b@ * @param member the Member to check for public accessibility (must not be {@code null}).@b@ * @return {@code true} if {@code member} is public and contained in a public class.@b@ * @throws NullPointerException if {@code member} is {@code null}.@b@ */@b@ public static <T extends AccessibleObject & Member> boolean isAccessible(final T member) {@b@ Objects.requireNonNull(member, "No member provided");@b@ return Modifier.isPublic(member.getModifiers()) && Modifier.isPublic(member.getDeclaringClass().getModifiers());@b@ }@b@@b@ /**@b@ * Makes a {@link Member} {@link AccessibleObject#isAccessible() accessible} if the member is not public.@b@ *@b@ * @param <T> type of the object to make accessible@b@ * @param member the Member to make accessible (must not be {@code null}).@b@ * @throws NullPointerException if {@code member} is {@code null}.@b@ */@b@ public static <T extends AccessibleObject & Member> void makeAccessible(final T member) {@b@ if (!isAccessible(member) && !member.isAccessible()) {@b@ member.setAccessible(true);@b@ }@b@ }@b@@b@ /**@b@ * Makes a {@link Field} {@link AccessibleObject#isAccessible() accessible} if it is not public or if it is final.@b@ *@b@ * <p>Note that using this method to make a {@code final} field writable will most likely not work very well due to@b@ * compiler optimizations and the like.</p>@b@ *@b@ * @param field the Field to make accessible (must not be {@code null}).@b@ * @throws NullPointerException if {@code field} is {@code null}.@b@ */@b@ public static void makeAccessible(final Field field) {@b@ Objects.requireNonNull(field, "No field provided");@b@ if ((!isAccessible(field) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {@b@ field.setAccessible(true);@b@ }@b@ }@b@@b@ /**@b@ * Gets the value of a {@link Field}, making it accessible if required.@b@ *@b@ * @param field the Field to obtain a value from (must not be {@code null}).@b@ * @param instance the instance to obtain the field value from or {@code null} only if the field is static.@b@ * @return the value stored by the field.@b@ * @throws NullPointerException if {@code field} is {@code null}, or if {@code instance} is {@code null} but@b@ * {@code field} is not {@code static}.@b@ * @see Field#get(Object)@b@ */@b@ public static Object getFieldValue(final Field field, final Object instance) {@b@ makeAccessible(field);@b@ if (!Modifier.isStatic(field.getModifiers())) {@b@ Objects.requireNonNull(instance, "No instance given for non-static field");@b@ }@b@ try {@b@ return field.get(instance);@b@ } catch (final IllegalAccessException e) {@b@ throw new UnsupportedOperationException(e);@b@ }@b@ }@b@@b@ /**@b@ * Gets the value of a static {@link Field}, making it accessible if required.@b@ *@b@ * @param field the Field to obtain a value from (must not be {@code null}).@b@ * @return the value stored by the static field.@b@ * @throws NullPointerException if {@code field} is {@code null}, or if {@code field} is not {@code static}.@b@ * @see Field#get(Object)@b@ */@b@ public static Object getStaticFieldValue(final Field field) {@b@ return getFieldValue(field, null);@b@ }@b@@b@ /**@b@ * Sets the value of a {@link Field}, making it accessible if required.@b@ *@b@ * @param field the Field to write a value to (must not be {@code null}).@b@ * @param instance the instance to write the value to or {@code null} only if the field is static.@b@ * @param value the (possibly wrapped) value to write to the field.@b@ * @throws NullPointerException if {@code field} is {@code null}, or if {@code instance} is {@code null} but@b@ * {@code field} is not {@code static}.@b@ * @see Field#set(Object, Object)@b@ */@b@ public static void setFieldValue(final Field field, final Object instance, final Object value) {@b@ makeAccessible(field);@b@ if (!Modifier.isStatic(field.getModifiers())) {@b@ Objects.requireNonNull(instance, "No instance given for non-static field");@b@ }@b@ try {@b@ field.set(instance, value);@b@ } catch (final IllegalAccessException e) {@b@ throw new UnsupportedOperationException(e);@b@ }@b@ }@b@@b@ /**@b@ * Sets the value of a static {@link Field}, making it accessible if required.@b@ *@b@ * @param field the Field to write a value to (must not be {@code null}).@b@ * @param value the (possibly wrapped) value to write to the field.@b@ * @throws NullPointerException if {@code field} is {@code null}, or if {@code field} is not {@code static}.@b@ * @see Field#set(Object, Object)@b@ */@b@ public static void setStaticFieldValue(final Field field, final Object value) {@b@ setFieldValue(field, null, value);@b@ }@b@@b@ /**@b@ * Gets the default (no-arg) constructor for a given class.@b@ *@b@ * @param clazz the class to find a constructor for@b@ * @param <T> the type made by the constructor@b@ * @return the default constructor for the given class@b@ * @throws IllegalStateException if no default constructor can be found@b@ */@b@ public static <T> Constructor<T> getDefaultConstructor(final Class<T> clazz) {@b@ Objects.requireNonNull(clazz, "No class provided");@b@ try {@b@ final Constructor<T> constructor = clazz.getDeclaredConstructor();@b@ makeAccessible(constructor);@b@ return constructor;@b@ } catch (final NoSuchMethodException ignored) {@b@ try {@b@ final Constructor<T> constructor = clazz.getConstructor();@b@ makeAccessible(constructor);@b@ return constructor;@b@ } catch (final NoSuchMethodException e) {@b@ throw new IllegalStateException(e);@b@ }@b@ }@b@ }@b@@b@ /**@b@ * Constructs a new {@code T} object using the default constructor of its class. Any exceptions thrown by the@b@ * constructor will be rethrown by this method, possibly wrapped in an@b@ * {@link java.lang.reflect.UndeclaredThrowableException}.@b@ *@b@ * @param clazz the class to use for instantiation.@b@ * @param <T> the type of the object to construct.@b@ * @return a new instance of T made from its default constructor.@b@ * @throws IllegalArgumentException if the given class is abstract, an interface, an array class, a primitive type,@b@ * or void@b@ * @throws IllegalStateException if access is denied to the constructor, or there are no default constructors@b@ */@b@ public static <T> T instantiate(final Class<T> clazz) {@b@ Objects.requireNonNull(clazz, "No class provided");@b@ final Constructor<T> constructor = getDefaultConstructor(clazz);@b@ try {@b@ return constructor.newInstance();@b@ } catch (final LinkageError | InstantiationException e) {@b@ // LOG4J2-1051@b@ // On platforms like Google App Engine and Android, some JRE classes are not supported: JMX, JNDI, etc.@b@ throw new IllegalArgumentException(e);@b@ } catch (final IllegalAccessException e) {@b@ throw new IllegalStateException(e);@b@ } catch (final InvocationTargetException e) {@b@ Throwables.rethrow(e.getCause());@b@ throw new InternalError("Unreachable");@b@ }@b@ }@b@}