001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.beans.finder;
026:
027: /**
028: * This is utility class that provides <code>static</code> methods
029: * to find a class with the specified name using the specified class loader.
030: *
031: * @since 1.7
032: *
033: * @version 1.8 05/05/07
034: * @author Sergey A. Malenkov
035: */
036: public final class ClassFinder {
037: /**
038: * Returns the <code>Class</code> object associated
039: * with the class or interface with the given string name,
040: * using the default class loader.
041: * <p>
042: * The <code>name</code> can denote an array class
043: * (see {@link Class#getName} for details).
044: *
045: * @param name fully qualified name of the desired class
046: * @return class object representing the desired class
047: *
048: * @exception ClassNotFoundException if the class cannot be located
049: * by the specified class loader
050: *
051: * @see Class#forName(String)
052: * @see Class#forName(String,boolean,ClassLoader)
053: * @see ClassLoader#getSystemClassLoader()
054: * @see Thread#getContextClassLoader()
055: */
056: public static Class findClass(String name)
057: throws ClassNotFoundException {
058: try {
059: ClassLoader loader = Thread.currentThread()
060: .getContextClassLoader();
061: if (loader == null) {
062: // can be null in IE (see 6204697)
063: loader = ClassLoader.getSystemClassLoader();
064: }
065: if (loader != null) {
066: return Class.forName(name, false, loader);
067: }
068:
069: } catch (ClassNotFoundException exception) {
070: // use current class loader instead
071: } catch (SecurityException exception) {
072: // use current class loader instead
073: }
074: return Class.forName(name);
075: }
076:
077: /**
078: * Returns the <code>Class</code> object associated with
079: * the class or interface with the given string name,
080: * using the given class loader.
081: * <p>
082: * The <code>name</code> can denote an array class
083: * (see {@link Class#getName} for details).
084: * <p>
085: * If the parameter <code>loader</code> is null,
086: * the class is loaded through the default class loader.
087: *
088: * @param name fully qualified name of the desired class
089: * @param loader class loader from which the class must be loaded
090: * @return class object representing the desired class
091: *
092: * @exception ClassNotFoundException if the class cannot be located
093: * by the specified class loader
094: *
095: * @see #findClass(String,ClassLoader)
096: * @see Class#forName(String,boolean,ClassLoader)
097: */
098: public static Class findClass(String name, ClassLoader loader)
099: throws ClassNotFoundException {
100: if (loader != null) {
101: try {
102: return Class.forName(name, false, loader);
103: } catch (ClassNotFoundException exception) {
104: // use default class loader instead
105: } catch (SecurityException exception) {
106: // use default class loader instead
107: }
108: }
109: return findClass(name);
110: }
111:
112: /**
113: * Returns the <code>Class</code> object associated
114: * with the class or interface with the given string name,
115: * using the default class loader.
116: * <p>
117: * The <code>name</code> can denote an array class
118: * (see {@link Class#getName} for details).
119: * <p>
120: * This method can be used to obtain
121: * any of the <code>Class</code> objects
122: * representing <code>void</code> or primitive Java types:
123: * <code>char</code>, <code>byte</code>, <code>short</code>,
124: * <code>int</code>, <code>long</code>, <code>float</code>,
125: * <code>double</code> and <code>boolean</code>.
126: *
127: * @param name fully qualified name of the desired class
128: * @return class object representing the desired class
129: *
130: * @exception ClassNotFoundException if the class cannot be located
131: * by the specified class loader
132: *
133: * @see #resolveClass(String,ClassLoader)
134: */
135: public static Class resolveClass(String name)
136: throws ClassNotFoundException {
137: return resolveClass(name, null);
138: }
139:
140: /**
141: * Returns the <code>Class</code> object associated with
142: * the class or interface with the given string name,
143: * using the given class loader.
144: * <p>
145: * The <code>name</code> can denote an array class
146: * (see {@link Class#getName} for details).
147: * <p>
148: * If the parameter <code>loader</code> is null,
149: * the class is loaded through the default class loader.
150: * <p>
151: * This method can be used to obtain
152: * any of the <code>Class</code> objects
153: * representing <code>void</code> or primitive Java types:
154: * <code>char</code>, <code>byte</code>, <code>short</code>,
155: * <code>int</code>, <code>long</code>, <code>float</code>,
156: * <code>double</code> and <code>boolean</code>.
157: *
158: * @param name fully qualified name of the desired class
159: * @param loader class loader from which the class must be loaded
160: * @return class object representing the desired class
161: *
162: * @exception ClassNotFoundException if the class cannot be located
163: * by the specified class loader
164: *
165: * @see #findClass(String,ClassLoader)
166: * @see PrimitiveTypeMap#getType(String)
167: */
168: public static Class resolveClass(String name, ClassLoader loader)
169: throws ClassNotFoundException {
170: Class type = PrimitiveTypeMap.getType(name);
171: return (type == null) ? findClass(name, loader) : type;
172: }
173:
174: /**
175: * Disable instantiation.
176: */
177: private ClassFinder() {
178: }
179: }
|