Class Object
Class myObjectClass = MyObject.class;
String className = ... ;//在运行期获取的类名字符串Class class = Class.forName(className);//类的全名,这个全名包括类所在的包的名字
Class aClass = ... //全限定类名(包含包名)String className = aClass.getName();//全限定类名(包含包名)
String simpleClassName = aClass.getSimpleName();//类的名字(不包含包名)
int modifiers = aClass.getModifiers();//类的修饰符, 即public,private,static 等等的关键字
Modifier.isAbstract(int modifiers);Modifier.isFinal(int modifiers);//.......检查修饰符的类型
Package package = aClass.getPackage();//获取包的相关信息,比如包名,你也可以通过 Manifest 文件访问位于编译路径下 jar 包的指定信息,比如你可以在 Manifest 文件中指定包的版本编号
Class superclass = aClass.getSuperclass();//访问类的父类
Class[] interfaces = aClass.getInterfaces();//获取指定类所实现的接口集合,当前类的父类如果实现了接口,这些接口是不会在返回的 Class 集合中的,尽管实际上当前类其实已经实现了父类接口
Constructor[] constructors = aClass.getConstructors();//构造器
Method[] method = aClass.getMethods();//所有方法
Field[] method = aClass.getFields();//类的成员变量
Annotation[] annotations = aClass.getAnnotations();//类的注解
Java Constructors
Class aClass = ...//获取Class对象Constructor[] constructors = aClass.getConstructors();//Constructor 数组包含每一个声明为公有的(Public)构造方法
Constructor constructor = aClass.getConstructor(new Class[]{String.class});//知道要访问的构造方法的参数类型,获取指定的构造方法
Class[] parameterTypes = constructor.getParameterTypes();//获取指定构造方法的方法参数信息
实例化一个类:
Constructor constructor = MyObject.class.getConstructor(String.class);MyObject myObject = (MyObject)constructor.newInstance("constructor-arg1");
Get Field Object
Class aClass = ...//获取Class对象Field[] methods = aClass.getFields();
Field field = aClass.getField("someField");
String fieldName = field.getName();//获取变量名称
Object fieldType = field.getType();//变量的类型
获取或设置(get/set)变量值:
Class aClass = MyObject.classField field = aClass.getField("someField");MyObject objectInstance = new MyObject();Object value = field.get(objectInstance);//获取实例objectInstance的变量someField的值field.set(objetInstance, value);//设置变量值
Get Method Object
Class aClass = ...//获取Class对象Method[] methods = aClass.getMethods();//数组包含了指定类中声明为公有的(public)的所有变量集合
Method method = aClass.getMethod("doSomething", new Class[]{String.class});//方法名称是“doSomething”,方法参数是 String 类型, 没有参数传入第二个参数传入null
Class[] parameterTypes = method.getParameterTypes();//获取指定方法的方法参数
Class returnType = method.getReturnType();//获取指定方法的返回类型
//获取一个方法名为doSomesthing,参数类型为String的方法Method method = MyObject.class.getMethod("doSomething", String.class);Object returnValue = method.invoke(null, "parameter-value1");//第一个参数为null表示该方法是静态方法
Getter
和 Setter
不能直接寻找 getters 和 setters,你需要检查一个类所有的方法来判断哪个方法是 getters 和 setters
Getter 方法的名字以 get 开头,没有方法参数,返回一个值。
The name of the Setter method begins with a set and has a method parameter.
Accessing private variables
Class.getdeclaredfield (String name) method or Class.getdeclaredfields () method
Privatestringfield.setaccessible (true) this line of code, by calling the Setaccessible () method, turns off the reflection access check for the specified class Field instance, after which the line of code executes, whether private, Protected as well as the scope of the package access, you can access it anywhere, even if you are not within the scope of his access rights. But if you use generic code to access the code that is not within the scope of your permission is still not possible, at compile time will be error.
Accessing Private methods
Class.getdeclaredmethod (String name, class[] parametertypes) or Class.getdeclaredmethods () method
Method.setacessible (true) functions like
Reflection/Proxy/spring AOP