Reflection of Java Foundation

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:

  1. Class object
  2. Class name
  3. Construction method
  4. Packet information
  5. Get method, specify parameter of method, return type
  6. variable
  7. Parent class
  8. Implemented interface
  9. Modifier
  10. 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);
    }
}

  1. Get Class object:
public class Test {
    public static void main(String[] args) {
        try {
            Class aClass = Class.forName("(Route).Reflection");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
  1. 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
  1. 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
  1. Get package information:
public class Test {
    public static void main(String[] args) {
        Class aClass = Reflection.class;
        System.out.print(aClass.getPackage());
    }
}
  1. 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
  1. 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());
        }
    }
}
  1. 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());
    }
}
  1. 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());
        }
    }
}
  1. Get modifier:
public class Test {
    public static void main(String[] args) {
        Class aClass = ReflectionFather.class;
        //Get modifier
        System.out.print(Modifier.toString(aClass.getModifiers()));
    }
}
  1. 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();
        }
    }
}

Tags: Java

Posted on Fri, 29 Nov 2019 10:47:36 -0800 by Baumusu