covariant return types: difference between java7 & java6 (IDEA-83599)

This commit is contained in:
anna
2012-03-28 13:27:06 +02:00
parent b4c42eb076
commit bc7e569fa1
5 changed files with 86 additions and 3 deletions
@@ -26,6 +26,7 @@ import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.IndexNotReadyException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.pom.java.LanguageLevel;
@@ -505,7 +506,17 @@ public class GenericsHighlightUtil {
final PsiType retErasure1 = TypeConversionUtil.erasure(checkMethod.getReturnType());
final PsiType retErasure2 = TypeConversionUtil.erasure(superMethod.getReturnType());
if (!Comparing.equal(retErasure1, retErasure2) &&
boolean differentReturnTypeErasure = !Comparing.equal(retErasure1, retErasure2);
if (checkEqualsSuper && JavaSdkVersion.isAtLeast(checkMethod, JavaSdkVersion.JDK_1_7)) {
if (retErasure1 != null && retErasure2 != null) {
differentReturnTypeErasure = !TypeConversionUtil.isAssignable(retErasure1, retErasure2);
} else {
differentReturnTypeErasure = !(retErasure1 == null && retErasure2 == null);
}
}
if (differentReturnTypeErasure &&
!TypeConversionUtil.isVoidType(retErasure1) &&
!TypeConversionUtil.isVoidType(retErasure2) &&
!(checkEqualsSuper && Arrays.equals(superSignature.getParameterTypes(), signatureToCheck.getParameterTypes()))) {
@@ -7,7 +7,7 @@ interface Matcher<T> {
}
interface ArgumentConstraintPhrases {
<T> T with(Matcher<T> matcher);
<error descr="'with(Matcher<T>)' clashes with 'with(Matcher<Boolean>)'; both methods have same erasure"><T> T with(Matcher<T> matcher)</error>;
boolean with(Matcher<Boolean> matcher);
byte with(Matcher<Byte> matcher);
short with(Matcher<Short> matcher);
@@ -19,7 +19,7 @@ interface ArgumentConstraintPhrases {
class ExpectationGroupBuilder implements ArgumentConstraintPhrases {
public <T> T with(final Matcher<T> matcher) {
<error descr="'with(Matcher<T>)' clashes with 'with(Matcher<Boolean>)'; both methods have same erasure">public <T> T with(final Matcher<T> matcher)</error> {
return null;
}
@@ -0,0 +1,48 @@
/** @noinspection UnusedDeclaration*/
interface Matcher<T> {
boolean matches(Object object);
void _dont_implement_Matcher___instead_extend_BaseMatcher_();
}
interface ArgumentConstraintPhrases {
<T> T with(Matcher<T> matcher);
boolean with(Matcher<Boolean> matcher);
byte with(Matcher<Byte> matcher);
int with(Matcher<Integer> matcher);
long with(Matcher<Long> matcher);
float with(Matcher<Float> matcher);
double with(Matcher<Double> matcher);
}
class ExpectationGroupBuilder implements ArgumentConstraintPhrases {
public <T> T with(final Matcher<T> matcher) {
return null;
}
public boolean with(final Matcher<Boolean> matcher) {
return false;
}
public byte with(final Matcher<Byte> matcher) {
return 0;
}
public int with(final Matcher<Integer> matcher) {
return 0;
}
public long with(final Matcher<Long> matcher) {
return 0;
}
public float with(final Matcher<Float> matcher) {
return 0;
}
public double with(final Matcher<Double> matcher) {
return 0;
}
}
@@ -4,6 +4,8 @@ import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection;
import com.intellij.codeInspection.unusedImport.UnusedImportLocalInspection;
import com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspection;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiClass;
@@ -34,6 +36,11 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(level);
}
@Override
protected Sdk getProjectJDK() {
return getTestName(false).contains("Jdk14") ? JavaSdkImpl.getMockJdk14() : super.getProjectJDK();
}
public void testReferenceTypeParams() throws Exception { doTest(false); }
public void testOverridingMethods() throws Exception { doTest(false); }
public void testTypeParameterBoundsList() throws Exception { doTest(false); }
@@ -85,6 +92,7 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testGenericExtendException() throws Exception { doTest(false); }
public void testSameErasureDifferentReturnTypes() throws Exception { doTest(false); }
public void testSameErasureDifferentReturnTypesJdk14() throws Exception { doTest(false); }
public void testDeepConflictingReturnTypes() throws Exception { doTest(false); }
public void testInheritFromTypeParameter() throws Exception { doTest(false); }
public void testAnnotationsAsPartOfModifierList() throws Exception { doTest(false); }
@@ -15,7 +15,11 @@
*/
package com.intellij.openapi.projectRoots;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
@@ -70,4 +74,16 @@ public enum JavaSdkVersion {
String.format("Can't map Java SDK by description (%s). Available values: %s", description, Arrays.toString(values()))
);
}
public static boolean isAtLeast(PsiElement element, JavaSdkVersion minVersion) {
final Module module = ModuleUtil.findModuleForPsiElement(element);
if (module != null) {
final Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
if (sdk != null && sdk.getSdkType() instanceof JavaSdk) {
final JavaSdkVersion version = JavaSdk.getInstance().getVersion(sdk);
return version != null && version.isAtLeast(minVersion);
}
}
return false;
}
}