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
public
static
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {
this.
$classname.
$field.name = $paramName;
}
My usual generation of Guava generator
public
static
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($StringUtil.split($field.type, true, "<").get(1) $paramName) {
this.
$classname.
$field.name = $paramName;
}