blob: 79b77415653ef014a6f6d5e58df0e4c468077769 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 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 */
25package com.sun.beans.finder;
26
27/**
28 * This is utility class that provides <code>static</code> methods
29 * to find a class with the specified name using the specified class loader.
30 *
31 * @since 1.7
32 *
33 * @author Sergey A. Malenkov
34 */
35public final class ClassFinder {
36 /**
37 * Returns the <code>Class</code> object associated
38 * with the class or interface with the given string name,
39 * using the default class loader.
40 * <p>
41 * The <code>name</code> can denote an array class
42 * (see {@link Class#getName} for details).
43 *
44 * @param name fully qualified name of the desired class
45 * @return class object representing the desired class
46 *
47 * @exception ClassNotFoundException if the class cannot be located
48 * by the specified class loader
49 *
50 * @see Class#forName(String)
51 * @see Class#forName(String,boolean,ClassLoader)
52 * @see ClassLoader#getSystemClassLoader()
53 * @see Thread#getContextClassLoader()
54 */
55 public static Class findClass( String name ) throws ClassNotFoundException {
56 try {
57 ClassLoader loader = Thread.currentThread().getContextClassLoader();
58 if ( loader == null ) {
59 // can be null in IE (see 6204697)
60 loader = ClassLoader.getSystemClassLoader();
61 }
62 if ( loader != null ) {
63 return Class.forName( name, false, loader );
64 }
65
66 } catch ( ClassNotFoundException exception ) {
67 // use current class loader instead
68 } catch ( SecurityException exception ) {
69 // use current class loader instead
70 }
71 return Class.forName( name );
72 }
73
74 /**
75 * Returns the <code>Class</code> object associated with
76 * the class or interface with the given string name,
77 * using the given class loader.
78 * <p>
79 * The <code>name</code> can denote an array class
80 * (see {@link Class#getName} for details).
81 * <p>
82 * If the parameter <code>loader</code> is null,
83 * the class is loaded through the default class loader.
84 *
85 * @param name fully qualified name of the desired class
86 * @param loader class loader from which the class must be loaded
87 * @return class object representing the desired class
88 *
89 * @exception ClassNotFoundException if the class cannot be located
90 * by the specified class loader
91 *
92 * @see #findClass(String,ClassLoader)
93 * @see Class#forName(String,boolean,ClassLoader)
94 */
95 public static Class findClass( String name, ClassLoader loader ) throws ClassNotFoundException {
96 if ( loader != null ) {
97 try {
98 return Class.forName( name, false, loader );
99 } catch ( ClassNotFoundException exception ) {
100 // use default class loader instead
101 } catch ( SecurityException exception ) {
102 // use default class loader instead
103 }
104 }
105 return findClass( name );
106 }
107
108 /**
109 * Returns the <code>Class</code> object associated
110 * with the class or interface with the given string name,
111 * using the default class loader.
112 * <p>
113 * The <code>name</code> can denote an array class
114 * (see {@link Class#getName} for details).
115 * <p>
116 * This method can be used to obtain
117 * any of the <code>Class</code> objects
118 * representing <code>void</code> or primitive Java types:
119 * <code>char</code>, <code>byte</code>, <code>short</code>,
120 * <code>int</code>, <code>long</code>, <code>float</code>,
121 * <code>double</code> and <code>boolean</code>.
122 *
123 * @param name fully qualified name of the desired class
124 * @return class object representing the desired class
125 *
126 * @exception ClassNotFoundException if the class cannot be located
127 * by the specified class loader
128 *
129 * @see #resolveClass(String,ClassLoader)
130 */
131 public static Class resolveClass( String name ) throws ClassNotFoundException {
132 return resolveClass( name, null );
133 }
134
135 /**
136 * Returns the <code>Class</code> object associated with
137 * the class or interface with the given string name,
138 * using the given class loader.
139 * <p>
140 * The <code>name</code> can denote an array class
141 * (see {@link Class#getName} for details).
142 * <p>
143 * If the parameter <code>loader</code> is null,
144 * the class is loaded through the default class loader.
145 * <p>
146 * This method can be used to obtain
147 * any of the <code>Class</code> objects
148 * representing <code>void</code> or primitive Java types:
149 * <code>char</code>, <code>byte</code>, <code>short</code>,
150 * <code>int</code>, <code>long</code>, <code>float</code>,
151 * <code>double</code> and <code>boolean</code>.
152 *
153 * @param name fully qualified name of the desired class
154 * @param loader class loader from which the class must be loaded
155 * @return class object representing the desired class
156 *
157 * @exception ClassNotFoundException if the class cannot be located
158 * by the specified class loader
159 *
160 * @see #findClass(String,ClassLoader)
161 * @see PrimitiveTypeMap#getType(String)
162 */
163 public static Class resolveClass( String name, ClassLoader loader ) throws ClassNotFoundException {
164 Class type = PrimitiveTypeMap.getType( name );
165 return ( type == null )
166 ? findClass( name, loader )
167 : type;
168 }
169
170 /**
171 * Disable instantiation.
172 */
173 private ClassFinder() {
174 }
175}