Java reflection using Class.getmethod should be noted

Source: Internet
Author: User

Java reflection using Class.getmethod should be notedRecord the

Method method= Clazz.getmethod (setmethodname,propertiesclass);
If you use this method, you cannot get the parameters passed in in a polymorphic manner
Will throw an exception java.lang.NoSuchMethodException
Like what:

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 ();  }} throws an exception at this point: java.lang.NoSuchMethodException:com.jcb.xml.model.Student.setList (java.util.ArrayList) Solution:/** * Gets the corresponding method of only one parameter * @param className * @param methodName * @return */private method GetMethod (String classname,string METHODN    Ame,class<?> propertiesclass) {method[] methods=getmethods (className); for (Method method:methods) {if (Method.getname (). Equals (MethodName)) {if (Method.getparametercount () = = 1) {//The function has the same parameter type as the incoming parameter type if (Method.getparametertypes () [0].gettypename ()]. Equals (PROPERTIESCL                    Ass.gettypename ())) {return method; The parameter type of the function is the parent class of the passed-in parameter type}else if (methOd.getparametertypes () [0].gettypename (). Equals (Propertiesclass.getsuperclass (). Gettypename ())) {return                Method }else {set<string> superclassandsuperinterfacelist= this.getallsuperclassandsuper                    Interface (Propertiesclass); If the passed-in parameter type is a subclass of the parameter type, also returns the Change function if (Superclassandsuperinterfacelist.contains (Method.getparametertypes () [0].get                TypeName ())) return method;    }}}} return null;} /** * Gets all parent class type and parent class interface type * @param clazz * @return */private set<string> getallsuperclassandsuperinterface (class<?& Gt    Clazz) {set<string> superclassandsuperinterfacelist = new hashset<> ();    Getallsupersclasss (Superclassandsuperinterfacelist,clazz);    Getallsuperinterfaces (Superclassandsuperinterfacelist,clazz); return superclassandsuperinterfacelist;} /** * Recursion Gets the parent class type * @param parentclasslist * @param clazz */private SEt<string> getallsupersclasss (set<string> parentclasslist,class<?> clazz) {ParentClassList.add (    Clazz.getsuperclass (). GetName ()); The IF (Object.class.getTypeName ()!=clazz.getsuperclass (). Gettypename ()) {///parent class may also implement an interface Getallsuperinterfaces (paren        Tclasslist,clazz.getsuperclass ());    Recursive query parent class GETALLSUPERSCLASSS (Parentclasslist,clazz.getsuperclass ()); } return parentclasslist;} /** * Recursive get parent class interface * @param parentinterfacelist * @param clazz */private set<string> getallsuperinterfaces (set<strin G> parentinterfacelist,class<?> Clazz) {for (class<?> aClass:clazz.getInterfaces ()) {Parentint        Erfacelist.add (Aclass.gettypename ());    Recursive query Implementation Interface getallsuperinterfaces (PARENTINTERFACELIST,ACLASS);    } return parentinterfacelist;} The use of this getmethod instead of the reflection Class.getmethod method can be a temporary thought of this solution multi-parameter function is similar

Java reflection using Class.getmethod should be noted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.