The IOC implements the reflection mechanism based on the underlying reflection mechanism of Java: Core: Class CLS = Class.forName (class name);
Class ptypes[] = new CLASS[2];
Ptypes[0] = Class.forName ("java.lang.String");
PTYPES[1] = Class.forName ("java.util.Hashtable");
parameter invocation Emphasis must specify the method name, and the corresponding number and type of arguments
method = Cls.getmethod ("TestMethod", ptypes); How to know the number of parameter types: Method []m= cls.getmethods ();
if (M!=null &&m.length>0)
{
for (int i = 0; i < m.length; i++) {
if (M[i].getname (). Equals ("TestMethod")) {
Parameter []p=m[i].getparameters ();
if (p!=null && p.length>0)
{
for (Parameter parameter:p) {
System.out.println (Parameter.gettype (). GetName ());
}
}
}
}
} How to invoke: Object args[] = new OBJECT[2];
Args[0] = "Hello, my dear!";
Hashtable<string, string> ht = new hashtable<string, string> ();
Ht.put ("name", "pistachio");
ARGS[1] = HT;
String returnstr = (string) method.invoke (new Reflectiontest (), args);
System.out.println ("returnstr=" + returnstr); Example:
Package com.gillion.javaReflection;
Import Java.awt.Button;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Parameter;
Import java.util.Hashtable;
/**
* @Description The reflection mechanism of Java
* @author[email protected]
* @time January 8, 2015 4:53:24
* @version 1.0.0
*/
public class Reflectiontest {
/**
* Entrance
* @param args
* @throws Exception
*/
public static void Main (string[] args) throws Exception {
Reflectiontest reflection = new Reflectiontest ();
Reflection.getnametest ();
System.out.println ("");
Reflection.getmethodtest ();
}
/**
* Get class name full path
* @throws Exception
*/
public void Getnametest () throws Exception {
System.out.println ("===========begin getnametest============");
String name = "a Honey fruit";
Class cls = Name.getclass ();
System.out.println ("String class Name:" + cls.getname ());
Button btn = New button ();
Class Btnclass = Btn.getclass ();
System.out.println ("button class Name:" + btnclass.getname ());
Class Superbtnclass = Btnclass.getsuperclass ();
System.out.println ("button's parent class name:" + superbtnclass.getname ());
Class clstest = Class.forName ("Java.awt.Button");
System.out.println ("Clstest name:" + clstest.getname ());
System.out.println ("===========end getnametest============");
}
public void Getmethodtest () throws Exception {
System.out.println ("===========begin getmethodtest==========");
Class cls = Class.forName ("Com.gillion.javaReflection.ReflectionTest");
Class ptypes[] = new CLASS[2];
Ptypes[0] = Class.forName ("java.lang.String");
PTYPES[1] = Class.forName ("java.util.Hashtable");
Parameter invocation
method = Cls.getmethod ("TestMethod", ptypes);
/***
* What parameters need to be injected, what is the parameter type, and how many parameters
*/
Method []m= cls.getmethods ();
if (M!=null &&m.length>0)
{
for (int i = 0; i < m.length; i++) {
if (M[i].getname (). Equals ("TestMethod")) {
Parameter []p=m[i].getparameters ();
if (p!=null && p.length>0)
{
for (Parameter parameter:p) {
System.out.println (Parameter.gettype (). GetName ());
}
}
}
}
}
Object args[] = new OBJECT[2];
Args[0] = "Hello, my dear!";
Hashtable<string, string> ht = new hashtable<string, string> ();
Ht.put ("name", "pistachio");
ARGS[1] = HT;
String returnstr = (string) method.invoke (new Reflectiontest (), args);
System.out.println ("returnstr=" + returnstr);
System.out.println ("===========end getmethodtest==========");
}
public string TestMethod (String str, Hashtable ht) throws Exception {
String returnstr = "return value";
System.out.println ("Test TestMethod () method call");
System.out.println ("str=" + str);
System.out.println ("name =" + (String) ht.get ("name"));
System.out.println ("End TestMethod () method call");
return returnstr;
}
} |
(full of hard goods) Spring in-depth study of an IOC implementation