diff --git a/java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java b/java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java index c6dcf130c3f8..656bae7f13e5 100644 --- a/java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java +++ b/java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java @@ -337,18 +337,21 @@ public class ExpectedTypesProvider { type = ((PsiAnnotationMethod)parent).getReturnType(); } if (type instanceof PsiArrayType) { - myResult.add(createInfoImpl(((PsiArrayType)type).getComponentType(), type)); + final PsiType componentType = ((PsiArrayType)type).getComponentType(); + myResult.add(createInfoImpl(componentType, componentType)); } } @Override public void visitNameValuePair(@NotNull PsiNameValuePair pair) { final PsiType type = getAnnotationMethodType(pair); if (type == null) return; - myResult.add(createInfoImpl(type, type)); if (type instanceof PsiArrayType) { PsiType componentType = ((PsiArrayType)type).getComponentType(); myResult.add(createInfoImpl(componentType, componentType)); } + else { + myResult.add(createInfoImpl(type, type)); + } } @Nullable diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java index 570d4d57f563..3e667cc07dd1 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java @@ -398,16 +398,20 @@ public class ExtractMethodProcessor implements MatchProvider { } private boolean isNullInferred(String exprText, boolean trueSet) { + final PsiElement codeFragment = ControlFlowUtil.findCodeFragment(myElements[0]); + final int offsetInBlock = myElements[myElements.length - 1].getTextRange().getEndOffset() - codeFragment.getTextRange().getStartOffset(); + final PsiCodeBlock block = myElementFactory.createCodeBlockFromText("{}", myElements[0]); - for (PsiElement element : myElements) { - block.add(element); - } - final PsiIfStatement statementFromText = (PsiIfStatement)myElementFactory.createStatementFromText("if (" + exprText + " == null);", null); - block.add(statementFromText); + PsiIfStatement statementFromText = (PsiIfStatement)myElementFactory.createStatementFromText("if (" + exprText + " == null);", null); + final PsiElement copy = codeFragment.copy(); + final PsiElement lastElementInSelection = copy.getContainingFile().findElementAt(offsetInBlock + copy.getTextRange().getStartOffset()); + final PsiElement parent = lastElementInSelection.getParent().getParent(); + statementFromText = (PsiIfStatement)parent.addAfter(statementFromText, lastElementInSelection.getParent()); + block.add(copy); final StandardDataFlowRunner dfaRunner = new StandardDataFlowRunner(); final StandardInstructionVisitor visitor = new StandardInstructionVisitor(); - final RunnerResult rc = dfaRunner.analyzeMethod(block, visitor); + final RunnerResult rc = dfaRunner.analyzeMethod(copy, visitor); if (rc == RunnerResult.OK) { final Pair, Set> expressions = dfaRunner.getConstConditionalExpressions(); final Set set = trueSet ? expressions.getFirst() : expressions.getSecond(); @@ -1322,6 +1326,7 @@ public class ExtractMethodProcessor implements MatchProvider { for (VariableData data : myVariableDatum) { if (data.passAsParameter) { PsiParameter parm = myElementFactory.createParameter(data.name, data.type); + final boolean inferred = isNullInferred(data.variable.getName(), false); copyParamAnnotations(parm); if (isFinal) { PsiUtil.setModifierProperty(parm, PsiModifier.FINAL, true); diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateConstantFieldFromUsageTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateConstantFieldFromUsageTest.groovy index 39da34053657..827cef5aa5bd 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateConstantFieldFromUsageTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateConstantFieldFromUsageTest.groovy @@ -25,6 +25,40 @@ class Test { ''' assert !myFixture.lookup } + + public void "test inside annotation argument with braces"() { + TemplateManagerImpl.setTemplateTesting(project, testRootDisposable) + myFixture.configureByText "a.java", ''' +interface A {} +@SuppressWarnings({A.CONST}) +class Test {} +''' + myFixture.launchAction(myFixture.findSingleIntention("Create constant field")) + myFixture.checkResult ''' +interface A { + String CONST = ; +} +@SuppressWarnings({A.CONST}) +class Test {} +''' + } + + public void "test inside annotation argument no braces"() { + TemplateManagerImpl.setTemplateTesting(project, testRootDisposable) + myFixture.configureByText "a.java", ''' +interface A {} +@SuppressWarnings(A.CONST) +class Test {} +''' + myFixture.launchAction(myFixture.findSingleIntention("Create constant field")) + myFixture.checkResult ''' +interface A { + String CONST = ; +} +@SuppressWarnings(A.CONST) +class Test {} +''' + } public void "test insert presentable name when showing type lookup"() { TemplateManagerImpl.setTemplateTesting(project, testRootDisposable)