javax.management.openmbean

Class CompositeDataInvocationHandler

public class CompositeDataInvocationHandler extends Object implements InvocationHandler

Provides an {@link java.lang.reflect.InvocationHandler} which implements a series of accessor methods (those beginning with {@code "get"} or {@code "is"}) using the content of a {@link CompositeData} object. An instance of {@link 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 {@link CompositeData} object with appropriate key-value mappings (e.g. "name" => "Fred", "birthday" => 30/06/1974), and then passing that to {@link java.lang.reflect.Proxy#newProxyInstance} along with the interface itself. The invocation handler implements the methods by calling {@link CompositeData#get(String)} 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 {@link 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 {@link Object#equals(Object)} implementation is provided. This returns true if the argument is a proxy with a {@link CompositeDataInvocationHandler} using an equivalent {@link CompositeData} instance. {@link 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 {@link CompositeDataInvocationHandler} with the specified {@link CompositeData} instance.
Method Summary
CompositeDatagetCompositeData()
Returns the {@link 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 {@link CompositeDataInvocationHandler} with the specified {@link CompositeData} instance.

Parameters: data the {@link CompositeData} instance to use.

Throws: IllegalArgumentException if {@code data} is {@code null}.

Method Detail

getCompositeData

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

Returns: the {@link 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 {@link CompositeData} instance, while {@code equals} and {@code hashCode} allow two proxies with a {@link CompositeDataInvocationHandler} using the same {@link 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.