📄 persistenceobjectmethodinterceptor.java
字号:
package cn.edu.nju.software.sd.torm.impl;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import cn.edu.nju.software.sd.torm.exception.IllegalOperationException;
/**
* The PersistenceObjectMethodInterceptor class is used to
* encapsulate a object. This is for the purpose of method
* interception.
*
* @author Yinfei XU
*
*/
public class PersistenceObjectMethodInterceptor implements MethodInterceptor {
private Set<Object> deletedObjects;
private Method interceptedMethod;
private Class clazz;
/**
* @param c
* @param interceptedMethod
*/
public PersistenceObjectMethodInterceptor(Class c, Method interceptedMethod) {
super();
deletedObjects = new HashSet<Object>();
this.clazz = c;
this.interceptedMethod = interceptedMethod;
}
/**
* @return
*/
public Object getEnhancedObject() {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setCallback(this);
return enhancer.create();
}
/* (non-Javadoc)
* @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], net.sf.cglib.proxy.MethodProxy)
*/
public Object intercept(Object o, Method m, Object[] args, MethodProxy proxy)
throws Throwable {
Iterator itr = deletedObjects.iterator();
while(itr.hasNext()){
if(itr.next() == o ){
throw new IllegalOperationException("The call to method is not allowed: " + m.getName());
}
}
if (m.equals(interceptedMethod)) {
throw new IllegalOperationException(
"The call to method is not allowed: " + m.getName());
} else {
return proxy.invokeSuper(o, args);
}
}
/**
* @param obj
*/
public void addDeletedObject(Object obj) {
this.deletedObjects.add(obj);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -