Java reflection mechanism can be used to obtain information Java classes at run time, relevant content can obtain the following:
Class Object
class name
modifier
package information
parent
interface of
the constructor
method of
variable
annotations
Examples of simple reflection:
1, to obtain class object (premise):
Class<Test> testClass = Test.class; or Class<?> aClass = Class.forName("com.alibaba.fastjson.JSONObject");
2, get the class name:
String name1 = testClass.getName(); System.out.println("类名:"+name1);
result:
Class Name: com.example.demo.Test
3, modifiers
. Class <the Test> = testClass the Test class ; // current class modifiers int modifiers = testClass.getModifiers (); System.out.println(Modifier.isAbstract(modifiers)); System.out.println(Modifier.isPublic(modifiers)); System.out.println(Modifier.isPrivate(modifiers)); // results false false false
Modifier method
Class<Test> testClass = Test.class; Method method = testClass.getMethod("getName", new Class[]{}); int modifiers1 = method.getModifiers(); System.out.println(Modifier.isPrivate(modifiers1)); System.out.println(Modifier.isPublic(modifiers1)); //result false true
4, package information
Class<Test> testClass = Test.class; Package aPackage = testClass.getPackage(); System.out.println(aPackage); // result package com.example.demo
5, the parent class
Class<Test> testClass = Test.class; Class<? super Test> superclass = testClass.getSuperclass(); String name = superclass.getName(); System.out.println(name); // result java.lang.Object
6, implemented interfaces
Class<Test> testClass = Test.class; Class<?>[] interfaces = testClass.getInterfaces(); for (Class<?> anInterface : interfaces) { System.out.println(anInterface.getName()); } // the Test class does not implement interfaces, interfaces to an empty array
7, the constructor (the constructor obtaining public shared modifier)
Class<Test> testClass = Test.class; Constructor<?>[] constructors = testClass.getConstructors(); for (Constructor<?> constructor : constructors) { System.out.println(constructor.getName()); } // result com.example.demo.Test
example:
Class<Test> testClass = Test.class; Constructor<Test> constructor = testClass.getConstructor(); Test test = constructor.newInstance(); System.out.println(test.delete()); // result delete
8, access to variables and methods
package com.example.demo; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Test { public static void main(String[] args) { //获取属性 Field[] fields = Test.class.getDeclaredFields(); for (Field field : fields) { System.out.println(field.getName()); } . The System OUT .println ( " Properties ================== method " ); Method[] methods = Test.class.getMethods(); for (Method method : methods) { String name = method.getName(); System.out.println(name); } } private String name; private String sex; public String delete() { return "delete"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
Output:
name sex Front to attribute all the way back to the current ================== class method (saved from the parent class that inherited method) main getName delete setName getSex setSex wait wait wait equals toString hashCode getClass notify notifyAll
example:
. Class <the Test> = testClass the Test class ; // method without parameters is acquired, so that the lower second parameter can be set to null Method, testClass.getMethod = getName ( " getName " , null ); // SET Method only one parameter, this method commuting second parameter is String.class method, the setName = testClass.getMethod ( " the setName " , String. class ); // if a method has two parameters, the second parameter set as required setSex = testClass.getMethod Method, ( " setSex " , new new class [] {String. class , String. class });
Gets the parameter type of a method:
Class<Test> testClass = Test.class; Method method = testClass.getMethod("getName", null); Class<?>[] parameterTypes = method.getParameterTypes(); for (Class<?> parameterType : parameterTypes) { System.out.println(parameterType.getName()); }
After return to a method of the type:
Class<Test> testClass = Test.class; Method method = testClass.getMethod("getName", null); Class<?> returnType = method.getReturnType(); System.out.println(returnType.getName()); // return type of the method is the result getname String java.lang.String
By calling the method method: If it was a static method, the first parameter method.invoke method may be set to null, the second argument is the need to perform a method argument
Test t = new Test(); t.setName("aaaaa"); Class<? extends Test> testClass = t.getClass(); Method method = testClass.getMethod("getName",null); String invoke = (String)method.invoke(t,null); System.out.println(invoke);
**** Method.setAcessible (true) This line of code by calling the setAccessible () method will be reflected off the access check Method instance specified class
9 comment
Class<Test> testClass = Test.class; Annotation[] annotations = testClass.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); }