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