Java Reflection mechanism Public classtesthibernate {@Test Public voidTESTHB () {Try{Class cs= Class.forName ("Testhibernate.test1");//Classload classObject OJ = Cs.newinstance ();//through class new an objectmethod[] Mt = Cs.getmethods ();//Get all methods for(Method method:mt) {//Polling out all function methods if(Method.getname () = = "M13") {//judging functions by function nameMethod.invoke (oj,1,2); for(Class parameter:method.getParameterTypes ()) {//to poll the parameters of a function methodSystem.out.println (Parameter.getname ());//Get parameter type} System.out.println ("Return value" +method.getreturntype ());//Gets the return value type}Else if(Method.getname () = = "M12") {Method.invoke (OJ); //Execute function method without parameter}Else if(Method.getname () = = "Getmeth") {System.out.println ("123" +Method.getreturntype ()); Object Object= Method.invoke (OJ);//gets the class object returned by the function methodClass CCC = Object.getclass ();//obtaining classes from class objectsField ff = Ccc.getfield ("X2");//Get Variable Object intX1 = (int) Ff.get (object);//Get Property value//Getdeclaredmethod to get private methodsMethod met = Ccc.getdeclaredmethod ("xxx",Newclass[]{int.class,int.class}); Met.setaccessible (true);//setting can be accessedObject i = Met.invoke (object, x1,2);//calling the Take parameter function methodSystem.out.println (i+ "Xxxxxxxxxxxxxxxxxxxxxxxxxx"); } } } Catch(ClassNotFoundException | instantiationexception | illegalaccessexception | IllegalArgumentException | InvocationTargetException | SecurityException | nosuchmethodexception |nosuchfieldexception e) { //TODO auto-generated Catch blockE.printstacktrace (); } }}classtest2{Static{System.out.println ("Classload test2"); } Public intx2 = 3; Private intxxxintX1,intx2) { returnX1*x2; }}classtest1{Static{System.out.println ("Classload test1"); } {System.out.println ("New Objcet Tset1"); } PublicTest2 Getmeth () {return NewTest2 (); } Public voidM12 () {System.out.println ("M12"); } PublicString M13 (intBintR) {System.out.println (b+0); returnB+r+ ""; }}
This dome is used for learning tests.
Java Reflection Mechanism
public class Testhibernate {
@Test
public void Testhb () {
try {
Class cs = class.forname ("Testhibernate.test1"); Classload class
Object OJ = Cs.newinstance (); // through the class new an object
Method [] mt = Cs.getmethods (); // Get all methods
For (method method:mt) {// poll out all function methods
if (method.getname () = = "M13") {// functions are judged by function name
Method.invoke (oj,1,2);
For (Class parameter:method.getParameterTypes ()) {// Polling The parameters of the function method
System.out.println (Parameter.getname ()); get parameter type
}
System.out.println (" return value " +method.getreturntype ()); Gets the return value type
}else if (method.getname () = = "M12") {
Method.invoke (OJ); Execute function method without parameter
}else if (method.getname () = = "Getmeth") {
System.out.println ("123" +method.getreturntype ());
Object object = Method.invoke (OJ); Gets the class object returned by the function method
Class CCC = Object.getclass (); obtaining classes from class objects
Field ff = Ccc.getfield ("X2"); Get Variable Object
int x1 = (int) ff.get (object); Get Property value
//getdeclaredmethod can get private methods
Method met = Ccc.getdeclaredmethod ("xxx", New Class[]{int.class,int.class});
met.setaccessible (TRUE);// setting can be accessed
Object i = Met.invoke (object, x1,2);// calling the Take parameter function method
System.out.println (i+ "xxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}
} catch (ClassNotFoundException | instantiationexception | illegalaccessexception | IllegalArgumentException | InvocationTargetException | SecurityException | nosuchmethodexception | Nosuchfieldexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Class test2{
static{
System.out.println ("Classload test2");
}
public int x2 = 3;
private int xxx (int x1,int x2) {
return x1*x2;
}
}
Class test1{
static{
System.out.println ("Classload test1");
}
{
System.out.println ("New Objcet Tset1");
}
Public Test2 Getmeth () {
return new Test2 ();
}
public void M12 () {
System.out.println ("M12");
}
Public String M13 (int b,int r) {
System.out.println (B+R);
return b+r+ "";
}
}
Java Reflection Mechanism Dome