IntelliJ IDEA 2024.2 Help

toString() Generation Settings dialog

Code | Generate - toString() - Settings

Settings tab

Item

Description

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 method

If this checkbox is selected, the caret scrolls to the generated toString() method.

Sort elements

If this checkbox is selected, the members are sorted in the selected order (ascending or descending).

When method already exists

In 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.

Exclude

In 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 a part of the available fields for the code generator.

  • Exclude transient fields: If this checkbox is selected, then any fields with transient modifiers won't be a 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) won't be a part of the 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 either Log4j Logger, Java JDK Logger, or a Jakarta Commons Logger won't be a 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 won't be a 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 won't be a 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 won't be a 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 won't be a 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

Item

Description

Click this button to create a new entry to the list of available templates. When the new entry is added, create the corresponding template.

Click this button to delete the selected user-created entry from the list of available patterns. Note that pre-defined templates cannot be deleted.

Click this button to create a copy of an existing template.

Variables used in Velocity templates

Variable

Returns

Description

$classname

String

The name of a class (can be qualified classname, if this is selected in the settings).

$FQClassname

String

@deprecated (use $class.qualifiedName) - The fully qualified name of the class

$fields

java.util.List

List of FieldElement objects

$methods

java.util.List

List of MethodElement objects

$members

java.util.List

List of both FieldElement and MethodElement objects

$member

Element

The Element object

$member.accessor

String

The accessor of a field or method. For a field it is $field.name and for a method it is $method.methodName.

$member.typeName

String

The classname of the type (Object, String, List etc.)

$member.typeQualifiedName

String

The qualified classname of the type (java.lang.Object, java.lang.String, java.uti.List etc.)

$member.array

boolean

Checks if the type is an array type (either a primitive array or object array).

$member.primitiveArray

boolean

Checks if the type is a primitive array type (int[], short[], float[] etc.)

$member.objectArray

boolean

Checks if the type is an Object array type (Object[], String[] etc.).

$member.stringArray

boolean

Checks if the type is a String array type (String[])

$member.collection

boolean

Checks if the type is assignable from java.util.Collection.

$member.list

boolean

Checks if the type is assignable from java.util.List

$member.map

boolean

Checks if the type is assignable from java.util.Map

$member.set

boolean

Checks if the type is assignable from java.util.Set

$member.primitive

boolean

Checks if the type is a primitive type (int, char, float etc.)

$member.modifierStatic

boolean

Does the type have a static modifier?

$member.modifierPublic

boolean

Does the type have a public modifier?

$member.modifierProtected

boolean

Does the type have a protected modifier?

$member.modifierPackageLocal

boolean

Does the type have a package-local modifier?

$member.modifierPrivate

boolean

Does the type have a private modifier?

$member.modifierFinal

boolean

Does the type have a final modifier?

$member.string

boolean

Is the type assignable from java.lang.String?

$member.numeric

boolean

Is the type assignable from java.lang.Numeric or a primitive type of byte, short, int, long, float, double?

$member.object

boolean

Is the type assignable from java.lang.Object?

$member.date

boolean

Is the type assignable from java.util.Date?

$member.calendar

boolean

Is the type assignable from java.util.Calendar?

$member.boolean

boolean

Is the type assignable from java.lang.Boolean or a primitive boolean?

$field

FieldElement

The FieldElement object

$field.name

String

The name of a field.

$field.modifierTransient

boolean

Does the field have a transient modifier?

$field.modifierVolatile

boolean

Does the field have a volatile modifier?

$field.constant

boolean

Is the field a constant type? (has static modifier and its name is in UPPERCASE only)

$field.matchName(regexp)

boolean

Performs a regular expression matching on a field name.

$field.enum

boolean

Is this field an enum type?

$method

MethodElement

The MethodElement object

$method.name

String

This 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.methodName

String

The name of the method (getFoo).

$method.fieldName

String

The name of the field this getter method covers - null if the method is not a getter for a field

$method.modifierAbstract

boolean

Is this method an abstract method?

$method.modifierSynchronized

boolean

Is this method a synchronized method?

$method.returnTypeVoid

boolean

Is this method a void method (does not return anything) ?

$method.getter

boolean

Is this a getter method?

$method.matchName(regexp)

boolean

Performs a regular expression matching on the method name.

$method.deprecated

boolean

Is this method deprecated?

$class

ClassElement

The ClassElement object

$class.name

String

The name of the class

$class.matchName(regexp)

boolean

Performs a regular expression matching on the classname.

$class.qualifiedName

String

The fully qualified name of the class

$class.hasSuper

boolean

Does the class have a superclass? (extends another class - note extending java.lang.Object is not considered having a superclass)

$class.superName

String

The name of the superclass (empty if no superclass)

$class.superQualifiedName

String

The fully qualified name of the superclass (empty if no superclass)

$class.isImplements("interfaceName")

boolean

Checks if the class implements the given interface. Checking names of several interfaces can be done by separating the names with commas.

$class.implementNames

String[]

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")

boolean

Checks if the class extends any of the given class names. Checking several class names can be done by separating the names with commas.

$class.exception

boolean

Is this class an exception class (extends Throwable)?

$class.deprecated

boolean

Is this class deprecated?

$class.enum

boolean

Is this class an enum class?

$class.abstract

boolean

Is this class abstract?

Output variables

The 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.

$autoImportPackages

String

Packagenames that should automatically be imported. Use comma to separate packagenames.

Last modified: 26 May 2024