JetBrains/intellij-community · error · CommonRefactoringUtil.RefactoringErrorHintException

No target class for the instance method is found: method par

Error message

No target class for the instance method is found: method parameter types are unknown.

What it means

ConvertToInstanceMethodHandler throws RefactoringErrorHintException when a static method qualifies for conversion but none of its parameters can serve as the new containing instance. The message is assembled from three cases: no parameters of reference type at all (convertToInstanceMethod.no.parameters.with.reference.type), reference-typed parameters whose classes are all unresolvable (unknown types), or all resolvable classes living outside the project/content roots; if a usable constructor is possible, a hint about a missing default constructor is appended.

Source

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

    }
    if (!qualifiers.isEmpty()) {
      return qualifiers.toArray();
    }
    
    String message;
    if (!classTypesFound) {
      message = JavaRefactoringBundle.message("convertToInstanceMethod.no.parameters.with.reference.type");
    }
    else if (!resolvableClassesFound) {
      message = JavaRefactoringBundle.message("convertToInstanceMethod.all.reference.type.parameters.have.unknown.types");
    }
    else {
      message = JavaRefactoringBundle.message("convertToInstanceMethod.all.reference.type.parameters.are.not.in.project");
    }
    if (canHaveUsableConstructor) {
      message += " " + JavaRefactoringBundle.message("convertToInstanceMethod.no.default.ctor");
    }
    throw new CommonRefactoringUtil.RefactoringErrorHintException(message);
  }

  @Override
  public boolean isAvailableForQuickList(@NotNull Editor editor, @NotNull PsiFile file, @NotNull DataContext dataContext) {
    PsiElement caretElement = BaseRefactoringAction.getElementAtCaret(editor, file);
    return MethodUtils.getJavaMethodFromHeader(caretElement) != null;
  }

  static @NlsContexts.DialogTitle String getRefactoringName() {
    return JavaRefactoringBundle.message("convert.to.instance.method.title");
  }
}

View on GitHub (pinned to be881553f2)

Solutions

  1. Add or select a parameter whose type is a project class (must resolve and be inside project content roots).
  2. Fix unresolved parameter types first: add missing dependencies so the code compiles without red references.
  3. If the only candidate host class is external (JDK/library), the refactoring cannot place a method there — introduce a wrapper parameter of a project type instead.
  4. Where the default-constructor hint is appended, ensure the target class has an accessible no-arg constructor or be ready to create one.

Example fix

// before:
class C { static void m(int x) { ... } }

// after:
class C { static void m(MyProjectObj o, int x) { ... } }  // conversion target now available
Defensive patterns

Strategy: validation

Validate before calling

boolean usable = false;
for (PsiParameter p : method.getParameterList().getParameters()) {
  if (p.getType() instanceof PsiClassType ct) {
    PsiClass c = ct.resolve();
    if (c != null && !(c instanceof PsiTypeParameter) && isInsideProjectSources(c)) usable = true;
  }
}
if (!usable) { /* do not invoke the refactoring; explain why */ }

Try / catch

try {
  ConvertToInstanceMethodHandler.calculatePossibleInstanceQualifiers(method);
} catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
  // message explains: no reference params / unresolved types / classes outside project (+ ctor hint)
}

Prevention

When it happens

Trigger: Running Convert to Instance Method on 'static void m(int x, long y)' (no reference parameters); on 'static void m(Unknown u)' where the type does not resolve; or on 'static void m(String s)' where java.lang.String resolves but is not in project sources and cannot host the method.

Common situations: Utility methods that only take primitives/arrays of primitives; broken classpath so parameter types fail to resolve (red code, missing libraries); parameters typed solely by JDK/third-party classes that the refactoring cannot modify.

Related errors


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