org.apache.myfaces.extensions.cdi.core.api.util
Class ClassUtils

java.lang.Object
  extended by org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils

@Typed
public abstract class ClassUtils
extends Object

Util methods for classes, ClassLoader and Manifest handling

Author:
Gerhard Petracek

Method Summary
static ClassLoader getClassLoader(Object o)
          Detect the right ClassLoader.
static String getJarVersion(Class targetClass)
          Reads the version of the jar which contains the given class
static String getRevision(Class targetClass)
          Reads the VCS revision which was used for creating the jar
static Object instantiateClassForName(String className)
          Creates an instance for the given class-name
static Class loadClassForName(String name)
          Loads class for the given name
static
<T> T
tryToInstantiateClass(Class<T> targetClass)
          Instantiates a given class via the default constructor
static Object tryToInstantiateClassForName(String className)
          Tries to instantiate a class for the given name via the default constructor
static
<T> T
tryToInstantiateClassForName(String className, Class<T> targetType)
          Tries to instantiate a class for the given name and type via the default constructor
static Class tryToLoadClassForName(String name)
          Tries to load a class based on the given name
static
<T> Class<T>
tryToLoadClassForName(String name, Class<T> targetType)
          Tries to load a class based on the given name and interface or abstract class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getClassLoader

public static ClassLoader getClassLoader(Object o)
Detect the right ClassLoader. The lookup order is determined by:
  1. ContextClassLoader of the current Thread
  2. ClassLoader of the given Object 'o'
  3. ClassLoader of this very CodiUtils class

Parameters:
o - if not null it may get used to detect the classloader.
Returns:
The ClassLoader which should get used to create new instances

tryToLoadClassForName

public static <T> Class<T> tryToLoadClassForName(String name,
                                                 Class<T> targetType)
Tries to load a class based on the given name and interface or abstract class.

Type Parameters:
T - current type
Parameters:
name - name of the concrete class
targetType - target type (interface or abstract class)
Returns:
loaded class or null if it isn't in the classpath

tryToLoadClassForName

public static Class tryToLoadClassForName(String name)
Tries to load a class based on the given name

Parameters:
name - name of the class
Returns:
loaded class or null if it isn't in the classpath

loadClassForName

public static Class loadClassForName(String name)
                              throws ClassNotFoundException
Loads class for the given name

Parameters:
name - name of the class
Returns:
loaded class
Throws:
ClassNotFoundException - if the class can't be loaded

tryToInstantiateClass

public static <T> T tryToInstantiateClass(Class<T> targetClass)
Instantiates a given class via the default constructor

Type Parameters:
T - current type
Parameters:
targetClass - class which should be instantiated
Returns:
created instance or null if the instantiation failed

tryToInstantiateClassForName

public static <T> T tryToInstantiateClassForName(String className,
                                                 Class<T> targetType)
Tries to instantiate a class for the given name and type via the default constructor

Type Parameters:
T - current type
Parameters:
className - name of the class
targetType - target type
Returns:
created instance or null if the instantiation failed

tryToInstantiateClassForName

public static Object tryToInstantiateClassForName(String className)
Tries to instantiate a class for the given name via the default constructor

Parameters:
className - name of the class
Returns:
created instance or null if the instantiation failed

instantiateClassForName

public static Object instantiateClassForName(String className)
                                      throws ClassNotFoundException,
                                             IllegalAccessException,
                                             InstantiationException
Creates an instance for the given class-name

Parameters:
className - name of the class which should be instantiated
Returns:
created instance
Throws:
ClassNotFoundException - if the instantiation failed
IllegalAccessException - if the instantiation failed
InstantiationException - if the instantiation failed

getJarVersion

public static String getJarVersion(Class targetClass)
Reads the version of the jar which contains the given class

Parameters:
targetClass - class within the jar
Returns:
version-string which has been found in the manifest or null if there is no version information available

getRevision

public static String getRevision(Class targetClass)
Reads the VCS revision which was used for creating the jar

Parameters:
targetClass - class within the jar
Returns:
revision-string which has been found in the manifest or null if there is no information available


Copyright © 2010-2011 The Apache Software Foundation. All Rights Reserved.