org.progeeks.util.beans
Class BeanInspector

java.lang.Object
  extended by org.progeeks.util.beans.BeanInspector

public class BeanInspector
extends java.lang.Object

Provides reflection utilities for a single bean class. This internally caches bean methods so that they don't have to be looked up repeatedly. In addition, it can provide additional info about the class that is important to know about things like multi-valued properties and such.

Note: This class functions different than the org.progeeks.util.Inspector class and may one day replace it. This class provides an object per class where as the Inspector class provides and object per object.

Version:
$Revision: 1.14 $
Author:
Paul Speed

Constructor Summary
protected BeanInspector(java.lang.Class beanClass)
           
 
Method Summary
 boolean addPropertyChangeListener(java.lang.Object bean, java.beans.PropertyChangeListener listener)
          Uses reflection to add the listener to the specified bean if it has the appropriate add property change listener method.
 void addPropertyElement(java.lang.Object bean, java.lang.String property, int index, java.lang.Object value)
           
 boolean addPropertyElement(java.lang.Object bean, java.lang.String property, java.lang.Object value)
           
protected  void cacheDescriptors()
           
 java.lang.Class getBeanClass()
           
 ContainerPropertyDescriptor getContainerDescriptor(java.lang.String property)
           
 java.beans.PropertyDescriptor getDescriptor(java.lang.String property)
           
static BeanInspector getInspector(java.lang.Class c)
           
 java.util.List getListProperty(java.lang.Object bean, java.lang.String property)
           
 java.lang.Object getProperty(java.lang.Object bean, java.lang.String property)
           
 java.lang.Object getPropertyElement(java.lang.Object bean, java.lang.String property, int index)
           
 java.util.Set getPropertyNames()
          Returns a read-only view of the set of property names.
 java.lang.Class getPropertyType(java.lang.String property)
           
 boolean isContainerProperty(java.lang.String property)
           
 boolean isListProperty(java.lang.String property)
           
 boolean isMutableProperty(java.lang.String property)
          Returns true if the specified property exists and is either a writeable property or a collection that can be modified.
 boolean isReadableProperty(java.lang.String property)
          Returns true if the specified property exists and has an accessor method.
 boolean isWriteableProperty(java.lang.String property)
          Returns true if the specified property exists and has a mutator method.
static void main(java.lang.String[] args)
           
static void releaseInspector(java.lang.Class c)
           
 void removePropertyChangeListener(java.lang.Object bean, java.beans.PropertyChangeListener listener)
          Removes a previously added listener from the specified bean.
 java.lang.Object removePropertyElement(java.lang.Object bean, java.lang.String property, int index)
           
 boolean removePropertyElement(java.lang.Object bean, java.lang.String property, java.lang.Object value)
           
 void setProperty(java.lang.Object bean, java.lang.String property, java.lang.Object value)
           
 java.lang.Object setPropertyElement(java.lang.Object bean, java.lang.String property, int index, java.lang.Object value)
           
protected static java.lang.reflect.AccessibleObject verifyAccess(java.lang.reflect.AccessibleObject obj)
          Work-around a sort of anomoly in the reflection classes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanInspector

protected BeanInspector(java.lang.Class beanClass)
Method Detail

getInspector

public static BeanInspector getInspector(java.lang.Class c)

releaseInspector

public static void releaseInspector(java.lang.Class c)

cacheDescriptors

protected void cacheDescriptors()
                         throws java.beans.IntrospectionException
Throws:
java.beans.IntrospectionException

verifyAccess

protected static java.lang.reflect.AccessibleObject verifyAccess(java.lang.reflect.AccessibleObject obj)
Work-around a sort of anomoly in the reflection classes. See, if an interface is public but the implementing class is not then we can't access the methods. This is because we're operating directly on the concrete class. If we were to replace our method objects with the ones defined on the interface then we'd be ok. However, Java provides this nifty setAccessible() thing to work around this. This method potentially does a little snooping (not at the moment) and makes sure the object is accessible. Null values are allowed and just ignored. Objects that are already accessible are also ignored. I return the accessible object just in case we decide to do the bait and switch approach at some point.


getBeanClass

public java.lang.Class getBeanClass()

isWriteableProperty

public boolean isWriteableProperty(java.lang.String property)
Returns true if the specified property exists and has a mutator method.


isMutableProperty

public boolean isMutableProperty(java.lang.String property)
Returns true if the specified property exists and is either a writeable property or a collection that can be modified.


isReadableProperty

public boolean isReadableProperty(java.lang.String property)
Returns true if the specified property exists and has an accessor method.


setProperty

public void setProperty(java.lang.Object bean,
                        java.lang.String property,
                        java.lang.Object value)

getPropertyType

public java.lang.Class getPropertyType(java.lang.String property)

getProperty

public java.lang.Object getProperty(java.lang.Object bean,
                                    java.lang.String property)

isListProperty

public boolean isListProperty(java.lang.String property)

isContainerProperty

public boolean isContainerProperty(java.lang.String property)

getListProperty

public java.util.List getListProperty(java.lang.Object bean,
                                      java.lang.String property)

addPropertyElement

public boolean addPropertyElement(java.lang.Object bean,
                                  java.lang.String property,
                                  java.lang.Object value)

addPropertyElement

public void addPropertyElement(java.lang.Object bean,
                               java.lang.String property,
                               int index,
                               java.lang.Object value)

setPropertyElement

public java.lang.Object setPropertyElement(java.lang.Object bean,
                                           java.lang.String property,
                                           int index,
                                           java.lang.Object value)

removePropertyElement

public boolean removePropertyElement(java.lang.Object bean,
                                     java.lang.String property,
                                     java.lang.Object value)

removePropertyElement

public java.lang.Object removePropertyElement(java.lang.Object bean,
                                              java.lang.String property,
                                              int index)

getPropertyElement

public java.lang.Object getPropertyElement(java.lang.Object bean,
                                           java.lang.String property,
                                           int index)

getPropertyNames

public java.util.Set getPropertyNames()
Returns a read-only view of the set of property names.


getDescriptor

public java.beans.PropertyDescriptor getDescriptor(java.lang.String property)

getContainerDescriptor

public ContainerPropertyDescriptor getContainerDescriptor(java.lang.String property)

addPropertyChangeListener

public boolean addPropertyChangeListener(java.lang.Object bean,
                                         java.beans.PropertyChangeListener listener)
Uses reflection to add the listener to the specified bean if it has the appropriate add property change listener method.


removePropertyChangeListener

public void removePropertyChangeListener(java.lang.Object bean,
                                         java.beans.PropertyChangeListener listener)
Removes a previously added listener from the specified bean.


main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception


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