FileDocCategorySizeDatePackage
CompositeDataInvocationHandler.javaAPI DocJava SE 6 API8510Tue Jun 10 00:26:16 BST 2008javax.management.openmbean

CompositeDataInvocationHandler

public class CompositeDataInvocationHandler extends Object implements InvocationHandler

An {@link InvocationHandler} that forwards getter methods to a {@link CompositeData}. If you have an interface that contains only getter methods (such as {@code String getName()} or {@code boolean isActive()}) then you can use this class in conjunction with the {@link Proxy} class to produce an implementation of the interface where each getter returns the value of the corresponding item in a {@code CompositeData}.

For example, suppose you have an interface like this:

public interface NamedNumber {
public int getNumber();
public String getName();
}
and a {@code CompositeData} constructed like this:
CompositeData cd =
new {@link CompositeDataSupport}(
someCompositeType,
new String[] {"number", "name"},
new Object[] {5, "five"}
);
then you can construct an object implementing {@code NamedNumber} and backed by the object {@code cd} like this:
InvocationHandler handler =
new CompositeDataInvocationHandler(cd);
NamedNumber nn = (NamedNumber)
Proxy.newProxyInstance(NamedNumber.class.getClassLoader(),
new Class[] {NamedNumber.class},
handler);
A call to {@code nn.getNumber()} will then return 5.

If the first letter of the property defined by a getter is a capital, then this handler will look first for an item in the {@code CompositeData} beginning with a capital, then, if that is not found, for an item beginning with the corresponding lowercase letter or code point. For a getter called {@code getNumber()}, the handler will first look for an item called {@code Number}, then for {@code number}. If the getter is called {@code getnumber()}, then the item must be called {@code number}.

If the method given to {@link #invoke invoke} is the method {@code boolean equals(Object)} inherited from {@code Object}, then it will return true if and only if the argument is a {@code Proxy} whose {@code InvocationHandler} is also a {@code CompositeDataInvocationHandler} and whose backing {@code CompositeData} is equal (not necessarily identical) to this object's. If the method given to {@code invoke} is the method {@code int hashCode()} inherited from {@code Object}, then it will return a value that is consistent with this definition of {@code equals}: if two objects are equal according to {@code equals}, then they will have the same {@code hashCode}.

since
1.6

Fields Summary
private final CompositeData
compositeData
private final MXBeanLookup
lookup
Constructors Summary
public CompositeDataInvocationHandler(CompositeData compositeData)

Construct a handler backed by the given {@code CompositeData}.

param
compositeData the {@code CompositeData} that will supply information to getters.
throws
IllegalArgumentException if {@code compositeData} is null.

        this(compositeData, null);
    
CompositeDataInvocationHandler(CompositeData compositeData, MXBeanLookup lookup)

Construct a handler backed by the given {@code CompositeData}.

param
mbsc the {@code MBeanServerConnection} related to this {@code CompositeData}. This is only relevant if a method in the interface for which this is an invocation handler returns a type that is an MXBean interface. Otherwise, it can be null.
param
compositeData the {@code CompositeData} that will supply information to getters.
throws
IllegalArgumentException if {@code compositeData} is null.

	if (compositeData == null)
	    throw new IllegalArgumentException("compositeData");
	this.compositeData = compositeData;
        this.lookup = lookup;
    
Methods Summary
private booleanequals(java.lang.Object proxy, java.lang.Object other)

        if (other == null)
            return false;

        final Class proxyClass = proxy.getClass();
        final Class otherClass = other.getClass();
	if (proxyClass != otherClass)
	    return false;
	InvocationHandler otherih = Proxy.getInvocationHandler(other);
	if (!(otherih instanceof CompositeDataInvocationHandler))
	    return false;
	CompositeDataInvocationHandler othercdih =
	    (CompositeDataInvocationHandler) otherih;
	return compositeData.equals(othercdih.compositeData);
    
public javax.management.openmbean.CompositeDatagetCompositeData()
Return the {@code CompositeData} that was supplied to the constructor.

return
the {@code CompositeData} that this handler is backed by. This is never null.

	assert compositeData != null;
        return compositeData;
    
public java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)

	final String methodName = method.getName();

	// Handle the methods from java.lang.Object
	if (method.getDeclaringClass() == Object.class) {
	    if (methodName.equals("toString") && args == null)
		return "Proxy[" + compositeData + "]";
	    else if (methodName.equals("hashCode") && args == null)
		return compositeData.hashCode() + 0x43444948;
	    else if (methodName.equals("equals") && args.length == 1
		&& method.getParameterTypes()[0] == Object.class)
		return equals(proxy, args[0]);
	    else {
		/* Either someone is calling invoke by hand, or
		   it is a non-final method from Object overriden
		   by the generated Proxy.  At the time of writing,
		   the only non-final methods in Object that are not
		   handled above are finalize and clone, and these
		   are not overridden in generated proxies.  */
		return method.invoke(this, args);
	    }
	}

	String propertyName = OpenConverter.propertyName(method);
	if (propertyName == null) {
	    throw new IllegalArgumentException("Method is not getter: " +
					       method.getName());
	}
	Object openValue;
	if (compositeData.containsKey(propertyName))
	    openValue = compositeData.get(propertyName);
	else {
	    String decap = OpenConverter.decapitalize(propertyName);
	    if (compositeData.containsKey(decap))
		openValue = compositeData.get(decap);
	    else {
		final String msg =
		    "No CompositeData item " + propertyName +
		    (decap.equals(propertyName) ? "" : " or " + decap) +
		    " to match " + methodName;
		throw new IllegalArgumentException(msg);
	    }
	}
        OpenConverter converter =
            OpenConverter.toConverter(method.getGenericReturnType());
	return converter.fromOpenValue(lookup, openValue);