Java Reflection Miscs



Java Reflection Miscs

Proxy Design Pattern
A proxy supports the interface of another object, its target, so that the proxy can substitute for the target for all practical purposes.
The proxy implements the same interface as the target so that it can be used in exactly the same way. The proxy delegates some or all of the calls that it receives to its target and thus acts as either an intermediary or a substitute. In its role as an intermediary, the proxy may add functionality either before or after the method is forwarded to the target. This gives the reflective programmer the capability to add behavior to objects
Using Java’s dynamic proxy
The Java reflection API contains a dynamic proxy-creation facility:
java.lang.reflect.Proxy.
Proxy class
Each class constructed by these factory methods is a public final subclass of Proxy, referred to as a proxy class. We refer to an instance of one of these dynamically constructed proxies as a proxy instance. We call the interfaces that the proxy class implements in this way proxied interfaces. A proxy instance is assignment-compatible with all of its proxied interfaces.
All proxy classes have a constructor that takes an InvocationHandler parameter. InvocationHandler is an interface for objects that handle methods received by proxy instances through their proxied interfaces.
Understanding invocation handlers
Proxy allows programmers to accomplish the delegation task by providing the
InvocationHandler interface.
invocation handlers are objects that handle each method call for a proxy instance. Invocation handlers are also responsible for holding any references to targets of the proxy instance.
A proxy instance forwards method calls to its invocation handler by calling invoke.
public interface InvocationHandler {
   public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable;
}
UndeclaredThrowableException
The exception UndeclaredThrowableException may be thrown by the execution of the invoke method.  UndeclaredThrowableException wraps non-runtime exceptions that are not declared by the interface for the method being called. The cause of the wrapped exception may be accessed with getCause.
Handling the methods of Object
hashCode, equals, toString and finalize are dispatched to the invoke method in the
same manner as any other proxied method.
Method intercession does not take place for the other methods declared by
java.lang.Object, such as wait, notify.
Chaining proxies

One of the strengths of using proxies is that they can be arranged in a chain, with each proxy but the last having another proxy as its target. The last target in the chain is the real target object.
Usage:
Object proxy = Proxy.newProxyInstance( SomeInterface.getClassLoader(), Class[]{SomeInterface.class}, new SomeIH( obj ) );
public static Object newInstance(Object obj) {
    return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
            .getClass().getInterfaces(), new DebugProxy(obj));
}
Java code digestion
public class Proxy implements java.io.Serializable {
// the usage of WeakHashMap
private static Map proxyClasses = Collections.synchronizedMap(new WeakHashMap());
public static Object newProxyInstance(ClassLoader loader,
Class[] interfaces,
InvocationHandler h)
{
      Class cl = getProxyClass(loader, interfaces);
    Constructor cons = cl.getConstructor(constructorParams);
      return (Object) cons.newInstance(new Object[] { h });                     
}

public static Class getProxyClass(ClassLoader loader, Class... interfaces)
{
    byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
    proxyName, interfaces);
    proxyClass = defineClass0(loader, proxyName,
    proxyClassFile, 0, proxyClassFile.length);
    proxyClasses.put(proxyClass, null);
}

Resources

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)