View Javadoc

1   /***
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * For more information visit
11   *         http://72miles.com and
12   *         http://architecturerules.googlecode.com/svn/docs/index.html
13   */
14  
15  package org.seventytwomiles.springframework.util;
16  
17  
18  /***
19   * <p>Miscellaneous class utility methods. Mainly for internal use within the
20   * spring framework; consider Jakarta's Commons Lang for a more comprehensive
21   * suite of class utilities.</p>
22   *
23   * <p>This was extracted from Spring in order to remove the dependency on apache
24   * commons-io. <a href="http://code.google.com/p/architecturerules/issues/detail?id=2&can=1">
25   * issue 2 (remove unnecessary dependencies)</a>/p>
26   *
27   * @author Keith Donald
28   * @author Rob Harrop
29   * @author Juergen Hoeller
30   */
31  public class ClassUtils {
32  
33  
34      /***
35       * <p>Return a default <code>ClassLoader</code> to use (never
36       * <code>null</code>). Returns the thread context ClassLoader, if available.
37       * The ClassLoader that loaded the ClassUtils class will be used as
38       * fallback. <p>Call this method if you intend to use the thread context
39       * ClassLoader in a scenario where you absolutely need a non-null
40       * ClassLoader reference: for example, for class path resource loading (but
41       * not necessarily for <code>Class.forName</code>, which accepts a
42       * <code>null</code> ClassLoader reference as well).</p>
43       *
44       * @return ClassLoader
45       * @see java.lang.Thread#getContextClassLoader()
46       */
47      public static ClassLoader getDefaultClassLoader() {
48  
49          ClassLoader classLoader = Thread.currentThread()
50                  .getContextClassLoader();
51  
52          // No thread context class loader -> use class loader of this class.
53          if (classLoader == null)
54              classLoader = ClassUtils.class.getClassLoader();
55  
56          return classLoader;
57      }
58  }