Differences between the GetFields () method and the Getdeclaredfields () method in Java reflection

Source: Internet
Author: User

There are two ways to get a field for a class: GetFields () and Getdeclaredfields (). Let's take a look at the difference between the two:

GetFields (): Gets all of the common (public) fields of a class, including the fields in the parent class.
Getdeclaredfields (): Gets all the declared fields of a class, that is, public, private, and proteced, but does not include the declaration field of the parent class.

Similarly, there are getconstructors () and Getdeclaredconstructors (), GetMethods (), and Getdeclaredmethods (), which represent the methods, constructors, for getting a class, respectively.

The specific code is as follows:

Let's start by creating a Pojo

[Java]View Plaincopy
  1. Public class User {
  2. private Long ID;
  3. private String name;
  4. public void SetId (long id) {
  5. this.id = ID;
  6. }
  7. public void SetName (String name) {
  8. this.name = name;
  9. }
  10. public Long GetId () {
  11. return ID;
  12. }
  13. Public String GetName () {
  14. return name;
  15. }
  16. }

To get all the fields in this class
field[] fields = User.class.getDeclaredFields ();

Gets the name of the field

String fieldName = Field.getname ();

Gets the modifier for the field

int fieldvalue = Field.getmodifiers ();//such as: Private, static, final, etc.

Compare with a specific modifier

Modifier.isstatic (fieldvalue)//See if this modifier is static

Get the claim type of a field

Field.gettype ();//Returns a class

Compare to a type

Field.gettype () = = Timestamp.class

Gets the value of this field in the specified object

Object fieldobject= field.get (user);//user can be seen as objects found from the database

If there is a field in the Pojo class that has a collection type, how do we get the value in the field?

First create a Pojo class

[Java]View Plaincopy
  1. Public class Bean {
  2. private string[] love;
  3. Public string[] Getlove () {
  4. return love;
  5. }
  6. public void Setlove (string[] love) {
  7. this.love = Love;
  8. }
  9. }

Create an Action class

[Java]View Plaincopy
  1. Import Java.lang.reflect.Method;
  2. Public class Admin {
  3. public static void Main (string[] args) {
  4. try {
  5. //Assigned value
  6. Object obj = Bean.  class.newinstance ();
  7. Class Paramclass = Class.forName ("[Ljava.lang.String;");
  8. string[] param = { "eat", "Drink", "play", "le"};
  9. method = Bean.  Class.getmethod ("Setlove", Paramclass);
  10. Method.invoke (obj, (Object) param);
  11. //value
  12. Bean bean = (bean) obj;
  13. For (int i = 0; i < bean.getlove (). length; i++) {
  14. System.out.println (Bean.getlove () [i]);
  15. }
  16. System.out.println ();
  17. } catch (Exception e) {
  18. E.printstacktrace ();
  19. }
  20. }
  21. }

The results are as follows:

Eat
Drink
Play
Le

There is one more thing we need to note about field: When the field modifier is private, we need to add

Field.setaccessible (TRUE);

The method of judging is: if (!field.isaccessible ())

For more detailed explanations, please click here.

Next, add the use of regular expressions in Java

First create a regular expression object

Pattern pattern = Pattern.compile ("[a-z][a-z]*");

A regular expression specified as a string must first be compiled into an instance of this class

Matcher Matcher = Pattern.matcher (fieldName);

Try to find the next subsequence of the input sequence that matches the pattern

Boolean ismatcher = Matcher.find ();

Returns the input subsequence that was matched by the previous match operation

String str = Matcher.group ();

Implement non-terminal add and replace steps to connect individual fields with ' _ '

Matcher.appendreplacement (SB, str + "_");

Original address: http://blog.csdn.net/zlb824/article/details/7494300

Differences between the GetFields () method and the Getdeclaredfields () method in Java reflection

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.