IDEA-90103 (Provide intention for changing base type in LHS to derived type when RHS type is more specific)

This commit is contained in:
Bas Leijdekkers
2012-08-14 11:30:12 +02:00
parent 90360aa0f3
commit c56a82b56a
11 changed files with 194 additions and 0 deletions
@@ -211,6 +211,11 @@
<categoryKey>intention.category.declaration</categoryKey>
</intentionAction>
<intentionAction>
<className>com.siyeh.ipp.decls.ChangeVariableTypeToRhsTypeIntention</className>
<categoryKey>intention.category.declaration</categoryKey>
</intentionAction>
<!-- Imports -->
<intentionAction>
@@ -169,6 +169,7 @@ make.protected.intention.name=Make 'protected'
make.protected.intention.family.name=Make Protected
make.private.intention.name=Make 'private'
make.private.intention.family.name=Make Private
change.variable.type.to.rhs.type.intention.family.name=Change Variable Type to Type of Initializer
#hand made
demorgans.intention.name1=Replace '\\&\\&' with '||'
@@ -202,6 +203,7 @@ flip.setter.call.intention.name=Flip Setter Call
adapter.to.listener.intention.name=Replace extension of ''{0}'' with ''Listener'' implementation
obscure.thrown.exceptions.intention.name=Replace with ''throws {0}''
add.array.creation.expression.intention.name=Add ''new {0}''
change.variable.type.to.rhs.type.intention.name=Declare ''{0}'' with type ''{1}''
#categories
@@ -0,0 +1,75 @@
/*
* 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.
*/
/*
* (c) 2012 Desert Island BV
* created: 14 08 2012
*/
package com.siyeh.ipp.decls;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class ChangeVariableTypeToRhsTypeIntention extends MutablyNamedIntention {
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new ChangeVariableTypeToRhsTypePredicate();
}
@Override
protected String getTextForElement(PsiElement element) {
final PsiVariable variable = (PsiVariable)element.getParent();
final PsiExpression initializer = variable.getInitializer();
assert initializer != null;
final PsiType type = initializer.getType();
assert type != null;
return IntentionPowerPackBundle.message("change.variable.type.to.rhs.type.intention.name",
variable.getName(), type.getPresentableText());
}
@Override
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
final PsiElement parent = element.getParent();
if (!(parent instanceof PsiVariable)) {
return;
}
final PsiVariable variable = (PsiVariable)parent;
final PsiExpression initializer = variable.getInitializer();
if (initializer == null) {
return;
}
final PsiType type = initializer.getType();
if (type == null) {
return;
}
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(element.getProject());
final PsiTypeElement typeElement = factory.createTypeElement(type);
final PsiTypeElement variableTypeElement = variable.getTypeElement();
if (variableTypeElement == null) {
return;
}
variableTypeElement.replace(typeElement);
}
}
@@ -0,0 +1,60 @@
/*
* 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.siyeh.ipp.decls;
import com.intellij.psi.*;
import com.siyeh.ipp.base.PsiElementPredicate;
/**
* @author Bas Leijdekkers
*/
class ChangeVariableTypeToRhsTypePredicate implements PsiElementPredicate {
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof PsiTypeElement)) {
return false;
}
final PsiElement parent = element.getParent();
if (!(parent instanceof PsiVariable)) {
return false;
}
final PsiVariable variable = (PsiVariable)parent;
final PsiExpression initializer = variable.getInitializer();
if (!(initializer instanceof PsiNewExpression)) {
return false;
}
final PsiType type = variable.getType();
if (!(type instanceof PsiClassType)) {
return false;
}
final PsiType initializerType = initializer.getType();
if (!(initializerType instanceof PsiClassType)) {
return false;
}
final PsiClassType initializerClassType = (PsiClassType)initializerType;
final PsiClass initializerClass = initializerClassType.resolve();
if (initializerClass == null) {
return false;
}
final PsiClassType classType = (PsiClassType)type;
final PsiClass variableClass = classType.resolve();
if (variableClass == null) {
return false;
}
return initializerClass.isInheritor(variableClass, true);
}
}
@@ -0,0 +1,3 @@
public class X {
private ArrayList<String> ss = new ArrayList<>();
}
@@ -0,0 +1,3 @@
public class X {
private <spot>List<String></spot> ss = new ArrayList<>();
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention changes the type of a variable to the type of its initializer if that is more specific.
</body>
</html>
@@ -0,0 +1,5 @@
import java.util.ArrayList;
class X {
private <caret>ArrayList<String> ss = new ArrayList<String>();
}
@@ -0,0 +1,6 @@
import java.util.ArrayList;
import java.util.List;
class X {
private <caret>List<String> ss = new ArrayList<>();
}
@@ -0,0 +1,6 @@
import java.util.ArrayList;
import java.util.List;
class X {
private ArrayList<String> ss = new ArrayList<>();
}
@@ -0,0 +1,24 @@
package com.siyeh.ipp.decls;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.IPPTestCase;
/**
* @author Bas Leijdekkers
*/
public class ChangeVariableTypeToRhsTypeIntentionTest extends IPPTestCase {
public void testSimple() { doTest("Declare 'ss' with type 'ArrayList<String>'"); }
public void testSameType() { assertIntentionNotAvailable(); }
@Override
protected String getIntentionName() {
return IntentionPowerPackBundle.message("change.variable.type.to.rhs.type.intention.name", "ss", "ArrayList<String>");
}
@Override
protected String getRelativePath() {
return "decls/change_variable_type_to_rhs_type";
}
}