Java Reflection GetMethod () and invoke use __java

Source: Internet
Author: User
Tags reflection

1, forname method

Forname is a static method that uses the call to get the class object corresponding to the classes name and to load the class object in.

If you save a class name in a string (such as XML), you can dynamically invoke the load when the program is run.

Note: It is only available if the calling parameter is a class name or a method.

2, Newinstance () method

Role: Instantiate an object. The return type is object. The difference with new is that new can take parameters, while newinstance () cannot initialize the parameterless class. Usually used in conjunction with forname ().

Cases:

String str = "Java.util.Date";
Class cl1= Class.forName (str);//Load Java.util.Date class
Object obj = cl1.newinstance ();//instantiated CL1

3, GetMethod () method

The GetMethod method is similar to the GetField method, and the GetField method returns a Field object based on the string representing the domain name. The GetMethod method locates the method object that needs to be found and returns it, based on its name and related parameters.

The difference between GetMethod and Getdeclaremethods methods is that the latter returns an array of method objects and needs to find the desired method object in the result.

Prototype: Method GetMethod (String name,class...parametertypes)

Parameter explanation: Name:method's name

List of parameter types for Parametertypes:method (parameter order is ordered by argument list when declaring method)

Returns: A method object that conforms to the method name and parameters

Throw Error: Nosuchmethodexception

Reason: Failed to find the method object or method name called "<init>" or "<clinit>" for which you want to query

NullPointerException

Reason: The name of the method object being queried is null

SecurityException

Reason: The calling class or its parent class does not have invoke permission

Cases:

Method M1 = Employee.class.getMethod ("GetName");
Method m2 = Employee.class.getMethod ("Raisesalary", Double.class);
The above example obtains the GetName method of the employee class and the method pointer m1,m2 of the Raisesalary method respectively.


4. Invoke method

Action: Invokes the method wrapped in the current methods object.

Prototype: Object invoke (Object Obj,object...args)

Parameter interpretation: obj: An instantiated object

Args: Parameters used for method calls

Returns: The return value of a method called based on obj and args

Throw Error: Illegalaccessexception

Reason: The method object enforces control of the Java language or does not have access to the Obj object

IllegalArgumentException

Reason: The method is instantiated and specifies that the object to be invoked is not an instantiated class or interface

Cases:

      Class L = class.forname ("Test1. A "); 
      Object obj1 = L.newinstance (); 
      object[] Obj2 = new object[1];
      Obj2[0] = new String ("Hello World"); 
      Method m = L.getmethod ("A1", new class[] {string.class});
      Object obj3 = M.invoke (obj1, OBJ2);



Related Article

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.