1 /* 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.reflect.generics.reflectiveObjects; 27 28 import sun.reflect.generics.tree.FieldTypeSignature; 29 30 import java.lang.reflect.MalformedParameterizedTypeException; 31 import java.lang.reflect.Method; 32 import java.lang.reflect.ParameterizedType; 33 import java.lang.reflect.Type; 34 import java.lang.reflect.TypeVariable; 35 import java.util.Arrays; 36 37 38 /** Implementing class for ParameterizedType interface. */ 39 40 public class ParameterizedTypeImpl implements ParameterizedType { 41 private Type[] actualTypeArguments; 42 private Class<?> rawType; 43 private Type ownerType; 44 45 private ParameterizedTypeImpl(Class<?> rawType, 46 Type[] actualTypeArguments, 47 Type ownerType) { 48 this.actualTypeArguments = actualTypeArguments; 49 this.rawType = rawType; 50 if (ownerType != null) { 51 this.ownerType = ownerType; 52 } else { this.ownerType = rawType.getDeclaringClass();} 53 validateConstructorArguments(); 54 } 55 56 private void validateConstructorArguments() { 57 TypeVariable/*<?>*/[] formals = rawType.getTypeParameters(); 58 // check correct arity of actual type args 59 if (formals.length != actualTypeArguments.length){ 60 throw new MalformedParameterizedTypeException(); 61 } 62 for (int i = 0; i < actualTypeArguments.length; i++) { 63 // check actuals against formals' bounds 64 } 65 66 } 67 68 /** 69 * Static factory. Given a (generic) class, actual type arguments 70 * and an owner type, creates a parameterized type. 71 * This class can be instantiated with a a raw type that does not 72 * represent a generic type, provided the list of actual type 73 * arguments is empty. 74 * If the ownerType argument is null, the declaring class of the 75 * raw type is used as the owner type. 76 * <p> This method throws a MalformedParameterizedTypeException 77 * under the following circumstances: 78 * If the number of actual type arguments (i.e., the size of the 79 * array <tt>typeArgs</tt>) does not correspond to the number of 80 * formal type arguments. 81 * If any of the actual type arguments is not an instance of the 82 * bounds on the corresponding formal. 83 * @param rawType the Class representing the generic type declaration being 84 * instantiated 85 * @param actualTypeArguments - a (possibly empty) array of types 172 173 if (false) { // Debugging 174 boolean ownerEquality = (ownerType == null ? 175 thatOwner == null : 176 ownerType.equals(thatOwner)); 177 boolean rawEquality = (rawType == null ? 178 thatRawType == null : 179 rawType.equals(thatRawType)); 180 181 boolean typeArgEquality = Arrays.equals(actualTypeArguments, // avoid clone 182 that.getActualTypeArguments()); 183 for (Type t : actualTypeArguments) { 184 System.out.printf("\t\t%s%s%n", t, t.getClass()); 185 } 186 187 System.out.printf("\towner %s\traw %s\ttypeArg %s%n", 188 ownerEquality, rawEquality, typeArgEquality); 189 return ownerEquality && rawEquality && typeArgEquality; 190 } 191 192 193 return 194 (ownerType == null ? 195 thatOwner == null : 196 ownerType.equals(thatOwner)) && 197 (rawType == null ? 198 thatRawType == null : 199 rawType.equals(thatRawType)) && 200 Arrays.equals(actualTypeArguments, // avoid clone 201 that.getActualTypeArguments()); 202 } else 203 return false; 204 } 205 206 @Override 207 public int hashCode() { 208 return 209 Arrays.hashCode(actualTypeArguments) ^ 210 (ownerType == null ? 0 : ownerType.hashCode() ) ^ 211 (rawType == null ? 0 : rawType.hashCode() ); 212 } 213 214 public String toString() { 215 StringBuilder sb = new StringBuilder(); 216 217 if (ownerType != null) { 218 if (ownerType instanceof Class) 219 sb.append(((Class)ownerType).getName()); 220 else 221 sb.append(ownerType.toString()); 222 223 sb.append("."); 224 225 if (ownerType instanceof ParameterizedTypeImpl) { 226 // Find simple name of nested type by removing the 227 // shared prefix with owner. 228 sb.append(rawType.getName().replace( ((ParameterizedTypeImpl)ownerType).rawType.getName() + "$", 229 "")); 230 } else 231 sb.append(rawType.getName()); 232 } else 233 sb.append(rawType.getName()); 234 235 if (actualTypeArguments != null && 236 actualTypeArguments.length > 0) { 237 sb.append("<"); 238 boolean first = true; 239 for(Type t: actualTypeArguments) { 240 if (!first) 241 sb.append(", "); 242 if (t instanceof Class) 243 sb.append(((Class)t).getName()); 244 else 245 sb.append(t.toString()); 246 first = false; 247 } 248 sb.append(">"); 249 } 250 251 return sb.toString(); 252 } 253 } | 1 /* 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.reflect.generics.reflectiveObjects; 27 28 import sun.reflect.generics.tree.FieldTypeSignature; 29 30 import java.lang.reflect.MalformedParameterizedTypeException; 31 import java.lang.reflect.Method; 32 import java.lang.reflect.ParameterizedType; 33 import java.lang.reflect.Type; 34 import java.lang.reflect.TypeVariable; 35 import java.util.Arrays; 36 import java.util.Objects; 37 38 /** Implementing class for ParameterizedType interface. */ 39 40 public class ParameterizedTypeImpl implements ParameterizedType { 41 private Type[] actualTypeArguments; 42 private Class<?> rawType; 43 private Type ownerType; 44 45 private ParameterizedTypeImpl(Class<?> rawType, 46 Type[] actualTypeArguments, 47 Type ownerType) { 48 this.actualTypeArguments = actualTypeArguments; 49 this.rawType = rawType; 50 this.ownerType = (ownerType != null) ? ownerType : rawType.getDeclaringClass(); 51 validateConstructorArguments(); 52 } 53 54 private void validateConstructorArguments() { 55 TypeVariable/*<?>*/[] formals = rawType.getTypeParameters(); 56 // check correct arity of actual type args 57 if (formals.length != actualTypeArguments.length){ 58 throw new MalformedParameterizedTypeException(); 59 } 60 for (int i = 0; i < actualTypeArguments.length; i++) { 61 // check actuals against formals' bounds 62 } 63 } 64 65 /** 66 * Static factory. Given a (generic) class, actual type arguments 67 * and an owner type, creates a parameterized type. 68 * This class can be instantiated with a a raw type that does not 69 * represent a generic type, provided the list of actual type 70 * arguments is empty. 71 * If the ownerType argument is null, the declaring class of the 72 * raw type is used as the owner type. 73 * <p> This method throws a MalformedParameterizedTypeException 74 * under the following circumstances: 75 * If the number of actual type arguments (i.e., the size of the 76 * array <tt>typeArgs</tt>) does not correspond to the number of 77 * formal type arguments. 78 * If any of the actual type arguments is not an instance of the 79 * bounds on the corresponding formal. 80 * @param rawType the Class representing the generic type declaration being 81 * instantiated 82 * @param actualTypeArguments - a (possibly empty) array of types 169 170 if (false) { // Debugging 171 boolean ownerEquality = (ownerType == null ? 172 thatOwner == null : 173 ownerType.equals(thatOwner)); 174 boolean rawEquality = (rawType == null ? 175 thatRawType == null : 176 rawType.equals(thatRawType)); 177 178 boolean typeArgEquality = Arrays.equals(actualTypeArguments, // avoid clone 179 that.getActualTypeArguments()); 180 for (Type t : actualTypeArguments) { 181 System.out.printf("\t\t%s%s%n", t, t.getClass()); 182 } 183 184 System.out.printf("\towner %s\traw %s\ttypeArg %s%n", 185 ownerEquality, rawEquality, typeArgEquality); 186 return ownerEquality && rawEquality && typeArgEquality; 187 } 188 189 return 190 Objects.equals(ownerType, thatOwner) && 191 Objects.equals(rawType, thatRawType) && 192 Arrays.equals(actualTypeArguments, // avoid clone 193 that.getActualTypeArguments()); 194 } else 195 return false; 196 } 197 198 @Override 199 public int hashCode() { 200 return 201 Arrays.hashCode(actualTypeArguments) ^ 202 Objects.hashCode(ownerType) ^ 203 Objects.hashCode(rawType); 204 } 205 206 public String toString() { 207 StringBuilder sb = new StringBuilder(); 208 209 if (ownerType != null) { 210 if (ownerType instanceof Class) 211 sb.append(((Class)ownerType).getName()); 212 else 213 sb.append(ownerType.toString()); 214 215 sb.append("."); 216 217 if (ownerType instanceof ParameterizedTypeImpl) { 218 // Find simple name of nested type by removing the 219 // shared prefix with owner. 220 sb.append(rawType.getName().replace( ((ParameterizedTypeImpl)ownerType).rawType.getName() + "$", 221 "")); 222 } else 223 sb.append(rawType.getName()); 224 } else 225 sb.append(rawType.getName()); 226 227 if (actualTypeArguments != null && 228 actualTypeArguments.length > 0) { 229 sb.append("<"); 230 boolean first = true; 231 for(Type t: actualTypeArguments) { 232 if (!first) 233 sb.append(", "); 234 sb.append(t.getTypeName()); 235 first = false; 236 } 237 sb.append(">"); 238 } 239 240 return sb.toString(); 241 } 242 } |