Java: improve message for cast fix

GitOrigin-RevId: 40cd180ca8fdae46a3ea0216ec7b08e82ca22364
This commit is contained in:
Bas Leijdekkers
2024-08-26 18:38:16 +00:00
committed by intellij-monorepo-bot
parent 2df3d8e9d0
commit 83bba063e7
38 changed files with 49 additions and 50 deletions
@@ -1,5 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
@@ -63,7 +62,7 @@ public class AddTypeCastFix extends PsiUpdateModCommandAction<PsiExpression> {
}
public static void addTypeCast(Project project, PsiExpression originalExpression, PsiType type) {
PsiExpression typeCast = createCastExpression(originalExpression, project, type);
PsiExpression typeCast = createCastExpression(originalExpression, type);
originalExpression.replace(Objects.requireNonNull(typeCast));
}
@@ -74,7 +73,7 @@ public class AddTypeCastFix extends PsiUpdateModCommandAction<PsiExpression> {
return null;
}
static PsiExpression createCastExpression(PsiExpression original, Project project, PsiType type) {
static PsiExpression createCastExpression(PsiExpression original, PsiType type) {
// remove nested casts
PsiElement expression = PsiUtil.deparenthesizeExpression(original);
if (expression == null) return null;
@@ -1,5 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
@@ -20,7 +19,8 @@ public final class CastMethodArgumentFix extends MethodArgumentFix implements Hi
String role = list.getExpressionCount() == 1
? QuickFixBundle.message("fix.expression.role.argument")
: QuickFixBundle.message("fix.expression.role.nth.argument", myIndex + 1);
return QuickFixBundle.message("add.typecast.cast.text", myToType.getPresentableText(), role);
boolean literal = AddTypeCastFix.createCastExpression(list.getExpressions()[myIndex], myToType) instanceof PsiLiteralExpression;
return QuickFixBundle.message(literal ? "add.typecast.convert.text" : "add.typecast.cast.text", myToType.getPresentableText(), role);
}
private static class MyFixerActionFactory extends ArgumentFixerActionFactory {
@@ -32,18 +32,18 @@ public final class CastMethodArgumentFix extends MethodArgumentFix implements Hi
@Override
protected PsiExpression getModifiedArgument(final PsiExpression expression, PsiType toType) throws IncorrectOperationException {
final PsiType exprType = expression.getType();
if (exprType instanceof PsiClassType && toType instanceof PsiPrimitiveType) {
PsiClassType boxed = ((PsiPrimitiveType)toType).getBoxedType(expression);
if (exprType instanceof PsiClassType && toType instanceof PsiPrimitiveType primitiveType) {
PsiClassType boxed = primitiveType.getBoxedType(expression);
assert boxed != null : toType + ":" + PsiUtil.getLanguageLevel(expression);
toType = boxed;
}
return AddTypeCastFix.createCastExpression(expression, expression.getProject(), toType);
return AddTypeCastFix.createCastExpression(expression, toType);
}
@Override
public boolean areTypesConvertible(@NotNull PsiType exprType, @NotNull PsiType parameterType, @NotNull final PsiElement context) {
if (exprType instanceof PsiClassType && parameterType instanceof PsiPrimitiveType) {
parameterType = ((PsiPrimitiveType)parameterType).getBoxedType(context); //unboxing from type of cast expression will take place at runtime
if (exprType instanceof PsiClassType && parameterType instanceof PsiPrimitiveType primitiveType) {
parameterType = primitiveType.getBoxedType(context); //unboxing from type of cast expression will take place at runtime
if (parameterType == null) return false;
}
if (exprType instanceof PsiPrimitiveType && parameterType instanceof PsiClassType) {
@@ -57,8 +57,8 @@ public final class CastMethodArgumentFix extends MethodArgumentFix implements Hi
return true;
}
return parameterType instanceof PsiEllipsisType &&
areTypesConvertible(exprType, ((PsiEllipsisType)parameterType).getComponentType(), context);
return parameterType instanceof PsiEllipsisType ellipsisType &&
areTypesConvertible(exprType, ellipsisType.getComponentType(), context);
}
}
@@ -1,4 +1,4 @@
// "Cast argument to 'double'" "true-preview"
// "Convert argument to 'double'" "true-preview"
class a {
void test(Double d) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'double'" "true-preview"
// "Convert argument to 'double'" "true-preview"
class a {
void test(Double d) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'double'" "true-preview"
// "Convert argument to 'double'" "true-preview"
class a {
void test(Double d) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class a {
void test(Float f) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'long'" "true-preview"
// "Convert argument to 'long'" "true-preview"
class a {
void test(Long l) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'int'" "true-preview"
// "Convert argument to 'int'" "true-preview"
class a {
void test(int d) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'double'" "true-preview"
// "Convert argument to 'double'" "true-preview"
class a {
void test(Double d) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'double'" "true-preview"
// "Convert argument to 'double'" "true-preview"
class a {
void test(Double d) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'double'" "true-preview"
// "Convert argument to 'double'" "true-preview"
class a {
void test(Double d) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class a {
void test(Float f) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'long'" "true-preview"
// "Convert argument to 'long'" "true-preview"
class a {
void test(Long l) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'int'" "true-preview"
// "Convert argument to 'int'" "true-preview"
class a {
void test(int d) {}
@@ -1,4 +1,4 @@
// "Cast 1st argument to 'double'" "true-preview"
// "Convert 1st argument to 'double'" "true-preview"
class x {}
class a extends x {
a(a a) {}
@@ -1,4 +1,4 @@
// "Cast 2nd argument to 'double'" "true-preview"
// "Convert 2nd argument to 'double'" "true-preview"
class x {}
class a extends x {
a(a a) {}
@@ -1,4 +1,4 @@
// "Cast 1st argument to 'double'" "true-preview"
// "Convert 1st argument to 'double'" "true-preview"
class x {}
class a extends x {
a(a a) {}
@@ -1,4 +1,4 @@
// "Cast 2nd argument to 'double'" "true-preview"
// "Convert 2nd argument to 'double'" "true-preview"
class x {}
class a extends x {
a(a a) {}
@@ -1,4 +1,4 @@
// "Cast argument to 'long'" "true-preview"
// "Convert argument to 'long'" "true-preview"
class a {
void f(Long l) {}
void g() {
@@ -1,4 +1,4 @@
// "Cast argument to 'long'" "true-preview"
// "Convert argument to 'long'" "true-preview"
class a {
void f(Long l, String... s) {}
void g() {
@@ -1,4 +1,4 @@
// "Cast 1st argument to 'long'" "true-preview"
// "Convert 1st argument to 'long'" "true-preview"
class a {
void f(Long l, String... s) {}
void g() {
@@ -1,4 +1,4 @@
// "Cast 1st argument to 'long'" "true-preview"
// "Convert 1st argument to 'long'" "true-preview"
class a {
void f(Long l, String... s) {}
void g() {
@@ -1,4 +1,4 @@
// "Cast argument to 'long'" "true-preview"
// "Convert argument to 'long'" "true-preview"
class a {
void f(Long l) {}
void g() {
@@ -1,4 +1,4 @@
// "Cast argument to 'long'" "true-preview"
// "Convert argument to 'long'" "true-preview"
class a {
void f(Long l, String... s) {}
void g() {
@@ -1,4 +1,4 @@
// "Cast 1st argument to 'long'" "true-preview"
// "Convert 1st argument to 'long'" "true-preview"
class a {
void f(Long l, String... s) {}
void g() {
@@ -1,4 +1,4 @@
// "Cast 1st argument to 'long'" "true-preview"
// "Convert 1st argument to 'long'" "true-preview"
class a {
void f(Long l, String... s) {}
void g() {
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(1e1F);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(2.F);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(.3F);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(0.0F);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(3.14F);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(1e-9F);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(1e<caret>1);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(2<caret>.);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(.<caret>3);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(0<caret>.0);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(3<caret>.14);
@@ -1,4 +1,4 @@
// "Cast argument to 'float'" "true-preview"
// "Convert argument to 'float'" "true-preview"
class Test {
void bar() {
foo(1e-9<caret>d);