Path: blob/master/src/java.desktop/share/classes/com/sun/beans/finder/ClassFinder.java
41161 views
/*1* Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.beans.finder;2526import static sun.reflect.misc.ReflectUtil.checkPackageAccess;2728/**29* This is utility class that provides {@code static} methods30* to find a class with the specified name using the specified class loader.31*32* @since 1.733*34* @author Sergey A. Malenkov35*/36public final class ClassFinder {3738/**39* Returns the {@code Class} object associated40* with the class or interface with the given string name,41* using the default class loader.42* <p>43* The {@code name} can denote an array class44* (see {@link Class#getName} for details).45*46* @param name fully qualified name of the desired class47* @return class object representing the desired class48*49* @throws ClassNotFoundException if the class cannot be located50* by the specified class loader51*52* @see Class#forName(String)53* @see Class#forName(String,boolean,ClassLoader)54* @see ClassLoader#getSystemClassLoader()55* @see Thread#getContextClassLoader()56*/57public static Class<?> findClass(String name) throws ClassNotFoundException {58checkPackageAccess(name);59try {60ClassLoader loader = Thread.currentThread().getContextClassLoader();61if (loader == null) {62// can be null in IE (see 6204697)63loader = ClassLoader.getSystemClassLoader();64}65if (loader != null) {66return Class.forName(name, false, loader);67}6869} catch (ClassNotFoundException exception) {70// use current class loader instead71} catch (SecurityException exception) {72// use current class loader instead73}74return Class.forName(name);75}7677/**78* Returns the {@code Class} object associated with79* the class or interface with the given string name,80* using the given class loader.81* <p>82* The {@code name} can denote an array class83* (see {@link Class#getName} for details).84* <p>85* If the parameter {@code loader} is null,86* the class is loaded through the default class loader.87*88* @param name fully qualified name of the desired class89* @param loader class loader from which the class must be loaded90* @return class object representing the desired class91*92* @throws ClassNotFoundException if the class cannot be located93* by the specified class loader94*95* @see #findClass(String,ClassLoader)96* @see Class#forName(String,boolean,ClassLoader)97*/98public static Class<?> findClass(String name, ClassLoader loader) throws ClassNotFoundException {99checkPackageAccess(name);100if (loader != null) {101try {102return Class.forName(name, false, loader);103} catch (ClassNotFoundException exception) {104// use default class loader instead105} catch (SecurityException exception) {106// use default class loader instead107}108}109return findClass(name);110}111112/**113* Returns the {@code Class} object associated114* with the class or interface with the given string name,115* using the default class loader.116* <p>117* The {@code name} can denote an array class118* (see {@link Class#getName} for details).119* <p>120* This method can be used to obtain121* any of the {@code Class} objects122* representing {@code void} or primitive Java types:123* {@code char}, {@code byte}, {@code short},124* {@code int}, {@code long}, {@code float},125* {@code double} and {@code boolean}.126*127* @param name fully qualified name of the desired class128* @return class object representing the desired class129*130* @throws ClassNotFoundException if the class cannot be located131* by the specified class loader132*133* @see #resolveClass(String,ClassLoader)134*/135public static Class<?> resolveClass(String name) throws ClassNotFoundException {136return resolveClass(name, null);137}138139/**140* Returns the {@code Class} object associated with141* the class or interface with the given string name,142* using the given class loader.143* <p>144* The {@code name} can denote an array class145* (see {@link Class#getName} for details).146* <p>147* If the parameter {@code loader} is null,148* the class is loaded through the default class loader.149* <p>150* This method can be used to obtain151* any of the {@code Class} objects152* representing {@code void} or primitive Java types:153* {@code char}, {@code byte}, {@code short},154* {@code int}, {@code long}, {@code float},155* {@code double} and {@code boolean}.156*157* @param name fully qualified name of the desired class158* @param loader class loader from which the class must be loaded159* @return class object representing the desired class160*161* @throws ClassNotFoundException if the class cannot be located162* by the specified class loader163*164* @see #findClass(String,ClassLoader)165* @see PrimitiveTypeMap#getType(String)166*/167public static Class<?> resolveClass(String name, ClassLoader loader) throws ClassNotFoundException {168Class<?> type = PrimitiveTypeMap.getType(name);169return (type == null)170? findClass(name, loader)171: type;172}173174/**175* Disable instantiation.176*/177private ClassFinder() {178}179}180181182