Java reflection mechanism: constructor!

Source: Internet
Author: User

Import Org. JUnit. test; </P> <p> public class constructortest {</P> <p> @ test @ suppresswarnings ("unchecked") <br/> Public void testconstructor () {<br/> try {<br/> class clazz = Class. forname ("CN. vicky. utils. user "); <br/> method = clazz. getdeclaredmethod ("getname"); <br/> constructor <? Extends live> constructor = clazz. getconstructor (string. class); <br/> system. out. println (method. invoke (constructor. newinstance ("Vicky"); <br/>} catch (classnotfoundexception e) {<br/> E. printstacktrace (); <br/>} catch (securityexception e) {<br/> E. printstacktrace (); <br/>} catch (nosuchmethodexception e) {<br/> E. printstacktrace (); <br/>}catch (illegalargumentexception e) {<br/> E. printstacktrace (); <br/>} catch (illegalaccessexception e) {<br/> E. printstacktrace (); <br/>} catch (invocationtargetexception e) {<br/> E. printstacktrace (); <br/>} catch (instantiationexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> abstract class live {<br/> string name; </P> <p> Public live (string name) {<br/> This. name = Name; <br/>}</P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> class user extends live {</P> <p> public user (string name) {<br/> super (name); <br/>}</P> <p> Public String getname () {<br/> return "--->" + name; <br/>}</P> <p>}

Print:

---> Vicky

 

Package CN. vicky. service. task; </P> <p> Import Java. io. serializable; <br/> Import Java. lang. reflect. constructor; <br/> Import Java. lang. reflect. method; <br/> Import Java. util. map; <br/> Import Java. util. logging. logger; </P> <p> Import CN. vicky. service. command. basecommand; </P> <p> Import COM. sun. SGS. app. appcontext; <br/> Import COM. sun. SGS. app. managedreference; <br/> Import COM. sun. SGS. app. task; </P> <p> Public clas S solutioncommandtask implements task, serializable {</P> <p> Private Static final long serialversionuid = 1l; <br/> Private Static final logger = logger. getlogger (solutioncommandtask. class. getname (); </P> <p> private string classname; <br/> private string functionname; <br/> private map mapmessage; <br/> private string username; <br/> private managedreference <task []> tasksref; </P> <p> Public Solution Commandtask (string classname, string functionname, map mapmessage, string username, <br/> task... tasks) {<br/> super (); <br/> This. classname = classname; <br/> This. functionname = functionname; <br/> This. mapmessage = mapmessage; <br/> This. username = username; <br/> If (tasks. length> 0) <br/> This. tasksref = appcontext. getdatamanager (). createreference (tasks); <br/>}</P> <p> Public void run () throws exce Ption {</P> <p> long time1 = system. currenttimemillis (); </P> <p> try {<br/> class clazz = Class. forname (classname); <br/> constructor <? Extends basecommand> command; <br/> method = clazz. getmethod (functionname); <br/> // method = clazz. getdeclaredmethod (functionname); <br/> If (tasksref = NULL) {<br/> command = clazz. getconstructor (map. class, String. class); <br/> method. invoke (command. newinstance (mapmessage, username); <br/>} else {<br/> command = clazz. getconstructor (map. class, String. class, task []. class); <br/> method. invoke (command. newinstance (mapmessage, username, tasksref. get (); <br/>}< br/>}catch (exception e) {<br/> E. printstacktrace (); <br/> logger. warning ("receivedmessagetask --> Run -->" + E. getmessage (); <br/>}</P> <p> long time2 = system. currenttimemillis (); </P> <p> system. out. println ("solutioncommandtask run () Time consumed:" + (time2-time1); <br/>}</P> <p >}< br/>

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.