Boost Java Enum Handling with the EnumHelper IntelliJ Plugin

This article introduces EnumHelper, an IntelliJ IDEA plugin that automatically generates common enum utility methods for Java developers, explains how to install and use it, and shares detailed troubleshooting tips and core source code to avoid repetitive boilerplate.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Boost Java Enum Handling with the EnumHelper IntelliJ Plugin

Background

As a Java developer, you often need to retrieve an enum instance by a specific field, which leads to repetitive code such as the following method:

public static TestEnum getByCode(Integer code) {
    return Arrays.asList(values()).stream()
        .filter(i -> i.getCode().equals(code))
        .findFirst()
        .orElse(null);
}

Because this pattern appears in many enum classes, I created an IntelliJ IDEA plugin called EnumHelper to generate such code automatically.

Plugin Showcase

The plugin is already published; you can find it in the JetBrains Marketplace by searching for EnumHelper and install it directly.

EnumHelper plugin screenshot
EnumHelper plugin screenshot

Usage Guide

Follow these steps to generate enum methods:

Copy the enum field you want to query.

Run the plugin to generate the corresponding enum method.

EnumHelper usage gif
EnumHelper usage gif

Pitfalls Summary

The project code is not overly complex, but the core implementation is shown below for reference.

Core code:

public class GenerateEnumMethodServiceImpl implements GenerateEnumMethodService {
    @Override
    public void doGenerate(Project project, DataContext dataContext, PsiFile psiFile) {
        GenerateContext generateContext = getGenerateContext(project, dataContext, psiFile);
        GenerateMethodInfo generateMethodInfo = getMethodInfoFromClipboard(generateContext);
        this.generateCode(generateContext, generateMethodInfo);
    }

    private GenerateMethodInfo getMethodInfoFromClipboard(GenerateContext generateContext) {
        String clipboardText = MyUtil.getSystemClipboardText().trim();
        if (clipboardText == null || clipboardText == "") {
            throw new ShipException("please copy enum field first!");
        }
        PsiClass psiClass = generateContext.getPsiClass();
        JvmClassKind classKind = psiClass.getClassKind();
        if (!classKind.equals(JvmClassKind.ENUM)) {
            throw new ShipException("the class must be Enum type!");
        }
        PsiField psiField = getField(psiClass, clipboardText);
        if (psiField == null) {
            throw new ShipException("please check you copy field!");
        }
        GenerateMethodInfo generateMethodInfo = new GenerateMethodInfo();
        generateMethodInfo.setClassName(psiClass.getName());
        generateMethodInfo.setFieldName(psiField.getName());
        generateMethodInfo.setFieldType(psiField.getType().getPresentableText());
        return generateMethodInfo;
    }

    private PsiField getField(PsiClass psiClass, String name) {
        for (PsiField psiField : psiClass.getFields()) {
            if (psiField.getName().equals(name)) {
                return psiField;
            }
        }
        return null;
    }

    private void generateCode(GenerateContext generateContext, GenerateMethodInfo generateMethodInfo) {
        Application application = ApplicationManager.getApplication();
        application.runWriteAction(() -> {
            if (MyUtil.isJava8OrHigher(generateContext)) {
                int currLineNumber = generateContext.getLineNumber();
                for (int i = 0; i < EnumHelperConstants.JDK_8_TOTAL_LINE_NUM; i++) {
                    int lineStartOffset = generateContext.getDocument().getLineStartOffset(currLineNumber++);
                    String codeLine = CodeTemplate.buildCodeLine(i, generateMethodInfo);
                    WriteCommandAction.runWriteCommandAction(generateContext.getProject(), () -> {
                        generateContext.getDocument().insertString(lineStartOffset, codeLine);
                        generateContext.getEditor().getCaretModel().moveToOffset(lineStartOffset + 2);
                        generateContext.getEditor().getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
                    });
                }
            } else {
                int currLineNumber = generateContext.getLineNumber();
                for (int i = 0; i < EnumHelperConstants.JDK_7_TOTAL_LINE_NUM; i++) {
                    int lineStartOffset = generateContext.getDocument().getLineStartOffset(currLineNumber++);
                    String codeLine = CodeTemplate.buildCodeLineForJdk7(i, generateMethodInfo);
                    WriteCommandAction.runWriteCommandAction(generateContext.getProject(), () -> {
                        generateContext.getDocument().insertString(lineStartOffset, codeLine);
                        generateContext.getEditor().getCaretModel().moveToOffset(lineStartOffset + 2);
                        generateContext.getEditor().getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
                    });
                }
            }
        });
    }

    private GenerateContext getGenerateContext(Project project, DataContext dataContext, PsiFile psiFile) {
        GenerateContext generateContext = new GenerateContext();
        generateContext.setProject(project);
        generateContext.setDataContext(dataContext);
        generateContext.setPsiFile(psiFile);
        Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
        PsiElement psiElement = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
        Document document = editor.getDocument();
        generateContext.setEditor(editor);
        generateContext.setDocument(document);
        generateContext.setPsiElement(psiElement);
        generateContext.setOffset(editor.getCaretModel().getOffset());
        generateContext.setLineNumber(document.getLineNumber(generateContext.getOffset()));
        generateContext.setStartOffset(document.getLineStartOffset(generateContext.getLineNumber()));
        generateContext.setEditorText(document.getCharsSequence());
        String clazzName = psiFile.getName().replace(".java", "");
        PsiClass[] psiClasses = PsiShortNamesCache.getInstance(generateContext.getProject())
            .getClassesByName(clazzName, GlobalSearchScope.projectScope(generateContext.getProject()));
        generateContext.setPsiClass(psiClasses[0]);
        return generateContext;
    }
}

Related tutorials and official documentation can be found at the JetBrains plugin development guide.

Problem 1: When creating a plugin project, the IntelliJ Platform Plugin SDK option is missing. Solution: Manually add the SDK via Project Structure → SDKs → +.

Problem 2: Runtime error java.lang.ClassNotFoundException. Solution: Add missing dependencies in plugin.xml:

<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.modules.java</depends>

Problem 3: Action shortcut does not work on macOS. Solution: Add appropriate keyboard-shortcut entries for the Mac keymap in plugin.xml:

<keyboard-shortcut keymap="$default" first-keystroke="control shift J"/>
<keyboard-shortcut keymap="Mac OS X" first-keystroke="control shift J" remove="true"/>
<keyboard-shortcut keymap="Mac OS X 10.5+" first-keystroke="control shift J" replace-all="true"/>

Problem 4: Build fails due to Gradle version mismatch. Solution: Align the org.jetbrains.intellij plugin version with the Gradle version, or downgrade the plugin version if you cannot upgrade Gradle.

Problem 5: patchPluginXml() method signature error in build.gradle. Solution: Comment out the problematic line; the build proceeds.

Problem 6: Plugin action does not appear after debugging. Solution: Clear the sandbox cache by deleting its contents and restart the IDE.

Note: The plugin currently requires JDK 8 or higher because the generated code uses lambda expressions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Code GenerationTutorialIDEIntelliJ Plugin
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.