prendendo spunto da un libro, volevo creare una classe che mi registrasse la chiamata ai metodi di una classe.
la classe serve per gestire il DB.
mi sono fatto questa classe qua:
codice:
public class MyProxy implements InvocationHandler {
private final Object obj;
private final List<Method> methods;
private final List<Method> history;
private MyProxy(Object obj) {
this.obj = obj;
methods = new ArrayList<>();
history = Collections.unmodifiableList(methods);
}
public static synchronized Object proxyFor(Object obj) {
Class<?> objClass = obj.getClass();
return Proxy.newProxyInstance(objClass.getClassLoader(), objClass.getInterfaces(), new MyProxy(obj));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
methods.add(method);
try {
return method.invoke(obj, args);
} catch(InvocationTargetException ex) {
throw ex.getCause();
}
}
public List<Method> getHistory() {
return history;
}
}
a questo punto nel mio JFrame ho creato questo metodo privato:
codice:
private DBmanger dbman = DBmanger.getInstance();
....................
private void getProxyObj() {
Object proxyObj = MyProxy.proxyFor(dbman);
MyProxy mp =(MyProxy) Proxy.getInvocationHandler(proxyObj);
List<Method> history = mp.getHistory();
System.out.println("METHOD: " + history.getClass().getSimpleName());
}
che richiamo in tutti gli eventi dove eseguo una query.
ad esempio durante la connessione.
dbman è l'oggetto della classe che gestice il db.
mi viene stampato solo questo:
codice:
METHOD: UnmodifiableRandomAccessList
dove sto sbagliando??