How to create a custom Intellij template set for use with Guava

I am trying to write a custom setter using the Intelij template, but for the Google Guava options. For example, if I have a class as follows:

public class Note {
    public Optional<String> title;
}

Using the default generation Setterfor Android Studio outputs:

public class Note {
    public Optional<String> title;

    public void setTitle(Optional<String> title) {
        this.title = title;
    } 
}

What I want to achieve:

public class Note {
    public Optional<String> title;

    public void setTitle(String title) {
        this.title = Optional.fromNullable(title);
    } 
}

What I have tried so far is copying the Intelij template and using the method StringUtil.split()to separate the Optional<signature part . However, I get an error while I use the template below.

Mistake

Incorrect method 'public void setTitle($StringUtil.split($field.type, true, "<").get(1) title) { mTitle = title; }

Any tips on what I should do?

Default Intelij DefaultTemplate for Setter Generation

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

My usual generation of Guava generator

#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
  static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($StringUtil.split($field.type, true, "<").get(1) $paramName) {
  #if ($field.name == $paramName)
    #if (!$field.modifierStatic)
      this.##
    #else
      $classname.##
    #end
  #end
  $field.name = $paramName;
}
+4
2

, StringUtils split() , .

Guava . , IntelliJ, com.google.common.base.Optional, StringsUtils, .

, $paramName Optional. , Guava. IntelliJ .

#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
    static ##
#end
#if($StringUtil.split($field.type, "<").get(0).equals("com.google.common.base.Optional"))    
    #set($paramSignature = $StringUtil.trimEnd($StringUtil.substringAfter($field.type, "<"), ">"))
    #set($fieldAssignment = "Optional.fromNullable(" + $paramName + ")") 
#else
    #set($paramSignature = $field.type)
    #set($fieldAssignment = $paramName)
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($paramSignature $paramName) { 
#if ($field.name == $paramName)
    #if (!$field.modifierStatic)
        this.##
    #else
        $classname.##
    #end
#end
$field.name = $fieldAssignment;
}

EDITED , @ChiefTwoPencils.

+2

, , , , ( , ?).

, , , ?

, , ; , . , Optional; Guava, .

#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
  static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention(
  $StringUtil.sanitizeJavaIdentifier(
    $helper.getPropertyName($field, $project)
  )
)($StringUtil.trimEnd($StringUtil.substringAfter($field.type, "<"), ">") $paramName) {
  #if ($field.name == $paramName)
    #if (!$field.modifierStatic)
      this.##
    #else
      $classname.##
    #end
  #end
  $field.name = Optional.fromNullable($paramName);
}
+3

Source: https://habr.com/ru/post/1629726/


All Articles