Insert code into a method - Java

Posted by DutrowLLC on Stack Overflow See other posts from Stack Overflow or by DutrowLLC
Published on 2010-06-13T05:53:07Z Indexed on 2010/06/13 6:02 UTC
Read the original article Hit count: 255

Is there a way to automatically insert code into a method?

I have the following and I would like to insert the indicated code:

 public class Person {

      Set<String> updatedFields = new LinkedHashSet<String>();

      String firstName;
      public String getFirstName(){
           return firstName;
      }

      boolean isFirstNameChanged = false;               // Insert
      public void setFirstName(String firstName){       
           if( !isFirstNameChanged ){                   // Insert
                isFirstNameChanged = true;              // Insert
                updatedFields.add("firstName");         // Insert
           }                                            // Insert
           this.firstName = firstName;
      }
 }

I'm also not sure if I can the subset of the method name as a string from inside the method itself as indicated on the line where I add the fieldName as a string into the set of updated fields: updatedFields.add("firstName");. And I'm not sure how to insert fields into a class where I add the boolean field that tracks if the field has been modified or not before (for efficiency to prevent having to manipulate the Set): boolean isFirstNameChanged = false;

It seems to most obvious answer to this would be to use code templates inside eclipse, but I'm concerned about having to go back and change the code later.

© Stack Overflow or respective owner

Related posts about java

Related posts about reflection