Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert code into a method - Java
    primarykey
    data
    text
    <p>Is there a way to automatically insert code into a method?</p> <p>I have the following typical field with a getter and setter and I would like to insert the indicated code into the setter method that records if the field was modified as well to insert the indicated "isFirstNameModified" field to also track if the field was modified or not.</p> <pre><code> public class Person { Set&lt;String&gt; updatedFields = new LinkedHashSet&lt;String&gt;(); String firstName; public String getFirstName(){ return firstName; } boolean isFirstNameChanged = false; // This code is inserted later public void setFirstName(String firstName){ if( !isFirstNameChanged ){ // This code is inserted later isFirstNameChanged = true; // This code is inserted later updatedFields.add("firstName"); // This code is inserted later } // This code is inserted later this.firstName = firstName; } } </code></pre> <p>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: <code>updatedFields.add("firstName");</code>. 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): <code>boolean isFirstNameChanged = false;</code></p> <p>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.</p> <h2>Edit:::::::::</h2> <p><strong>I Should have used this simpler code instead of the example above. All it does is add the name of the field as a string to a set.</strong></p> <pre><code> public class Person { Set&lt;String&gt; updatedFields = new LinkedHashSet&lt;String&gt;(); String firstName; public String getFirstName(){ return firstName; } public void setFirstName(String firstName){ updatedFields.add("firstName"); // This code is inserted later this.firstName = firstName; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload