Merge remote-tracking branch 'origin/master'

This commit is contained in:
Ilya.Kazakevich
2015-04-09 00:32:35 +03:00
12 changed files with 170 additions and 25 deletions
@@ -26,6 +26,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
@@ -39,7 +40,7 @@ import org.jetbrains.annotations.Nullable;
import static com.intellij.util.ObjectUtils.assertNotNull;
public class AddTypeCastFix extends LocalQuickFixAndIntentionActionOnPsiElement {
public class AddTypeCastFix extends LocalQuickFixAndIntentionActionOnPsiElement implements HighPriorityAction {
private final PsiType myType;
public AddTypeCastFix(@NotNull PsiType type, @NotNull PsiExpression expression) {
@@ -25,11 +25,12 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public class CastMethodArgumentFix extends MethodArgumentFix {
public class CastMethodArgumentFix extends MethodArgumentFix implements HighPriorityAction {
private CastMethodArgumentFix(PsiExpressionList list, int i, PsiType toType, final ArgumentFixerActionFactory factory) {
super(list, i, toType, factory);
}
@@ -53,6 +53,7 @@ import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -190,25 +191,31 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
private LocalQuickFix[] createNPEFixes(PsiExpression qualifier, PsiExpression expression, boolean onTheFly) {
if (qualifier == null || expression == null) return null;
if (qualifier instanceof PsiMethodCallExpression) return null;
if (qualifier instanceof PsiLiteralExpression && ((PsiLiteralExpression)qualifier).getValue() == null) return null;
try {
final List<LocalQuickFix> fixes = new SmartList<LocalQuickFix>();
if (PsiUtil.getLanguageLevel(qualifier).isAtLeast(LanguageLevel.JDK_1_4)) {
final Project project = qualifier.getProject();
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
final PsiBinaryExpression binary = (PsiBinaryExpression)elementFactory.createExpressionFromText("a != null", null);
binary.getLOperand().replace(qualifier);
ContainerUtil.addIfNotNull(fixes, createAssertFix(binary, expression));
if (!(qualifier instanceof PsiLiteralExpression && ((PsiLiteralExpression)qualifier).getValue() == null)) {
if (PsiUtil.getLanguageLevel(qualifier).isAtLeast(LanguageLevel.JDK_1_4)) {
final Project project = qualifier.getProject();
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
final PsiBinaryExpression binary = (PsiBinaryExpression)elementFactory.createExpressionFromText("a != null", null);
binary.getLOperand().replace(qualifier);
ContainerUtil.addIfNotNull(fixes, createAssertFix(binary, expression));
}
addSurroundWithIfFix(qualifier, fixes, onTheFly);
if (ReplaceWithTernaryOperatorFix.isAvailable(qualifier, expression)) {
fixes.add(new ReplaceWithTernaryOperatorFix(qualifier));
}
}
addSurroundWithIfFix(qualifier, fixes, onTheFly);
if (ReplaceWithTernaryOperatorFix.isAvailable(qualifier, expression)) {
fixes.add(new ReplaceWithTernaryOperatorFix(qualifier));
if (PsiUtil.getLanguageLevel(qualifier).isAtLeast(LanguageLevel.JDK_1_8)) {
ContainerUtil.addIfNotNull(fixes, ReplaceOptionalOfWithOfNullableFix.registerReplaceOptionalOfWithOfNullableFix(qualifier));
}
return fixes.toArray(new LocalQuickFix[fixes.size()]);
return fixes.isEmpty() ? null : fixes.toArray(new LocalQuickFix[fixes.size()]);
}
catch (IncorrectOperationException e) {
LOG.error(e);
@@ -760,4 +767,56 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
boolean normalOk;
}
}
private static class ReplaceOptionalOfWithOfNullableFix implements LocalQuickFix {
private static LocalQuickFix registerReplaceOptionalOfWithOfNullableFix(PsiExpression qualifier) {
final PsiElement argList = PsiUtil.skipParenthesizedExprUp(qualifier).getParent();
if (argList instanceof PsiExpressionList) {
final PsiElement parent = argList.getParent();
if (parent instanceof PsiMethodCallExpression) {
final PsiMethod method = ((PsiMethodCallExpression)parent).resolveMethod();
if (method != null) {
final PsiClass containingClass = method.getContainingClass();
if ("of".equals(method.getName()) &&
containingClass != null &&
"java.util.Optional".equals(containingClass.getQualifiedName())) {
return new ReplaceOptionalOfWithOfNullableFix();
}
}
}
}
return null;
}
@Nls
@NotNull
@Override
public String getName() {
return getFamilyName();
}
@NotNull
@Override
public String getFamilyName() {
return "Replace with '.ofNullable()'";
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiMethodCallExpression
methodCallExpression = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethodCallExpression.class);
if (methodCallExpression != null) {
final PsiElement ofNullableExprName =
((PsiMethodCallExpression)JavaPsiFacade.getElementFactory(project)
.createExpressionFromText("Optional.ofNullable(null)", null)).getMethodExpression();
final PsiElement referenceNameElement = methodCallExpression.getMethodExpression().getReferenceNameElement();
if (referenceNameElement != null) {
final PsiElement ofNullableNameElement = ((PsiReferenceExpression)ofNullableExprName).getReferenceNameElement();
LOG.assertTrue(ofNullableNameElement != null);
referenceNameElement.replace(ofNullableNameElement);
}
}
}
}
}
@@ -425,10 +425,14 @@ public class RedundantCastUtil {
PsiTypeCastExpression castExpression = computeCastExpression.fun(newReturnExpression);
PsiExpression castOperand = castExpression.getOperand();
if (castOperand == null) return;
castExpression.replace(castOperand);
castOperand = (PsiExpression)castExpression.replace(castOperand);
final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
if (interfaceType.equals(functionalInterfaceType)) {
addToResults(returnExpression);
final PsiType interfaceReturnType = LambdaUtil.getFunctionalInterfaceReturnType(interfaceType);
final PsiType castExprType = castOperand.getType();
if (interfaceReturnType != null && castExprType != null && interfaceReturnType.isAssignableFrom(castExprType)) {
addToResults(returnExpression);
}
}
}
@@ -0,0 +1,7 @@
import java.util.stream.Stream;
class Test {
public void filterByBoolean(Stream<Object[]> stream) {
stream.filter(it -> (Boolean) it[0]);
}
}
@@ -0,0 +1,6 @@
// "Replace with '.ofNullable()'" "true"
class A{
void test(){
java.util.Optional.ofNullable(null);
}
}
@@ -0,0 +1,6 @@
// "Replace with '.ofNullable()'" "false"
class A{
void test(){
java.util.Optional.of(1<caret>1);
}
}
@@ -0,0 +1,6 @@
// "Replace with '.ofNullable()'" "true"
class A{
void test(){
java.util.Optional.of(nu<caret>ll);
}
}
@@ -0,0 +1,6 @@
// "Replace with '.ofNullable()'" "false"
class A{
void test(){
Optional.of(nu<caret>ll);
}
}
@@ -38,6 +38,8 @@ public class LambdaRedundantCastTest extends LightDaemonAnalyzerTestCase {
public void testSer() { doTest(); }
public void testLambdaReturnExpressions() { doTest(); }
public void testLambdaReturnExpressions1() { doTest(); }
public void testPreventBadReturnTypeInReturnExpression() { doTest(); }
private void doTest() {
doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
}
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2015 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.
*/
/*
* User: anna
* Date: 21-Mar-2008
*/
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.testFramework.IdeaTestUtil;
import org.jetbrains.annotations.NotNull;
public class ReplaceWithOfNullableFixTest extends LightQuickFixParameterizedTestCase {
@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new DataFlowInspection()};
}
public void test() throws Exception {
doAllTests();
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithOfNullable";
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk18();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2015 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.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.ide.impl;
import com.intellij.ide.SelectInContext;
@@ -33,15 +32,14 @@ public class ProjectPaneSelectInTarget extends ProjectViewSelectInTarget impleme
super(project);
}
@Override
public String toString() {
return SelectInManager.PROJECT;
}
@Override
public boolean canSelect(PsiFileSystemItem file) {
if (!super.canSelect(file)) return false;
final VirtualFile vFile = file.getVirtualFile();
return canSelect(vFile);
return super.canSelect(file) && canSelect(file.getVirtualFile());
}
@Override
@@ -49,14 +47,14 @@ public class ProjectPaneSelectInTarget extends ProjectViewSelectInTarget impleme
return canSelect(context);
}
private boolean canSelect(final VirtualFile vFile) {
private boolean canSelect(VirtualFile vFile) {
if (vFile != null && vFile.isValid()) {
ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
if (projectFileIndex.getModuleForFile(vFile) != null) {
ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
if (index.getContentRootForFile(vFile, false) != null) {
return true;
}
if (projectFileIndex.isInLibraryClasses(vFile) || projectFileIndex.isInLibrarySource(vFile)) {
if (index.isInLibraryClasses(vFile) || index.isInLibrarySource(vFile)) {
return true;
}