{"record":{"id":"084181b7854d0d8b","repo":"JetBrains/intellij-community","slug":"cannot-reset-package-private-modifier","errorCode":null,"errorMessage":"Cannot reset package-private modifier.","messagePattern":"Cannot reset package-private modifier\\.","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-psi-impl/src/com/intellij/psi/impl/source/PsiModifierListImpl.java","lineNumber":328,"sourceCode":"      }\n      else if (parent instanceof PsiMethod && grandParent instanceof PsiClass && ((PsiClass)grandParent).isInterface()) {\n        if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD) return;\n      }\n      else if (parent instanceof PsiClass && grandParent instanceof PsiClass && ((PsiClass)grandParent).isInterface()) {\n        if (type == JavaTokenType.PUBLIC_KEYWORD) return;\n      }\n      else if (parent instanceof PsiAnnotationMethod && grandParent instanceof PsiClass && ((PsiClass)grandParent).isAnnotationType()) {\n        if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD) return;\n      }\n\n      if (treeElement.findChildByType(type) == null) {\n        TreeElement keyword = Factory.createSingleLeafElement(type, name, null, getManager());\n        treeElement.addInternal(keyword, keyword, null, null);\n      }\n    }\n    else {\n      if (type == null /* package-private */) {\n        throw new IncorrectOperationException(\"Cannot reset package-private modifier.\"); //?\n      }\n\n      ASTNode child = treeElement.findChildByType(type);\n      if (child != null) {\n        SourceTreeToPsiMap.treeToPsiNotNull(child).delete();\n      }\n    }\n  }\n\n  @Override\n  public void checkSetModifierProperty(@NotNull String name, boolean value) throws IncorrectOperationException{\n    CheckUtil.checkWritable(this);\n  }\n\n  @Override\n  public PsiAnnotation @NotNull [] getAnnotations() {\n    PsiAnnotation[] own = getStubOrPsiChildren(JavaStubElementTypes.ANNOTATION, PsiAnnotation.ARRAY_FACTORY);\n    List<PsiAnnotation> ext = PsiAugmentProvider.collectAugments(this, PsiAnnotation.class, null);","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiModifierListImpl.java#L310-L346","documentation":"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.\").","triggerScenarios":"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.","commonSituations":"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.","solutions":["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).","Filter out the non-keyword pseudo-modifier before looping: skip names where modifierList.hasExplicitModifier(name) is false or name equals PsiModifier.PACKAGE_LOCAL.","Use PsiModifierListUtil or higher-level visibility helpers that model package-local as absence of other modifiers."],"exampleFix":"// before\nfor (String m : PsiModifier.MODIFIERS) {\n  modifierList.setModifierProperty(m, false); // throws on \"package-private\"\n}\n\n// after\nfor (String m : PsiModifier.MODIFIERS) {\n  if (PsiModifier.PACKAGE_LOCAL.equals(m)) continue;\n  modifierList.setModifierProperty(m, false);\n}","handlingStrategy":"validation","validationCode":"if (PsiModifier.PACKAGE_LOCAL.equals(name)) return; // never set package-private to false","typeGuard":"static boolean isKeywordModifier(String m) { return !PsiModifier.PACKAGE_LOCAL.equals(m); }","tryCatchPattern":"try { list.setModifierProperty(name, value); } catch (IncorrectOperationException e) { /* skip pseudo-modifier */ }","preventionTips":["Model package-private as absence of other visibility modifiers.","Filter PACKAGE_LOCAL out of modifier iteration loops."],"tags":["java","psi","modifiers","visibility","package-private"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}