use of com.helger.jcodemodel.JTryBlock in project androidannotations by androidannotations.
the class RestMethodHandler method surroundWithRestTryCatch.
/**
* Adds the try/catch around the rest execution code.
*
* If an exception is caught, it will first check if the handler is set. If
* the handler is set, it will call the handler and return null (or nothing
* if void). If the handler isn't set, it will re-throw the exception so
* that it behaves as it did previous to this feature.
*/
private JBlock surroundWithRestTryCatch(RestHolder holder, JBlock block, boolean methodReturnVoid) {
if (holder.getRestErrorHandlerField() != null) {
JBlock newBlock = new JBlock().bracesRequired(false).indentRequired(false);
JTryBlock tryBlock = newBlock._try();
codeModelHelper.copy(block, tryBlock.body());
JCatchBlock jCatch = tryBlock._catch(getJClass(NESTED_RUNTIME_EXCEPTION));
JBlock catchBlock = jCatch.body();
JConditional conditional = catchBlock._if(JOp.ne(holder.getRestErrorHandlerField(), JExpr._null()));
JVar exceptionParam = jCatch.param("e");
JBlock thenBlock = conditional._then();
// call the handler method if it was set.
thenBlock.add(holder.getRestErrorHandlerField().invoke("onRestClientExceptionThrown").arg(exceptionParam));
// return null if exception was caught and handled.
if (!methodReturnVoid) {
thenBlock._return(JExpr._null());
}
// re-throw the exception if handler wasn't set.
conditional._else()._throw(exceptionParam);
return newBlock;
}
return block;
}
use of com.helger.jcodemodel.JTryBlock in project androidannotations by androidannotations.
the class RoboGuiceHandler method onDestroyMethod.
private void onDestroyMethod(EActivityHolder holder, JFieldVar eventManager) {
JBlock onDestroyBlock = new JBlock().bracesRequired(false).indentRequired(false);
JTryBlock tryBlock = onDestroyBlock._try();
fireEvent(eventManager, tryBlock.body(), getJClass(RoboGuiceClasses.ON_DESTROY_EVENT));
JBlock finallyBody = tryBlock._finally();
JTryBlock tryInFinally = finallyBody._try();
tryInFinally.body().add(getJClass(RoboGuiceClasses.ROBO_GUICE).staticInvoke("destroyInjector").arg(_this()));
tryInFinally._finally().invoke(_super(), "onDestroy");
JMethod onDestroy = holder.getOnDestroy();
codeModelHelper.replaceSuperCall(onDestroy, onDestroyBlock);
}
use of com.helger.jcodemodel.JTryBlock in project androidannotations by androidannotations.
the class RoboGuiceHandler method onStopMethod.
private void onStopMethod(EActivityHolder holder, JFieldVar eventManager) {
JBlock onStopBlock = new JBlock().bracesRequired(false).indentRequired(false);
JTryBlock tryBlock = onStopBlock._try();
fireEvent(eventManager, tryBlock.body(), getJClass(RoboGuiceClasses.ON_STOP_EVENT));
JBlock finallyBody = tryBlock._finally();
finallyBody.invoke(_super(), "onStop");
JMethod onStop = holder.getOnStop();
codeModelHelper.replaceSuperCall(onStop, onStopBlock);
}
use of com.helger.jcodemodel.JTryBlock in project androidannotations by androidannotations.
the class WakeLockHandler method process.
@Override
public void process(Element element, EComponentHolder holder) {
ExecutableElement executableElement = (ExecutableElement) element;
WakeLock annotation = executableElement.getAnnotation(WakeLock.class);
String tag = extractTag(executableElement);
Level level = annotation.level();
Flag[] flags = annotation.flags();
JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
JBlock previousMethodBody = codeModelHelper.removeBody(method);
JBlock methodBody = method.body();
IJExpression levelAndFlags = getClasses().POWER_MANAGER.staticRef(level.name());
if (flags.length > 0) {
for (Flag flag : flags) {
levelAndFlags = levelAndFlags.bor(getClasses().POWER_MANAGER.staticRef(flag.name()));
}
}
JInvocation newWakeLock = holder.getPowerManagerRef().invoke("newWakeLock").arg(levelAndFlags).arg(JExpr.lit(tag));
JVar wakeLock = methodBody.decl(getClasses().WAKE_LOCK, "wakeLock", JExpr._null());
JTryBlock tryBlock = methodBody._try();
tryBlock.body().assign(wakeLock, newWakeLock);
tryBlock.body().add(wakeLock.invoke("acquire"));
tryBlock.body().add(previousMethodBody);
JBlock finallyBlock = tryBlock._finally();
JConditional ifStatement = finallyBlock._if(wakeLock.ne(JExpr._null()));
ifStatement._then().add(wakeLock.invoke("release"));
}
use of com.helger.jcodemodel.JTryBlock in project androidannotations by androidannotations.
the class TraceHandler method process.
@Override
public void process(Element element, EComponentHolder holder) throws Exception {
ExecutableElement executableElement = (ExecutableElement) element;
String tag = extractTag(executableElement);
int level = executableElement.getAnnotation(Trace.class).level();
JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
JBlock previousMethodBody = codeModelHelper.removeBody(method);
JBlock methodBody = method.body();
JInvocation isLoggableInvocation = getClasses().LOG.staticInvoke("isLoggable");
isLoggableInvocation.arg(tag).arg(logLevelFromInt(level, getClasses().LOG));
JConditional ifStatement = methodBody._if(isLoggableInvocation);
JInvocation currentTimeInvoke = getClasses().SYSTEM.staticInvoke("currentTimeMillis");
JBlock thenBody = ifStatement._then();
// Log In
String logMethodName = logMethodNameFromLevel(level);
JInvocation logEnterInvoke = getClasses().LOG.staticInvoke(logMethodName);
logEnterInvoke.arg(tag);
logEnterInvoke.arg(getEnterMessage(method, executableElement));
thenBody.add(logEnterInvoke);
JVar startDeclaration = thenBody.decl(getCodeModel().LONG, "traceStart" + generationSuffix(), currentTimeInvoke);
JTryBlock tryBlock;
JVar result = null;
if (method.type().fullName().equals("void")) {
tryBlock = thenBody._try();
tryBlock.body().add(previousMethodBody);
} else {
JInvocation superCall = codeModelHelper.getSuperCall(holder, method);
result = thenBody.decl(getJClass(Object.class), "traceResult" + generationSuffix(), JExpr._null());
tryBlock = thenBody._try();
tryBlock.body().assign(result, superCall);
tryBlock.body()._return(JExpr.cast(boxify(method.type()), result));
}
JBlock finallyBlock = tryBlock._finally();
JVar durationDeclaration = finallyBlock.decl(getCodeModel().LONG, "traceDuration" + generationSuffix(), currentTimeInvoke.minus(startDeclaration));
JInvocation logExitInvoke = getClasses().LOG.staticInvoke(logMethodName);
logExitInvoke.arg(tag);
logExitInvoke.arg(getExitMessage(executableElement, method, result, durationDeclaration));
finallyBlock.add(logExitInvoke);
JBlock elseBlock = ifStatement._else();
elseBlock.add(previousMethodBody);
}
Aggregations