JetBrains/intellij-community · error · IncorrectOperationException

Reference is qualified:

Error message

Reference is qualified: 

What it means

PsiReferenceExpressionImpl.bindToElementViaStaticImport rewrites an unqualified static reference (e.g. assertThat(...)) by adding a static import of the member's class and dropping the qualifier. If the reference expression already has a qualifier (FOO.bar), there is nothing meaningful to import — the call throws IncorrectOperationException("Reference is qualified: "+getText()). A null qualifiedName on the class also throws (message-less IncorrectOperationException).

Source

Thrown at java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java:137

  private volatile String myCachedQName;
  private volatile String myCachedNormalizedText;

  public PsiReferenceExpressionImpl() {
    super(JavaElementType.REFERENCE_EXPRESSION);
  }

  @Override
  public PsiExpression getQualifierExpression() {
    return (PsiExpression)findChildByRoleAsPsiElement(ChildRole.QUALIFIER);
  }

  @Override
  public PsiElement bindToElementViaStaticImport(@NotNull PsiClass qualifierClass) throws IncorrectOperationException {
    String qualifiedName = qualifierClass.getQualifiedName();
    if (qualifiedName == null) throw new IncorrectOperationException();

    if (getQualifierExpression() != null) {
      throw new IncorrectOperationException("Reference is qualified: "+getText());
    }
    String staticName = getReferenceName();
    PsiFile containingFile = getContainingFile();
    PsiImportList importList = null;
    boolean doImportStatic;
    if (containingFile instanceof PsiJavaFile) {
      importList = ((PsiJavaFile)containingFile).getImportList();
      assert importList != null : containingFile;
      PsiImportStatementBase singleImportStatement = importList.findSingleImportStatement(staticName);
      doImportStatic = singleImportStatement == null;
      if (singleImportStatement instanceof PsiImportStaticStatement) {
        String qName = qualifierClass.getQualifiedName() + "." + staticName;
        if (qName.equals(singleImportStatement.getImportReference().getQualifiedName())) return this;
      }
    }
    else {
      doImportStatic = false;
    }

View on GitHub (pinned to be881553f2)

Solutions

  1. Guard the call: only invoke when refExpr.getQualifierExpression() == null and qualifierClass.getQualifiedName() != null.
  2. For qualified references, first remove the qualifier (or replace the whole expression with the unqualified name), then bind via static import.
  3. Skip local/anonymous classes — they cannot be static-imported; qualify explicitly instead.

Example fix

// before
refExpr.bindToElementViaStaticImport(clazz); // refExpr is 'Assertions.assertEquals' -> throws

// after
if (refExpr.getQualifierExpression() == null && clazz.getQualifiedName() != null) {
  refExpr.bindToElementViaStaticImport(clazz);
} else {
  // remove qualifier or use fully-qualified name directly
}
Defensive patterns

Strategy: validation

Validate before calling

if (refExpr.getQualifierExpression() != null) return; // qualified: cannot static-import
if (qualifierClass.getQualifiedName() == null) return; // local/anonymous class

Type guard

static boolean canBindViaStaticImport(PsiReferenceExpression r, PsiClass c) {
  return r.getQualifierExpression() == null && c.getQualifiedName() != null;
}

Try / catch

try { refExpr.bindToElementViaStaticImport(c); } catch (IncorrectOperationException e) { /* strip qualifier first, then retry */ }

Prevention

When it happens

Trigger: Calling refExpr.bindToElementViaStaticImport(clazz) when refExpr.getQualifierExpression() != null, or when qualifierClass.getQualifiedName() == null (local/anonymous class).

Common situations: Java refactoring 'add static import' / migrate qualifier to static import; plugins that auto-static-import assertions and accidentally process already-qualified calls or references resolved to anonymous classes.

Related errors


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