J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.reflect.generics.factory; |
| 27 | |
| 28 | import java.lang.reflect.Constructor; |
| 29 | import java.lang.reflect.GenericDeclaration; |
| 30 | import java.lang.reflect.Method; |
| 31 | import java.lang.reflect.ParameterizedType; |
| 32 | import java.lang.reflect.Type; |
| 33 | import java.lang.reflect.TypeVariable; |
| 34 | import java.lang.reflect.WildcardType; |
| 35 | |
| 36 | |
| 37 | import sun.reflect.generics.reflectiveObjects.*; |
| 38 | import sun.reflect.generics.scope.Scope; |
| 39 | import sun.reflect.generics.tree.FieldTypeSignature; |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Factory for reflective generic type objects for use by |
| 44 | * core reflection (java.lang.reflect). |
| 45 | */ |
| 46 | public class CoreReflectionFactory implements GenericsFactory { |
| 47 | private GenericDeclaration decl; |
| 48 | private Scope scope; |
| 49 | |
| 50 | private CoreReflectionFactory(GenericDeclaration d, Scope s) { |
| 51 | decl = d; |
| 52 | scope = s; |
| 53 | } |
| 54 | |
| 55 | private GenericDeclaration getDecl(){ return decl;} |
| 56 | |
| 57 | private Scope getScope(){ return scope;} |
| 58 | |
| 59 | |
| 60 | private ClassLoader getDeclsLoader() { |
| 61 | if (decl instanceof Class) {return ((Class) decl).getClassLoader();} |
| 62 | if (decl instanceof Method) { |
| 63 | return ((Method) decl).getDeclaringClass().getClassLoader(); |
| 64 | } |
| 65 | assert decl instanceof Constructor : "Constructor expected"; |
| 66 | return ((Constructor) decl).getDeclaringClass().getClassLoader(); |
| 67 | |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Factory for this class. Returns an instance of |
| 72 | * <tt>CoreReflectionFactory</tt> for the declaration and scope |
| 73 | * provided. |
| 74 | * This factory will produce reflective objects of the appropriate |
| 75 | * kind. Classes produced will be those that would be loaded by the |
| 76 | * defining class loader of the declaration <tt>d</tt> (if <tt>d</tt> |
| 77 | * is a type declaration, or by the defining loader of the declaring |
| 78 | * class of <tt>d</tt> otherwise. |
| 79 | * <p> Type variables will be created or lookup as necessary in the |
| 80 | * scope <tt> s</tt>. |
| 81 | * @param d - the generic declaration (class, interface, method or |
| 82 | * constructor) that thsi factory services |
| 83 | * @param s the scope in which the factory will allocate and search for |
| 84 | * type variables |
| 85 | * @return an instance of <tt>CoreReflectionFactory</tt> |
| 86 | */ |
| 87 | public static CoreReflectionFactory make(GenericDeclaration d, Scope s) { |
| 88 | return new CoreReflectionFactory(d, s); |
| 89 | } |
| 90 | |
| 91 | public TypeVariable<?> makeTypeVariable(String name, |
| 92 | FieldTypeSignature[] bounds){ |
| 93 | return TypeVariableImpl.make(getDecl(), name, bounds, this); |
| 94 | } |
| 95 | |
| 96 | public WildcardType makeWildcard(FieldTypeSignature[] ubs, |
| 97 | FieldTypeSignature[] lbs) { |
| 98 | return WildcardTypeImpl.make(ubs, lbs, this); |
| 99 | } |
| 100 | |
| 101 | public ParameterizedType makeParameterizedType(Type declaration, |
| 102 | Type[] typeArgs, |
| 103 | Type owner) { |
| 104 | return ParameterizedTypeImpl.make((Class<?>) declaration, |
| 105 | typeArgs, owner); |
| 106 | } |
| 107 | |
| 108 | public TypeVariable<?> findTypeVariable(String name){ |
| 109 | return getScope().lookup(name); |
| 110 | } |
| 111 | |
| 112 | public Type makeNamedType(String name){ |
| 113 | try {return Class.forName(name, false, // don't initialize |
| 114 | getDeclsLoader());} |
| 115 | catch (ClassNotFoundException c) { |
| 116 | throw new TypeNotPresentException(name, c); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public Type makeArrayType(Type componentType){ |
| 121 | return GenericArrayTypeImpl.make(componentType); |
| 122 | } |
| 123 | |
| 124 | public Type makeByte(){return byte.class;} |
| 125 | public Type makeBool(){return boolean.class;} |
| 126 | public Type makeShort(){return short.class;} |
| 127 | public Type makeChar(){return char.class;} |
| 128 | public Type makeInt(){return int.class;} |
| 129 | public Type makeLong(){return long.class;} |
| 130 | public Type makeFloat(){return float.class;} |
| 131 | public Type makeDouble(){return double.class;} |
| 132 | |
| 133 | public Type makeVoid(){return void.class;} |
| 134 | } |