JetBrains/intellij-community · error · IncorrectOperationException

''{0}'' is not an identifier.

Error message

''{0}'' is not an identifier.

What it means

Thrown by PsiUtil.checkIsIdentifier when the given text is not a valid Java identifier according to PsiNameHelper (e.g. it contains spaces, dots, operators, or is a keyword). It is a guard used by PSI rename and element-factory code paths that must produce syntactically valid names. The throw is an IncorrectOperationException signaling the caller supplied an unusable name.

Source

Thrown at java/java-psi-api/src/com/intellij/psi/util/PsiUtil.java:1115

    if (substitutionMap == null) return null;
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(aClass.getProject());
    return factory.createType(aClass, factory.createSubstitutor(substitutionMap));
  }

  public static boolean isInsideJavadocComment(PsiElement element) {
    return PsiTreeUtil.getParentOfType(element, PsiDocComment.class, true, PsiMember.class) != null;
  }

  public static @NotNull @Unmodifiable List<PsiTypeElement> getParameterTypeElements(@NotNull PsiParameter parameter) {
    PsiTypeElement typeElement = parameter.getTypeElement();
    return typeElement != null && typeElement.getType() instanceof PsiDisjunctionType
           ? PsiTreeUtil.getChildrenOfTypeAsList(typeElement, PsiTypeElement.class)
           : Collections.singletonList(typeElement);
  }

  public static void checkIsIdentifier(@NotNull PsiManager manager, String text) throws IncorrectOperationException{
    if (!PsiNameHelper.getInstance(manager.getProject()).isIdentifier(text)){
      throw new IncorrectOperationException(JavaPsiBundle.message("0.is.not.an.identifier", text) );
    }
  }

  public static @Nullable VirtualFile getJarFile(@NotNull PsiElement candidate) {
    VirtualFile file = candidate.getContainingFile().getVirtualFile();
    if (file != null && file.getFileSystem().getProtocol().equals("jar")) {
      return VfsUtilCore.getVirtualFileForJar(file);
    }
    return file;
  }

  public static boolean isAnnotationMethod(PsiElement element) {
    if (!(element instanceof PsiAnnotationMethod)) return false;
    PsiClass psiClass = ((PsiAnnotationMethod)element).getContainingClass();
    return psiClass != null && psiClass.isAnnotationType();
  }

  /**

View on GitHub (pinned to be881553f2)

Solutions

  1. Validate the name first with PsiNameHelper.getInstance(project).isIdentifier(name) and reject or sanitize it before calling setName.
  2. If the name comes from user input, show an input validator (e.g. Messages.showInputDialog with a validator) that only accepts identifiers.
  3. Strip illegal characters or convert the string with Java convention (e.g. NameUtil or Introspector.decapitalize on a sanitized base) before use.
  4. If the element legitimately has a non-identifier name (keyword or operator overloading in other JVM languages), avoid this API for that element type.

Example fix

// before
psiVariable.setName(userInput); // throws when userInput = "my value"

// after
PsiNameHelper helper = PsiNameHelper.getInstance(project);
if (!helper.isIdentifier(userInput)) {
  userInput = sanitizeToIdentifier(userInput); // strip/space->camel yourself
}
psiVariable.setName(userInput);
Defensive patterns

Strategy: validation

Validate before calling

PsiNameHelper helper = PsiNameHelper.getInstance(project);
if (!helper.isIdentifier(newName)) {
  // reject or sanitize before any setName call
  return;
}

Type guard

static boolean isValidIdentifier(Project p, String s) {
  return s != null && PsiNameHelper.getInstance(p).isIdentifier(s);
}

Try / catch

try { psiElement.setName(name); } catch (IncorrectOperationException e) { /* report invalid name to user */ }

Prevention

When it happens

Trigger: Calling PsiUtil.checkIsIdentifier(manager, text) directly, or any PSI setName/createElementFromText flow that routes through it, with text like "my class", "foo.bar", "int", "", or a name with illegal characters.

Common situations: Refactoring plugins or rename handlers that pass a user-typed or generated string straight into PsiElement.setName; live templates or code generators producing names with whitespace; programmatic element creation from unvalidated input.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/82006d800b92557c. Report an issue: GitHub.