diamonds; actual types accepted in java 7 even if no formal types were defined!

(cherry picked from commit 903984b162008a56def605c72743c22517304a2d)
This commit is contained in:
anna
2011-05-02 21:14:20 +02:00
parent 430f611e7d
commit 3bb0c9d03a
8 changed files with 124 additions and 18 deletions
@@ -133,11 +133,16 @@ public class GenericsHighlightUtil {
if (targetParametersNum != refParametersNum && refParametersNum != 0) {
final String description;
if (targetParametersNum == 0) {
description = JavaErrorMessages.message(
"generics.type.or.method.does.not.have.type.parameters",
typeParameterListOwnerCategoryDescription(typeParameterListOwner),
typeParameterListOwnerDescription(typeParameterListOwner)
);
if (PsiTreeUtil.getParentOfType(referenceParameterList, PsiCall.class) != null &&
PsiUtil.isLanguageLevel7OrHigher(referenceParameterList)) {
description = null;
} else {
description = JavaErrorMessages.message(
"generics.type.or.method.does.not.have.type.parameters",
typeParameterListOwnerCategoryDescription(typeParameterListOwner),
typeParameterListOwnerDescription(typeParameterListOwner)
);
}
}
else {
description = JavaErrorMessages.message(
@@ -145,17 +150,19 @@ public class GenericsHighlightUtil {
);
}
final HighlightInfo highlightInfo = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, referenceParameterList, description);
if (registerIntentions) {
PsiElement pparent = referenceParameterList.getParent().getParent();
if (pparent instanceof PsiTypeElement) {
PsiElement variable = pparent.getParent();
if (variable instanceof PsiVariable) {
VariableParameterizedTypeFix.registerIntentions(highlightInfo, (PsiVariable)variable, referenceParameterList);
if (description != null) {
final HighlightInfo highlightInfo = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, referenceParameterList, description);
if (registerIntentions) {
PsiElement pparent = referenceParameterList.getParent().getParent();
if (pparent instanceof PsiTypeElement) {
PsiElement variable = pparent.getParent();
if (variable instanceof PsiVariable) {
VariableParameterizedTypeFix.registerIntentions(highlightInfo, (PsiVariable)variable, referenceParameterList);
}
}
}
return highlightInfo;
}
return highlightInfo;
}
// bounds check
@@ -226,6 +233,14 @@ public class GenericsHighlightUtil {
} else {
referenceClass = null;
}
final PsiType psiType = substitutor.substitute(classParameter);
if (psiType instanceof PsiClassType && !(PsiUtil.resolveClassInType(psiType) instanceof PsiTypeParameter)) {
if (checkNotInBounds(type, psiType)) {
final String description = "Actual type argument and inferred type contradict each other";
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, typeElement2Highlight, description);
}
}
final PsiClassType[] bounds = classParameter.getSuperTypes();
for (PsiClassType type1 : bounds) {
PsiType bound = substitutor.substitute(type1);
@@ -108,4 +108,26 @@ class FI1 {
return null;
}
};
}
}
class Super<X,Y> {
private Super(Integer i, Y y, X x) {}
public Super(Number n, X x, Y y) {}
}
class TestMySuper {
Super<String,Integer> ssi1 = new Super<>(1, "", 2);
}
class TestLocal<X> {
class Member { }
static class Nested {}
void test() {
class Local {}
Member m = new Member<<error descr="Diamond operator is not applicable for non-parameterized types"></error>>();
Nested n = new Nested<<error descr="Diamond operator is not applicable for non-parameterized types"></error>>();
Local l = new Local<<error descr="Diamond operator is not applicable for non-parameterized types"></error>>();
}
}
@@ -0,0 +1,10 @@
class Neg12 {
static class Foo<X> {
<T> Foo(T t) {}
}
Foo<Integer> fi1 = new <<error descr="Actual type argument and inferred type contradict each other">String</error>> Foo<>(1);
Foo<Integer> fi2 = new <<error descr="Actual type argument and inferred type contradict each other">String</error>> Foo<Integer>(1);
Foo<Integer> fi3 = new Foo<Integer>(1);
}
@@ -0,0 +1,9 @@
class Neg13 {
static class Foo<X> {
<T> Foo(T t) {}
}
Foo<Integer> fi1 = new <error descr="Wrong number of type arguments: 2; required: 1"><String, Integer></error> Foo<>("");
Foo<Integer> fi2 = new <error descr="Wrong number of type arguments: 2; required: 1"><String, Integer></error> Foo<Integer>("");
}
@@ -0,0 +1,9 @@
class Neg14 {
static class Foo<X> {
<T extends Integer> Foo(T t) {}
}
Foo<Integer> fi1 = new <<error descr="Actual type argument and inferred type contradict each other">String</error>> Foo<>(1);
Foo<Integer> fi2 = new <<error descr="Actual type argument and inferred type contradict each other">String</error>> Foo<Integer>(1);
}
@@ -0,0 +1,13 @@
class Pos8 {
static class Foo<X> {
Foo(X t) {}
}
Foo<Integer> fi1 = new Foo<>(1);
Foo<Integer> fi2 = new Foo<Integer>(1);
Foo<Integer> fi3 = new <String> Foo<>(1);
Foo<Integer> fi4 = new <String> Foo<Integer>(1);
Foo<Integer> fi5 = new <String, String> Foo<>(1);
Foo<Integer> fi6 = new <String, String> Foo<Integer>(1);
}
@@ -0,0 +1,10 @@
class Pos9<X> {
Pos9(X x) {}
Pos9<X> test(X x) {
return new Pos9<>(x);
}
}
@@ -68,16 +68,22 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase {
doTest(false, false);
}
/*
should be negative?
intersection types are not allowed in <>?!
public void testDiamondPos6() throws Exception {
doTest(false, false);
}
public void testDiamondPos7() throws Exception {
doTest(false, false);
}*/
}
public void testDiamondPos8() throws Exception {
doTest(false, false);
}
public void testDiamondPos9() throws Exception {
doTest(false, false);
}
public void testDiamondNeg1() throws Exception {
doTest(false, false);
@@ -123,6 +129,18 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase {
doTest(false, false);
}
public void testDiamondNeg12() throws Exception {
doTest(false, false);
}
public void testDiamondNeg13() throws Exception {
doTest(false, false);
}
public void testDiamondNeg14() throws Exception {
doTest(false, false);
}
public void testDiamondMisc() throws Exception {
doTest(false, false);
}