Java Reflection Mechanism invocation method

Source: Internet
Author: User
This is a test example of how the object is invoked by reflection. Testref.java import Java.lang.reflect.Method;
Import java.lang.reflect.InvocationTargetException;

/**
* Created by IntelliJ idea.
* File:TestRef.java
* User:leizhimin
* Date:2008-1-28 14:48:44
*/
public class Testref {

public static void Main (String args[]) throws Nosuchmethodexception, Illegalaccessexception, invocationtargetexception {
Foo foo = new Foo ("This is a Foo object.") ");
Class clazz = Foo.getclass ();
Method M1 = Clazz.getdeclaredmethod ("Outinfo");
Method m2 = Clazz.getdeclaredmethod ("Setmsg", String. Class);
Method m3 = Clazz.getdeclaredmethod ("getmsg");
M1.invoke (foo);
M2.invoke (foo, "re-set MSG information.) ");
String msg = (string) m3.invoke (foo);
SYSTEM.OUT.PRINTLN (msg);
}
}

Class Foo {
Private String msg;

Public Foo (String msg) {
this.msg = msg;
}

public void Setmsg (String msg) {
this.msg = msg;
}

Public String getmsg () {
return msg;
}

public void Outinfo () {
System.out.println ("This is the test class for testing Java reflection");
}
Console output: This is the test class for testing Java reflection
Re-set MSG information.

Process finished with exit code 0






Transmission door: http://blog.csdn.net/hbcui1984/article/details/2719089

Java Reflection Use Notes

This article is a summary of the experience of using Java reflection in the work, but also can be said to be some tips, learn new tricks, will be constantly updated. This article is not prepared to discuss the Java reflection mechanism, there are a lot of online, you can just google it.

Before I start, I'll define a test class student, and the code is as follows:[Java]  View plain copy package chb.test.reflect;      public class student {        private int age;       private  String name;       public int getage ()  {           return age;       }        public void setage (int age)  {            this.age = age;       }        public string getname ()  {            return name;       }       public  void setname (string name)  {            This.name = name;       }               public static void hi (int age,string name) {            system.out.println ("Hello, my name is +name+", this year "+age+");        }  }<pre></pre>  

general steps for using Java reflection

Reflection invocation is generally divided into 3 steps: Get the class you want to call the class to get the method call (methods) method invocation (Invoke)

code example: [Csharp] view plain copy Class cls = Class.forName ("chb.test.reflect.Student");   Method m = Cls.getdeclaredmethod ("Hi", New Class[]{int.class,string.class}); M.invoke (Cls.newinstance (), "ChB");<pre></pre>

the type of the parameter in the method call

In a method call, the parameter type must be correct, and it is important to note that you cannot replace the base type with a wrapper class, such as Integer.class instead of Int.class.

If I want to call the student Setage method, the following call is correct:

[Java] view plain copy Class cls = Class.forName ("chb.test.reflect.Student");   Method Setmethod = Cls.getdeclaredmethod ("Setage", Int.class); Setmethod.invoke (Cls.newinstance ());<pre></pre>

And if we replace Int.class with Integer.class, we can make mistakes, such as: [Java] view plain copy Class cls = Class.forName ("   Chb.test.reflect.Student "); Method Setmethod = Cls.getdeclaredmethod ("Setage"

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.