Merge remote-tracking branch 'origin/master'

This commit is contained in:
Roman Shevchenko
2016-10-17 19:33:20 +02:00
10 changed files with 174 additions and 11 deletions
@@ -23,6 +23,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Conditions;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil;
@@ -102,7 +103,7 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
final PsiExpression candidate =
canBeMethodReferenceProblem(body, expression.getParameterList().getParameters(), functionalInterfaceType, null);
if (candidate != null) {
holder.registerProblem(candidate,
holder.registerProblem(InspectionProjectProfileManager.isInformationLevel(getShortName(), expression) ? expression : candidate,
"Can be replaced with method reference",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithMethodRefFix());
}
@@ -586,8 +587,11 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
PsiElement element = descriptor.getPsiElement();
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
if (element instanceof PsiLambdaExpression) {
element = LambdaUtil.extractSingleExpressionFromBody(((PsiLambdaExpression)element).getBody());
}
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
if (lambdaExpression == null) return;
tryConvertToMethodReference(lambdaExpression, element);
@@ -345,6 +345,17 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
}
}
@Override
public void visitParameter(PsiParameter parameter) {
super.visitParameter(parameter);
final PsiElement declarationScope = parameter.getDeclarationScope();
for (PsiVariable variable : outputVariables) {
if (Comparing.strEqual(variable.getName(), parameter.getName())) {
replacementMap.put(parameter, myElementFactory.createStatementFromText(myInnerClassName + ".this." + var2FieldNames.get(variable.getName()) + " = " + parameter.getName() + ";", declarationScope));
}
}
}
@Override
public void visitReferenceExpression(final PsiReferenceExpression expression) {
super.visitReferenceExpression(expression);
@@ -368,16 +379,37 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
}
}
Map<PsiStatement, PsiForeachStatement> blocksToReplace = new LinkedHashMap<>();
for (PsiElement statement : replacementMap.keySet()) {
final PsiElement replacement = replacementMap.get(statement);
if (replacement != null) {
if (statement instanceof PsiLocalVariable) {
if (statement instanceof PsiParameter) {
PsiCodeBlock codeBlock = null;
final PsiElement declarationScope = ((PsiParameter)statement).getDeclarationScope();
if (declarationScope instanceof PsiForeachStatement) {
final PsiStatement loopBody = ((PsiForeachStatement)declarationScope).getBody();
if (loopBody instanceof PsiBlockStatement) {
codeBlock = ((PsiBlockStatement)loopBody).getCodeBlock();
}
else {
blocksToReplace.put((PsiStatement)replacement, (PsiForeachStatement)declarationScope);
}
}
else if (declarationScope instanceof PsiCatchSection){
codeBlock = ((PsiCatchSection)declarationScope).getCatchBlock();
}
if (codeBlock != null) {
codeBlock.addBefore(replacement, codeBlock.getFirstBodyElement());
}
}
else if (statement instanceof PsiLocalVariable) {
PsiLocalVariable variable = (PsiLocalVariable)statement;
variable.normalizeDeclaration();
PsiDeclarationStatement declaration = PsiTreeUtil.getParentOfType(statement, PsiDeclarationStatement.class);
LOG.assertTrue(declaration != null);
declaration.replace(replacement);
} else {
}
else {
if (statement instanceof PsiReturnStatement) {
final PsiExpression returnValue = ((PsiReturnStatement)statement).getReturnValue();
if (!(returnValue instanceof PsiReferenceExpression || returnValue == null || returnValue instanceof PsiLiteralExpression)) {
@@ -392,6 +424,10 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
}
}
for (PsiStatement statement : blocksToReplace.keySet()) {
RefactoringUtil.putStatementInLoopBody(statement, blocksToReplace.get(statement), null);
}
myChangeReturnType = true;
}
@@ -333,7 +333,7 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
/* do nothing */
}
public static List<PsiMethod> getEnclosingMethods(PsiMethod nearest) {
public static List<PsiMethod> getEnclosingMethods(@NotNull PsiMethod nearest) {
List<PsiMethod> enclosingMethods = new ArrayList<>();
enclosingMethods.add(nearest);
PsiMethod method = nearest;
@@ -531,7 +531,11 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
if (inplaceIntroducer instanceof InplaceIntroduceParameterPopup) {
return false;
}
final List<PsiMethod> enclosingMethods = getEnclosingMethods(Util.getContainingMethod(elements[0]));
final PsiMethod containingMethod = Util.getContainingMethod(elements[0]);
if (containingMethod == null) {
return false;
}
final List<PsiMethod> enclosingMethods = getEnclosingMethods(containingMethod);
if (enclosingMethods.isEmpty()) {
return false;
}
@@ -550,7 +554,9 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
? new PsiElement[]{exprInRange}
: CodeInsightUtil.findStatementsInRange(copy, range.getStartOffset(), range.getEndOffset());
}
final List<PsiMethod> enclosingMethodsInCopy = getEnclosingMethods(Util.getContainingMethod(elementsCopy[0]));
final PsiMethod containingMethodCopy = Util.getContainingMethod(elementsCopy[0]);
LOG.assertTrue(containingMethodCopy != null);
final List<PsiMethod> enclosingMethodsInCopy = getEnclosingMethods(containingMethodCopy);
final MyExtractMethodProcessor processor = new MyExtractMethodProcessor(project, editor, elementsCopy,
enclosingMethodsInCopy.get(enclosingMethodsInCopy.size() - 1));
try {
@@ -702,7 +708,7 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
private static class MyExtractMethodProcessor extends ExtractMethodProcessor {
private final PsiMethod myTopEnclosingMethod;
public MyExtractMethodProcessor(Project project, Editor editor, PsiElement[] elements, PsiMethod topEnclosing) {
public MyExtractMethodProcessor(Project project, Editor editor, PsiElement[] elements, @NotNull PsiMethod topEnclosing) {
super(project, editor, elements, null, REFACTORING_NAME, null, null);
myTopEnclosingMethod = topEnclosing;
}
@@ -142,6 +142,16 @@ public class CreateTestAction extends PsiElementBaseIntentionAction {
return module;
}
}
if (computeSuitableTestRootUrls(productionModule).isEmpty()) {
final HashSet<Module> modules = new HashSet<>();
ModuleUtilCore.collectModulesDependsOn(productionModule, modules);
modules.remove(productionModule);
for (Module module : modules) {
if (!computeSuitableTestRootUrls(module).isEmpty()) return module;
}
}
return productionModule;
}
@@ -0,0 +1,34 @@
import java.util.Arrays;
import java.util.List;
class Foo {
private List<Foo> foos = Arrays.asList(new Foo("one"), new Foo("two"));
private String name;
public Foo(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<Foo> getFoos() {
return foos;
}
public Foo getFoo(String name) {
if (name != null)
<selection>for (Foo foo : getFoos())
if (foo.getName().equals(name)) {
return foo;
}</selection>
return null;
}
}
@@ -0,0 +1,59 @@
import java.util.Arrays;
import java.util.List;
class Foo {
private List<Foo> foos = Arrays.asList(new Foo("one"), new Foo("two"));
private String name;
public Foo(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<Foo> getFoos() {
return foos;
}
public Foo getFoo(String name) {
if (name != null)
Inner inner = new Inner(name).invoke();if (inner.is()) return inner.getFoo();
return null;
}
private class Inner {
private boolean myResult;
private String name;
private Foo foo;
public Inner(String name) {
this.name = name;
}
boolean is() {
return myResult;
}
public Foo getFoo() {
return foo;
}
public Inner invoke() {
for (Foo foo : getFoos()) {
Inner.this.foo = foo;
if (foo.getName().equals(name)) {
myResult = true;
return this;
}
}
myResult = false;
return this;
}
}
}
@@ -146,4 +146,8 @@ public class ExtractMethodObjectWithMultipleExitPointsTest extends LightRefactor
public void testFormattingInside() throws Exception {
doTest();
}
public void testAssignReturnValueToForeachParameter() throws Exception {
doTest();
}
}
@@ -15,6 +15,8 @@
*/
package com.intellij.profile.codeInspection;
import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.InspectionProfile;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
@@ -40,4 +42,13 @@ public abstract class InspectionProjectProfileManager implements InspectionProfi
public InspectionProfile getInspectionProfile(PsiElement element){
return getCurrentProfile();
}
public static boolean isInformationLevel(String shortName, @NotNull PsiElement element) {
final HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
if (key != null) {
final HighlightDisplayLevel errorLevel = getInstance(element.getProject()).getCurrentProfile().getErrorLevel(key, element);
return HighlightDisplayLevel.DO_NOT_SHOW.equals(errorLevel);
}
return false;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* 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.
@@ -89,7 +89,6 @@ public class NavBarPopup extends LightweightHint implements Disposable{
@Override
protected void onPopupCancel() {
myPanel.resetSelection(); // select last item if popup cancelled
final JComponent component = getComponent();
if (component != null) {
Object o = component.getClientProperty(JBLIST_KEY);
@@ -146,7 +146,7 @@ public abstract class ControlFlowStatementVisitorBase extends BaseInspectionVisi
}
if (myKey != null) {
final Project project = element.getProject();
final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getCurrentProfile();
final HighlightDisplayLevel errorLevel = profile.getErrorLevel(myKey, element);
return !HighlightDisplayLevel.DO_NOT_SHOW.equals(errorLevel);
}