javax.management.openmbean

Class CompositeDataInvocationHandler

public class CompositeDataInvocationHandler extends Object implements InvocationHandler

Provides an java.lang.reflect.InvocationHandler which implements a series of accessor methods (those beginning with {@code "get"} or {@code "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, {@code name} and {@code 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 java.lang.reflect.Proxy#newProxyInstance along with the interface itself. The invocation handler implements the methods by calling get with the appropriate key.

The attribute name is derived by taking the remainder of the method name following {@code "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
CompositeDatagetCompositeData()
Returns the CompositeData instance which provides the key-value mappings for this instance.
Objectinvoke(Object proxy, Method method, Object[] args)
Called by the proxy class whenever a method is called.

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 {@code data} is {@code null}.

Method Detail

getCompositeData

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

Returns: the CompositeData instance.

invoke

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

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.