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
- Public class User {
- private Long ID;
- private String name;
- public void SetId (long id) {
- this.id = ID;
- }
- public void SetName (String name) {
- this.name = name;
- }
- public Long GetId () {
- return ID;
- }
- Public String GetName () {
- return name;
- }
- }
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
- Public class Bean {
- private string[] love;
- Public string[] Getlove () {
- return love;
- }
- public void Setlove (string[] love) {
- this.love = Love;
- }
- }
Create an Action class
[Java]View Plaincopy
- Import Java.lang.reflect.Method;
- Public class Admin {
- public static void Main (string[] args) {
- try {
- //Assigned value
- Object obj = Bean. class.newinstance ();
- Class Paramclass = Class.forName ("[Ljava.lang.String;");
- string[] param = { "eat", "Drink", "play", "le"};
- method = Bean. Class.getmethod ("Setlove", Paramclass);
- Method.invoke (obj, (Object) param);
- //value
- Bean bean = (bean) obj;
- For (int i = 0; i < bean.getlove (). length; i++) {
- System.out.println (Bean.getlove () [i]);
- }
- System.out.println ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }
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