mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fix casting to primitives according to the spec (IDEA-131107; IDEA-148880)
This commit is contained in:
@@ -137,9 +137,9 @@ public class TypeConversionUtil {
|
||||
if (languageLevel.isAtLeast(LanguageLevel.JDK_1_7)) {
|
||||
final PsiClassType classType = (PsiClassType)fromType;
|
||||
final PsiClass psiClass = classType.resolve();
|
||||
if (psiClass == null || psiClass instanceof PsiTypeParameter) return false;
|
||||
if (psiClass == null) return false;
|
||||
final PsiClassType boxedType = ((PsiPrimitiveType)toType).getBoxedType(psiClass.getManager(), psiClass.getResolveScope());
|
||||
if (boxedType != null && isAssignable(fromType, boxedType)) {
|
||||
if (boxedType != null && isNarrowingReferenceConversionAllowed(fromType, boxedType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
class Test {
|
||||
public <T> void foo(T valIn){
|
||||
double val = (double ) valIn;
|
||||
}
|
||||
|
||||
public <T extends Double> void foo1(T valIn){
|
||||
double val = (double ) valIn;
|
||||
}
|
||||
|
||||
public <T extends String> void foo2(T valIn){
|
||||
double val = <error descr="Inconvertible types; cannot cast 'T' to 'double'">(double ) valIn</error>;
|
||||
}
|
||||
|
||||
public <T extends S, S extends Double> void foo2(T valIn){
|
||||
double val = (double ) valIn;
|
||||
}
|
||||
}
|
||||
|
||||
class Foo<T> {
|
||||
|
||||
private T _value;
|
||||
|
||||
T getValue() {
|
||||
return _value;
|
||||
}
|
||||
|
||||
static Foo<?> getFoo() {
|
||||
return new Foo<>();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Foo<?> foo = getFoo();
|
||||
double value = (double) foo.getValue();
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class Foo<T> {
|
||||
|
||||
private T _value;
|
||||
|
||||
T getValue() {
|
||||
return _value;
|
||||
}
|
||||
|
||||
static Foo<?> getFoo() {
|
||||
return new Foo<>();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Foo<?> foo = getFoo();
|
||||
double value = <error descr="Inconvertible types; cannot cast 'capture<?>' to 'double'">(double) foo.getValue()</error>;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -546,7 +546,11 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
|
||||
public void testIDEA130243() throws Exception {
|
||||
doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, false);
|
||||
}
|
||||
|
||||
|
||||
public void testCastingToPrimitive() throws Exception {
|
||||
doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, false);
|
||||
}
|
||||
|
||||
public void testProvablyDistinctForWildcardsWithArrayBounds() throws Exception {
|
||||
doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, false);
|
||||
}
|
||||
|
||||
+4
@@ -942,4 +942,8 @@ public class GenericsHighlighting8Test extends LightDaemonAnalyzerTestCase {
|
||||
public void testIDEA139096() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testCastingCapturedWildcardToPrimitive() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user