Fix review suggestions.
This commit is contained in:
parent
6a82d1cde6
commit
af0db9610e
@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
|
public interface Animal {
|
||||||
|
|
||||||
|
String getName();
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
|
public class Box<T> {
|
||||||
|
|
||||||
|
private T content;
|
||||||
|
|
||||||
|
public T getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(T content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
|
public class Frog extends Reptile {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return super.getName() + ": Frog";
|
||||||
|
}
|
||||||
|
}
|
@ -1,94 +0,0 @@
|
|||||||
package com.baeldung.exceptions.classcastexception;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
checkedCasts();
|
|
||||||
uncheckedConversion();
|
|
||||||
genericConversion();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void checkedCasts() {
|
|
||||||
Animal animal = new Frog();
|
|
||||||
try {
|
|
||||||
Mammal mammal = (Mammal) animal;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
System.out.println("A checked downcast to Mammal is incompatible from Frog because Frog is not a subtype of Mammal.");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Serializable serial = (Serializable) animal;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
System.out.println("A checked cast to Serializable is incompatible from Frog because Frog is not a subtype of Serializable.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Object primitives = new int[1];
|
|
||||||
try {
|
|
||||||
Integer[] integers = (Integer[]) primitives;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
System.out.println("A checked cast to Integer[] is incompatible from primitive arrays. Auto-boxing does not work for arrays.");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
long[] longs = (long[]) primitives;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
System.out.println("A checked cast to long[] is incompatible from int[]. Type promotion does not work for arrays.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void uncheckedConversion() {
|
|
||||||
Box<Long> originalBox = new Box<>();
|
|
||||||
Box raw = originalBox;
|
|
||||||
raw.setContent(2.5);
|
|
||||||
Box<Long> bound = (Box<Long>) raw;
|
|
||||||
try {
|
|
||||||
Long content = bound.getContent();
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
System.out.println("An incompatible element was found in the raw box.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void genericConversion() {
|
|
||||||
try {
|
|
||||||
String shouldBeNull = convertInstanceOfObject(123);
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
System.out.println("Should have been null, but due to type erasure, inside convertInstanceOfObject, " +
|
|
||||||
"it will attempt to cast to Object instead of String, so it casts to Object, which is always possible.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> T convertInstanceOfObject(Object o) {
|
|
||||||
try {
|
|
||||||
return (T) o;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface Animal {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Reptile implements Animal {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Frog extends Reptile {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Mammal implements Animal {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Box<T> {
|
|
||||||
private T content;
|
|
||||||
|
|
||||||
public T getContent() {
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setContent(T content) {
|
|
||||||
this.content = content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
|
public class Mammal implements Animal {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Mammal";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
|
public class Reptile implements Animal {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Reptile";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class CheckedCastsUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenBaseTypeVariableReferencingChildInstance_whenCastToIncompatibleSubtype_thenClassCastException() {
|
||||||
|
Animal animal = new Frog();
|
||||||
|
|
||||||
|
//A checked downcast to Mammal is incompatible from Frog because Frog is not a subtype of Mammal.
|
||||||
|
Mammal mammal = (Mammal) animal;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenBaseTypeVariableReferencingChildInstance_whenCastToIncompatibleInterface_thenClassCastException() {
|
||||||
|
Animal animal = new Frog();
|
||||||
|
|
||||||
|
//A checked cast to Serializable is incompatible from Frog because Frog is not a subtype of Serializable.
|
||||||
|
Serializable serial = (Serializable) animal;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenObjectVariableReferencingPrimitiveArray_whenCastToBoxedTypeArray_thenClassCastException() {
|
||||||
|
Object primitives = new int[1];
|
||||||
|
|
||||||
|
//A checked cast to Integer[] is incompatible from primitive arrays. Auto-boxing does not work for arrays.
|
||||||
|
Integer[] integers = (Integer[]) primitives;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenObjectVariableReferencingPrimitiveArray_whenCastToPromotedTypeArray_thenClassCastException() {
|
||||||
|
Object primitives = new int[1];
|
||||||
|
|
||||||
|
//A checked cast to long[] is incompatible from int[]. Type promotion does not work for arrays.
|
||||||
|
long[] longs = (long[]) primitives;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class GenericConversionUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenIncompatibleType_whenConvertInstanceOfObject_thenClassCastException() {
|
||||||
|
// Should have been null, but due to type erasure, inside convertInstanceOfObject,
|
||||||
|
// it will attempt to cast to Object instead of String, so it casts to Object, which is always possible.
|
||||||
|
String shouldBeNull = convertInstanceOfObject(123);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T convertInstanceOfObject(Object o) {
|
||||||
|
try {
|
||||||
|
return (T) o; // Casts to Object due to type erasure
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
return null; // Will never reach this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.exceptions.classcastexception;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class UncheckedConversionUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = ClassCastException.class)
|
||||||
|
public void givenPollutedGenericType_whenGetProperty_thenClassCastException() {
|
||||||
|
Box<Long> originalBox = new Box<>();
|
||||||
|
Box raw = originalBox;
|
||||||
|
raw.setContent(2.5);
|
||||||
|
Box<Long> bound = (Box<Long>) raw;
|
||||||
|
|
||||||
|
//An incompatible element was found in the raw box.
|
||||||
|
Long content = bound.getContent();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user