src/share/classes/com/sun/beans/TypeResolver.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2012, 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


 221     }
 222 
 223     /**
 224      * Converts the given {@code type} to the corresponding class.
 225      * This method implements the concept of type erasure,
 226      * that is described in section 4.6 of
 227      * <cite>The Java&trade; Language Specification</cite>.
 228      *
 229      * @param type  the array of types to convert
 230      * @return a corresponding class
 231      */
 232     public static Class<?> erase(Type type) {
 233         if (type instanceof Class) {
 234             return (Class<?>) type;
 235         }
 236         if (type instanceof ParameterizedType) {
 237             ParameterizedType pt = (ParameterizedType) type;
 238             return (Class<?>) pt.getRawType();
 239         }
 240         if (type instanceof TypeVariable) {
 241             TypeVariable tv = (TypeVariable)type;
 242             Type[] bounds = tv.getBounds();
 243             return (0 < bounds.length)
 244                     ? erase(bounds[0])
 245                     : Object.class;
 246         }
 247         if (type instanceof WildcardType) {
 248             WildcardType wt = (WildcardType)type;
 249             Type[] bounds = wt.getUpperBounds();
 250             return (0 < bounds.length)
 251                     ? erase(bounds[0])
 252                     : Object.class;
 253         }
 254         if (type instanceof GenericArrayType) {
 255             GenericArrayType gat = (GenericArrayType)type;
 256             return Array.newInstance(erase(gat.getGenericComponentType()), 0).getClass();
 257         }
 258         throw new IllegalArgumentException("Unknown Type kind: " + type.getClass());
 259     }
 260 
 261     /**
 262      * Converts all {@code types} in the given array
 263      * to the corresponding classes.
 264      *
 265      * @param types  the array of types to convert
 266      * @return an array of corresponding classes
 267      *
 268      * @see #erase(Type)
 269      */
 270     public static Class[] erase(Type[] types) {
 271         int length = types.length;
 272         Class[] classes = new Class[length];
 273         for (int i = 0; i < length; i++) {
 274             classes[i] = TypeResolver.erase(types[i]);
 275         }
 276         return classes;
 277     }
 278 
 279     /**
 280      * Fills the map from type parameters
 281      * to types as seen by the given {@code type}.
 282      * The method is recursive because the {@code type}
 283      * inherits mappings from its parent classes and interfaces.
 284      * The {@code type} can be either a {@link Class Class}
 285      * or a {@link ParameterizedType ParameterizedType}.
 286      * If it is a {@link Class Class}, it is either equivalent
 287      * to a {@link ParameterizedType ParameterizedType} with no parameters,
 288      * or it represents the erasure of a {@link ParameterizedType ParameterizedType}.
 289      *
 290      * @param map   the mappings of all type variables
 291      * @param type  the next type in the hierarchy
 292      */


   1 /*
   2  * Copyright (c) 2003, 2014, 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


 221     }
 222 
 223     /**
 224      * Converts the given {@code type} to the corresponding class.
 225      * This method implements the concept of type erasure,
 226      * that is described in section 4.6 of
 227      * <cite>The Java&trade; Language Specification</cite>.
 228      *
 229      * @param type  the array of types to convert
 230      * @return a corresponding class
 231      */
 232     public static Class<?> erase(Type type) {
 233         if (type instanceof Class) {
 234             return (Class<?>) type;
 235         }
 236         if (type instanceof ParameterizedType) {
 237             ParameterizedType pt = (ParameterizedType) type;
 238             return (Class<?>) pt.getRawType();
 239         }
 240         if (type instanceof TypeVariable) {
 241             TypeVariable<?> tv = (TypeVariable<?>)type;
 242             Type[] bounds = tv.getBounds();
 243             return (0 < bounds.length)
 244                     ? erase(bounds[0])
 245                     : Object.class;
 246         }
 247         if (type instanceof WildcardType) {
 248             WildcardType wt = (WildcardType)type;
 249             Type[] bounds = wt.getUpperBounds();
 250             return (0 < bounds.length)
 251                     ? erase(bounds[0])
 252                     : Object.class;
 253         }
 254         if (type instanceof GenericArrayType) {
 255             GenericArrayType gat = (GenericArrayType)type;
 256             return Array.newInstance(erase(gat.getGenericComponentType()), 0).getClass();
 257         }
 258         throw new IllegalArgumentException("Unknown Type kind: " + type.getClass());
 259     }
 260 
 261     /**
 262      * Converts all {@code types} in the given array
 263      * to the corresponding classes.
 264      *
 265      * @param types  the array of types to convert
 266      * @return an array of corresponding classes
 267      *
 268      * @see #erase(Type)
 269      */
 270     public static Class<?>[] erase(Type[] types) {
 271         int length = types.length;
 272         Class<?>[] classes = new Class<?>[length];
 273         for (int i = 0; i < length; i++) {
 274             classes[i] = TypeResolver.erase(types[i]);
 275         }
 276         return classes;
 277     }
 278 
 279     /**
 280      * Fills the map from type parameters
 281      * to types as seen by the given {@code type}.
 282      * The method is recursive because the {@code type}
 283      * inherits mappings from its parent classes and interfaces.
 284      * The {@code type} can be either a {@link Class Class}
 285      * or a {@link ParameterizedType ParameterizedType}.
 286      * If it is a {@link Class Class}, it is either equivalent
 287      * to a {@link ParameterizedType ParameterizedType} with no parameters,
 288      * or it represents the erasure of a {@link ParameterizedType ParameterizedType}.
 289      *
 290      * @param map   the mappings of all type variables
 291      * @param type  the next type in the hierarchy
 292      */