JavaSE reflection-get and decompile Method

One, get Method

  • The inherited method Method cannot be reflected and will be enforced by the compiler. Therefore, the method of the parent class is not considered when reflecting the method of a class, only the method of the current class is considered

1. Common methods

  • Reference: Common methods of the Method class
  • Class.getMethod(String name,Class…parameterTypes);
    According to the method name and related parameters, locate the Method object that needs to be searched and return, parameterTypes: a list of Method parameter types (the order of parameters should be arranged according to the parameter list when the Method is declared)
  • Class.getMethods();
    Get all public methods, including public methods declared by the class itself, public methods in the parent class, and implemented interface methods
  • Class.getDeclaredMethod(String name, Class... parameterTypes);
    returns a Method object reflecting the specified declared method of the class or interface represented by this Class object
  • Class.getDeclaredMethods();
    Get all the methods in this class, including the methods of the class itself, the methods of the overridden parent class, and the implemented interface methods

2. Test cases

public class User {
    
    

	public int id;
	
	private String name;
	
	String address;
	
	protected int sno;
	
	public User() {
    
    }
	
	public User(int id, String name, String address, int sno) {
    
    
		super();
		this.id = id;
		this.name = name;
		this.address = address;
		this.sno = sno;
	}
	
	public void study() {
    
    
		System.out.println("study...");
	}
	
	public void eat(int a,String b) {
    
    
		System.out.println("eat...");
	}

	@Override
	public String toString() {
    
    
		return "User [id=" + id + ", name=" + name + ", address=" + address + ", sno=" + sno + "]";
	}

	@Override
	public int hashCode() {
    
    
		final int prime = 31;
		int result = 1;
		result = prime * result + ((address == null) ? 0 : address.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + sno;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
    
    
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (address == null) {
    
    
			if (other.address != null)
				return false;
		} else if (!address.equals(other.address))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
    
    
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sno != other.sno)
			return false;
		return true;
	}
}
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Test {
    
    
	public static void main(String[] args) throws Exception {
    
    
		// 获取类
        Class usrClass = Class.forName("com.lzj.reflect.pojo.User");

        // 获取所有的Method(包括私有)
        Method[] methods = usrClass.getDeclaredMethods();
        
        // 遍历Method
        for(Method method : methods){
    
    
            // 获取修饰符列表
            System.out.println(Modifier.toString(method.getModifiers()));
            // 获取方法的返回值类型
            System.out.println(method.getReturnType().getSimpleName());
            // 获取方法名
            System.out.println(method.getName());
            // 方法的修饰符列表(一个方法的参数可能会有多个)
            Class[] parameterTypes = method.getParameterTypes();
            for(Class parameterType : parameterTypes){
    
    
                System.out.println(parameterType.getSimpleName());
            }
        }
	}
}

Second, decompile Method

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Test {
    
    
	public static void main(String[] args) throws Exception {
    
    
		StringBuilder s = new StringBuilder();
        Class userClass = Class.forName("com.lzj.reflect.pojo.User");
        s.append(Modifier.toString(userClass.getModifiers()) + " class "+userClass.getSimpleName()+" {\n");

        Method[] methods = userClass.getDeclaredMethods();
        for(Method method : methods){
    
    
        	
            s.append("\t");
            s.append(Modifier.toString(method.getModifiers()));
            s.append(" ");
            s.append(method.getReturnType().getSimpleName());
            s.append(" ");
            s.append(method.getName());
            s.append("(");
            
            Class[] parameterTypes = method.getParameterTypes();
            for(Class parameterType : parameterTypes){
    
    
                s.append(parameterType.getSimpleName());
                s.append(",");
            }
            
            if(parameterTypes.length > 0) {
    
    
            	s.deleteCharAt(s.length() - 1);
            }
            
            s.append("){}\n");
        }
        s.append("}");
        System.out.println(s);
	}
}

Guess you like

Origin blog.csdn.net/LvJzzZ/article/details/108977504