카테고리 없음

InvokeHandler

НooпeУ 2011. 12. 15. 17:52

//오버로딩된 메소드들... Proxy에서 인스턴스를 만들어서 리턴해준다.
//우리는 참조만 해서 사용하면 된다. 즉, 인터페이스로 참조하면 끝
//그러면 인터페이스에서 메서드를 호출할 때, 어떻게 되는가? 
//그 때마다, invoke메서드가 호출되는데, 메서드 이름과 파라미터로 넘어온 값을 받아서 처리를 할 수 있다.
//이를 적절히 자료구조를 만들거나, 함수마다 다르게 처리하기 위해 분기문을 사용하거나,
//아무튼 적절히 처리해서 리턴해주면 끝.
public static Object getProxyInstance(String className){
Class<?>[] it = null;
try {
it = new Class[1];
it[0] = Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Proxy.newProxyInstance(InvokeHandler.class.getClassLoader(), it, new InvokeHandler());
}
public static Object getProxyObject(Class<?> cls){
Class<?>[] it = new Class[1];
it[0] = cls;
return Proxy.newProxyInstance(InvokeHandler.class.getClassLoader(), it, new InvokeHandler());
}
//Proxy.newProxyInstance를 이용해 생성된 객체에서 메소드를 호출 할 때마다, invoke메소드가 호출되고 
//Method method가 method로 들어오고, Object[] args가 파라미터로 들어온다.
//어떻게 처리할지는 적당한 자료구조를 생성해서 입 맛에 맡게 invoke 메서드를 구현한다.
@Override
public Object invoke(Object obj, Method method, Object[] args)
throws Throwable