1 package sun.reflect.generics.reflectiveObjects;
3 import sun.reflect.generics.tree.FieldTypeSignature;
5 import java.lang.reflect.MalformedParameterizedTypeException;
6 import java.lang.reflect.Method;
7 import java.lang.reflect.ParameterizedType;
8 import java.lang.reflect.Type;
9 import java.lang.reflect.TypeVariable;
10 import java.util.Arrays;
11 import java.util.Objects;
14 * MJI model class for sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
16 * This is a JPF specific version of a system class because we can't use the real,
17 * platform VM specific version (it's native all over the place, its field
18 * structure isn't documented, most of its methods are private, hence we can't
19 * even instantiate it properly).
21 * Note that this class never gets seen by the real VM - it's for JPF's eyes only.
25 /** Implementing class for ParameterizedType interface. */
26 public class ParameterizedTypeImpl implements ParameterizedType {
27 private final Type[] actualTypeArguments;
28 private final Class<?> rawType;
29 private final Type ownerType;
31 private ParameterizedTypeImpl(Class<?> rawType,
32 Type[] actualTypeArguments,
34 this.actualTypeArguments = actualTypeArguments;
35 this.rawType = rawType;
36 this.ownerType = (ownerType != null) ? ownerType : rawType.getDeclaringClass();
37 validateConstructorArguments();
40 private void validateConstructorArguments() {
41 TypeVariable<?>[] formals = rawType.getTypeParameters();
42 // check correct arity of actual type args
43 if (formals.length != actualTypeArguments.length){
44 throw new MalformedParameterizedTypeException();
46 for (int i = 0; i < actualTypeArguments.length; i++) {
47 // check actuals against formals' bounds
52 * Static factory. Given a (generic) class, actual type arguments
53 * and an owner type, creates a parameterized type.
55 public static ParameterizedTypeImpl make(Class<?> rawType,
56 Type[] actualTypeArguments,
58 return new ParameterizedTypeImpl(rawType, actualTypeArguments,
62 public Type[] getActualTypeArguments() {
63 return actualTypeArguments.clone();
67 * Returns the <tt>Type</tt> object representing the class or interface
68 * that declared this type.
70 * @return the <tt>Type</tt> object representing the class or interface
71 * that declared this type
73 public Class<?> getRawType() {
78 public Type getOwnerType() {
83 public boolean equals(Object o) {
84 if (o instanceof ParameterizedType) {
85 // Check that information is equivalent
86 ParameterizedType that = (ParameterizedType) o;
91 Type thatOwner = that.getOwnerType();
92 Type thatRawType = that.getRawType();
94 if (false) { // Debugging
95 boolean ownerEquality = (ownerType == null ?
97 ownerType.equals(thatOwner));
98 boolean rawEquality = (rawType == null ?
100 rawType.equals(thatRawType));
102 boolean typeArgEquality = Arrays.equals(actualTypeArguments, // avoid clone
103 that.getActualTypeArguments());
104 for (Type t : actualTypeArguments) {
105 System.out.printf("\t\t%s%s%n", t, t.getClass());
108 System.out.printf("\towner %s\traw %s\ttypeArg %s%n",
109 ownerEquality, rawEquality, typeArgEquality);
110 return ownerEquality && rawEquality && typeArgEquality;
114 Objects.equals(ownerType, thatOwner) &&
115 Objects.equals(rawType, thatRawType) &&
116 Arrays.equals(actualTypeArguments, // avoid clone
117 that.getActualTypeArguments());
123 public int hashCode() {
125 Arrays.hashCode(actualTypeArguments) ^
126 Objects.hashCode(ownerType) ^
127 Objects.hashCode(rawType);
130 public String toString() {
131 StringBuilder sb = new StringBuilder();
133 if (ownerType != null) {
134 if (ownerType instanceof Class)
135 sb.append(((Class)ownerType).getName());
137 sb.append(ownerType.toString());
141 if (ownerType instanceof ParameterizedTypeImpl) {
142 // Find simple name of nested type by removing the
143 // shared prefix with owner.
144 sb.append(rawType.getName().replace( ((ParameterizedTypeImpl)ownerType).rawType.getName() + "$",
147 sb.append(rawType.getName());
149 sb.append(rawType.getName());
151 if (actualTypeArguments != null &&
152 actualTypeArguments.length > 0) {
154 boolean first = true;
155 for(Type t: actualTypeArguments) {
158 sb.append(t.getTypeName());
164 return sb.toString();