反射
Java反射机制是在运行状态中,对于任何一个类,都可以知道该类的所有属性和方法。对于任何一个类都可以访问该类的所有属性和方法。这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
JVM
我们编写的.java文件在被编译器编译之后会生成.class问价,然后被JVM加载生成Class对象。Class是描述每个对象的说明书。
获取Class
反射的入口是Class。
Object.getClass()
在可以获得对象的情况下直接调用getClass()
方法即可获得Class对象。
.class 标识
通过类的class
属性获得Class对象。
Class.forName() 方法
当无法访问一个类的时候,例如Android系统把一些类加上了@hide注解,所示这些类就不会出现在 Android SDK中。可以通过类的全限定名来获取Class对象。
Class名字
可以根据需要选择适当的API进行类名的获取。
Class修饰符
Class.getModifiers()
方法可以获取对应属性和方法的修饰符。
private static
。
public class Modifier {
/*
* Bootstrapping protocol between java.lang and java.lang.reflect
* packages
*/
static {
sun.reflect.ReflectionFactory factory =
AccessController.doPrivileged(
new ReflectionFactory.GetReflectionFactoryAction());
factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
}
/**
* Return {@code true} if the integer argument includes the
* {@code public} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code public} modifier; {@code false} otherwise.
*/
public static boolean isPublic(int mod) {
return (mod & PUBLIC) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code private} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code private} modifier; {@code false} otherwise.
*/
public static boolean isPrivate(int mod) {
return (mod & PRIVATE) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code protected} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code protected} modifier; {@code false} otherwise.
*/
public static boolean isProtected(int mod) {
return (mod & PROTECTED) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code static} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code static} modifier; {@code false} otherwise.
*/
public static boolean isStatic(int mod) {
return (mod & STATIC) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code final} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code final} modifier; {@code false} otherwise.
*/
public static boolean isFinal(int mod) {
return (mod & FINAL) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code synchronized} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code synchronized} modifier; {@code false} otherwise.
*/
public static boolean isSynchronized(int mod) {
return (mod & SYNCHRONIZED) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code volatile} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code volatile} modifier; {@code false} otherwise.
*/
public static boolean isVolatile(int mod) {
return (mod & VOLATILE) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code transient} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code transient} modifier; {@code false} otherwise.
*/
public static boolean isTransient(int mod) {
return (mod & TRANSIENT) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code native} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code native} modifier; {@code false} otherwise.
*/
public static boolean isNative(int mod) {
return (mod & NATIVE) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code interface} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code interface} modifier; {@code false} otherwise.
*/
public static boolean isInterface(int mod) {
return (mod & INTERFACE) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code abstract} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code abstract} modifier; {@code false} otherwise.
*/
public static boolean isAbstract(int mod) {
return (mod & ABSTRACT) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code strictfp} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code strictfp} modifier; {@code false} otherwise.
*/
public static boolean isStrict(int mod) {
return (mod & STRICT) != 0;
}
public static String toString(int mod) {
StringBuilder sb = new StringBuilder();
int len;
if ((mod & PUBLIC) != 0) sb.append("public ");
if ((mod & PROTECTED) != 0) sb.append("protected ");
if ((mod & PRIVATE) != 0) sb.append("private ");
/* Canonical order */
if ((mod & ABSTRACT) != 0) sb.append("abstract ");
if ((mod & STATIC) != 0) sb.append("static ");
if ((mod & FINAL) != 0) sb.append("final ");
if ((mod & TRANSIENT) != 0) sb.append("transient ");
if ((mod & VOLATILE) != 0) sb.append("volatile ");
if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
if ((mod & NATIVE) != 0) sb.append("native ");
if ((mod & STRICT) != 0) sb.append("strictfp ");
if ((mod & INTERFACE) != 0) sb.append("interface ");
if ((len = sb.length()) > 0) /* trim trailing space */
return sb.toString().substring(0, len-1);
return "";
}
/*
* Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
* <cite>The Java™ Virtual Machine Specification</cite>
*/
/**
* The {@code int} value representing the {@code public}
* modifier.
*/
public static final int PUBLIC = 0x00000001;
/**
* The {@code int} value representing the {@code private}
* modifier.
*/
public static final int PRIVATE = 0x00000002;
/**
* The {@code int} value representing the {@code protected}
* modifier.
*/
public static final int PROTECTED = 0x00000004;
/**
* The {@code int} value representing the {@code static}
* modifier.
*/
public static final int STATIC = 0x00000008;
/**
* The {@code int} value representing the {@code final}
* modifier.
*/
public static final int FINAL = 0x00000010;
/**
* The {@code int} value representing the {@code synchronized}
* modifier.
*/
public static final int SYNCHRONIZED = 0x00000020;
/**
* The {@code int} value representing the {@code volatile}
* modifier.
*/
public static final int VOLATILE = 0x00000040;
/**
* The {@code int} value representing the {@code transient}
* modifier.
*/
public static final int TRANSIENT = 0x00000080;
/**
* The {@code int} value representing the {@code native}
* modifier.
*/
public static final int NATIVE = 0x00000100;
/**
* The {@code int} value representing the {@code interface}
* modifier.
*/
public static final int INTERFACE = 0x00000200;
/**
* The {@code int} value representing the {@code abstract}
* modifier.
*/
public static final int ABSTRACT = 0x00000400;
/**
* The {@code int} value representing the {@code strictfp}
* modifier.
*/
public static final int STRICT = 0x00000800;
// Bits not (yet) exposed in the public API either because they
// have different meanings for fields and methods and there is no
// way to distinguish between the two in this class, or because
// they are not Java programming language keywords
static final int BRIDGE = 0x00000040;
static final int VARARGS = 0x00000080;
static final int SYNTHETIC = 0x00001000;
static final int ANNOTATION = 0x00002000;
static final int ENUM = 0x00004000;
static final int MANDATED = 0x00008000;
static boolean isSynthetic(int mod) {
return (mod & SYNTHETIC) != 0;
}
static boolean isMandated(int mod) {
return (mod & MANDATED) != 0;
}
// Note on the FOO_MODIFIERS fields and fooModifiers() methods:
// the sets of modifiers are not guaranteed to be constants
// across time and Java SE releases. Therefore, it would not be
// appropriate to expose an external interface to this information
// that would allow the values to be treated as Java-level
// constants since the values could be constant folded and updates
// to the sets of modifiers missed. Thus, the fooModifiers()
// methods return an unchanging values for a given release, but a
// value that can potentially change over time.
/**
* The Java source modifiers that can be applied to a class.
* @jls 8.1.1 Class Modifiers
*/
private static final int CLASS_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
Modifier.STRICT;
/**
* The Java source modifiers that can be applied to an interface.
* @jls 9.1.1 Interface Modifiers
*/
private static final int INTERFACE_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT;
/**
* The Java source modifiers that can be applied to a constructor.
* @jls 8.8.3 Constructor Modifiers
*/
private static final int CONSTRUCTOR_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
/**
* The Java source modifiers that can be applied to a method.
* @jls8.4.3 Method Modifiers
*/
private static final int METHOD_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT;
/**
* The Java source modifiers that can be applied to a field.
* @jls 8.3.1 Field Modifiers
*/
private static final int FIELD_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
Modifier.VOLATILE;
/**
* The Java source modifiers that can be applied to a method or constructor parameter.
* @jls 8.4.1 Formal Parameters
*/
private static final int PARAMETER_MODIFIERS =
Modifier.FINAL;
/**
*
*/
static final int ACCESS_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a class.
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a class.
*
* @jls 8.1.1 Class Modifiers
* @since 1.7
*/
public static int classModifiers() {
return CLASS_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to an interface.
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to an interface.
*
* @jls 9.1.1 Interface Modifiers
* @since 1.7
*/
public static int interfaceModifiers() {
return INTERFACE_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a constructor.
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a constructor.
*
* @jls 8.8.3 Constructor Modifiers
* @since 1.7
*/
public static int constructorModifiers() {
return CONSTRUCTOR_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a method.
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a method.
*
* @jls 8.4.3 Method Modifiers
* @since 1.7
*/
public static int methodModifiers() {
return METHOD_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a field.
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a field.
*
* @jls 8.3.1 Field Modifiers
* @since 1.7
*/
public static int fieldModifiers() {
return FIELD_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a parameter.
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a parameter.
*
* @jls 8.4.1 Formal Parameters
* @since 1.8
*/
public static int parameterModifiers() {
return PARAMETER_MODIFIERS;
}
}
复制代码
Modifier 还提供了一系列的静态工具方法用来对修饰符进行操作。
获取 Class 的成员
获取 Filed
//获取所有的属性,但不包括从父类继承下来的属性
public Field[] getDeclaredFields() throws SecurityException {}
//获取自身的所有的 public 属性,包括从父类继承下来的。
public Field[] getFields() throws SecurityException {}
复制代码
获取 Method
public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
public Method getMethod(String name, Class<?>... parameterTypes)
public Method[] getDeclaredMethods() throws SecurityException
public Method getMethod(String name, Class<?>... parameterTypes)
复制代码
获取 Constructor
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
public Constructor<T> getConstructor(Class<?>... parameterTypes)
public Constructor<?>[] getDeclaredConstructors() throws SecurityException
public Constructor<?>[] getConstructors() throws SecurityException
复制代码
需要注意的是,Constructor 不能从父类继承。
操作Field
获取Field类型
//能够获取到泛型类型
public Type getGenericType() {}
//基础类型
public Class<?> getType() {}
复制代码
获取Field类型修饰符
public int getModifiers() {}
复制代码
读取与设置Field内容
读取内容
public Object get(Object obj);
public int getInt(Object obj);
public long getLong(Object obj) throws IllegalArgumentException, IllegalAccessException;
public float getFloat(Object obj) throws IllegalArgumentException, IllegalAccessException;
public short getShort(Object obj) throws IllegalArgumentException, IllegalAccessException;
public double getDouble(Object obj) throws IllegalArgumentException, IllegalAccessException;
public char getChar(Object obj) throws IllegalArgumentException, IllegalAccessException;
public byte getByte(Object obj) throws IllegalArgumentException, IllegalAccessException;
public boolean getBoolean(Object obj) throws IllegalArgumentException, IllegalAccessException
复制代码
设置内容
public void set(Object obj, Object value);
public void getInt(Object obj,int value);
public void getLong(Object obj,long value) throws IllegalArgumentException, IllegalAccessException;
public void getFloat(Object obj,float value) throws IllegalArgumentException, IllegalAccessException;
public void getShort(Object obj,short value) throws IllegalArgumentException, IllegalAccessException;
public void getDouble(Object obj,double value) throws IllegalArgumentException, IllegalAccessException;
public void getChar(Object obj,char value) throws IllegalArgumentException, IllegalAccessException;
public void getByte(Object obj,byte b) throws IllegalArgumentException, IllegalAccessException;
public void getBoolean(Object obj,boolean b) throws IllegalArgumentException, IllegalAccessException
复制代码
Class 本身不对成员进行储存,它只提供检索,所以需要用Field、Method、Constructor 对象来承载这些成员,所以,针对成员的操作时,一般需要为成员指定类的实例引用。
操作Method
获取Method类型
public String getName() {}
复制代码
获取Method方法参数
public Parameter[] getParameters() {}
复制代码
Parameter.java
// 获取参数名字
public String getName() {}
// 获取参数类型
public Class<?> getType() {}
// 获取参数的修饰符
public int getModifiers() {}
// 获取所有的参数类型
public Class<?>[] getParameterTypes() {}
// 获取所有的参数类型,包括泛型
public Type[] getGenericParameterTypes() {}
复制代码
获取Method返回值类型
// 获取返回值类型
public Class<?> getReturnType() {}
// 获取返回值类型包括泛型
public Type getGenericReturnType() {}
复制代码
获取Method修饰符
public int getModifiers() {}
复制代码
获取Method异常类型
public Class<?>[] getExceptionTypes() {}
public Type[] getGenericExceptionTypes() {}
复制代码
执行Method方法
public Object invoke(Object obj, Object... args) {}
复制代码
Method 调用 invoke() 的时候,存在许多细节:
1.invoke() 方法中第一个参数 Object 实质上是 Method 所依附的 Class 对应的类的实例,如果这个方法是一个静态方法,那么 ojb 为 null,后面的可变参数 Object 对应的自然就是参数。
2.invoke() 返回的对象是 Object,所以实际上执行的时候要进行强制转换。
3.在对 Method 调用 invoke() 的时候,如果方法本身会抛出异常,那么这个异常就会经过包装,由 Method 统一抛出 InvocationTargetException。而通过 InvocationTargetException.getCause() 可以获取真正的异常。
操作Constructor
在 Java 反射机制中有两种方法可以用来创建类的对象实例:Class.newInstance() 和 Constructor.newInstance()。
1.Class.newInstance() 只能调用无参的构造方法,而 Constructor.newInstance() 则可以调用任意的构造方法。
2.Class.newInstance() 通过构造方法直接抛出异常,而 Constructor.newInstance() 会把抛出来的异常包装到 InvocationTargetException 里面去,这个和 Method 行为一致。
3.Class.newInstance() 要求构造方法能够被访问,而 Constructor.newInstance() 却能够访问 private 修饰的构造器。
操作数组
动态创建数组
Array.newInstance()
public static Object newInstance(Class<?> componentType, int... dimensions)
throws IllegalArgumentException, NegativeArraySizeException {}
复制代码
第一个参数是数组内的元素类型,第一个参数是可变参数,表示的是相应维度的数组长度限制。
Array.newInstance(String.class,2,3);
表示创建一个String[2][3]的数组,
读取与赋值数组
public static void set(Object array,
int index,
Object value)
throws IllegalArgumentException,
ArrayIndexOutOfBoundsException;
public static Object get(Object array,
int index)
throws IllegalArgumentException,
ArrayIndexOutOfBoundsException;
复制代码
操作Enum
// 用来判定 Class 对象是不是枚举类型
Class.isEnum()
// 获取所有的枚举常量
Class.getEnumConstants()
// 判断一个 Field 是不是枚举常量
java.lang.reflect.Field.isEnumConstant()
复制代码
获取与设定枚举
因为等同于 Class,所以枚举的获取与设定就可以通过 Field 中的 get() 和 set() 方法。
需要注意的是,如果要获取枚举里面的 Field、Method、Constructor 可以调用 Class 的通用 API。