org.onetwo.common.proxy.AbstractMethodInterceptor Maven / Gradle / Ivy
package org.onetwo.common.proxy;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.google.common.cache.Cache;
/**
* @author wayshall
*
*/
public abstract class AbstractMethodInterceptor> implements MethodInterceptor {
final protected Cache methodCache;
public AbstractMethodInterceptor(Cache methodCache) {
this.methodCache = methodCache;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
Object proxy = invocation.getThis();
if(Object.class == method.getDeclaringClass()) {
String name = method.getName();
if("equals".equals(name)) {
return proxy == invocation.getArguments()[0];
} else if("hashCode".equals(name)) {
return System.identityHashCode(proxy);
} else if("toString".equals(name)) {
return proxy.getClass().getName() + "@" +
Integer.toHexString(System.identityHashCode(proxy)) + ", InvocationHandler " + this;
} else {
throw new IllegalStateException(String.valueOf(method));
}
}
// M invokeMethod = methodCache.get(method);
M invokeMethod = methodCache.get(method, () -> {
return createMethod(method);
});
return doInvoke(invocation, invokeMethod);
}
protected M createMethod(Method method) {
throw new UnsupportedOperationException();
}
abstract protected Object doInvoke(MethodInvocation invocation, M invokeMethod) throws Throwable ;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy