1

I found out how to change the default templates in IntelliJ when generating toString and hashCode/equals implementations but I cannot find any proper documentation on what variables are accessible. Autocomplete helps but it does not show me any such option.

Basically, I want to change the toString template to generate a prefix of Foo.Bar for the following inner class

class Foo {
  class Bar { }
}

where it now simply puts Bar without the prefix. I do not want to add the package name!

Bonus questions: How can I set the global parameters? I am also trying to change the parameter name of the equals template from o to object.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192

1 Answers1

1

A documentation on what variables are accessible within the template can be found here: Documentation of the toString settings dialog.

Unfortunately, I cannot find anything variable like class.hasOuterClass, class.outerClassName or something similar.

After a little research I found an ugly solution but it works, if you class name start with a capital letter and your package names are all lower case.

Put the following at the beginning of your template:

#set( $classname = $FQClassname.substring($StringUtil.indexOfAny($FQClassname, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")))

Bonus question:

I did not find a similar documentation for the equals/hashCode template but I looked at the source code of the equalsHelper.vm on GitHub.

If you put #set( $baseParamName = "object" ) before the #parse("equalsHelper.vm") the $paramName will then be named object if there will be no other local variable with that name. This happens, if you have a member variable with that name.

enter image description here

Peter
  • 4,752
  • 2
  • 20
  • 32
  • Hi, thank you a lot. The thing with the renaming of *o* to *object* did work but I had to put the statement after the *parse* statement. At the same time, Velocity does not accept the string manipulation for the `toString` method. Instead, it emits an error. – Rafael Winterhalter Mar 08 '16 at 23:18
  • The `$classname` overriding work for me. What's the error message, which idea version are you using and which template did you use? – Peter Mar 09 '16 at 19:12