Reflection in Java, Invoke method

Source: Internet
Author: User

Invoke, which is called by the function name reflection calls the corresponding function. The following code simply describes the invoke method in Java reflection

package org.curry.tool;import java.lang.reflect.method;public class invokemethods { Public static void main (String[] args)  {  employee emp = new  employee ();   class cl = emp.getclass ();//Is Class, not class  //  GetClass gets the object of the type to which the EMP object belongs, class is the classes that describe the class   // class is specifically used to describe classes, such as describing a class with those fields, methods, constructors, and so on! The first parameter of the   try {   // getmethod method specifies a method name that needs to be called, which is the Setage method of the Employee class,    //  The second parameter is a list of parameter types that need to call the method, which is the parameter type! If no parameter can specify NULL or omit    //  the method returns a method object    //parameter must be the same as in the method, int and integer, Double and double are treated as different types of    method sage = cl.getmethod ("Setage",  new class [] { int.class }];    method gage = cl.getmethod ("GetAge",  NULL);    method pname = cl.getmethod ("Printname",  NEW CLASS[]&NBsp { string.class });    object[] args1 = { new integer (+)  };   // In the  invoke method, the second argument is a parameter list, which is a object[] array    // emp is an implicit argument that the method is not a static method that must be specified     Sage.invoke (EMP, ARGS1);//Assign value by Setter method    Integer AGE =  (Integer)   Gage.invoke (emp, null);//return value by Getter Method    int age = age.intvalue ();// Integer converted to int   system.out.println ("the employee age is: "  + age) ;    object[] args3 = { new string ("Jack")  };    Pname.invoke (EMP, ARGS3);  } catch  (exception e)  {    E.printstacktrace ();   }  system.exit (0); }}class employee { //  Define an Employee Class  public employee ()  {  age = 0;  name = null;  } //  the method that will be called  public void setage (Int a)  {  age = a; }  //  method to be called  public int getage ()  {  return age; } //   method to be called  public void printname (string n)  {  name = n;   system.out.println ("the employee name is: "  + name);  } private  int age; private string name;}

Project code:

/** * uses KeyName to filter the result set with search as the query condition  /public map<object, t> getmap (search  search,string keyname) {List<t> entities=this.getbasedao (). Search (search,  Daoparam.search_option_normal); Map<object, t> map=new linkedhashmap<object, t> ();//Get the Get method name by parameter KeyName, For example getname,getagestring methodname= "get" +keyname.substring (0,1). toUpperCase () +keyname.substring (1); for (T  entity:entities) {object key=null;try {        // Getter methods do not require parameter Method method=entity.getclass (). GetMethod (MethodName);//Reflection Call Method Key=method.invoke (entity); if ( Key instanceof basepojo) {        //If the Getter method returns a BasePojo class, Then take the ID of the Basepojo as key        key= ((basepojo<?>) key). GetId (); Map.put (key, entity);}  catch  (exception e)  {logger.error (e,e);}  }entities=null;return map;} 



Reflection in Java, Invoke method

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.