fix to switch intersection types order in casts when one of the last types is not an interface

This commit is contained in:
anna
2013-10-31 15:45:24 +01:00
parent 5eb0e62d58
commit 5bd8b62070
6 changed files with 171 additions and 1 deletions

View File

@@ -0,0 +1,96 @@
/*
* 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.
*/
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.diagnostic.Logger;
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.codeStyle.CodeStyleManager;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* User: anna
* Date: 10/31/13
*/
class FlipIntersectionSidesFix implements IntentionAction {
private static final Logger LOG = Logger.getInstance("#" + FlipIntersectionSidesFix.class.getName());
private final String myClassName;
private final List<PsiTypeElement> myConjuncts;
private final PsiTypeElement myConjunct;
private final PsiTypeElement myCastTypeElement;
public FlipIntersectionSidesFix(String className,
@NotNull List<PsiTypeElement> conjList,
PsiTypeElement conjunct,
PsiTypeElement castTypeElement) {
myClassName = className;
myConjuncts = conjList;
myConjunct = conjunct;
myCastTypeElement = castTypeElement;
}
@NotNull
@Override
public String getText() {
return "Move '" + myClassName + "' to the beginning";
}
@NotNull
@Override
public String getFamilyName() {
return "Move to front";
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
for (PsiTypeElement typeElement : myConjuncts) {
if (!typeElement.isValid()) return false;
}
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
myConjuncts.remove(myConjunct);
myConjuncts.add(0, myConjunct);
final String intersectionTypeText = StringUtil.join(myConjuncts, new Function<PsiTypeElement, String>() {
@Override
public String fun(PsiTypeElement element) {
return element.getText();
}
}, " & ");
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
final PsiTypeCastExpression fixedCast =
(PsiTypeCastExpression)elementFactory.createExpressionFromText("(" + intersectionTypeText + ") a", myCastTypeElement);
final PsiTypeElement fixedCastCastType = fixedCast.getCastType();
LOG.assertTrue(fixedCastCastType != null);
final PsiElement flippedTypeElement = myCastTypeElement.replace(fixedCastCastType);
CodeStyleManager.getInstance(project).reformat(flippedTypeElement);
}
@Override
public boolean startInWriteAction() {
return true;
}
}

View File

@@ -290,7 +290,7 @@ public class HighlightUtil extends HighlightUtilBase {
@Nullable
static HighlightInfo checkInconvertibleTypeCast(@NotNull PsiTypeCastExpression expression) {
PsiTypeElement castTypeElement = expression.getCastType();
final PsiTypeElement castTypeElement = expression.getCastType();
if (castTypeElement == null) return null;
PsiType castType = castTypeElement.getType();
@@ -306,6 +306,27 @@ public class HighlightUtil extends HighlightUtilBase {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
}
if (castType instanceof PsiIntersectionType && PsiUtil.isLanguageLevel8OrHigher(expression)) {
final PsiTypeElement[] conjuncts = PsiTreeUtil.getChildrenOfType(castTypeElement, PsiTypeElement.class);
if (conjuncts != null) {
final List<PsiTypeElement> conjList = new ArrayList<PsiTypeElement>(Arrays.asList(conjuncts));
for (int i = 1; i < conjuncts.length; i++) {
final PsiTypeElement conjunct = conjuncts[i];
final PsiType conjType = conjunct.getType();
if (conjType instanceof PsiClassType) {
final PsiClass aClass = ((PsiClassType)conjType).resolve();
if (aClass != null && !aClass.isInterface()) {
final HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.range(conjunct)
.descriptionAndTooltip(JavaErrorMessages.message("interface.expected")).create();
QuickFixAction.registerQuickFixAction(errorResult, new FlipIntersectionSidesFix(aClass.getName(), conjList, conjunct, castTypeElement), null);
return errorResult;
}
}
}
}
}
return null;
}

View File

@@ -0,0 +1,11 @@
import java.io.Serializable;
public class FooBar1 {
{
Object x = null;
Object y = (CharSequence & Serializable) x;
Object y2 = (CharSequence & Integer) x;
Object y3 = (Integer & CharSequence) x;
<error descr="Incompatible types. Found: 'java.lang.CharSequence & java.io.Serializable', required: 'int'">int y1 = (CharSequence & Serializable) x;</error>
}
}

View File

@@ -0,0 +1,7 @@
// "Move 'Number' to the beginning" "true"
class C {
{
Object x = null;
Object y2 = (Number & CharSequence) x;
}
}

View File

@@ -0,0 +1,7 @@
// "Move 'Number' to the beginning" "true"
class C {
{
Object x = null;
Object y2 = (CharSequence & Num<caret>ber) x;
}
}

View File

@@ -0,0 +1,28 @@
/*
* 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.
*/
package com.intellij.codeInsight.daemon.quickFix;
public class FlipIntersectionTypesTest extends LightQuickFixTestCase {
public void test() throws Exception {
doAllTests();
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/flipIntersection";
}
}