org.progeeks.util
Class Inspector

java.lang.Object
  extended by org.progeeks.util.Inspector

public class Inspector
extends java.lang.Object

A utility for doing simple Bean-like reflection.

Since:
1.0
Version:
$Revision: 1.49 $
Author:
Paul Speed

Field Summary
protected  java.lang.Object object
          The object upon which inspection is done.
protected  java.lang.Class type
          The class of the object upon which inspection is done.
 
Constructor Summary
Inspector()
          Creates a new Inspector object with no internal object.
Inspector(java.lang.Object object)
          Creates a new Inspector object with the specified internal object.
 
Method Summary
static boolean areTypesCompatible(java.lang.Class[] targets, java.lang.Class[] sources)
          Returns true if all classes in the sources list are assignment compatible with the targets list.
 java.lang.Object callMethod(java.lang.String name)
           
 java.lang.Object callMethod(java.lang.String name, java.lang.Object[] params)
           
 java.lang.Object callMethod(java.lang.String name, java.lang.String param)
           
static java.lang.Object constructFromString(java.lang.String value, java.lang.Class type)
          Attempts to create an object of the specified type using the specified String value.
static java.lang.String encodeAsString(java.lang.Object value)
          Attempts to encode an object as a string using the specified object value.
protected static java.lang.reflect.Constructor findConstructor(java.lang.Class type, java.lang.Class[] argTypes)
          Searches for a compatible constructor for the specified arg types using a widening scope.
 java.lang.Object get(java.lang.String name)
          Returns the value of the specified property.
static java.lang.Class getClassForName(java.lang.String name)
          Returns a Class for the specified class name or null if the specified class could not be found in the context class loader.
static java.lang.Object getConstant(java.lang.String name, java.lang.Class type)
          Searches the specified type for a constant denoted by name.
static java.lang.Object getDefaultPrimitiveValue(java.lang.Class primitive)
          Returns the Object version of the Java initial value for the specified primitive type.
 java.lang.Class getDynamicType()
          Returns the type for dynamic properties.
 java.lang.Class getMutableType(java.lang.String name)
          Returns the type of the specified mutable property.
 java.lang.Object getObject()
          Returns the object upon which inspection is done.
 java.lang.Class getType(java.lang.String name)
          Returns the type of the specified property.
 boolean hasAccessor(java.lang.String name)
          Returns true if the contained object has a get method for the specified field.
static boolean hasConstructor(java.lang.Class type, java.lang.Class[] argTypes)
           
 boolean hasMethod(java.lang.String name)
           
 boolean hasMethod(java.lang.String name, java.lang.Class[] paramTypes)
           
 boolean hasMutator(java.lang.String name)
          Returns true if the contained object has a set method for the specified field that matches the return type for the associated accessor method ("get").
 boolean hasMutator(java.lang.String name, java.lang.Class type)
          Returns true if the contained object has a set method for the specified field and type.
static boolean hasOverloadedConstructor(java.lang.Class type, java.lang.Class[] argTypes)
          Returns true if the specified class has constructor matching the specified arg types using a widening scope.
static java.lang.Object newInstance(java.lang.Class type)
          Creates a new object of the specified class using the no-argument constructor.
static java.lang.Object newInstance(java.lang.Class type, java.lang.Object[] args)
          Creates a new object of the specified class using the constructor that takes the specified parameters.
static java.lang.Object newInstance(java.lang.Class type, java.lang.Object[] args, java.lang.Class[] argTypes)
          Creates a new object of the specified class using the constructor that takes the specified parameters of the specified types.
static java.lang.Object newInstance(java.lang.String type)
          Creates a new object of the specified class name using the no-argument constructor.
static java.lang.Object newInstance(java.lang.String className, java.lang.Object[] args, java.lang.Class[] argTypes)
          Creates a new object of the specified class by converting the class name to a class object and using the constructor that takes the specified parameters of the specified types.
 void set(java.lang.String name, java.lang.Object value)
          Sets the value of the specified property.
 void set(java.lang.String name, java.lang.Object value, java.lang.Class type)
          Sets the value of the specified property using a specifically overloaded mutator method.
 void setObject(java.lang.Object object)
          Sets the object upon which inspection will be performed.
static java.lang.Class translateFromPrimitive(java.lang.Class primitive)
          If this specified class represents a primitive type (int, float, etc.) then it is translated into its wrapper type (Integer, Float, etc.).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

object

protected java.lang.Object object
The object upon which inspection is done.


type

protected java.lang.Class type
The class of the object upon which inspection is done.

Constructor Detail

Inspector

public Inspector()
Creates a new Inspector object with no internal object.


Inspector

public Inspector(java.lang.Object object)
Creates a new Inspector object with the specified internal object.

Method Detail

translateFromPrimitive

public static java.lang.Class translateFromPrimitive(java.lang.Class primitive)
If this specified class represents a primitive type (int, float, etc.) then it is translated into its wrapper type (Integer, Float, etc.). If the passed class is not a primitive then it is just returned.


getDefaultPrimitiveValue

public static java.lang.Object getDefaultPrimitiveValue(java.lang.Class primitive)
Returns the Object version of the Java initial value for the specified primitive type. For example, int = Integer(0), long = Long(0), boolean = Boolean.FALSE, and so on. Returns null for anything non-primitive.


getClassForName

public static java.lang.Class getClassForName(java.lang.String name)
Returns a Class for the specified class name or null if the specified class could not be found in the context class loader. This method takes into account the primitive types "boolean", "double", etc. and will translate the strings appropriately.


newInstance

public static java.lang.Object newInstance(java.lang.Class type)
Creates a new object of the specified class using the no-argument constructor.


newInstance

public static java.lang.Object newInstance(java.lang.String type)
Creates a new object of the specified class name using the no-argument constructor.


newInstance

public static java.lang.Object newInstance(java.lang.Class type,
                                           java.lang.Object[] args)
Creates a new object of the specified class using the constructor that takes the specified parameters.


newInstance

public static java.lang.Object newInstance(java.lang.Class type,
                                           java.lang.Object[] args,
                                           java.lang.Class[] argTypes)
Creates a new object of the specified class using the constructor that takes the specified parameters of the specified types.


newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           java.lang.Object[] args,
                                           java.lang.Class[] argTypes)
Creates a new object of the specified class by converting the class name to a class object and using the constructor that takes the specified parameters of the specified types.


hasConstructor

public static boolean hasConstructor(java.lang.Class type,
                                     java.lang.Class[] argTypes)

hasOverloadedConstructor

public static boolean hasOverloadedConstructor(java.lang.Class type,
                                               java.lang.Class[] argTypes)
Returns true if the specified class has constructor matching the specified arg types using a widening scope.


findConstructor

protected static java.lang.reflect.Constructor findConstructor(java.lang.Class type,
                                                               java.lang.Class[] argTypes)
                                                        throws java.lang.NoSuchMethodException
Searches for a compatible constructor for the specified arg types using a widening scope.

Throws:
java.lang.NoSuchMethodException

constructFromString

public static java.lang.Object constructFromString(java.lang.String value,
                                                   java.lang.Class type)
Attempts to create an object of the specified type using the specified String value.


encodeAsString

public static java.lang.String encodeAsString(java.lang.Object value)
Attempts to encode an object as a string using the specified object value. This is the reciprocal to the constructFromString() method so that any custom encoding conventions that we support are properly handled in both directions. For example, we support a specially encoded string for color attributes that resembles HTML color defs.


areTypesCompatible

public static boolean areTypesCompatible(java.lang.Class[] targets,
                                         java.lang.Class[] sources)
Returns true if all classes in the sources list are assignment compatible with the targets list. In other words, if all targets[n].isAssignableFrom( sources[n] ) then this method returns true.


getConstant

public static java.lang.Object getConstant(java.lang.String name,
                                           java.lang.Class type)
Searches the specified type for a constant denoted by name. If name contains a '.' the it is assumed to include its own class reference and the type parameter is ignored.


setObject

public void setObject(java.lang.Object object)
Sets the object upon which inspection will be performed.


getObject

public java.lang.Object getObject()
Returns the object upon which inspection is done.


set

public void set(java.lang.String name,
                java.lang.Object value)
         throws InspectionException
Sets the value of the specified property.

Throws:
InspectionException - If the property is not found.

set

public void set(java.lang.String name,
                java.lang.Object value,
                java.lang.Class type)
         throws InspectionException
Sets the value of the specified property using a specifically overloaded mutator method.

Throws:
InspectionException - If the property is not found.

hasMutator

public boolean hasMutator(java.lang.String name,
                          java.lang.Class type)
Returns true if the contained object has a set method for the specified field and type.


hasMutator

public boolean hasMutator(java.lang.String name)
Returns true if the contained object has a set method for the specified field that matches the return type for the associated accessor method ("get"). In other words, this is basically a check to see if the bean has a read/write property specified by name.


hasAccessor

public boolean hasAccessor(java.lang.String name)
Returns true if the contained object has a get method for the specified field. In other words, this is basically a check to see if the bean has a readable property specified by name.


get

public java.lang.Object get(java.lang.String name)
Returns the value of the specified property.


getType

public java.lang.Class getType(java.lang.String name)
Returns the type of the specified property.


getMutableType

public java.lang.Class getMutableType(java.lang.String name)
Returns the type of the specified mutable property. If the caller is only planning to set the value then this may be a safer type to check against since it may be more specific than the one associated with the get.


getDynamicType

public java.lang.Class getDynamicType()
Returns the type for dynamic properties.


callMethod

public java.lang.Object callMethod(java.lang.String name,
                                   java.lang.String param)

callMethod

public java.lang.Object callMethod(java.lang.String name,
                                   java.lang.Object[] params)

hasMethod

public boolean hasMethod(java.lang.String name)

hasMethod

public boolean hasMethod(java.lang.String name,
                         java.lang.Class[] paramTypes)

callMethod

public java.lang.Object callMethod(java.lang.String name)


Copyright © 2002-2003 Paul Speed. All Rights Reserved.