How to create comments, as well as automatically generate getters and setters in Android Studio

I want to also generate comments when automatically generating getters and setters

Android Studio:

/**
 * username
 */
private String name;

public String getName() {
    return name;
}

I want to:

/**
 * username
 */
private String name;

/**
 * Get username
 * @return username
 */
public String getName() {
    return name;
}
+4
source share
4 answers

The ability to create custom patterns and getter patterns was added to IntelliJ IDEA v14.1 (in particular, build 141.177) by querying the IDEA-28206 function . Allow setting of the generated activator / setter . I do not know if this change has been merged into the Android Studio branch yet.

Using the (new) function, when you run the intent to insert a recipient / setter, a dialog box allows you to select a template to use:

enter image description here

enter image description here, . Velocity. , . - . .

, (IDEABKL-4910 Javadocs /), Javadocs . , , IDEA-28206 , , - .

+1

, , , .

, create you own custom settings on the getters and setters options, Intellij settings getters setters , .

Getter :

/**
*@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;
}

$field.name .

:

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

:

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

$field.name . , $classname.##, .

, comments enabling in Android Studio when doing a generate getters and setters for the the fields.

, - . .

+5

, java, /, .

  • Alt + Shift + S
  • .
  • , .
  • " " .
  • /, .
  • Apply ok
  • "". .

, .

0

It is not supported by Android Studio (at least in version 1.2).
You can download the plugin (Preferences / Plugins), for example JavaDoc, which adds additional options to the "Create ..." menu and allows you to create javadoc comments for selected or all fields / methods.

0
source

All Articles