org.apache.velocity.tools

## Class ClassUtils

- java.lang.Object
- - org.apache.velocity.tools.ClassUtils

    public class ClassUtils
    extends Object

Repository for common class and reflection methods.

Version:
  $Id: ClassUtils.java 511959 2007-02-26 19:24:39Z nbubna $
Author:
  Nathan Bubna

### Method Summary

All Methods Static Methods Concrete Methods
`static Method` `findDeclaredMethod(Class clazz, String name, Class... params)` Find a declared method in a class.
`static Method` `findFactoryMethod(Class factory, Class target)` Given a factory class and a target class, search for the following methods:
`static Method` `findGetter(String getterName, Class clazz)`
`static Method` `findGetter(String getterName, Class clazz, boolean mandatory)`
`static Method` `findMethod(Class clazz, String name, Class... params)` Find a callable method in a class
`static Method` `findSetter(String setterName, Class clazz)`
`static Method` `findSetter(String setterName, Class clazz, Predicate<Class> argumentClassFilter)`
`static Method` `findSetter(String setterName, Class clazz, Predicate<Class> argumentClassFilter, boolean mandatory)`
`static Class` `getClass(String name)` Load a class with a given name.
`static Object` `getFieldValue(Class clazz, String fieldname)` Given a class and a static field name, get the field value.
`static Object` `getFieldValue(String fieldPath)` Given a static field path, aka classname.field, get the field value.
`static Object` `getInstance(String classname)` Get an instance of a named class.
`static Iterator` `getIterator(Object obj)` Retrieves an Iterator from or creates and Iterator for the specified object.
`static URL` `getResource(String name, Object caller)` Load a given resource.
`static InputStream` `getResourceAsStream(String name, Object caller)` This is a convenience method to load a resource as a stream.
`static List<URL>` `getResources(String name, Object caller)` Load all resources with the specified name.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### getClass

    public static Class getClass(String name)
                          throws ClassNotFoundException

Load a class with a given name. It will try to load the class in the following order:

- From `Thread`.currentThread().getContextClassLoader()
- Using the basic `Class.forName(java.lang.String)`
- From `ClassUtils`.class.getClassLoader()

Parameters:
  `name` - Fully qualified class name to be loaded
Returns:
  Class object
Throws:
  `ClassNotFoundException` - if the class cannot be found

#### getInstance

    public static Object getInstance(String classname)
                              throws ClassNotFoundException,
                                     IllegalAccessException,
                                     InstantiationException

Get an instance of a named class.

Parameters:
  `classname` - class name
Returns:
  new class instance
Throws:
  `ClassNotFoundException` - if class is not found
  `IllegalAccessException` - if not granted
  `InstantiationException` - if instance creation throwed

#### getResources

    public static List<URL> getResources(String name,
                                         Object caller)

Load all resources with the specified name. If none are found, we prepend the name with '/' and try again. This will attempt to load the resources from the following methods (in order):

- Thread.currentThread().getContextClassLoader().getResources(name)
- `ClassUtils`.class.getClassLoader().getResources(name)
- `ClassUtils`.class.getResource(name)
- `getCallerLoader(Object caller)`.getResources(name)
- caller.getClass().getResource(name)

Parameters:
  `name` - The name of the resources to load
  `caller` - The instance or `Class` calling this method
Returns:
  the list of found resources

#### getResource

    public static URL getResource(String name,
                                  Object caller)

Load a given resource. This method will try to load the resource using the following methods (in order):

- Thread.currentThread().getContextClassLoader().getResource(name)
- `ClassUtils`.class.getClassLoader().getResource(name)
- `ClassUtils`.class.getResource(name)
- caller.getClass().getResource(name) or, if caller is a Class, caller.getResource(name)

Parameters:
  `name` - The name of the resource to load
  `caller` - The instance or `Class` calling this method
Returns:
  the found URL, or null if not found

#### getResourceAsStream

    public static InputStream getResourceAsStream(String name,
                                                  Object caller)

This is a convenience method to load a resource as a stream. The algorithm used to find the resource is given in getResource()

Parameters:
  `name` - The name of the resource to load
  `caller` - The instance or `Class` calling this method
Returns:
  the resource input stream or null if not found

#### findMethod

    public static Method findMethod(Class clazz,
                                    String name,
                                    Class... params)
                             throws SecurityException

Find a callable method in a class

Parameters:
  `clazz` - target class
  `name` - method name
  `params` - method arguments classes
Returns:
  method object
Throws:
  `SecurityException` - if not granted

#### findDeclaredMethod

    public static Method findDeclaredMethod(Class clazz,
                                            String name,
                                            Class... params)
                                     throws SecurityException

Find a declared method in a class. It will be made accessible if needed and allowed.

Parameters:
  `clazz` - target class
  `name` - method name
  `params` - method arguments classes
Returns:
Throws:
  `SecurityException` - if not allowed

#### getFieldValue

    public static Object getFieldValue(String fieldPath)
                                throws ClassNotFoundException,
                                       NoSuchFieldException,
                                       SecurityException,
                                       IllegalAccessException

Given a static field path, aka classname.field, get the field value.

Parameters:
  `fieldPath` - field path
Returns:
  field value
Throws:
  `ClassNotFoundException` - if class hasn't been found
  `NoSuchFieldException` - if field hasn't been found
  `SecurityException` - if not granted
  `IllegalAccessException` - if field is not accessible

#### getFieldValue

    public static Object getFieldValue(Class clazz,
                                       String fieldname)
                                throws NoSuchFieldException,
                                       SecurityException,
                                       IllegalAccessException

Given a class and a static field name, get the field value.

Parameters:
  `clazz` - target class
  `fieldname` - field name
Returns:
  field value
Throws:
  `NoSuchFieldException` - if field hasn't been found
  `SecurityException` - if not granted
  `IllegalAccessException` - if field is not accessible

#### getIterator

    public static Iterator getIterator(Object obj)
                                throws NoSuchMethodException,
                                       IllegalAccessException,
                                       InvocationTargetException

Retrieves an Iterator from or creates and Iterator for the specified object. This method is almost entirely copied from Engine's UberspectImpl class.

Parameters:
  `obj` - the target obj
Returns:
  an iterator over the content of obj, or null if not found
Throws:
  `NoSuchMethodException` - if no iterator() method
  `IllegalAccessException` - if iterator() method not callable
  `InvocationTargetException` - if iterator() method throwed

#### findFactoryMethod

    public static Method findFactoryMethod(Class factory,
                                           Class target)

Given a factory class and a target class, search for the following methods:

- `createTargetClassname()`,
- `newTargetClassname()`, or
- `getTargetClassname()`.

Parameters:
  `factory` - factory class
  `target` - target class
Returns:
  first factory method found, or null otherwise

#### findGetter

    public static Method findGetter(String getterName,
                                    Class clazz)
                             throws NoSuchMethodException

Throws:
  `NoSuchMethodException`

#### findGetter

    public static Method findGetter(String getterName,
                                    Class clazz,
                                    boolean mandatory)
                             throws NoSuchMethodException

Throws:
  `NoSuchMethodException`

#### findSetter

    public static Method findSetter(String setterName,
                                    Class clazz)
                             throws NoSuchMethodException

Throws:
  `NoSuchMethodException`

#### findSetter

    public static Method findSetter(String setterName,
                                    Class clazz,
                                    Predicate<Class> argumentClassFilter)
                             throws NoSuchMethodException

Throws:
  `NoSuchMethodException`

#### findSetter

    public static Method findSetter(String setterName,
                                    Class clazz,
                                    Predicate<Class> argumentClassFilter,
                                    boolean mandatory)
                             throws NoSuchMethodException

Throws:
  `NoSuchMethodException`