junitx.ddtunit.util
Class ClassAnalyser

java.lang.Object
  extended by junitx.ddtunit.util.ClassAnalyser

public class ClassAnalyser
extends Object

This example is taken from Thinking in Java secd. ed. (p.680f) This class used reflection to show all the methods of a class, even if the methods are defined in the base class.

Author:
jgellien

Field Summary
static String CLASS_CONSTRUCTOR
          Constant to define constructor select in method findMethodByParams(String, String, Class[])
 
Method Summary
static String classPackage(Class<?> clazz)
          Extract package from class
static String classPackage(Object obj)
          Extract package from object instance
static Object createPrimitiveArray(Object value)
          Convert an array of object to its primitive counterpart.
static Method findMethodByName(String qualifiedClassName, String methodName)
          Display all methods of class quaifiedClassName to the configured appender specified by Log4j
static Object findMethodByParams(String className, String methodName, Class[] args)
          Find a Constructor/Method of class className by using the argument list args and try to vary arguments which could be of primitive type.
static String[] getAllMethods(String qualifiedClassName)
          Put all method names of the class qualifiedClassName into a String array and return it.
static Class getPrimitiveArrayBaseType(Class clazz)
           
static Class getPrimitiveClass(Class checkClass)
          Retrieve primitove type of specified clazz
static Field getSelectedField(String qualifiedClassName, String searchTerm)
          Search for exact match of searchTerm in the list of declared fields of the class qualifiedClassName.
static String getShortName(Class className)
          Extract simple class name without package information
static String getShortName(Object obj)
          Extract simple class name without package information
static String getShortName(String className)
          Extract simple class name without package information
static Set getSuperElements(Class clazz)
           
static boolean hasPrimitive(Class checkClass)
          Verify if provided clazz has primitive type
static void main(String[] args)
          The main program for the ShowMethods class
static void showAllFields(String qualifiedClassName)
          Display all attributes/fields of class quaifiedClassName to the configured appender specified by Log4j
static void showAllMethods(String qualifiedClassName)
          Display all methods of class quaifiedClassName to the configured appender specified by Log4j
static void showSelectedFields(String qualifiedClassName, String searchTerm)
          Display all attributes/fields of class quaifiedClassName matching searchTerm to the configured appender specified by Log4j
static void showSelectedMethods(String qualifiedClassName, String searchTerm)
          Display all constructors and methods of qualifiedClassName class which match with searchTerm.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_CONSTRUCTOR

public static final String CLASS_CONSTRUCTOR
Constant to define constructor select in method findMethodByParams(String, String, Class[])

See Also:
Constant Field Values
Method Detail

main

public static void main(String[] args)
The main program for the ShowMethods class

Parameters:
args - The command line arguments

getShortName

public static String getShortName(Object obj)
Extract simple class name without package information

Parameters:
obj - object to analyse
Returns:
class name

getShortName

public static String getShortName(Class className)
Extract simple class name without package information

Parameters:
className - to analyse
Returns:
name without package extension

getShortName

public static String getShortName(String className)
Extract simple class name without package information

Parameters:
className - qualified class name to analyse
Returns:
class name

showAllFields

public static void showAllFields(String qualifiedClassName)
Display all attributes/fields of class quaifiedClassName to the configured appender specified by Log4j

Parameters:
qualifiedClassName - name of class under analysis
Throws:
RuntimeException - ClassAnalyserException if an error occures

showSelectedFields

public static void showSelectedFields(String qualifiedClassName,
                                      String searchTerm)
Display all attributes/fields of class quaifiedClassName matching searchTerm to the configured appender specified by Log4j

Parameters:
qualifiedClassName - name of class under analysis
searchTerm - match string to check against
Throws:
RuntimeException - ClassAnalyserException if an error occures

getSelectedField

public static Field getSelectedField(String qualifiedClassName,
                                     String searchTerm)
Search for exact match of searchTerm in the list of declared fields of the class qualifiedClassName.

Parameters:
qualifiedClassName - Description of the Parameter
searchTerm - Description of the Parameter
Returns:
Field that was found or null if field does not exists
Throws:
RuntimeException - ClassAnalyserException if an error occures

showAllMethods

public static void showAllMethods(String qualifiedClassName)
Display all methods of class quaifiedClassName to the configured appender specified by Log4j

Parameters:
qualifiedClassName - name of class under analysis
Throws:
RuntimeException - ClassAnalyserException if an error occures

getAllMethods

public static String[] getAllMethods(String qualifiedClassName)
Put all method names of the class qualifiedClassName into a String array and return it.

Parameters:
qualifiedClassName - Description of the Parameter
Returns:
array of all method names of the class to analyse
Throws:
RuntimeException - ClassAnalyserException if an error occures

showSelectedMethods

public static void showSelectedMethods(String qualifiedClassName,
                                       String searchTerm)
Display all constructors and methods of qualifiedClassName class which match with searchTerm.
Output is set to Log4J Info level of this classes Logger.

Parameters:
qualifiedClassName - Name of class to analyse
searchTerm - Match term for methods to display
Throws:
RuntimeException - ClassAnalyserException if an error occures

findMethodByParams

public static Object findMethodByParams(String className,
                                        String methodName,
                                        Class[] args)
Find a Constructor/Method of class className by using the argument list args and try to vary arguments which could be of primitive type.
The args list only contains classes.

Parameters:
className -
methodName -
args -
Returns:
Method/Constructor object

findMethodByName

public static Method findMethodByName(String qualifiedClassName,
                                      String methodName)
Display all methods of class quaifiedClassName to the configured appender specified by Log4j

Parameters:
qualifiedClassName - name of class under analysis
methodName - to search for
Throws:
RuntimeException - ClassAnalyserException if an error occures

getSuperElements

public static Set getSuperElements(Class clazz)

getPrimitiveClass

public static Class getPrimitiveClass(Class checkClass)
Retrieve primitove type of specified clazz

Parameters:
checkClass - to retrieve associated primitive type from
Returns:
associated primitive type or null if non exists

createPrimitiveArray

public static Object createPrimitiveArray(Object value)
Convert an array of object to its primitive counterpart. Values of null will be replaced by the primitive default value.

Parameters:
value - - array of Object to convert
Returns:
array of primitive counterpart

getPrimitiveArrayBaseType

public static Class getPrimitiveArrayBaseType(Class clazz)
Parameters:
clazz -
Returns:
primitive clazz

hasPrimitive

public static boolean hasPrimitive(Class checkClass)
Verify if provided clazz has primitive type

Parameters:
checkClass - to look for associated primitive type
Returns:
true if primitive type is found

classPackage

public static String classPackage(Object obj)
Extract package from object instance

Returns:
package name of object instance

classPackage

public static String classPackage(Class<?> clazz)
Extract package from class

Returns:
package name of class


Copyright © 2004-2011. All Rights Reserved.