Use reflection to get the variable name of the class Java

Source: Internet
Author: User

Use reflection to get the variable name of the class, give you an example
public class MyTools {
public static void Setallcomponentsname (Object f) {
Gets all domains in the corresponding class of the F object
field[] fields = F.getclass (). Getdeclaredfields ();
for (int i = 0, len = fields.length; i < Len; i++) {
For each property, get the property name
String varName = Fields[i].getname ();
try {
Get the original access control permissions
Boolean accessflag = Fields[i].isaccessible ();
Modify access Control permissions
Fields[i].setaccessible (TRUE);
Gets the variable in object F of the property fields[i] corresponding to
Object o = Fields[i].get (f);
System.out.println ("The incoming object contains a variable as follows:" + varName + "=" + O);
Restore access control permissions
Fields[i].setaccessible (Accessflag);
} catch (IllegalArgumentException ex) {
Ex.printstacktrace ();
} catch (Illegalaccessexception ex) {
Ex.printstacktrace ();
}
}
}

Use reflection to get the variable name of the class Java

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.