d9ba1788fc791268b940e7adfadf05e8ff6d0f49
[jpf-core.git] / src / classes / sun / reflect / generics / reflectiveObjects / GenericArrayTypeImpl.java
1 package sun.reflect.generics.reflectiveObjects;
2
3 import java.lang.reflect.GenericArrayType;
4 import java.lang.reflect.Type;
5 import java.util.Objects;
6
7 /**
8  * MJI model class for sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl
9  *
10  * This is a JPF specific version of a system class because we can't use the real,
11  * platform VM specific version (it's native all over the place, its field
12  * structure isn't documented, most of its methods are private, hence we can't
13  * even instantiate it properly).
14  *
15  * Note that this class never gets seen by the real VM - it's for JPF's eyes only.
16  *
17  * For now this only supports a few basic methods.
18  */
19 public class GenericArrayTypeImpl
20         implements GenericArrayType {
21     private final Type genericComponentType;
22
23     // private constructor enforces use of static factory
24     private GenericArrayTypeImpl(Type ct) {
25         genericComponentType = ct;
26     }
27
28     /**
29      * Factory method.
30      * @param ct - the desired component type of the generic array type
31      * being created
32      * @return a generic array type with the desired component type
33      */
34     public static GenericArrayTypeImpl make(Type ct) {
35         return new GenericArrayTypeImpl(ct);
36     }
37
38
39     /**
40      * Returns  a <tt>Type</tt> object representing the component type
41      * of this array.
42      *
43      * @return  a <tt>Type</tt> object representing the component type
44      *     of this array
45      * @since 1.5
46      */
47     public Type getGenericComponentType() {
48         return genericComponentType; // return cached component type
49     }
50
51     public String toString() {
52         Type componentType = getGenericComponentType();
53         StringBuilder sb = new StringBuilder();
54
55         if (componentType instanceof Class)
56             sb.append(((Class)componentType).getName() );
57         else
58             sb.append(componentType.toString());
59         sb.append("[]");
60         return sb.toString();
61     }
62
63     @Override
64     public boolean equals(Object o) {
65         if (o instanceof GenericArrayType) {
66             GenericArrayType that = (GenericArrayType) o;
67
68             return Objects.equals(genericComponentType, that.getGenericComponentType());
69         } else
70             return false;
71     }
72
73     @Override
74     public int hashCode() {
75         return Objects.hashCode(genericComponentType);
76     }
77 }