Framework Professional Edition Package


com.prosyst.util.beans
Class Introspector

java.lang.Object
  extended by com.prosyst.util.beans.Introspector

public class Introspector
extends java.lang.Object

The Introspector is much more advanced tool comparing to BeanUtils that supports transparent type conversion and deep, nested or even indexed properties.

Even if you don't use nested properties, it is much simpler to use

  Introspector.getInstance().setProperty(key, value, bean);
 
instead of
  BeanProperty bp = BeanUtils.getBeanProperty(key, beanClass);
  if (bp != null) {
    Method m = bp.getWriteMethod();
    if (m != null) {
      m.invoke(bean, new Object[] {
        value
      });
    }
  }
 
This tool will also allow you to use properties like car.color.red and car.door[5].open. This, along with the build in conversion from String to Simple Type, makes this class a valuable tool for web developers.

Author:
Valentin Valchev

Method Summary
static java.lang.Object convert(java.lang.Object fromValue, java.lang.Class toType)
          This is an utility method which is very useful for converting simple types.
static java.lang.Long decodeLong(java.lang.String nm)
           
static Introspector getInstance()
          This method return a singleton.
static java.lang.Class getPrimitiveWrapperClass(java.lang.Class type)
          This method looks for the wrapper class of the primitive object.
 java.lang.Object getProperty(java.lang.String key, java.lang.Object bean)
          This method returns the value of and bean property.
 void setProperty(java.lang.String key, java.lang.Object value, java.lang.Object bean)
          Sets a property of the bean to the specified value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static Introspector getInstance()
This method return a singleton. It prevents multiple instances of the Introspector.

Returns:
the only one (in the whole JVM) instance of the Introspector

setProperty

public void setProperty(java.lang.String key,
                        java.lang.Object value,
                        java.lang.Object bean)
                 throws java.lang.IllegalArgumentException,
                        java.lang.IllegalAccessException,
                        java.lang.reflect.InvocationTargetException,
                        BeanException
Sets a property of the bean to the specified value. This is much more powerful mechanism compared to the usage of BeanUtil class. It is able to search deep in the properties and allows indexes.

When the key is car.manufacturer.url this method will execute the following sequence
bean.getCar().getManufacturer().setUrl(value);

You can also specify an index: car.doors[3].open which will be usually interpreted to getCar().getDoors(3).setOpen(value). In addition, if you didn't declared method getDoors(index) but you have getDoors() that returns an array or Vector the previous example will be interpreted to:
getCar().getDoors()[3].setOpen(value) or getCar().getDoors().elementAt(3).setOpen(value) respectively for array or Vector

Parameters:
key - the name of the property
value - the value which will be set
bean - the target
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
BeanException
See Also:
Method.invoke(java.lang.Object, java.lang.Object[])

getProperty

public java.lang.Object getProperty(java.lang.String key,
                                    java.lang.Object bean)
                             throws java.lang.IllegalArgumentException,
                                    java.lang.IllegalAccessException,
                                    java.lang.reflect.InvocationTargetException,
                                    BeanException
This method returns the value of and bean property. Similarly to the setProperty method this one allows deep, property search and indexed values.

Parameters:
key - the name of the property to read
bean - the source
Returns:
the value of the object or null if it cannot be read
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
BeanException
See Also:
setProperty(String, Object, Object)

convert

public static java.lang.Object convert(java.lang.Object fromValue,
                                       java.lang.Class toType)
This is an utility method which is very useful for converting simple types. It is used internally by the introspector for converting String values to the corresponding property type.

Additionally if the toType is java.lang.Number it converts the value to Integer.

If the fromValue is array and toType class equals the array elements type, this method will return the first element in the array.

Parameters:
fromValue - the value that is going to be converted
toType - the desired type
Returns:
the if a converter is found - the converted object. Otherwise - the original object.

decodeLong

public static java.lang.Long decodeLong(java.lang.String nm)
                                 throws java.lang.NumberFormatException
Throws:
java.lang.NumberFormatException

getPrimitiveWrapperClass

public static java.lang.Class getPrimitiveWrapperClass(java.lang.Class type)
This method looks for the wrapper class of the primitive object. If the specified type is not primitive or wrapper class is not found it will return the value in the type parameter

Parameters:
type - the type of the (primitive) class
Returns:
the wrapper class for the primitive or type

Framework Professional Edition Package


Copyright © 1999-2007 ProSyst Software GmbH. All Rights Reserved