Java Reflection Mechanism Dome

Source: Internet
Author: User
Tags object object

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

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.