[java-intentions] ConvertDoubleToFloatFix removed; superseded with CastMethodArgumentFix

GitOrigin-RevId: 861631447c4281fd75f77545702e8540ed4ed38b
This commit is contained in:
Tagir Valeev
2022-06-30 08:49:35 +00:00
committed by intellij-monorepo-bot
parent 3b01ccb40d
commit 92a5282ab9
17 changed files with 34 additions and 139 deletions
@@ -960,7 +960,6 @@ public final class HighlightMethodUtil {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createSurroundWithArrayFix(methodCall, null));
CastMethodArgumentFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
ConvertDoubleToFloatFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
AddTypeArgumentsFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
CandidateInfo[] candidates = resolveHelper.getReferencedMethodCandidates(methodCall, true);
@@ -1881,7 +1880,6 @@ public final class HighlightMethodUtil {
if (classReference != null) {
ConstructorParametersFixer.registerFixActions(classReference, constructorCall, info, fixRange);
ChangeTypeArgumentsFix.registerIntentions(results, list, info, aClass, fixRange);
ConvertDoubleToFloatFix.registerIntentions(results, list, info, fixRange);
}
ChangeStringLiteralToCharInMethodCallFix.registerFixes(constructors, constructorCall, info, fixRange);
QuickFixAction.registerQuickFixAction(info, fixRange, QUICK_FIX_FACTORY.createSurroundWithArrayFix(constructorCall, null));
@@ -1,113 +0,0 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.java.analysis.JavaAnalysisBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ConvertDoubleToFloatFix implements IntentionAction {
private final PsiExpression myExpression;
public ConvertDoubleToFloatFix(PsiExpression expression) {
myExpression = expression;
}
@NotNull
@Override
public String getText() {
return JavaAnalysisBundle.message("convert.0.to.float", myExpression.getText());
}
@NotNull
@Override
public String getFamilyName() {
return getText();
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (myExpression.isValid()) {
if (!StringUtil.endsWithIgnoreCase(myExpression.getText(), "f")) {
final PsiLiteralExpression expression = (PsiLiteralExpression)createFloatingPointExpression(project);
final Object value = expression.getValue();
return value instanceof Float && !((Float)value).isInfinite() && !(((Float)value).floatValue() == 0 && !TypeConversionUtil.isFPZero(expression.getText()));
}
}
return false;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
myExpression.replace(createFloatingPointExpression(project));
}
private PsiExpression createFloatingPointExpression(Project project) {
final String text = myExpression.getText();
if (StringUtil.endsWithIgnoreCase(text, "d")) {
return JavaPsiFacade.getElementFactory(project).createExpressionFromText(text.substring(0, text.length() - 1) + "f", myExpression);
} else {
return JavaPsiFacade.getElementFactory(project).createExpressionFromText(text + "f", myExpression);
}
}
@Override
public boolean startInWriteAction() {
return true;
}
public static void registerIntentions(JavaResolveResult @NotNull [] candidates,
@NotNull PsiExpressionList list,
@Nullable HighlightInfo highlightInfo,
TextRange fixRange) {
if (candidates.length == 0) return;
PsiExpression[] expressions = list.getExpressions();
for (JavaResolveResult candidate : candidates) {
registerIntention(expressions, highlightInfo, fixRange, candidate, list);
}
}
private static void registerIntention(PsiExpression @NotNull [] expressions,
@Nullable HighlightInfo highlightInfo,
TextRange fixRange,
@NotNull JavaResolveResult candidate,
@NotNull PsiElement context) {
if (!candidate.isStaticsScopeCorrect()) return;
PsiMethod method = (PsiMethod)candidate.getElement();
if (method != null && BaseIntentionAction.canModify(method)) {
final PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length == expressions.length) {
for (int i = 0, length = parameters.length; i < length; i++) {
PsiParameter parameter = parameters[i];
final PsiExpression expression = expressions[i];
if (expression instanceof PsiLiteralExpression && PsiType.FLOAT.equals(parameter.getType()) && PsiType.DOUBLE.equals(expression.getType())) {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new ConvertDoubleToFloatFix(expression));
}
}
}
}
}
}
@@ -666,12 +666,15 @@ public final class PsiLiteralUtil {
}
if (PsiType.DOUBLE.equals(exprType) && PsiType.FLOAT.equals(wantedType)) {
Double value = ObjectUtils.tryCast(literal.getValue(), Double.class);
if (value != null && (double)(float)(double)value == value) {
String text = literal.getText();
if (StringUtil.endsWithIgnoreCase(text, "D")) {
text = text.substring(0, text.length() - 1);
if (value != null) {
float f = (float)(double)value;
if (Float.isFinite(f) && (f != 0.0 || value == 0.0)) {
String text = literal.getText();
if (StringUtil.endsWithIgnoreCase(text, "D")) {
text = text.substring(0, text.length() - 1);
}
return text + "F";
}
return text + "F";
}
}
if (PsiType.FLOAT.equals(exprType) && PsiType.DOUBLE.equals(wantedType)) {
@@ -1,7 +1,7 @@
// "Convert '1e1' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(1e1f);
foo(1e1F);
}
void foo(float f){}
}
@@ -1,7 +1,7 @@
// "Convert '2.' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(2.f);
foo(2.F);
}
void foo(float f){}
}
@@ -1,7 +1,7 @@
// "Convert '.3' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(.3f);
foo(.3F);
}
void foo(float f){}
}
@@ -1,7 +1,7 @@
// "Convert '0.0' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(0.0f);
foo(0.0F);
}
void foo(float f){}
}
@@ -1,7 +1,7 @@
// "Convert '3.14' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(3.14f);
foo(3.14F);
}
void foo(float f){}
}
@@ -1,7 +1,7 @@
// "Convert '1e-9d' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(1e-9f);
foo(1e-9F);
}
void foo(float f){}
}
@@ -0,0 +1,7 @@
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo((float) 1e137);
}
void foo(float f){}
}
@@ -1,4 +1,4 @@
// "Convert '1e1' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(1e<caret>1);
@@ -1,4 +1,4 @@
// "Convert '2.' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(2<caret>.);
@@ -1,4 +1,4 @@
// "Convert '.3' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(.<caret>3);
@@ -1,4 +1,4 @@
// "Convert '0.0' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(0<caret>.0);
@@ -1,4 +1,4 @@
// "Convert '3.14' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(3<caret>.14);
@@ -1,4 +1,4 @@
// "Convert '1e-9d' to float" "true"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(1e-9<caret>d);
@@ -1,4 +1,4 @@
// "Convert '1e137' to float" "false"
// "Cast argument to 'float'" "true"
class Test {
void bar() {
foo(1e1<caret>37);