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

Save This Page
Home » openjdk-7 » com.sun » beans » finder » [javadoc | source]