Description
Custom postfix templates allow you to transform expressions on the fly (e.g., turning myVar.toHex into Utils.toHex(myVar)). However, configuring these for primitive types (int, double, boolean) or primitive arrays (byte[], int[]) requires a specific approach because they do not appear in standard class searches.
This article explains how to map any primitive type or array to a custom postfix template and how to solve common "missing template" issues.
Open the Postfix Completion Settings
- Open Settings (
Cmd + ,on macOS orCtrl + Alt + Son Windows). - Navigate to Editor | General | Postfix Completion.
- Click the + icon and select Java.
- Configure the Template Basics:
- Key: The shorthand wanted to type (e.g.,
toHex,print,abs). - Minimum Language Level: Set this to 8 for broad compatibility.
- Apply to topmost expression: Checked. This ensures the template wraps the entire variable or method call preceding the dot.
- Key: The shorthand wanted to type (e.g.,
Specifying Primitive Types (The Secret Syntax)
IntelliJ's "Applicable expression types" list often doesn't find primitives through the standard search. To add these, click the + button in the types area and select "Enter class name".
For Primitive Arrays (e.g., byte[], int[])
- Standard approach: Type the type followed by brackets, like
byte[]orint[]. - The JVM Descriptor approach: If the standard approach fails, use the internal signatures. These are guaranteed to work even if the text turns red in the settings:
byte[]→[Bint[]→[Idouble[]→[D
For Single Primitives (e.g., int, long)
- Select "Enter class name" and type the primitive name exactly:
int,double, orboolean.
Working Example: for toHexbyte[]
To create a hex converter shortcut specifically for byte arrays:
- Key:
toHex - Applicable Types:
- Choose Enter class name -> Type
byte[] - Choose Enter class name -> Type
java.lang.Byte[](for wrapper arrays)
- Choose Enter class name -> Type
- Template Code:
ByteArrayHexConverter.toHex($EXPR$)$END$
Troubleshooting Common Issues
If all steps were followed, but the template still isn't appearing when typing .toHex, check these common culprits:
-
The "Red Text" in Settings
When using "Enter class name" for
byte[]orint, the IDE sometimes marks the text in red.- The fix: Ignore it. This is a UI validation quirk. As long as the string matches the type in the code, the template engine will still fire correctly in the editor.
-
Using the Wrong Type String
If
byte[]isn't triggering the template, the IDE's internal type system might be looking for the JVM signature.- The fix: Go back to settings and add
[Bas an applicable type. This is the "bulletproof" internal name for a primitive byte array.
- The fix: Go back to settings and add
-
Language Level Mismatch
If the template is set to "Minimum Language Level: 21" but the project is using Java 17, the template will be hidden.
- The fix: Lower the Minimum Language Level to 8 or 1.3.
-
Overlapping Expressions (Topmost vs. Non-topmost)
If "Apply to topmost expression" is checked and typed
myArray.toHexinside another method call likeSystem.out.println(myArray).toHex, the IDE might try to apply the template to theprintlnresult (which isvoid) instead ofmyArray.- The fix: If more control is wanted, uncheck "Apply to topmost."
-
The "Nuclear" Fix: Clearing Caches
If all settings are verified and the troubleshooting steps above are followed, but the template still fails to appear in the autocomplete list, the IDE's indexes may be out of sync. Follow these steps to reset the system state:
- Go to File | Cache Recovery | Invalidate Caches...
- In the dialog, check:
- Clear file system cache and Local History
- Click Invalidate and Restart.
- After the restart, wait for indexing to finish and try typing the postfix again. This should resolve the issue and make the custom template visible.
Comments
0 comments
Please sign in to leave a comment.