Without using any third party libraries that might be available on Android, there is no simple way to wrap methods of a class. If you can extract your application functionality into an interface, you can use java.lang.reflect.Proxy to implement your interface - the proxy implementation is a single method that calls your real implementation method, and caches and handles the exception.
I can provide more details if factoring out the code into a separate class and interface is a workable approach for you.
EDIT: Here's the details:
You currently are using myService
, which implements the methods. If you don't have one already, create an interface UserService that declares the service methods:
public interface UserService {
User getUser(String name);
List<User> getFriends(User user);
List<Game> getGames(User user);
}
And declare this interface on your existing MyService
class,
class MyService implements UserService {
// .. existing methods unchanged
// interface implemented since methods were already present
}
To then avoid repetition, the exception handling is implemented as an InvocationHandler
class HandleNoInternet implements InvocationHandler {
private final Object delegate; // set fields from constructor args
private final Application app;
public HandleNoInternet(Application app, Object delegate) {
this.app = app;
this.delegate = delegate;
}
public Object invoke(Object proxy, Method method, Object[] args) {
try {
// invoke the method on the delegate and handle the exception
method.invoke(delegate, args);
} catch (Exception ex) {
if ( ex.getCause() instanceof NoInternetException ) {
NoInternetToast.show(app);
} else {
throw new RuntimeException(ex);
}
}
}
}
This is then used as a proxy, in your Application class:
InvocationHandler handler = new HandleNoInternet(this, myService);
UserService appUserService = (UserService)Proxy.newProxyInstance(
getClass().getClassLoader(), new Class[] { UserService.class }, handler);
You then use the appUserService
without needing to worry about catching the NoInternetException.