{"record":{"id":"350bee371cb4ff7f","repo":"JetBrains/intellij-community","slug":"inconvertible-type-cast","errorCode":"inconvertible.type.cast","errorMessage":"Inconvertible types; cannot cast ''{0}'' to ''{1}''","messagePattern":"Inconvertible types; cannot cast ''(.+?)'' to ''(.+?)''","errorType":"exception","errorClass":"EvaluateException","httpStatus":null,"severity":"error","filePath":"java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/EvaluatorBuilderImpl.java","lineNumber":1512,"sourceCode":"    public void visitTypeCastExpression(@NotNull PsiTypeCastExpression expression) {\n      PsiExpression operandExpr = expression.getOperand();\n      if (operandExpr == null) {\n        throw expressionInvalid(expression);\n      }\n      operandExpr.accept(this);\n      Evaluator operandEvaluator = myResult;\n      PsiTypeElement castTypeElem = expression.getCastType();\n      if (castTypeElem == null) {\n        throw expressionInvalid(expression);\n      }\n      PsiType castType = castTypeElem.getType();\n      PsiType operandType = operandExpr.getType();\n\n      // if operand type can not be resolved in current context - leave it for runtime checks\n      if (operandType != null &&\n          !TypeConversionUtil.areTypesConvertible(operandType, castType) &&\n          PsiUtil.resolveClassInType(operandType) != null) {\n        throw new EvaluateRuntimeException(\n          new EvaluateException(\n            JavaErrorBundle.message(\"inconvertible.type.cast\", JavaHighlightUtil.formatType(operandType), JavaHighlightUtil\n              .formatType(castType)))\n        );\n      }\n\n      boolean shouldPerformBoxingConversion = operandType != null && TypeConversionUtil.boxingConversionApplicable(castType, operandType);\n      final boolean castingToPrimitive = castType instanceof PsiPrimitiveType;\n      if (shouldPerformBoxingConversion && castingToPrimitive) {\n        operandEvaluator = new UnBoxingEvaluator(operandEvaluator);\n      }\n\n      final boolean performCastToWrapperClass = shouldPerformBoxingConversion && !castingToPrimitive;\n\n      if (!(PsiUtil.resolveClassInClassTypeOnly(castType) instanceof PsiTypeParameter)) {\n        if (performCastToWrapperClass) {\n          castType = ObjectUtils.notNull(PsiPrimitiveType.getUnboxedType(castType), operandType);\n        }","sourceCodeStart":1494,"sourceCodeEnd":1530,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/EvaluatorBuilderImpl.java#L1494-L1530","documentation":"Thrown at evaluator-build time when a cast expression in the evaluated fragment is provably invalid: the operand's resolved PSI type and the target cast type are not convertible per TypeConversionUtil and the operand class resolves. This mirrors the Java compiler's inconvertible-types error, catching it before the code ever runs in the debuggee.","triggerScenarios":"Visiting a PsiTypeCastExpression where operandType != null, PsiUtil.resolveClassInType(operandType) != null, and TypeConversionUtil.areTypesConvertible(operandType, castType) is false; e.g. casting a String literal to int, or an Integer to List. Boxing casts to primitives are handled separately after this check.","commonSituations":"User writes '(int) str' or '(List<String>) integerValue' in the Evaluate dialog; generics erasure edge cases where the static evaluator type is an unrelated class; refactoring changed the operand type since the breakpoint was hit.","solutions":["Fix the cast to a type the operand can actually convert to (e.g. Integer.parseInt(str) instead of (int) str)","Cast through an intermediate type: first (Object) then the target, when you know better than the static types","Re-resolve: if the code changed since compile, rebuild the project so PSI types match the running classes"],"exampleFix":"// before: (int) someString\n\n// after: Integer.parseInt(someString)\n// or when certain: (int)(Object) boxedInteger","handlingStrategy":"type-guard","validationCode":"PsiType operandType = operandExpr.getType();\nif (operandType != null && PsiUtil.resolveClassInType(operandType) != null\n    && !TypeConversionUtil.areTypesConvertible(operandType, castType)) {\n  // reject before evaluating, or go through (Object)\n}","typeGuard":"static boolean castIsSafe(@Nullable PsiType from, PsiType to) {\n  return from == null || PsiUtil.resolveClassInType(from) == null\n      || TypeConversionUtil.areTypesConvertible(from, to);\n}","tryCatchPattern":"try {\n  result = evaluator.evaluate(context);\n} catch (EvaluateRuntimeException e) {\n  if (e.getCause() instanceof EvaluateException && /* inconvertible cast */) {\n    // suggest (Object) intermediate cast to the user\n  }\n}","preventionTips":["Use double-cast through Object when intentionally downcasting unrelated static types","Rebuild the project after refactors so evaluator types match loaded classes","Prefer conversion APIs (parseInt, valueOf) over casts between unrelated types"],"tags":["debugger","evaluation","cast","type-check"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}