Heycz
Heycz

Reputation: 59

How to generate comments as well when automatically generating getters and setters in Android Studio

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;
}

Upvotes: 4

Views: 4614

Answers (4)

mike20132013
mike20132013

Reputation: 5425

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.

Upvotes: 6

Javaru
Javaru

Reputation: 31936

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.

Upvotes: 1

andrejs
andrejs

Reputation: 572

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

Upvotes: 0

Ganesa Vijayakumar
Ganesa Vijayakumar

Reputation: 2602

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.

Upvotes: 1

Related Questions