java 反射使用 Class.getMethod 應注意

來源:互聯網
上載者:User

標籤:scl   exception   try   equal   cat   ram   記錄   rac   rgs   

java 反射使用 Class.getMethod 應注意 記錄下

Method method= clazz.getMethod(setMethodName,propertiesClass);
如果使用該方法 會不能擷取以多態方式傳入的參數
會拋異常 java.lang.NoSuchMethodException
比如:

public class Student {

private String name;private int age;private List list = new ArrayList();public String getName() {    return name;}public void setName(String name) {    this.name = name;}public int getAge() {    return age;}public void setAge(int age) {    this.age = age;}public List<Object> getList() {    return list;}public void setList(List<Object> list) {    this.list = list;}

}

 public static void main(String[] args) {    try {        Class<?> clazz= Class.forName(Student.class.getTypeName());        List list = new ArrayList();        clazz.getMethod("setList",list.getClass());    } catch (ClassNotFoundException | NoSuchMethodException e) {        e.printStackTrace();    }}    此時會拋異常:    java.lang.NoSuchMethodException: com.jcb.xml.model.Student.setList(java.util.ArrayList)    解決方案:    /** * 擷取只有一個參數的對應方法 * @param className * @param methodName * @return */private Method getMethod(String className,String methodName,Class<?> propertiesClass){    Method[] methods=getMethods(className);    for (Method method : methods) {        if(method.getName().equals(methodName)){            if(method.getParameterCount() == 1){                //該函數的參數類型和傳入參數類型相同                if(method.getParameterTypes()[0].getTypeName().equals(propertiesClass.getTypeName())){                    return method;                    //該函數的參數類型是傳入參數類型的父類                }else if(method.getParameterTypes()[0].getTypeName().equals(propertiesClass.getSuperclass().getTypeName())){                    return method;                }else                {                    Set<String> superClassAndSuperInterfaceList= this.getAllSuperClassAndSuperInterface(propertiesClass);                    //如果傳入參數類型是參數類型的子類 也返回改函數                    if(superClassAndSuperInterfaceList.contains(method.getParameterTypes()[0].getTypeName()))                        return method;                }            }        }    }    return  null;}    /** * 擷取所有父類類型和父類介面類型 * @param clazz * @return */private Set<String> getAllSuperClassAndSuperInterface(Class<?> clazz){    Set<String> superClassAndSuperInterfaceList = new HashSet<>();    getAllSupersClasss(superClassAndSuperInterfaceList,clazz);    getAllSuperInterfaces(superClassAndSuperInterfaceList,clazz);    return superClassAndSuperInterfaceList;}/** * 遞迴擷取所父類 類型 * @param parentClassList * @param clazz */private Set<String> getAllSupersClasss(Set<String> parentClassList,Class<?> clazz){    parentClassList.add(clazz.getSuperclass().getName());    if(Object.class.getTypeName()!=clazz.getSuperclass().getTypeName()){        //父類也可能實現介面        getAllSuperInterfaces(parentClassList,clazz.getSuperclass());        //遞迴查詢父類        getAllSupersClasss(parentClassList,clazz.getSuperclass());    }    return parentClassList;}/** * 遞迴擷取父類介面 * @param parentInterfaceList * @param clazz */private Set<String> getAllSuperInterfaces(Set<String> parentInterfaceList,Class<?> clazz){    for (Class<?> aClass : clazz.getInterfaces()) {        parentInterfaceList.add(aClass.getTypeName());        //遞迴查詢實現介面        getAllSuperInterfaces(parentInterfaceList,aClass);    }    return parentInterfaceList;}    使用該getMethod 代替反射 Class.getMethod 方法 就可以      暫時想到的就是這一個解決方案    多參數函數類似

java 反射使用 Class.getMethod 應注意

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.