Definition: a mechanism that allows Java to obtain various properties in a specified class at runtime. (including interface, variable, method, etc.)
The properties that Java reflection can obtain are:
- Class object
- Class name
- Construction method
- Packet information
- Get method, specify parameter of method, return type
- variable
- Parent class
- Implemented interface
- Modifier
- generic paradigm
Before listing the method, you have an example that contains the above 10 attributes:
//Parent class public class ReflectionFather { protected void showFather(){ System.out.print("Parent class"); } } //Interface (with generics) public interface ReflectionInter<T> { void show(T t); } //Implementation class (with variable) public class Reflection extends ReflectionFather implements ReflectionInter<String>{ public String name = "reflex"; public Reflection(){ } public Reflection(String name){ this.name = name; } @Override public void show(String s) { System.out.print("name Yes,"+name+"\n"+"Generics are:"+s); } }
- Get Class object:
public class Test { public static void main(String[] args) { try { Class aClass = Class.forName("(Route).Reflection"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
- Get class name:
public class Test { public static void main(String[] args) { Class aClass = Reflection.class; System.out.print(aClass.getName()); } }
Print results:
(path). Reflection
- Get construction method:
public class Test { public static void main(String[] args) { Class aClass = Reflection.class; //Return to the list of construction methods Constructor[] constructors = aClass.getConstructors(); //Get construction method //Constructor constructor = aClass.getConstructor(new Class[]{String.class}); / / this is equivalent Constructor constructor = constructors[1]; //Get constructor parameters Class[] parameterTypes = constructor.getParameterTypes(); System.out.print(parameterTypes[0].getName()+"\n"); //Instantiate a class by constructing a method try { Object obj = constructor.newInstance("New creation"); Reflection reflection = (Reflection) obj; reflection.show("New"); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
Print results:
java.lang.String name is: new creation Generics are: New
- Get package information:
public class Test { public static void main(String[] args) { Class aClass = Reflection.class; System.out.print(aClass.getPackage()); } }
- Get method, parameter of specified method, return type:
public class Test { public static void main(String[] args) { Class aClass = Reflection.class; //Get method list Method[] methods = aClass.getMethods(); for(Method method:methods){ // System.out.print(method.getName()+"\n"); } //Get method object by method name and return type try { Method method = aClass.getMethod("show",new Class[]{String.class}); // System.out.print(method.getName()+"\n"); //Gets the parameters of the specified method Class[] parameterTypes = method.getParameterTypes(); //Gets the return type of the specified method Class returnType = method.getReturnType(); //Calling method try { Object obj = method.invoke(new Reflection(),"Pretty good"); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
Print results:
name is: Reflection The generics are: good
- Get variable:
public class Test { public static void main(String[] args) { Class aClass = Reflection.class; //Get variables Field[] fields = aClass.getFields(); for(Field f:fields){ System.out.print(f.getName()); } } }
- Get parent class:
public class Test { public static void main(String[] args) { Class aClass = Reflection.class; //Get parent class Class superclass = aClass.getSuperclass(); System.out.print(superclass.getName()); } }
- Get the implemented interface:
public class Test { public static void main(String[] args) { Class aClass = Reflection.class; //Get the implemented interface Class[] interfaces = aClass.getInterfaces(); for(Class c:interfaces){ System.out.print(c.getName()); } } }
- Get modifier:
public class Test { public static void main(String[] args) { Class aClass = ReflectionFather.class; //Get modifier System.out.print(Modifier.toString(aClass.getModifiers())); } }
- Get generics:
public class Test { List<String> list = new ArrayList<>(); public static void main(String[] args) { Class aClass = Test.class; //Get variables try { Field field = aClass.getDeclaredField("list"); //Get type Type type = field.getGenericType(); //Concrete realization class of wall brick ParameterizedTypeImpl parameterizedType = (ParameterizedTypeImpl) type; //Get type with generics Type[] genericTypes = parameterizedType.getActualTypeArguments(); System.out.print(genericTypes[0]); } catch (NoSuchFieldException e) { e.printStackTrace(); } } }