From 6cc89365984b181faf5a2c706c94127c72bc077e Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 25 Jan 2018 15:40:55 +0700 Subject: [PATCH] Compute constant expression as an inspection; minor improvements Works for unary operation Do not suggested if replacement is the same as the original code Replacement is displayed in the quick-fix name --- .../ConstantExpression.html | 7 + .../constantExpression/afterLongString.java | 7 + .../constantExpression/afterSimple.java | 4 + .../constantExpression/afterStrings.java | 16 ++ .../constantExpression/afterUnary.java | 4 + .../constantExpression/beforeLongString.java | 7 + .../constantExpression/beforeSimple.java | 4 + .../constantExpression/beforeStrings.java | 16 ++ .../constantExpression/beforeUnary.java | 4 + .../constantExpression/beforeUnaryMinus.java | 4 + .../beforeVeryLongString.java | 11 ++ .../siyeh/InspectionGadgetsBundle.properties | 7 +- .../style/ConstantExpressionInspection.java | 150 ++++++++++++++++++ .../src/META-INF/InspectionGadgets.xml | 4 + .../ConstantExpressionInspectionTest.java | 23 +++ .../src/META-INF/IntentionPowerPack.xml | 5 - .../siyeh/IntentionPowerPackBundle.properties | 1 - .../constant/ConstantExpressionIntention.java | 102 ------------ .../constant/ConstantExpressionPredicate.java | 67 -------- .../after.java.template | 5 - .../before.java.template | 5 - .../description.html | 6 - 22 files changed, 267 insertions(+), 192 deletions(-) create mode 100644 java/java-impl/src/inspectionDescriptions/ConstantExpression.html create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterLongString.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterSimple.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterStrings.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterUnary.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeLongString.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeSimple.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeStrings.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeUnary.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeUnaryMinus.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeVeryLongString.java create mode 100644 plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ConstantExpressionInspection.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/ConstantExpressionInspectionTest.java delete mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/constant/ConstantExpressionIntention.java delete mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/constant/ConstantExpressionPredicate.java delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/after.java.template delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/before.java.template delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/description.html diff --git a/java/java-impl/src/inspectionDescriptions/ConstantExpression.html b/java/java-impl/src/inspectionDescriptions/ConstantExpression.html new file mode 100644 index 000000000000..3b72ecee9b3a --- /dev/null +++ b/java/java-impl/src/inspectionDescriptions/ConstantExpression.html @@ -0,0 +1,7 @@ + + +Allows you to replace compile-time constant expression with its actual value (e.g. "2 + 2" with "4"). + +New in 2018.1 + + \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterLongString.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterLongString.java new file mode 100644 index 000000000000..39c244b21d66 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterLongString.java @@ -0,0 +1,7 @@ +// "Compute constant value of '"The quick brown fox jumps " + 100000 + " times" + " over the lazy dog"'" "true" +class Test { + void test() { + // Do not display the result in action name + String foo = "The quick brown fox jumps 100000 times over the lazy dog"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterSimple.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterSimple.java new file mode 100644 index 000000000000..214b5a1ca3c2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterSimple.java @@ -0,0 +1,4 @@ +// "Replace '2 * 2' with constant value '4'" "true" +class Test { + int x = 4; +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterStrings.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterStrings.java new file mode 100644 index 000000000000..2a7e67c9239e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterStrings.java @@ -0,0 +1,16 @@ +// "Fix all 'Constant expression can be evaluated' problems in file" "true" +class Test { + void test() { + String[] squares = new String[10]; + squares[0] = "0*0=0"; + squares[1] = "1*1=1"; + squares[2] = "2*2=4"; + squares[3] = "3*3=9"; + squares[4] = "4*4=16"; + squares[5] = "5*5=25"; + squares[6] = "6*6=36"; + squares[7] = "7*7=49"; + squares[8] = "8*8=64"; + squares[9] = "9*9=81"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterUnary.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterUnary.java new file mode 100644 index 000000000000..52d3d79d9058 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/afterUnary.java @@ -0,0 +1,4 @@ +// "Replace '~2' with constant value '-3'" "true" +class Test { + int x = -3; +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeLongString.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeLongString.java new file mode 100644 index 000000000000..210f13603c49 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeLongString.java @@ -0,0 +1,7 @@ +// "Compute constant value of '"The quick brown fox jumps " + 100000 + " times" + " over the lazy dog"'" "true" +class Test { + void test() { + // Do not display the result in action name + String foo = "The quick brown fox jumps " + 100000 + " times" + " over the lazy dog"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeSimple.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeSimple.java new file mode 100644 index 000000000000..4d8e2872ff71 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeSimple.java @@ -0,0 +1,4 @@ +// "Replace '2 * 2' with constant value '4'" "true" +class Test { + int x = 2*2; +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeStrings.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeStrings.java new file mode 100644 index 000000000000..a447df3f5a7b --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeStrings.java @@ -0,0 +1,16 @@ +// "Fix all 'Constant expression can be evaluated' problems in file" "true" +class Test { + void test() { + String[] squares = new String[10]; + squares[0] = 0 + "*" + 0 + "=" + 0 * 0; + squares[1] = 1 + "*" + 1 + "=" + 1 * 1; + squares[2] = 2 + "*" + 2 + "=" + 2 * 2; + squares[3] = 3 + "*" + 3 + "=" + 3 * 3; + squares[4] = 4 + "*" + 4 + "=" + 4 * 4; + squares[5] = 5 + "*" + 5 + "=" + 5 * 5; + squares[6] = 6 + "*" + 6 + "=" + 6 * 6; + squares[7] = 7 + "*" + 7 + "=" + 7 * 7; + squares[8] = 8 + "*" + 8 + "=" + 8 * 8; + squares[9] = 9 + "*" + 9 + "=" + 9 * 9; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeUnary.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeUnary.java new file mode 100644 index 000000000000..437451621a45 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeUnary.java @@ -0,0 +1,4 @@ +// "Replace '~2' with constant value '-3'" "true" +class Test { + int x = ~2; +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeUnaryMinus.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeUnaryMinus.java new file mode 100644 index 000000000000..6d85fc90a49d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeUnaryMinus.java @@ -0,0 +1,4 @@ +// "Fix all 'Constant expression can be evaluated' problems in file" "false" +class Test { + int x = -2; +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeVeryLongString.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeVeryLongString.java new file mode 100644 index 000000000000..10c870c5d094 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression/beforeVeryLongString.java @@ -0,0 +1,11 @@ +// "Fix all 'Constant expression can be evaluated' problems in file" "false" +class Test { + void test() { + // Do not suggest to compute for performance reasons + String foo = "The quick brown fox jumps " + 100000 + "times" + "over the lazy dog"+ + "The quick brown fox jumps " + 100000 + "times" + "over the lazy dog"+ + "The quick brown fox jumps " + 100000 + "times" + "over the lazy dog"+ + "The quick brown fox jumps " + 100000 + "times" + "over the lazy dog"+ + "The quick brown fox jumps " + 100000 + "times" + "over the lazy dog"; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 570e51173815..567a27878581 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -2261,4 +2261,9 @@ copy.constructor.misses.field.problem.descriptor.3=Copy constructor does not cop copy.constructor.misses.field.problem.descriptor.many=Copy constructor does not copy {0} fields fix.add.argument.family.name=Add argument -fix.add.argument.name=Add ''{0}'' argument \ No newline at end of file +fix.add.argument.name=Add ''{0}'' argument + +inspection.constant.expression.display.name=Constant expression can be evaluated +inspection.constant.expression.fix.name=Compute constant value of ''{0}'' +inspection.constant.expression.fix.name.with.value=Replace ''{0}'' with constant value ''{1}'' +inspection.constant.expression.fix.family.name=Compute constant value diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ConstantExpressionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ConstantExpressionInspection.java new file mode 100644 index 000000000000..cfb216173340 --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ConstantExpressionInspection.java @@ -0,0 +1,150 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.siyeh.ig.style; + +import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool; +import com.intellij.codeInspection.LocalQuickFix; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.*; +import com.intellij.psi.util.ConstantEvaluationOverflowException; +import com.intellij.psi.util.PsiExpressionTrimRenderer; +import com.intellij.psi.util.PsiUtil; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.PsiReplacementUtil; +import com.siyeh.ig.psiutils.CommentTracker; +import com.siyeh.ig.psiutils.ExpressionUtils; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +public class ConstantExpressionInspection extends AbstractBaseJavaLocalInspectionTool { + @NotNull + @Override + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { + return new JavaElementVisitor() { + @Override + public void visitUnaryExpression(PsiUnaryExpression expression) { + handle(expression); + } + + @Override + public void visitPolyadicExpression(PsiPolyadicExpression expression) { + handle(expression); + } + + void handle(PsiExpression expression) { + // intention disabled for long expressions because of performance issues on + // relatively common large string expressions. + if (expression.getTextLength() > 200) return; + if (expression.getType() == null) return; + if (!PsiUtil.isConstantExpression(expression)) return; + final PsiElement parent = expression.getParent(); + if (parent instanceof PsiExpression && PsiUtil.isConstantExpression((PsiExpression)parent)) return; + try { + final Object value = ExpressionUtils.computeConstantExpression(expression, true); + if (value != null) { + String valueText = getValueText(value); + if (!expression.textMatches(valueText)) { + holder.registerProblem(expression, InspectionGadgetsBundle.message("inspection.constant.expression.display.name"), + new ComputeConstantValueFix(expression, valueText)); + } + } + } + catch (ConstantEvaluationOverflowException ignore) { + } + } + }; + } + + private static class ComputeConstantValueFix implements LocalQuickFix { + private final String myText; + private String myValueText; + + public ComputeConstantValueFix(PsiExpression expression, String valueText) { + myText = PsiExpressionTrimRenderer.render(expression); + myValueText = valueText; + } + + @Nls + @NotNull + @Override + public String getName() { + if (myValueText.length() > 50) { + return InspectionGadgetsBundle.message("inspection.constant.expression.fix.name", myText); + } + return InspectionGadgetsBundle.message("inspection.constant.expression.fix.name.with.value", myText, myValueText); + } + + @Nls + @NotNull + @Override + public String getFamilyName() { + return InspectionGadgetsBundle.message("inspection.constant.expression.fix.family.name"); + } + + @Override + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { + final PsiExpression expression = (PsiExpression)descriptor.getStartElement(); + final Object value = ExpressionUtils.computeConstantExpression(expression); + @NonNls final String newExpression = getValueText(value); + PsiReplacementUtil.replaceExpression(expression, newExpression, new CommentTracker()); + } + } + + private static String getValueText(Object value) { + @NonNls final String newExpression; + if (value instanceof String) { + final String string = (String)value; + newExpression = '"' + StringUtil.escapeStringCharacters(string) + '"'; + } + else if (value instanceof Character) { + newExpression = '\'' + StringUtil.escapeStringCharacters(value.toString()) + '\''; + } + else if (value instanceof Long) { + newExpression = value.toString() + 'L'; + } + else if (value instanceof Double) { + final double v = ((Double)value).doubleValue(); + if (Double.isNaN(v)) { + newExpression = "java.lang.Double.NaN"; + } + else if (Double.isInfinite(v)) { + if (v > 0.0) { + newExpression = "java.lang.Double.POSITIVE_INFINITY"; + } + else { + newExpression = "java.lang.Double.NEGATIVE_INFINITY"; + } + } + else { + newExpression = Double.toString(v); + } + } + else if (value instanceof Float) { + final float v = ((Float)value).floatValue(); + if (Float.isNaN(v)) { + newExpression = "java.lang.Float.NaN"; + } + else if (Float.isInfinite(v)) { + if (v > 0.0F) { + newExpression = "java.lang.Float.POSITIVE_INFINITY"; + } + else { + newExpression = "java.lang.Float.NEGATIVE_INFINITY"; + } + } + else { + newExpression = Float.toString(v) + 'f'; + } + } + else if (value == null) { + newExpression = "null"; + } + else { + newExpression = String.valueOf(value); + } + return newExpression; + } +} diff --git a/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml index 11aac0be2a23..7b41f660b2d0 100644 --- a/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml @@ -2788,6 +2788,10 @@ bundle="com.siyeh.InspectionGadgetsBundle" key="variable.type.can.be.explicit.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.language.level.specific.issues.and.migration.aids10" implementationClass="com.intellij.codeInspection.VariableTypeCanBeExplicitInspection"/> + diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/ConstantExpressionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/ConstantExpressionInspectionTest.java new file mode 100644 index 000000000000..e3bc4bbc334b --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/ConstantExpressionInspectionTest.java @@ -0,0 +1,23 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.siyeh.ig.style; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; +import com.intellij.codeInspection.LocalInspectionTool; +import org.jetbrains.annotations.NotNull; + +public class ConstantExpressionInspectionTest extends LightQuickFixParameterizedTestCase { + @NotNull + @Override + protected LocalInspectionTool[] configureLocalInspectionTools() { + return new LocalInspectionTool[]{ + new ConstantExpressionInspection() + }; + } + + public void test() { doAllTests(); } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/constantExpression"; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml index 72c4eb430381..e1eb9ee3389c 100644 --- a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml +++ b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml @@ -435,11 +435,6 @@ com.siyeh.IntentionPowerPackBundle intention.category.other - - com.siyeh.ipp.constant.ConstantExpressionIntention - com.siyeh.IntentionPowerPackBundle - intention.category.other - com.siyeh.ipp.constant.ConstantSubexpressionIntention com.siyeh.IntentionPowerPackBundle diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index cf9733be8c21..526279faf637 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -169,7 +169,6 @@ negate.comparison.intention.name=Negate ''{0}'' negate.comparison.intention.name1=Negate ''{0}'' to ''{1}'' flip.commutative.method.call.intention.name=Flip ''.{0}()'' flip.commutative.method.call.intention.name1=Flip ''.{0}()'' (may change semantics) -constant.expression.intention.name=Compute constant value of ''{0}'' status.bar.escape.highlighting.message=Press Escape to remove the highlighting 1.fully.qualified.name.status.bar.escape.highlighting.message=1 fully qualified name replaced with import (press Escape to remove highlighting) multiple.fully.qualified.names.status.bar.escape.highlighting.message={0} fully qualified names replaced with import (press Escape to remove highlighting) diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/constant/ConstantExpressionIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/constant/ConstantExpressionIntention.java deleted file mode 100644 index e05f67b1d8e5..000000000000 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/constant/ConstantExpressionIntention.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2003-2018 Dave Griffith, Bas Leijdekkers - * - * 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.siyeh.ipp.constant; - -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiExpression; -import com.siyeh.IntentionPowerPackBundle; -import com.siyeh.ig.PsiReplacementUtil; -import com.siyeh.ig.psiutils.CommentTracker; -import com.siyeh.ig.psiutils.ExpressionUtils; -import com.siyeh.ipp.base.MutablyNamedIntention; -import com.siyeh.ipp.base.PsiElementPredicate; -import com.siyeh.ipp.psiutils.HighlightUtil; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; - -public class ConstantExpressionIntention extends MutablyNamedIntention { - - @Override - protected String getTextForElement(PsiElement element) { - final String text = HighlightUtil.getPresentableText(element); - return IntentionPowerPackBundle.message("constant.expression.intention.name", text); - } - - @Override - @NotNull - protected PsiElementPredicate getElementPredicate() { - return new ConstantExpressionPredicate(); - } - - @Override - public void processIntention(PsiElement element) { - final PsiExpression expression = (PsiExpression)element; - final Object value = ExpressionUtils.computeConstantExpression(expression); - @NonNls final String newExpression; - if (value instanceof String) { - final String string = (String)value; - newExpression = '"' + StringUtil.escapeStringCharacters(string) + '"'; - } - else if (value instanceof Character) { - newExpression = '\'' + StringUtil.escapeStringCharacters(value.toString()) + '\''; - } - else if (value instanceof Long) { - newExpression = value.toString() + 'L'; - } - else if (value instanceof Double) { - final double v = ((Double)value).doubleValue(); - if (Double.isNaN(v)) { - newExpression = "java.lang.Double.NaN"; - } - else if (Double.isInfinite(v)) { - if (v > 0.0) { - newExpression = "java.lang.Double.POSITIVE_INFINITY"; - } - else { - newExpression = "java.lang.Double.NEGATIVE_INFINITY"; - } - } - else { - newExpression = Double.toString(v); - } - } - else if (value instanceof Float) { - final float v = ((Float)value).floatValue(); - if (Float.isNaN(v)) { - newExpression = "java.lang.Float.NaN"; - } - else if (Float.isInfinite(v)) { - if (v > 0.0F) { - newExpression = "java.lang.Float.POSITIVE_INFINITY"; - } - else { - newExpression = "java.lang.Float.NEGATIVE_INFINITY"; - } - } - else { - newExpression = Float.toString(v) + 'f'; - } - } - else if (value == null) { - newExpression = "null"; - } - else { - newExpression = String.valueOf(value); - } - PsiReplacementUtil.replaceExpression(expression, newExpression, new CommentTracker()); - } -} diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/constant/ConstantExpressionPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/constant/ConstantExpressionPredicate.java deleted file mode 100644 index a70489b7c8a7..000000000000 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/constant/ConstantExpressionPredicate.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers - * - * 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.siyeh.ipp.constant; - -import com.intellij.psi.*; -import com.intellij.psi.util.ConstantEvaluationOverflowException; -import com.intellij.psi.util.PsiUtil; -import com.siyeh.ig.psiutils.ExpressionUtils; -import com.siyeh.ipp.base.PsiElementPredicate; - -import static com.intellij.psi.CommonClassNames.JAVA_LANG_STRING; - -class ConstantExpressionPredicate implements PsiElementPredicate { - - public boolean satisfiedBy(PsiElement element) { - if (!(element instanceof PsiPolyadicExpression)) { - return false; - } - if (element instanceof PsiLiteralExpression || element instanceof PsiClassObjectAccessExpression) { - return false; - } - final PsiPolyadicExpression expression = (PsiPolyadicExpression)element; - final PsiType expressionType = expression.getType(); - if (expressionType == null || expressionType.equalsToText(JAVA_LANG_STRING)) { - // intention disabled for string concatenations because of performance issues on - // relatively common large string expressions. - return false; - } - final PsiExpression[] operands = expression.getOperands(); - for (PsiExpression operand : operands) { - if (operand == null) { - return false; - } - final PsiType type = operand.getType(); - if (type == null || type.equalsToText(JAVA_LANG_STRING)) { - return false; - } - } - if (!PsiUtil.isConstantExpression(expression)) { - return false; - } - try { - final Object value = ExpressionUtils.computeConstantExpression(expression, true); - if (value == null) { - return false; - } - } - catch (ConstantEvaluationOverflowException ignore) { - return false; - } - final PsiElement parent = element.getParent(); - return !(parent instanceof PsiExpression) || !PsiUtil.isConstantExpression((PsiExpression)parent); - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/after.java.template deleted file mode 100644 index 700c60d07c6f..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/after.java.template +++ /dev/null @@ -1,5 +0,0 @@ -public class X { - void f() { - int i = 3600; - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/before.java.template deleted file mode 100644 index cbf5f7883bd1..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/before.java.template +++ /dev/null @@ -1,5 +0,0 @@ -public class X { - void f() { - int i = 60 * 60; - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/description.html deleted file mode 100644 index a5b1eddae228..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ConstantExpressionIntention/description.html +++ /dev/null @@ -1,6 +0,0 @@ - - -This intention calculates the value of a compile -time constant expression, and replaces it with an equivalent literal expression. - -