IntelliJ IDEA 2021.1 Help

toString() Generation Settings dialog

Code | Generate - toString() - Settings

Settings tab

ItemDescription
Use fully qualified class name in code generation ($classname)If this checkbox is selected, the dumped classnames will include their package names. (The $classname variable in the Velocity templates)
Enable getters in code generation ($method)If this checkbox is selected, the code generator will have $methods variable in the Velocity Macro Language.

See example 2.

Move caret to the generated methodIf this checkbox is selected, the caret scrolls to the generated toString() method.
Sort elementsIf this checkbox is selected, the members are sorted in the selected order (ascending or descending).
When method already existsIn this section, choose the default conflict resolution policy:
  • Ask: Choose this option to ask for confirmation, if a toString() method already exists.

    IntelliJ IDEA shows the dialog Replace existing toString method.

    The answer Yes results in generating the new toString() method in place of the existing one; the answer No results in creating a duplicate method.

  • Replace existing: Choose this option to automatically replace the existing toString() code.

  • Generate duplicating method: Choose this option to create a duplicate toString() method; in doing so, the new method will have the name toString(); the existing code will not be erased.

Where to insert?In this section, choose the place to insert the generated toString() method. The possible options are:
  • At caret.

  • After equals() and hashCode(): the generated toString() method will be inserted after the equals/hashCode, if present in the Java class; otherwise, the new method will be inserted at the current caret position.

  • At the end of class: the generated toString() method will be inserted as the last method.

ExcludeIn this section, select the checkboxes next to the elements to be excluded from the toString() method generation:
  • Exclude constant fields: If this checkbox is selected, then any constants won't be a part of the available fields for the code generator.

  • Exclude static fields: If this checkbox is selected, then any fields that have static modifiers won't be part of available fields for the code generator.

  • Exclude transient fields: If this checkbox is selected, then any fields with transient modifiers won't be be part of the available fields for the code generator.

  • Exclude enum fields: If this checkbox is selected, then any fields of the type enum (JDK1.5) will not be part of available fields for the code generator.

  • Exclude logger fields (Log4j, JDK Logging, Jakarta Common Logging): If this checkbox is selected, then any field that is a either a Log4j Logger, Java JDK Logger or a Jakarta Commons Logger will not be part of the available fields for the code generator.

  • Exclude fields by name (reg exp): If this checkbox is selected, then IntelliJ IDEA performs a regular expression matching on the field name. If the result is true, then the field will not be part of the available fields for the code generator.

  • Exclude fields by type name (reg exp): If this checkbox is selected, then IntelliJ IDEA performs a regular expression matching on the field type name (fully qualified name). If the result is true, the field will not be part of the available fields for the code generator.

  • Exclude methods by name (reg exp): If this checkbox is selected, then IntelliJ IDEA performs a regular expression matching on the method name. If the result is true, the method will not be part of the available methods for the code generator.

  • Exclude methods by return type name (reg exp): If this checkbox is selected, then IntelliJ IDEA performs a regular expression matching on the method return type name (fully qualified name). If the result is true, the method will not be part of the available methods for the code generator.

Templates tab

Use this page to view and manage the list of Velocity templates to be used for the toString() method generation.

Templates toolbar

ItemDescription
Icons general addClick this button to create a new entry to the list of available templates. When the new entry is added, create the corresponding template.
Icons general removeClick this button to delete the selected user-created entry from the list of available patterns. Note that pre-defined templates cannot be deleted.
Icons actions copyClick this button to create a copy of an existing template.

Variables used in Velocity templates

VariableReturnsDescription
$classnameStringThe name of a class (can be qualified classname, if this is selected in the settings).
$FQClassnameString@deprecated (use $class.qualifiedName) - The fully qualified name of the class
$fieldsjava.util.ListList of FieldElement objects
$methodsjava.util.ListList of MethodElement objects
$membersjava.util.ListList of both FieldElement and MethodElement objects
$memberElementThe Element object
$member.accessorStringThe accessor of a field or method. For a field it is $field.name and for a method it is $method.methodName.
$member.typeNameStringThe classname of the type (Object, String, List etc.)
$member.typeQualifiedNameStringThe qualified classname of the type (java.lang.Object, java.lang.String, java.uti.List etc.)
$member.arraybooleanChecks if the type is an array type (either a primitive array or object array).
$member.primitiveArraybooleanChecks if the type is a primitive array type (int[], short[], float[] etc.)
$member.objectArraybooleanChecks if the type is an Object array type (Object[], String[] etc.).
$member.stringArraybooleanChecks if the type is a String array type (String[])
$member.collectionbooleanChecks if the type is assignable from java.util.Collection.
$member.listbooleanChecks if the type is assignable from java.util.List
$member.mapbooleanChecks if the type is assignable from java.util.Map
$member.setbooleanChecks if the type is assignable from java.util.Set
$member.primitivebooleanChecks if the type is a primitive type (int, char, float etc.)
$member.modifierStaticbooleanDoes the type have a static modifier?
$member.modifierPublicbooleanDoes the type have a public modifier?
$member.modifierProtectedbooleanDoes the type have a protected modifier?
$member.modifierPackageLocalbooleanDoes the type have a package-local modifier?
$member.modifierPrivatebooleanDoes the type have a private modifier?
$member.modifierFinalbooleanDoes the type have a final modifier?
$member.stringbooleanIs the type assignable from java.lang.String?
$member.numericbooleanIs the type assignable from java.lang.Numeric or a primitive type of byte, short, int, long, float, double?
$member.objectbooleanIs the type assignable from java.lang.Object?
$member.datebooleanIs the type assignable from java.util.Date?
$member.calendarbooleanIs the type assignable from java.util.Calendar?
$member.booleanbooleanIs the type assignable from java.lang.Boolean or a primitive boolean?
$fieldFieldElementThe FieldElement object
$field.nameStringThe name of a field.
$field.modifierTransientbooleanDoes the field have a transient modifier?
$field.modifierVolatilebooleanDoes the field have a volatile modifier?
$field.constantbooleanIs the field a constant type? (has static modifier and its name is in UPPERCASE only)
$field.matchName(regexp)booleanPerforms a regular expression matching on a field name.
$field.enumbooleanIs this field a enum type?
$methodMethodElementThe MethodElement object
$method.nameStringThis variable returns one of the following:
  • The name of the field this getter method covers

  • The name of the method 'getFoo' when the method does not cover a field

$method.methodNameStringThe name of the method (getFoo).
$method.fieldNameStringThe name of the field this getter method covers - null if the method is not a getter for a field
$method.modifierAbstractbooleanIs this method an abstract method?
$method.modifierSynchronizedbooleanIs this method a synchronized method?
$method.returnTypeVoidbooleanIs this method a void method (does not return anything) ?
$method.getterbooleanIs this a getter method?
$method.matchName(regexp)booleanPerforms a regular expression matching on the method name.
$method.deprecatedbooleanIs this method deprecated?
$classClassElementThe ClassElement object
$class.nameStringThe name of the class
$class.matchName(regexp)booleanPerforms a regular expression matching on the classname.
$class.qualifiedNameStringThe fully qualified name of the class
$class.hasSuperbooleanDoes the class have a superclass? (extends another class - note extending java.lang.Object is not considered having a superclass)
$class.superNameStringThe name of the superclass (empty if no superclass)
$class.superQualifiedNameStringThe fully qualified name of the superclass (empty if no superclass)
$class.isImplements("interfaceName")booleanChecks if the class implements the given interface. Checking names of several interfaces can be done by separating the names with commas.
$class.implementNamesString[]Returns the class names of the interfaces the class implements. An empty array is returned, if the class does not implement any interfaces.
$class.isExtends("className")booleanChecks if the class extends any of the given class names. Chcecking several class names can be done by separating the names with commas.
$class.exceptionbooleanIs this class an exception class (extends Throwable)?
$class.deprecatedbooleanIs this class deprecated?
$class.enumbooleanIs this class an enum class?
$class.abstractbooleanIs this class abstract?
Output variablesThe output variables are possible in the Velocity Template (variables are stored in the Velocity Context): Output parameters will be available for the Generate action after the Velocity context has been executed and act upon.
$autoImportPackagesStringPackagenames that should automatically be imported. Use comma to separate packagenames.
Last modified: 08 March 2021