IDEA-84307 Useless intention (change Comparator to Comparator)

This commit is contained in:
Alexey Kudravtsev
2012-04-10 13:01:54 +04:00
parent d5f38caa39
commit 03152e9fa5
5 changed files with 69 additions and 10 deletions
@@ -34,16 +34,19 @@ public class MakeTypeGenericAction extends PsiElementBaseIntentionAction {
private String variableName;
private String newTypeName;
@Override
@NotNull
public String getFamilyName() {
return CodeInsightBundle.message("intention.make.type.generic.family");
}
@Override
@NotNull
public String getText() {
return CodeInsightBundle.message("intention.make.type.generic.text", variableName, newTypeName);
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
if (!PsiUtil.isLanguageLevel5OrHigher(element)) return false;
if (!element.isWritable()) return false;
@@ -52,21 +55,20 @@ public class MakeTypeGenericAction extends PsiElementBaseIntentionAction {
private Pair<PsiVariable,PsiType> findVariable(final PsiElement element) {
PsiVariable variable = null;
PsiElement elementParent = element.getParent();
if (element instanceof PsiIdentifier) {
if (element.getParent() instanceof PsiVariable) {
variable = (PsiVariable)element.getParent();
if (elementParent instanceof PsiVariable) {
variable = (PsiVariable)elementParent;
}
}
else if (element instanceof PsiJavaToken) {
final PsiJavaToken token = (PsiJavaToken)element;
if (token.getTokenType() != JavaTokenType.EQ) return null;
if (token.getParent() instanceof PsiVariable) {
variable = (PsiVariable)token.getParent();
if (elementParent instanceof PsiVariable) {
variable = (PsiVariable)elementParent;
}
}
if (variable == null) {
return null;
}
if (variable == null) return null;
variableName = variable.getName();
final PsiExpression initializer = variable.getInitializer();
if (initializer == null) return null;
@@ -81,14 +83,16 @@ public class MakeTypeGenericAction extends PsiElementBaseIntentionAction {
final PsiClassType.ClassResolveResult variableResolveResult = variableClassType.resolveGenerics();
final PsiClassType.ClassResolveResult initializerResolveResult = initializerClassType.resolveGenerics();
if (initializerResolveResult.getElement() == null) return null;
final PsiSubstitutor targetSubstitutor = TypeConversionUtil.getClassSubstitutor(variableResolveResult.getElement(), initializerResolveResult.getElement(), initializerResolveResult.getSubstitutor());
PsiClass variableResolved = variableResolveResult.getElement();
PsiSubstitutor targetSubstitutor = TypeConversionUtil.getClassSubstitutor(variableResolved, initializerResolveResult.getElement(), initializerResolveResult.getSubstitutor());
if (targetSubstitutor == null) return null;
PsiType type =
JavaPsiFacade.getInstance(variable.getProject()).getElementFactory().createType(variableResolveResult.getElement(), targetSubstitutor);
PsiType type = JavaPsiFacade.getInstance(variable.getProject()).getElementFactory().createType(variableResolved, targetSubstitutor);
if (variableType.equals(type)) return null;
newTypeName = type.getCanonicalText();
return Pair.create(variable, type);
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final CaretModel caretModel = editor.getCaretModel();
final int position = caretModel.getOffset();
@@ -0,0 +1,10 @@
// "Change type of TT to java.util.Comparator<java.lang.String>" "true"
import java.util.*;
class CCCC {
private final static Comparator<String> <caret>TT = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return 0; //To change body of implemented methods use File | Settings | File Templates.
}
};
}
@@ -0,0 +1,10 @@
// "Change type of TT to java.util.Comparator<java.lang.String>" "true"
import java.util.*;
class CCCC {
private final static Comparator <caret>TT = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return 0; //To change body of implemented methods use File | Settings | File Templates.
}
};
}
@@ -0,0 +1,9 @@
// "Change type of TTT to java.util.Comparator" "false"
import java.util.*;
class CCCC implements Comparator {
private final static Comparator <caret>TTT = new CCCC();
@Override
public int compare(Object o1, Object o2) {
return 0;
}
}
@@ -0,0 +1,26 @@
/*
* Copyright 2000-2012 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;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
public class MakeTypeGenericTest extends LightIntentionActionTestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/makeTypeGeneric";
}
}