javax.management.openmbean
Class CompositeDataInvocationHandler

java.lang.Object
  extended by javax.management.openmbean.CompositeDataInvocationHandler
All Implemented Interfaces:
InvocationHandler

public class CompositeDataInvocationHandler
extends Object
implements InvocationHandler

Provides an InvocationHandler which implements a series of accessor methods (those beginning with "get" or "is") using the content of a CompositeData object. An instance of CompositeData consists of a series of key-value mappings. This handler assumes these keys to be the names of attributes, and thus provides accessor methods by returning the associated value.

As an example, consider the following interface:

 public interface Person
 {
   public String getName();
   public Date getBirthday();
 }
 

This specifies two accessor methods for retrieving the attributes, name and birthday. An implementation of this interface can be provided by creating an instance of this class, using a CompositeData object with appropriate key-value mappings (e.g. "name" => "Fred", "birthday" => 30/06/1974), and then passing that to Proxy.newProxyInstance(java.lang.ClassLoader, java.lang.Class[], java.lang.reflect.InvocationHandler) along with the interface itself. The invocation handler implements the methods by calling CompositeData.get(String) with the appropriate key.

The attribute name is derived by taking the remainder of the method name following "get". If the first letter of this substring is uppercase, then two attempts are made to retrieve the attribute from the CompositeData instance: one using the original substring, and one with the first letter changed to its lower-case equivalent. If the first letter is lowercase, only the exact substring is used.

An Object.equals(Object) implementation is provided. This returns true if the argument is a proxy with a CompositeDataInvocationHandler using an equivalent CompositeData instance. Object.hashCode() is also defined so as to match this implementation and give equivalent instances the same hashcode.

Since:
1.6

Constructor Summary
CompositeDataInvocationHandler(CompositeData data)
          Constructs a new CompositeDataInvocationHandler with the specified CompositeData instance.
 
Method Summary
 CompositeData getCompositeData()
          Returns the CompositeData instance which provides the key-value mappings for this instance.
 Object invoke(Object proxy, Method method, Object[] args)
          Called by the proxy class whenever a method is called.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompositeDataInvocationHandler

public CompositeDataInvocationHandler(CompositeData data)
Constructs a new CompositeDataInvocationHandler with the specified CompositeData instance.

Parameters:
data - the CompositeData instance to use.
Throws:
IllegalArgumentException - if data is null.
Method Detail

getCompositeData

public CompositeData getCompositeData()
Returns the CompositeData instance which provides the key-value mappings for this instance. This is never null.

Returns:
the CompositeData instance.

invoke

public Object invoke(Object proxy,
                     Method method,
                     Object[] args)
              throws Throwable
Called by the proxy class whenever a method is called. The handler only deals with accessor methods (beginning with "get" or "is"), equals, and "hashCode". Accessor methods are implemented by returning the appropriate value from the CompositeData instance, while equals and hashCode allow two proxies with a CompositeDataInvocationHandler using the same CompositeData instance to be classified as equivalent.

Specified by:
invoke in interface InvocationHandler
Parameters:
proxy - the proxy on which the method was called.
method - the method which was called.
args - the arguments supplied to the method.
Returns:
the return value from the method.
Throws:
Throwable - if an exception is thrown in the process.
See Also:
Proxy, UndeclaredThrowableException