JetBrains/intellij-community · error · CommonRefactoringUtil.RefactoringErrorHintException

Method {0} is not static

Error message

Method {0} is not static

What it means

ConvertToInstanceMethodHandler.calculatePossibleInstanceQualifiers throws CommonRefactoringUtil.RefactoringErrorHintException when the method passed to the 'Convert To Instance Method' refactoring lacks the static modifier. The refactoring works by demoting a static method to an instance method of one of its parameter types, so a non-static method has nothing to convert; the exception message names the offending method. In interactive mode the handler catches it and shows an error hint; in unit test mode it propagates.

Source

Thrown at java/java-impl-refactorings/src/com/intellij/refactoring/convertToInstanceMethod/ConvertToInstanceMethodHandler.java:81

  @Override
  public void invoke(@NotNull Project project, PsiElement @NotNull [] elements, DataContext dataContext) {
    if (elements.length != 1 || !(elements[0] instanceof PsiMethod method)) return;
    try {
      new ConvertToInstanceMethodDialog(method, calculatePossibleInstanceQualifiers(method)).show();
    }
    catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
      if (ApplicationManager.getApplication().isUnitTestMode()) throw e;
      Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
      CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.getCannotRefactorMessage(e.getMessage()),
                                          getRefactoringName(), HelpID.CONVERT_TO_INSTANCE_METHOD);
    }
  }

  @VisibleForTesting
  public static Object @NotNull [] calculatePossibleInstanceQualifiers(@NotNull PsiMethod method) {
    if (!method.hasModifierProperty(PsiModifier.STATIC)) {
      throw new CommonRefactoringUtil.RefactoringErrorHintException(
        JavaRefactoringBundle.message("convertToInstanceMethod.method.is.not.static", method.getName()));
    }
    List<Object> qualifiers = new ArrayList<>();
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    boolean classTypesFound = false;
    boolean resolvableClassesFound = false;
    for (final PsiParameter parameter : parameters) {
      final PsiType type = parameter.getType();
      if (type instanceof PsiClassType classType) {
        classTypesFound = true;
        final PsiClass psiClass = classType.resolve();
        if (psiClass != null && !(psiClass instanceof PsiTypeParameter)) {
          resolvableClassesFound = true;
          if (method.getManager().isInProject(psiClass)) {
            qualifiers.add(parameter);
          }
        }
      }

View on GitHub (pinned to be881553f2)

Solutions

  1. Select a static method (add the 'static' modifier or move the caret into a static method) before invoking the refactoring.
  2. If the method should become an instance method of a parameter type, make it static first, convert, then adjust.
  3. Programmatic callers: check method.hasModifierProperty(PsiModifier.STATIC) before calling and skip/report instead.
  4. If the editor and model disagree, sync the file (resolve stale PSI) and retry.

Example fix

// before:
class C { void m(A a) { ... } }  // convert-to-instance fails

// after:
class C { static void m(A a) { ... } }  // then Convert to Instance Method succeeds
Defensive patterns

Strategy: validation

Validate before calling

if (!method.hasModifierProperty(PsiModifier.STATIC)) {
  // skip or report 'method is not static' before invoking the refactoring
}

Try / catch

try {
  ConvertToInstanceMethodHandler.calculatePossibleInstanceQualifiers(method);
} catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
  // show hint message; in unit-test mode this exception propagates
}

Prevention

When it happens

Trigger: Invoking Convert to Instance Method (Refactor menu or intention) with the caret on a non-static method, or calling calculatePossibleInstanceQualifiers(method) programmatically where !method.hasModifierProperty(PsiModifier.STATIC).

Common situations: Invoking the refactoring from a quicklist or keyboard shortcut while the caret resolves to an instance method; plugins/upgrade scripts batch-applying the refactoring without checking modifiers; caret on a method that was just made non-static in the editor but the PSI used is stale.

Related errors


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