简体   繁体   中英

How does Eclipse generate setters and getters?

Just curious to know how Eclipse creates setters and getters (not how to myself). I am sure it must be Java reflection but just wanted to seek some more information.

Eclipse actually embeds an entire compiler to allow it to do incremental compilation and still give you useful information when you have a syntax error in your code. The generators look at the abstract syntax tree (AST) representation of the code, identify the fields, and use the JavaBeans formula to create getters and setters for them. You can use the "Outline" view in Eclipse to see a graphic representation of Eclipse's model of the class.

step1:

A simple User.java, with 4 fields, but no getters and setters method.

 public class user{

 private String firstname();
 private  String lastname();
 private String username();
 private String password();

 }
  step2: Generate It

 Right click on the file, select “Source” –> “Generate Getters and Setters…”

-> Choose which field you want to generate, and click on “OK” button.

 step3:

 All selected getters and setters methods will be generated automatically.


   public class user{

    private String firstname();
     private  String lastname();
    private String username();
     private String password();
   public String getFirstName(){
   return firstname;
  }
   public void setFirstName(String firstname){
   this.firstname=firstname;
   }
    public String getFirstName(){
   return lastname;
  }
   public void setLastName(String lastname){
   this.lastname=lastname;
   }
    public String getUserName(){
   return username;
  }
   public void setUserName(String username){
   this.username=username;
   }
     public String getPasword(){
   return password;
  }
   public void setPassword(String password){
   this.password=password;
   }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM