com.cloudera.lib.lang
Class ClassUtils

java.lang.Object
  extended by com.cloudera.lib.lang.ClassUtils

public class ClassUtils
extends Object

Class related utilities.


Constructor Summary
ClassUtils()
           
 
Method Summary
static void createJar(File jarFile, Class... classes)
          Creates a JAR file with the specified classes.
static void createJar(OutputStream os, Class... classes)
          Writes the specified classes to an outputstream.
static Object findConstant(String className, String constantName)
          Finds a constant by name in a class.
static Method findMethod(String className, String methodName)
          Finds a public-static method by name in a class.
static String getJar(Class klass)
          Finds the JAR file containing a class.
static InputStream getResource(String name)
          Convenience method that returns a resource as inputstream from the classpath.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassUtils

public ClassUtils()
Method Detail

getJar

public static String getJar(Class klass)
Finds the JAR file containing a class.

Parameters:
klass - class to find its JAR.
Returns:
the path to the JAR.

getResource

public static InputStream getResource(String name)
Convenience method that returns a resource as inputstream from the classpath.

It first attempts to use the Thread's context classloader and if not set it uses the ClassUtils classloader.

Parameters:
name - resource to retrieve.
Returns:
inputstream with the resource, NULL if the resource does not exist.

createJar

public static void createJar(File jarFile,
                             Class... classes)
                      throws IOException
Creates a JAR file with the specified classes.

Parameters:
jarFile - jar file path.
classes - classes to add to the JAR.
Throws:
IOException - thrown if an IO error occurred.

createJar

public static void createJar(OutputStream os,
                             Class... classes)
                      throws IOException
Writes the specified classes to an outputstream.

Parameters:
os - outputstream to write the classes to.
classes - classes to write to the outputstream.
Throws:
IOException - thrown if an IO error occurred.

findMethod

public static Method findMethod(String className,
                                String methodName)
Finds a public-static method by name in a class.

In case of method overloading it will return the first method found.

Parameters:
className - name to look for the method.
methodName - method name to look in the class.
Returns:
the Method instance.
Throws:
IllegalArgumentException - thrown if the method does not exist, it is not public or it is not static.

findConstant

public static Object findConstant(String className,
                                  String constantName)
                           throws ServiceException
Finds a constant by name in a class.

Parameters:
className - name to look for the method.
constantName - constant name to look in the class.
Returns:
the constant instance.
Throws:
IllegalArgumentException - thrown if the constant does not exist, it is not public or it is not static.
ServiceException


Copyright © 2012 Cloudera. All Rights Reserved.