javautils
Class Classes

java.lang.Object
  |
  +--javautils.Classes

public class Classes
extends java.lang.Object

Static utility methods for dealing with classes and reflection.

Note: None of these methods throws checked exceptions. Checked exceptions will be translated to unchecked RuntimeException-exceptions. This makes these methods far more pleasant to use compared to direct use of the Class-class.


Constructor Summary
Classes()
           
 
Method Summary
static java.lang.Class asObjectType(java.lang.Class type)
          If the given type is a primitive type (say int.class), returns the corresponding object type (say Integer.class).
static java.lang.Class forName(java.lang.String name)
           return Class.forName(name);
static java.lang.String getBaseName(java.lang.Class cls)
          Returns the last part (the part after the last dot) of the type name.
static java.lang.reflect.Field getField(java.lang.Class cls, java.lang.String fieldName)
           return cls.getField(cls, fieldName);
static java.lang.Object getFieldValue(java.lang.Object obj, java.lang.String fieldName)
           return obj.getClass().getField(fieldName).get(obj);
static java.lang.reflect.Method getMethod(java.lang.Class cls, java.lang.String methodName)
           return getMethod(cls, methodName, null);
static java.lang.reflect.Method getMethod(java.lang.Class cls, java.lang.String methodName, java.lang.Class[] argTypes)
           return cls.getMethod(methodName, argTypes);
static java.lang.reflect.Method getMethod(java.lang.Object obj, java.lang.String methodName, java.lang.Class[] argTypes)
           return getMethod(obj.getClass(), methodName, argTypes);
static java.lang.Object getStaticFieldValue(java.lang.Class cls, java.lang.String fieldName)
           return cls.getField(fieldName).get(null);
static java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object self, java.lang.Object[] args)
           return method.invoke(self, args);
static java.lang.Object newInstance(java.lang.Class cls)
           return cls.newInstance();
static java.lang.Object newInstance(java.lang.Class cls, java.lang.Class[] paramTypes, java.lang.Object[] params)
           return cls.getConstructor(paramTypes).newInstance(params);
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Classes

public Classes()
Method Detail

asObjectType

public static java.lang.Class asObjectType(java.lang.Class type)

If the given type is a primitive type (say int.class), returns the corresponding object type (say Integer.class). Otherwise returns the given type.


forName

public static java.lang.Class forName(java.lang.String name)
 return Class.forName(name);
 


getBaseName

public static java.lang.String getBaseName(java.lang.Class cls)

Returns the last part (the part after the last dot) of the type name.


getField

public static java.lang.reflect.Field getField(java.lang.Class cls,
                                               java.lang.String fieldName)
 return cls.getField(cls, fieldName);
 


getFieldValue

public static java.lang.Object getFieldValue(java.lang.Object obj,
                                             java.lang.String fieldName)
 return obj.getClass().getField(fieldName).get(obj);
 


getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class cls,
                                                 java.lang.String methodName)
 return getMethod(cls, methodName, null);
 


getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class cls,
                                                 java.lang.String methodName,
                                                 java.lang.Class[] argTypes)
 return cls.getMethod(methodName, argTypes);
 


getMethod

public static java.lang.reflect.Method getMethod(java.lang.Object obj,
                                                 java.lang.String methodName,
                                                 java.lang.Class[] argTypes)
 return getMethod(obj.getClass(), methodName, argTypes);
 


getStaticFieldValue

public static java.lang.Object getStaticFieldValue(java.lang.Class cls,
                                                   java.lang.String fieldName)
 return cls.getField(fieldName).get(null);
 


invoke

public static java.lang.Object invoke(java.lang.reflect.Method method,
                                      java.lang.Object self,
                                      java.lang.Object[] args)
 return method.invoke(self, args);
 


newInstance

public static java.lang.Object newInstance(java.lang.Class cls)
 return cls.newInstance();
 


newInstance

public static java.lang.Object newInstance(java.lang.Class cls,
                                           java.lang.Class[] paramTypes,
                                           java.lang.Object[] params)
 return cls.getConstructor(paramTypes).newInstance(params);