我有很多样板代码基本上遵循这种模式:
function doSomething() {
try {
[implementation]
[implementation]
[implementation]
[implementation]
} catch (Exception e) {
MyEnv.getLogger().log(e);
} finally {
genericCleanUpMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
我想创建自己的注释来清理我的代码:
@TryCatchWithLoggingAndCleanUp
function doSomething() {
[implementation]
[implementation]
[implementation]
[implementation]
}
Run Code Online (Sandbox Code Playgroud)
方法签名变化很大(取决于方法的实际实现),但样板try/catch/finally部分始终相同.
我想到的注释会自动用整个try...catch...finallyhoopla 包装带注释的方法的内容.
我一直在寻找一种直截了当的方法,但却没有找到任何东西.我不知道,也许我只是看不到所有注释树木的树林.
关于如何实现这样的注释的任何指针都将非常感激.
我使用Retrofit 1.9.0和OkHttp 2.0.0
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
Run Code Online (Sandbox Code Playgroud)
但是没有找到拦截器方法
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(50000, TimeUnit.MILLISECONDS);
client.setReadTimeout(50000, TimeUnit.MILLISECONDS);
client.interceptor().add(...); // cannot resolve method interceptor
Run Code Online (Sandbox Code Playgroud)
我正在处理改造中的重试请求,这篇文章建议 如何使用OkHttp/Retrofit重试HTTP请求?
我在这里错过了一些图书馆吗?