Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 733
Heycz Avatar asked Feb 09 '23 21:02

Heycz


1 Answers

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.

like image 50
mike20132013 Avatar answered Feb 15 '23 09:02

mike20132013