replace diamonds with explicit type params (IDEA-73932 )

This commit is contained in:
anna
2011-09-06 12:38:46 +02:00
parent 8b10212d58
commit 36b2da5c5e
10 changed files with 102 additions and 3 deletions
+2
View File
@@ -26,6 +26,8 @@
<orderEntry type="module" module-name="ui-designer" scope="TEST" />
<orderEntry type="module" module-name="eclipse" scope="TEST" />
<orderEntry type="module" module-name="execution-openapi" />
<orderEntry type="module" module-name="IntentionPowerPackPlugin" />
<orderEntry type="module" module-name="InspectionGadgetsPlugin" />
</component>
</module>
@@ -114,6 +114,10 @@ public class PsiDiamondType extends PsiType {
return DiamondInferenceResult.EXPLICIT_CONSTRUCTOR_TYPE_ARGS;
}
return resolveInferredTypesNoCheck(newExpression, context);
}
public static DiamondInferenceResult resolveInferredTypesNoCheck(PsiNewExpression newExpression, PsiElement context) {
final PsiClass psiClass = findClass(newExpression);
if (psiClass == null) return DiamondInferenceResult.NULL_RESULT;
final PsiExpressionList argumentList = newExpression.getArgumentList();
@@ -129,7 +133,8 @@ public class PsiDiamondType extends PsiType {
final PsiTypeParameter[] classParameters = psiClass.getTypeParameters();
final PsiJavaCodeReferenceElement classOrAnonymousClassReference = newExpression.getClassOrAnonymousClassReference();
LOG.assertTrue(classOrAnonymousClassReference != null);
final DiamondInferenceResult result = new DiamondInferenceResult(classOrAnonymousClassReference.getReferenceName() + "<>", newExpression.getProject());
final DiamondInferenceResult
result = new DiamondInferenceResult(classOrAnonymousClassReference.getReferenceName() + "<>", newExpression.getProject());
for (PsiTypeParameter parameter : parameters) {
for (PsiTypeParameter classParameter : classParameters) {
if (Comparing.strEqual(classParameter.getName(), parameter.getName())) {
@@ -16,6 +16,7 @@
package com.siyeh.ipp.types;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
@@ -45,9 +46,11 @@ public class ReplaceDiamondWithExplicitTypeArgumentsIntention extends Intention
final StringBuilder text = new StringBuilder();
text.append(javaCodeReferenceElement.getQualifiedName());
text.append('<');
final PsiType[] typeArguments = referenceParameterList.getTypeArguments();
final PsiTypeElement[] typeElements = referenceParameterList.getTypeParameterElements();
final PsiNewExpression newExpression = PsiTreeUtil.getParentOfType(typeElements[0], PsiNewExpression.class);
final PsiDiamondType.DiamondInferenceResult result = PsiDiamondType.resolveInferredTypesNoCheck(newExpression, newExpression);
boolean first = true;
for (PsiType typeArgument : typeArguments) {
for (PsiType typeArgument : result.getInferredTypes()) {
if (first) {
first = false;
} else {
@@ -0,0 +1,7 @@
public interface Anonymous<T> {}
class Test {
public static void main(String[] args) {
Anonymous<Integer> anon = new Anonymous<<caret>>() {};
}
}
@@ -0,0 +1,7 @@
public interface Anonymous<T> {}
class Test {
public static void main(String[] args) {
Anonymous<Integer> anon = new Anonymous<Integer>() {};
}
}
@@ -0,0 +1,7 @@
public class MyClass<T> {}
class Test {
public static void main(String[] args) {
MyClass<Integer> anon = new MyClass<<caret>>();
}
}
@@ -0,0 +1,7 @@
public class MyClass<T> {}
class Test {
public static void main(String[] args) {
MyClass<Integer> anon = new MyClass<Integer>();
}
}
@@ -0,0 +1,9 @@
class Foo<T> {
<U> Foo(T t) {}
}
class Test {
public static void main(String[] args) {
Foo<String> c = new <Integer>Foo<<caret>>("");
}
}
@@ -0,0 +1,9 @@
class Foo<T> {
<U> Foo(T t) {}
}
class Test {
public static void main(String[] args) {
Foo<String> c = new <Integer>Foo<String>("");
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2011 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.types;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.IPPTestCase;
public class ReplaceDiamondWithExplicitTypeIntentionTest extends IPPTestCase {
public void testAnonymousClass() {
doTest();
}
public void testApplicableDiamond() {
doTest();
}
public void testExplicitTypeArgs() {
doTest();
}
@Override
protected String getIntentionName() {
return IntentionPowerPackBundle.message("replace.diamond.with.explicit.type.arguments.intention.name");
}
@Override
protected String getRelativePath() {
return "types/diamond2explicit";
}
}