Java notes (6) -- call methods in reflection

Source: Internet
Author: User


/**
*
* The Method Class is used to describe the methods in the Class: Method getMethod (String name, Class ... ParameterTypes)
* This Class object is returned to indicate the specified public Method of the Class and its parent Class. Method [] getMethods ():
* This Class object is returned to indicate all public methods of the Class and its parent Class. Method getDeclaredMethod (String name,
* Class ... ParameterTypes) returns the Class object that represents the specified method of the Class. It has nothing to do with access permissions, but does not include inherited methods. Method []
* GetDeclaredMethods (): obtains all methods of the class, including public, protected, default (Package) Access, and private methods, but not inherited methods;
*
*
*
*/

Package cn.com. java. wwh. www;



Import java. lang. reflect. Array;
Import java. lang. reflect. Method;
Import java. util. ArrayList;
Import java. util. List;


/**
* @ Class: used to call the reflection method.
*
*
* @ Author
* @ Version 1.0
* @ Creation Time: 4:11:32
*
*/


Class Person {

Public void sayHello1 (){
System. out. println (hello !);
}


Public static String sayHello2 (){
Return a boat;
}


Private String sayHello3 (String name, int age ){
System. out. println (name + ----> + age );
Return name + age;
}


// Use generic processing
Public List sayHello4 (String name, int age ){
List list = new ArrayList ();
List. add (name );
List. add (age );
Return list;
}




Public String [] sayHello5 (String name, int age ){
Return new String [] {name, age + };
}
}


Public class ReflectDemo {


Public static void main (String [] args) throws Exception {
// Call the sayHello method in Person Using Reflection
/**
* 1. Get the bytecode of the Class where the method is located. -----> Class Object
* 2. Get the sayHello Method; -------> Method object
* 3. Execute the Method (sayHello );
*/
// Obtain the class bytecode
Class Claz = Person. class;
Method meth = claz. getMethod (sayHello2 );
/**
* Object invoke (Object obj, Object... args) calls this Method for a specified Object with a specified parameter.
* Underlying method of object representation.
*
* Parameter: obj-indicates the object to which the underlying method belongs. Here, sayHello is the underlying method, and obj should be an object of the class where sayHello is located; args
*-Actual parameters required for calling the underlying method: access the execution result of the underlying method;
*
*
* If the underlying method is static, you can ignore the specified obj parameter. This parameter can be null.
*/


Object obj = meth. invoke (claz. newInstance (); // both static and non-static
Object obj1 = meth. invoke (null); // static
System. out. println (obj1 );


Meth = claz. getDeclaredMethod (sayHello3, String. class, int. class );
System. out. println (meth );
// You can set it to accessible before calling
Meth. setAccessible (true );
// Input parameters
Object obj2 = meth. invoke (claz. newInstance (), Fujian Normal University, 22 );
System. out. println (obj2 );
// Use generic processing
Method meth2 = claz. getMethod (sayHello4, String. class, int. class );


Object obj3 = meth2.invoke (claz. newInstance (), fl, 30 );
System. out. println (obj3 );
List list = (List) obj3;
System. out. println (obj3 );
System. out. println (list. get (0 ));


// Method exercise 5
// This method must be public; otherwise, an exception occurs during the call.
Method meth5 = claz. getMethod (sayHello5, String. class, int. class );
Object obj5 = meth5.invoke (claz. newInstance (), no regrets, 22 );
String [] array = (String []) obj5;
System. out. println (meth5 );
System. out. println (array [0]);
/**
* Array: static Object get (Object array, int index) returns the index value of the specified Array Object.
*
* Static int getLength (Object array) returns the length of the specified array Object in the form of an int.
*
*
* Static void set (Object array, int index, Object value)
* Set the value of the index component in the specified array object to the specified new value.
*/
Array. set (array, 0, haha );
Array. set (array, 1, 18 );
// System. out. println (Array. get (array, 0 ));
// System. out. println (Array. get (array, 1 ));
// Obtain the length of the array
Int number = Array. getLength (array );
System. out. println (the length of the array is: + number );
For (int I = 0; I <number; I ++ ){
System. out. println (Array. get (array, I ));
}
// Method without Parameters
// If the shape parameter required by the underlying method is 0, the length of the provided args array can be 0 or null.

Method me = claz. getMethod (sayHello1 );
Me. invoke (claz. newInstance (); // correct
Me. invoke (claz. newInstance (), null); // correct
Me. invoke (claz. newInstance (), new Object [] {}); // correct


Method met = claz. getDeclaredMethod (sayHello3, String. class, int. class );
// Set accessible
Met. setAccessible (true );
Met. invoke (claz. newInstance (), flat boat & no regrets, 22 );
Met. invoke (claz. newInstance (), Yiyepianzhou, 22 );
Met. invoke (claz. newInstance (), new Object [] {ha, 20 });
}
}

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.