All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rapidpm.proxybuilder.dynamicobjectadapter.ExtendedInvocationHandler Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package org.rapidpm.proxybuilder.dynamicobjectadapter;




import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sven on 12.05.15.
 */
public abstract class ExtendedInvocationHandler implements InvocationHandler {

  private Map adaptedMethods = new HashMap<>();
  private Map adapters = new HashMap<>();
  private T original;

  public void setOriginal(T original) {
    this.original = original;
  }

  public void addAdapter(Object adapter) {
    final Class adapterClass = adapter.getClass();
    Method[] methods = adapterClass.getDeclaredMethods();
    for (Method m : methods) {
      final MethodIdentifier key = new MethodIdentifier(m);
      adaptedMethods.put(key, m);
      adapters.put(key, adapter);
    }
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      final MethodIdentifier key = new MethodIdentifier(method);
      Method other = adaptedMethods.get(key);
      if (other != null) {
        other.setAccessible(true); //Lambdas...
        final Object result = other.invoke(adapters.get(key), args);
        other.setAccessible(false);
        return result;
      } else {
        return method.invoke(original, args);
      }
    } catch (InvocationTargetException e) {
      throw e.getTargetException();
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy