Skip to content

Commit af0db96

Browse files
author
Cristian Stancalau
committed
Fix review suggestions.
1 parent 6a82d1c commit af0db96

9 files changed

Lines changed: 125 additions & 94 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.exceptions.classcastexception;
2+
3+
public interface Animal {
4+
5+
String getName();
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.exceptions.classcastexception;
2+
3+
public class Box<T> {
4+
5+
private T content;
6+
7+
public T getContent() {
8+
return content;
9+
}
10+
11+
public void setContent(T content) {
12+
this.content = content;
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.exceptions.classcastexception;
2+
3+
public class Frog extends Reptile {
4+
5+
@Override
6+
public String getName() {
7+
return super.getName() + ": Frog";
8+
}
9+
}

core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/classcastexception/Main.java

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.exceptions.classcastexception;
2+
3+
public class Mammal implements Animal {
4+
5+
@Override
6+
public String getName() {
7+
return "Mammal";
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.exceptions.classcastexception;
2+
3+
public class Reptile implements Animal {
4+
5+
@Override
6+
public String getName() {
7+
return "Reptile";
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.exceptions.classcastexception;
2+
3+
import org.junit.Test;
4+
5+
import java.io.Serializable;
6+
7+
public class CheckedCastsUnitTest {
8+
9+
@Test(expected = ClassCastException.class)
10+
public void givenBaseTypeVariableReferencingChildInstance_whenCastToIncompatibleSubtype_thenClassCastException() {
11+
Animal animal = new Frog();
12+
13+
//A checked downcast to Mammal is incompatible from Frog because Frog is not a subtype of Mammal.
14+
Mammal mammal = (Mammal) animal;
15+
}
16+
17+
@Test(expected = ClassCastException.class)
18+
public void givenBaseTypeVariableReferencingChildInstance_whenCastToIncompatibleInterface_thenClassCastException() {
19+
Animal animal = new Frog();
20+
21+
//A checked cast to Serializable is incompatible from Frog because Frog is not a subtype of Serializable.
22+
Serializable serial = (Serializable) animal;
23+
}
24+
25+
@Test(expected = ClassCastException.class)
26+
public void givenObjectVariableReferencingPrimitiveArray_whenCastToBoxedTypeArray_thenClassCastException() {
27+
Object primitives = new int[1];
28+
29+
//A checked cast to Integer[] is incompatible from primitive arrays. Auto-boxing does not work for arrays.
30+
Integer[] integers = (Integer[]) primitives;
31+
}
32+
33+
@Test(expected = ClassCastException.class)
34+
public void givenObjectVariableReferencingPrimitiveArray_whenCastToPromotedTypeArray_thenClassCastException() {
35+
Object primitives = new int[1];
36+
37+
//A checked cast to long[] is incompatible from int[]. Type promotion does not work for arrays.
38+
long[] longs = (long[]) primitives;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.exceptions.classcastexception;
2+
3+
import org.junit.Test;
4+
5+
public class GenericConversionUnitTest {
6+
7+
@Test(expected = ClassCastException.class)
8+
public void givenIncompatibleType_whenConvertInstanceOfObject_thenClassCastException() {
9+
// Should have been null, but due to type erasure, inside convertInstanceOfObject,
10+
// it will attempt to cast to Object instead of String, so it casts to Object, which is always possible.
11+
String shouldBeNull = convertInstanceOfObject(123);
12+
}
13+
14+
public static <T> T convertInstanceOfObject(Object o) {
15+
try {
16+
return (T) o; // Casts to Object due to type erasure
17+
} catch (ClassCastException e) {
18+
return null; // Will never reach this
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.exceptions.classcastexception;
2+
3+
import org.junit.Test;
4+
5+
public class UncheckedConversionUnitTest {
6+
7+
@Test(expected = ClassCastException.class)
8+
public void givenPollutedGenericType_whenGetProperty_thenClassCastException() {
9+
Box<Long> originalBox = new Box<>();
10+
Box raw = originalBox;
11+
raw.setContent(2.5);
12+
Box<Long> bound = (Box<Long>) raw;
13+
14+
//An incompatible element was found in the raw box.
15+
Long content = bound.getContent();
16+
}
17+
}

0 commit comments

Comments
 (0)