Search in sources :

Example 1 with JTryBlock

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;
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JConditional(com.helger.jcodemodel.JConditional) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) JVar(com.helger.jcodemodel.JVar)

Example 2 with JTryBlock

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);
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JTryBlock(com.helger.jcodemodel.JTryBlock) JMethod(com.helger.jcodemodel.JMethod)

Example 3 with JTryBlock

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);
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JTryBlock(com.helger.jcodemodel.JTryBlock) JMethod(com.helger.jcodemodel.JMethod)

Example 4 with JTryBlock

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"));
}
Also used : WakeLock(org.androidannotations.annotations.WakeLock) ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) Flag(org.androidannotations.annotations.WakeLock.Flag) JBlock(com.helger.jcodemodel.JBlock) Level(org.androidannotations.annotations.WakeLock.Level) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JVar(com.helger.jcodemodel.JVar)

Example 5 with JTryBlock

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);
}
Also used : Trace(org.androidannotations.annotations.Trace) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JVar(com.helger.jcodemodel.JVar)

Aggregations

JTryBlock (com.helger.jcodemodel.JTryBlock)10 JBlock (com.helger.jcodemodel.JBlock)9 JMethod (com.helger.jcodemodel.JMethod)7 JVar (com.helger.jcodemodel.JVar)7 JCatchBlock (com.helger.jcodemodel.JCatchBlock)5 JInvocation (com.helger.jcodemodel.JInvocation)5 IJExpression (com.helger.jcodemodel.IJExpression)4 ExecutableElement (javax.lang.model.element.ExecutableElement)4 AbstractJClass (com.helger.jcodemodel.AbstractJClass)3 JConditional (com.helger.jcodemodel.JConditional)3 JDefinedClass (com.helger.jcodemodel.JDefinedClass)2 IJStatement (com.helger.jcodemodel.IJStatement)1 JFieldRef (com.helger.jcodemodel.JFieldRef)1 JFieldVar (com.helger.jcodemodel.JFieldVar)1 JSynchronizedBlock (com.helger.jcodemodel.JSynchronizedBlock)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Background (org.androidannotations.annotations.Background)1 HttpsClient (org.androidannotations.annotations.HttpsClient)1 Trace (org.androidannotations.annotations.Trace)1 WakeLock (org.androidannotations.annotations.WakeLock)1