net.sf.kdgcommons.lang
Class ClassUtil

java.lang.Object
  extended by net.sf.kdgcommons.lang.ClassUtil

public class ClassUtil
extends Object

Static utility methods for working with class instances. reflection to do their work.

Since:
1.0.4

Constructor Summary
ClassUtil()
           
 
Method Summary
static Method[] getAllMethods(Class<?> klass)
          Deprecated. - In retrospect, there are few if any good reasons for calling this method. getVisibleMethods(java.lang.Class) is the better choice.
static Method[] getAnnotatedMethods(Class<?> klass, Class<? extends Annotation> annotationKlass)
          Returns the visible methods defined by the class and its superclasses that have the specified annotation.
static Method getBestMethod(Class<?> klass, String methodName, Object... args)
          Returns the most appropriate method to invoke for the specified parameter values.
static Method[] getDeclaredMethodsByAccess(Class<?> klass, boolean isPublic, boolean isProtected, boolean isPrivate, boolean isDefault)
          Returns the declared methods of the specified class that have the desired access modifiers.
static Class<?> getPrimitiveType(Object val)
          Extracts the TYPE field value from a primitive wrapper type.
static HashMultimap<String,Method> getVisibleMethodMap(Class<?> klass)
          Returns a multi-map of the methods visible to the given class.
static Method[] getVisibleMethods(Class<?> klass)
          Returns all methods visible to the specified class.
static String internalNameToExternal(String name)
          Converts the "internal" name of a class (eg: "[Ljava/lang/String;") to its "external" representation (eg: "java.lang.String[]").
static boolean isOverridden(Method method, Method[] methods)
          Determines whether a given method is overridden by methods in the passed array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassUtil

public ClassUtil()
Method Detail

internalNameToExternal

public static String internalNameToExternal(String name)
Converts the "internal" name of a class (eg: "[Ljava/lang/String;") to its "external" representation (eg: "java.lang.String[]").


getPrimitiveType

public static Class<?> getPrimitiveType(Object val)
Extracts the TYPE field value from a primitive wrapper type. Returns null if the passed value is not a wrapper type.

This method is useful for reflective parameter matching.

Since:
1.0.9

getAllMethods

@Deprecated
public static Method[] getAllMethods(Class<?> klass)
Deprecated. - In retrospect, there are few if any good reasons for calling this method. getVisibleMethods(java.lang.Class) is the better choice.

Returns all methods defined by the class and its superclasses, without regard to access modifiers. Equivalent to recursively calling Class.getDeclaredMethods().


getDeclaredMethodsByAccess

public static Method[] getDeclaredMethodsByAccess(Class<?> klass,
                                                  boolean isPublic,
                                                  boolean isProtected,
                                                  boolean isPrivate,
                                                  boolean isDefault)
Returns the declared methods of the specified class that have the desired access modifiers. Note that this method does not look at superclass methods.

Since:
1.0.9

getVisibleMethods

public static Method[] getVisibleMethods(Class<?> klass)
Returns all methods visible to the specified class. This includes all methods declared by the class, as well as protected, package, and protected methods declared by the class' superclass.

The order of the returned methods is undefined.

Since:
1.0.9

getVisibleMethodMap

public static HashMultimap<String,Method> getVisibleMethodMap(Class<?> klass)
Returns a multi-map of the methods visible to the given class. The key of this map is the method name, the values are the various methods associated with that name.

This method examines all declared methods of the specified class, and the public, protected, and default-access methods declared by the class' ancestors. For overridden methods, the lowest (most subclassed) method is returned.

Since:
1.0.9

getAnnotatedMethods

public static Method[] getAnnotatedMethods(Class<?> klass,
                                           Class<? extends Annotation> annotationKlass)
Returns the visible methods defined by the class and its superclasses that have the specified annotation.


getBestMethod

public static Method getBestMethod(Class<?> klass,
                                   String methodName,
                                   Object... args)
Returns the most appropriate method to invoke for the specified parameter values. This method applies method selection rules described in JLS 15.12 (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12). If unable to find a single appropriate method, returns null.

Notes:

Parameters:
klass - The class to examine.
methodName - The name of the method.
args - Actual argument values for the method. See note above regarding the problems that null causes.
Since:
1.0.9

isOverridden

public static boolean isOverridden(Method method,
                                   Method[] methods)
Determines whether a given method is overridden by methods in the passed array.