diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SplitDeclarationAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SplitDeclarationAction.java index cda54e2f1372..5f67007c500d 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SplitDeclarationAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SplitDeclarationAction.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2013 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-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.intellij.codeInsight.intention.impl; import com.intellij.codeInsight.CodeInsightBundle; @@ -28,9 +14,9 @@ import com.intellij.psi.impl.source.tree.Factory; import com.intellij.psi.impl.source.tree.TreeElement; import com.intellij.psi.javadoc.PsiDocComment; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.PsiTypesUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.refactoring.util.RefactoringUtil; -import com.intellij.util.IncorrectOperationException; import com.siyeh.ig.psiutils.CommentTracker; import org.jetbrains.annotations.NotNull; @@ -84,7 +70,7 @@ public class SplitDeclarationAction extends PsiElementBaseIntentionAction { if (declaredElements.length == 1) { PsiLocalVariable var = (PsiLocalVariable)declaredElements[0]; if (var.getInitializer() == null) return false; - if (var.getTypeElement().isInferredType()) { + if (var.getTypeElement().isInferredType() && !PsiTypesUtil.isDenotableType(var.getType(), var)) { return false; } PsiElement parent = decl.getParent(); @@ -128,7 +114,7 @@ public class SplitDeclarationAction extends PsiElementBaseIntentionAction { } @Override - public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { + public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) { final PsiDeclarationStatement decl = PsiTreeUtil.getParentOfType(element, PsiDeclarationStatement.class); final PsiManager psiManager = PsiManager.getInstance(project); @@ -144,23 +130,32 @@ public class SplitDeclarationAction extends PsiElementBaseIntentionAction { } public static PsiAssignmentExpression invokeOnDeclarationStatement(PsiDeclarationStatement decl, PsiManager psiManager, - Project project) throws IncorrectOperationException { + Project project) { if (decl.getDeclaredElements().length == 1) { PsiLocalVariable var = (PsiLocalVariable)decl.getDeclaredElements()[0]; var.normalizeDeclaration(); + final PsiTypeElement typeElement = var.getTypeElement(); + if (typeElement.isInferredType()) { + PsiTypesUtil.replaceWithExplicitType(typeElement); + } + final String name = var.getName(); + assert name != null; PsiExpressionStatement statement = (PsiExpressionStatement)JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory() - .createStatementFromText(var.getName() + "=xxx;", decl); + .createStatementFromText(name + "=xxx;", decl); statement = (PsiExpressionStatement)CodeStyleManager.getInstance(project).reformat(statement); PsiAssignmentExpression assignment = (PsiAssignmentExpression)statement.getExpression(); PsiExpression initializer = var.getInitializer(); + assert initializer != null; PsiExpression rExpression = RefactoringUtil.convertInitializerToNormalExpression(initializer, var.getType()); - assignment.getRExpression().replace(rExpression); + final PsiExpression expression = assignment.getRExpression(); + assert expression != null; + expression.replace(rExpression); PsiElement block = decl.getParent(); if (block instanceof PsiForStatement) { final PsiDeclarationStatement varDeclStatement = - JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createVariableDeclarationStatement(var.getName(), var.getType(), null); + JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createVariableDeclarationStatement(name, var.getType(), null); // For index can't be final, right? for (PsiElement varDecl : varDeclStatement.getDeclaredElements()) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/splitDeclaration/afterVar.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/splitDeclaration/afterVar.java new file mode 100644 index 000000000000..7705ed13bdac --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/splitDeclaration/afterVar.java @@ -0,0 +1,7 @@ +// "Split into declaration and assignment" "true" +class Test { + { + String l; + l = "foo"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/splitDeclaration/beforeVar.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/splitDeclaration/beforeVar.java new file mode 100644 index 000000000000..eff080034959 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/splitDeclaration/beforeVar.java @@ -0,0 +1,6 @@ +// "Split into declaration and assignment" "true" +class Test { + { + var l = "foo"; + } +} \ No newline at end of file