Java reflection obtains the attributes and types of classes.

Source: Internet
Author: User

 

Obtain class attribute fields and call class methods through reflection
Public class modelclasshelper {

Public static hashmap <string, class> Init (string classpath)
{
Try {
// "Com. geocompass. model. ststbprpmodel"
Hashmap <string, class> fieldhashmap = new hashmap <string, class> ();
Class CLS = Class. forname (classpath); // com. geocompass. model. ststbprpmodel
Field [] fieldlist = Cls. getdeclaredfields ();
For (INT I = 0; I <fieldlist. length; I ++ ){
Field identifier = fieldlist [I];
Fieldhashmap. Put (response. getname (), response. GetType ());
// System. Out. println ("name =" + response. getname ());
// System. Out. println ("Decl class =" + response. getdeclaringclass ());
// System. Out. println ("type =" + response. GetType ());
// System. Out. println ("-----");
}
Return fieldhashmap;
} Catch (classnotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return NULL;
} Public static string gettablename (string classpath)
{
Try {
Class CLS = Class. forname (classpath );
Method test = Cls. getdeclaredmethod ("gettablename ");
Object invoke =Test. Invoke (CLS. newinstance ());
Return invoke. tostring ();
// Cls. assubclass (tabmodel. Class );
} Catch (classnotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (securityexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (nosuchmethodexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalargumentexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalaccessexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (invocationtargetexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (instantiationexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return "";
}}

 Dynamic acquisition of any Java instance Using Reflection TechnologyAttribute Value

 

 

Package org. Apache. easframework. Core. entity. impl;

Import java. Lang. Reflect. field;

Public class testentity {

Private string code;
Private string name;

Public void setcode (string code)
{
This. Code = code;
}

Public String getcode ()
{
Return this. Code;
}

Public void setname (string name)
{
This. Name = Name;
}

Public String getname ()
{
Return this. Name;
}

Public static void main (string [] ARGs) throws securityexception, classnotfoundexception, illegalargumentexception, illegalaccessexception
{
Testentity OBJ = new testentity ();
OBJ. setname ("name value ");
OBJ. setcode ("code value ");
Field [] FDS = Class. forname
("Org. Apache. easframework. Core. entity. impl. testentity"). getdeclaredfields ();

System. Out. println (FDS. Length );
For (INT I = 0; I <FDS. length; I ++)
{
System. Out. println (FDS [I]. Get (OBJ ));

}
}

}

 

Reprinted from:

Http://blog.csdn.net/wpcxyking/article/details/6139549

 

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.