4

I want to generate comments as well when automatically generating getters and setters

Android Studio:

/**
 * username
 */
private String name;

public String getName() {
    return name;
}

I Want:

/**
 * username
 */
private String name;

/**
 * Get username
 * @return username
 */
public String getName() {
    return name;
}
Heycz
  • 59
  • 1
  • 5

4 Answers4

6

I know the answer has already been accepted for this post but I came across the same issue and though i'll give it a shot as well.

As Mark explained how to create you own custom settings on the getters and setters options, I tried to use the Intellij's settings for both getters and setters and customized it the way I want to be.

This is how the Getter Template looks like for me:

/**
*@return Gets the value of $field.name and returns $field.name 
*/
public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
  #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
    #set($name = $StringUtil.decapitalize($name))
  #else
    is##
#end
#else
  get##
#end
${name}() {
  return $field.name;
}

For explanation, I used the $field.name as a comment value and used a regular comment structure to place the value before the method generation starts.

Eg :

    /**
    *@return Gets the value of $field.name and returns $field.name 
    */

This is how my Setter Template looks like :

/**
* Sets the $field.name
  You can use get$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))() to get the value of $field.name
*/
#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
  static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {
  #if ($field.name == $paramName)
    #if (!$field.modifierStatic)
      this.##
    #else
      $classname.##
    #end
  #end
  $field.name = $paramName;
}

And the value for $field.name is the same as the one in the getter. You can always customize the comment structure this way and can use other attributes like $classname.## as well if required.

This was just a small example on how I did my comments enabling in Android Studio when doing a generate getters and setters for the the fields.

Hope this helps someone in the future. Good Luck.

mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • You rock !! Can you tell which script or language is used to write these templates ? And can we add comments written over fields as part of getter/setter comments in this template? – Priya Singhal Sep 25 '15 at 06:10
  • We can actually create this in android studio itself. I modified a few items in the setters and getter auto generated comments from it. We have an option in Android Studio for it .. :) – mike20132013 Sep 27 '15 at 00:26
  • I think you misunderstood my comment. I have tried what you have done and its working good , and I am able to auto generate comments. Suppose there a field in POJO class like // unique id for passport String id; /** * Returns the id * return java.lang.String value of id */ public String getId() { return id; } /** * Sets the id * You can use getId() to set the value of id * * param id java.lang.String */ public void setId(String id) { this.id = id; } You can see the getter and setter did their job . – Priya Singhal Sep 28 '15 at 04:48
  • But I want an additional thing to be done by getter and setter , that is I want the comment written over my field **id** to included in the comments generated by the getter ans setter. something like this : /** * Sets the **unique id for passport** * You can use getId() to set the value of id * * @param id java.lang.String */ public void setId(String id) { this.id = id; } Can you help me with this ? – Priya Singhal Sep 28 '15 at 04:52
  • Haven't tried that but maybe I can try when I get some time with that.. :) – mike20132013 Sep 29 '15 at 17:24
  • By the way which script is used to write templates like these in android studio ? – Priya Singhal Sep 29 '15 at 17:27
1

I believe this is not possible in java but you can change comment/code templates by following steps.

  1. create a class with a field
  2. Click Alt + Shift + S
  3. Select option Generate Getters and Setters..
  4. Select check box of the field which one you created
  5. Find link Code Templates in the bottom of the dialog and click that.
  6. Now you can enable the project specific settings option and change the comments/code templates as you want.
  7. Click apply and ok
  8. Select option Generate method comments and click OK. Now the getter and setter will be created with comments as per your template changes.

Please try to get archive your idea with using above comments.

Ganesa Vijayakumar
  • 2,422
  • 5
  • 28
  • 41
1

The ability to create custom setter & getter templates was added in IntelliJ IDEA v14.1 (specifically build 141.177) via the feature request IDEA-28206 Allow customization of generated getter/setter. I do not know if that change has been merged into the Android Studio branch yet.

With the (new) feature, when you trigger the insert getter/setter intention, the dialog box allows you to select the template to use:

enter image description here

You can click the browse button enter image description here to create a new template. It uses the Velocity template language. You could create a template that would include the desired comments. In the ticket, someone has posted their custom setter. The ticket to document the feature is still pending.

Finally, there is an open feature request (IDEABKL-4910 Javadocs for getters/setters) to have Javadocs automatically included when generating setters and getters. However, this is on the backlog and now that IDEA-28206 has been implemented, I doubt this will get any attention.

Javaru
  • 30,412
  • 11
  • 93
  • 70
0

It's not supported by Android Studio (at least in 1.2 version).
You can download a plugin (Preferences/Plugins) e.g JavaDoc which adds additional options under "Generate..." menu and allow you to generate javadoc comments for selected or all fields/methods

andrejs
  • 562
  • 6
  • 10