From a1d710d5ee183ad74cf57947a01bfa66175a2ae3 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 6 Aug 2021 14:36:08 +0200 Subject: [PATCH] [java] disable char -> string conversion on erroneous code compilation fix is provided, let's skip second one GitOrigin-RevId: 0717eac74cca13e80e5c2fd8313df75c11fab576 --- .../ConvertToStringLiteralAction.java | 26 +++++++---------- .../convertToStringLiteral/NotAvailable.java | 20 +++++++++++++ .../convertToStringLiteral/Simple.java | 2 +- .../convertToStringLiteral/Simple_after.java | 2 +- .../intention/ConvertToStringLiteralTest.java | 29 +++++++++---------- 5 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/convertToStringLiteral/NotAvailable.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertToStringLiteralAction.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertToStringLiteralAction.java index 294a85375762..ef26c83cef8f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertToStringLiteralAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertToStringLiteralAction.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2011 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. - */ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl.quickfix; import com.intellij.codeInsight.daemon.QuickFixBundle; @@ -21,6 +7,7 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; +import com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl; import com.intellij.psi.util.PsiUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; @@ -41,7 +28,14 @@ public class ConvertToStringLiteralAction implements IntentionActionWithFixAllOp @Override public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) { final PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); - return PsiUtil.isJavaToken(element, JavaTokenType.CHARACTER_LITERAL); + if (PsiUtil.isJavaToken(element, JavaTokenType.CHARACTER_LITERAL)) { + StringBuilder chars = new StringBuilder(); + String text = element.getText(); + return text.length() > 1 && + PsiLiteralExpressionImpl.parseStringCharacters(text.substring(1, text.length() - 1), chars, null) && + chars.length() == 1; + } + return false; } @Override diff --git a/java/java-tests/testData/codeInsight/convertToStringLiteral/NotAvailable.java b/java/java-tests/testData/codeInsight/convertToStringLiteral/NotAvailable.java new file mode 100644 index 000000000000..0e9909682eef --- /dev/null +++ b/java/java-tests/testData/codeInsight/convertToStringLiteral/NotAvailable.java @@ -0,0 +1,20 @@ +/* + * Copyright 2000-2011 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. + */ +class C { + { + System.out.println('ab'); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/convertToStringLiteral/Simple.java b/java/java-tests/testData/codeInsight/convertToStringLiteral/Simple.java index 285c7ab4993d..f5a8d80214e2 100644 --- a/java/java-tests/testData/codeInsight/convertToStringLiteral/Simple.java +++ b/java/java-tests/testData/codeInsight/convertToStringLiteral/Simple.java @@ -15,6 +15,6 @@ */ class C { { - System.out.println('aaa'); + System.out.println('a'); } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/convertToStringLiteral/Simple_after.java b/java/java-tests/testData/codeInsight/convertToStringLiteral/Simple_after.java index d28758e50af4..81cab01d1957 100644 --- a/java/java-tests/testData/codeInsight/convertToStringLiteral/Simple_after.java +++ b/java/java-tests/testData/codeInsight/convertToStringLiteral/Simple_after.java @@ -15,6 +15,6 @@ */ class C { { - System.out.println("aaa"); + System.out.println("a"); } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/ConvertToStringLiteralTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/ConvertToStringLiteralTest.java index ce3aca9cf83d..8aa9a0d73edc 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/ConvertToStringLiteralTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/ConvertToStringLiteralTest.java @@ -1,25 +1,15 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.java.codeInsight.intention; import com.intellij.JavaTestUtil; import com.intellij.codeInsight.daemon.QuickFixBundle; +import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.testFramework.fixtures.CodeInsightTestUtil; import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase; +import org.junit.Assert; + +import java.util.List; public class ConvertToStringLiteralTest extends JavaCodeInsightFixtureTestCase { @@ -35,6 +25,15 @@ public class ConvertToStringLiteralTest extends JavaCodeInsightFixtureTestCase { CodeInsightTestUtil.doIntentionTest(myFixture, myIntention, "Simple.java", "Simple_after.java"); } + public void testNotAvailable() { + myFixture.configureByFile(getTestName(false) + ".java"); + List availableIntentions = myFixture.getAvailableIntentions(); + final IntentionAction intentionAction = CodeInsightTestUtil.findIntentionByText(availableIntentions, myIntention); + if (intentionAction != null) { + Assert.fail("Action found: " + myIntention); + } + } + @Override protected String getTestDataPath() { return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/convertToStringLiteral/";