-
Notifications
You must be signed in to change notification settings - Fork 723
Expand file tree
/
Copy pathInappropriateCastCheckSample.java
More file actions
113 lines (92 loc) · 4.09 KB
/
Copy pathInappropriateCastCheckSample.java
File metadata and controls
113 lines (92 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package checks;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class InappropriateCastCheckSample {
interface Animal {}
interface Vehicle {}
interface Drawable {}
static class Dog implements Animal {}
static final class FinalDog implements Animal {}
static class Car implements Vehicle {}
static class Circle extends Shape implements Drawable {}
static class Shape {}
static abstract class AbstractShape {}
enum Color { RED, GREEN, BLUE }
enum Size { SMALL, MEDIUM, LARGE }
// Noncompliant: unrelated concrete classes
void unrelatedConcreteClasses(Dog dog, Car car, String str) {
Car c = (Car) dog; // Noncompliant {{"Dog" cannot be cast to "Car" without a risk of "ClassCastException".}}
Dog d = (Dog) car; // Noncompliant {{"Car" cannot be cast to "Dog" without a risk of "ClassCastException".}}
Dog d2 = (Dog) str; // Noncompliant {{"String" cannot be cast to "Dog" without a risk of "ClassCastException".}}
}
// Noncompliant: final class to unrelated interface
void finalClassToUnrelatedInterface(FinalDog finalDog, String str) {
Vehicle v = (Vehicle) finalDog; // Noncompliant {{"FinalDog" cannot be cast to "Vehicle" without a risk of "ClassCastException".}}
Drawable d = (Drawable) str; // Noncompliant {{"String" cannot be cast to "Drawable" without a risk of "ClassCastException".}}
}
// Noncompliant: interface to unrelated final class
void interfaceToUnrelatedFinalClass(Vehicle vehicle, Drawable drawable) {
FinalDog fd = (FinalDog) vehicle; // Noncompliant {{"Vehicle" cannot be cast to "FinalDog" without a risk of "ClassCastException".}}
String s = (String) drawable; // Noncompliant {{"Drawable" cannot be cast to "String" without a risk of "ClassCastException".}}
}
// Noncompliant: enums are implicitly final
void enumCasts(Color color, Size size) {
Size s = (Size) color; // Noncompliant {{"Color" cannot be cast to "Size" without a risk of "ClassCastException".}}
Drawable d = (Drawable) color; // Noncompliant {{"Color" cannot be cast to "Drawable" without a risk of "ClassCastException".}}
}
// Compliant: upcast (subtype to supertype)
void upcast(Circle circle, Dog dog) {
Shape s = (Shape) circle; // Compliant
Object o = (Object) dog; // Compliant
Animal a = (Animal) dog; // Compliant
}
// Compliant: downcast along hierarchy
void downcast(Shape shape, Animal animal) {
Circle c = (Circle) shape; // Compliant
Dog d = (Dog) animal; // Compliant
}
// Compliant: cast to/from Object
void objectCasts(Object obj, Dog dog) {
Dog d = (Dog) obj; // Compliant
Object o = (Object) dog; // Compliant
}
// Compliant: cast between interfaces
void interfaceCasts(Animal animal, Vehicle vehicle) {
Vehicle v = (Vehicle) animal; // Compliant
Animal a = (Animal) vehicle; // Compliant
}
// Compliant: non-final class to unrelated interface
void nonFinalClassToInterface(Dog dog, Shape shape) {
Vehicle v = (Vehicle) dog; // Compliant
Serializable s = (Serializable) shape; // Compliant
}
// Compliant: cast involving generics/wildcards
void genericCasts(List<?> wildcardList, List<Integer> intList) {
List<String> strList = (List<String>) wildcardList; // Compliant
ArrayList<Integer> al = (ArrayList<Integer>) intList; // Compliant
}
// Compliant: instanceof guard (still a valid downcast along hierarchy)
void instanceofGuard(Object obj) {
if (obj instanceof String) {
String s = (String) obj; // Compliant
}
}
// Compliant: cast to related interface (class implements interface)
void relatedInterfaceCast(Circle circle) {
Drawable d = (Drawable) circle; // Compliant
}
// Compliant: abstract class to interface
void abstractClassToInterface(AbstractShape abstractShape) {
Drawable d = (Drawable) abstractShape; // Compliant
}
// Compliant: cast involving type variables
<T> void typeVariableCast(T obj) {
String s = (String) obj; // Compliant
}
// Compliant: primitive casts
void primitiveCast(int i) {
long l = (long) i; // Compliant
double d = (double) i; // Compliant
}
}