JetBrains/intellij-community · error · IncorrectOperationException

Cannot reset package-private modifier.

Error message

Cannot reset package-private modifier.

What it means

PsiModifierListImpl.setModifierProperty maps modifiers to keyword tokens. Package-private visibility is not a keyword — it is the absence of public/protected/private — so its token type is null. Calling setModifierProperty with value=false for the package-private pseudo-modifier has nothing to remove and throws IncorrectOperationException("Cannot reset package-private modifier.").

Source

Thrown at java/java-psi-impl/src/com/intellij/psi/impl/source/PsiModifierListImpl.java:328

      }
      else if (parent instanceof PsiMethod && grandParent instanceof PsiClass && ((PsiClass)grandParent).isInterface()) {
        if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD) return;
      }
      else if (parent instanceof PsiClass && grandParent instanceof PsiClass && ((PsiClass)grandParent).isInterface()) {
        if (type == JavaTokenType.PUBLIC_KEYWORD) return;
      }
      else if (parent instanceof PsiAnnotationMethod && grandParent instanceof PsiClass && ((PsiClass)grandParent).isAnnotationType()) {
        if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD) return;
      }

      if (treeElement.findChildByType(type) == null) {
        TreeElement keyword = Factory.createSingleLeafElement(type, name, null, getManager());
        treeElement.addInternal(keyword, keyword, null, null);
      }
    }
    else {
      if (type == null /* package-private */) {
        throw new IncorrectOperationException("Cannot reset package-private modifier."); //?
      }

      ASTNode child = treeElement.findChildByType(type);
      if (child != null) {
        SourceTreeToPsiMap.treeToPsiNotNull(child).delete();
      }
    }
  }

  @Override
  public void checkSetModifierProperty(@NotNull String name, boolean value) throws IncorrectOperationException{
    CheckUtil.checkWritable(this);
  }

  @Override
  public PsiAnnotation @NotNull [] getAnnotations() {
    PsiAnnotation[] own = getStubOrPsiChildren(JavaStubElementTypes.ANNOTATION, PsiAnnotation.ARRAY_FACTORY);
    List<PsiAnnotation> ext = PsiAugmentProvider.collectAugments(this, PsiAnnotation.class, null);

View on GitHub (pinned to be881553f2)

Solutions

  1. Never set package-private to false; to make an element package-private, set the other visibility modifiers to false: setModifierProperty(PsiModifier.PUBLIC/PUBLIC/PROTECTED/PRIVATE, false).
  2. Filter out the non-keyword pseudo-modifier before looping: skip names where modifierList.hasExplicitModifier(name) is false or name equals PsiModifier.PACKAGE_LOCAL.
  3. Use PsiModifierListUtil or higher-level visibility helpers that model package-local as absence of other modifiers.

Example fix

// before
for (String m : PsiModifier.MODIFIERS) {
  modifierList.setModifierProperty(m, false); // throws on "package-private"
}

// after
for (String m : PsiModifier.MODIFIERS) {
  if (PsiModifier.PACKAGE_LOCAL.equals(m)) continue;
  modifierList.setModifierProperty(m, false);
}
Defensive patterns

Strategy: validation

Validate before calling

if (PsiModifier.PACKAGE_LOCAL.equals(name)) return; // never set package-private to false

Type guard

static boolean isKeywordModifier(String m) { return !PsiModifier.PACKAGE_LOCAL.equals(m); }

Try / catch

try { list.setModifierProperty(name, value); } catch (IncorrectOperationException e) { /* skip pseudo-modifier */ }

Prevention

When it happens

Trigger: Calling modifierList.setModifierProperty("package-private", false) (or setModifierProperty(PsiModifier.PACKAGE_LOCAL, false)); iterating PsiModifier.MODIFIERS and resetting each to false, which hits the synthetic package-local entry.

Common situations: Code that loops over all modifiers to clear them; visibility-change intentions that compute a modifier name and call setModifierProperty(false); refactoring plugins normalizing modifier lists.

Related errors


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