Search in sources :

Example 66 with JBlock

use of com.helger.jcodemodel.JBlock in project androidannotations by androidannotations.

the class ActivityIntentBuilder method createCallWithIfGuard.

private JBlock createCallWithIfGuard(JVar requestCode, JBlock thenBlock, IJExpression invocationTarget) {
    JConditional guardIf = thenBlock._if(getClasses().BUILD_VERSION.staticRef("SDK_INT").gte(getClasses().BUILD_VERSION_CODES.staticRef("JELLY_BEAN")));
    JBlock startInvocationBlock = guardIf._then();
    String methodName = requestCode != null ? "startActivityForResult" : "startActivity";
    JInvocation invocation = guardIf._else().invoke(invocationTarget, methodName).arg(intentField);
    if (requestCode != null) {
        invocation.arg(requestCode);
    }
    return startInvocationBlock;
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JConditional(com.helger.jcodemodel.JConditional) JInvocation(com.helger.jcodemodel.JInvocation)

Example 67 with JBlock

use of com.helger.jcodemodel.JBlock 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)

Example 68 with JBlock

use of com.helger.jcodemodel.JBlock in project androidannotations by androidannotations.

the class TransactionalHandler method process.

@Override
public void process(Element element, EComponentHolder holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    String returnTypeName = executableElement.getReturnType().toString();
    AbstractJClass returnType = getJClass(returnTypeName);
    JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    codeModelHelper.removeBody(method);
    JVar db = method.params().get(0);
    JBlock body = method.body();
    body.invoke(db, "beginTransaction");
    JTryBlock tryBlock = body._try();
    IJExpression activitySuper = holder.getGeneratedClass().staticRef("super");
    JInvocation superCall = JExpr.invoke(activitySuper, method);
    for (JVar param : method.params()) {
        superCall.arg(param);
    }
    JBlock tryBody = tryBlock.body();
    if (returnTypeName.equals("void")) {
        tryBody.add(superCall);
        tryBody.invoke(db, "setTransactionSuccessful");
        tryBody._return();
    } else {
        JVar result = tryBody.decl(returnType, "result_", superCall);
        tryBody.invoke(db, "setTransactionSuccessful");
        tryBody._return(result);
    }
    JCatchBlock catchBlock = tryBlock._catch(getJClass(RuntimeException.class));
    JVar exceptionParam = catchBlock.param("e");
    JBlock catchBody = catchBlock.body();
    JInvocation errorInvoke = catchBody.staticInvoke(getClasses().LOG, "e");
    errorInvoke.arg(logTagForClassHolder(holder));
    errorInvoke.arg("Error in transaction");
    errorInvoke.arg(exceptionParam);
    catchBody._throw(exceptionParam);
    tryBlock._finally().invoke(db, "endTransaction");
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) JVar(com.helger.jcodemodel.JVar)

Example 69 with JBlock

use of com.helger.jcodemodel.JBlock in project androidannotations by androidannotations.

the class UiThreadHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousBody = codeModelHelper.removeBody(delegatingMethod);
    JDefinedClass anonymousRunnableClass = codeModelHelper.createDelegatingAnonymousRunnableClass(previousBody);
    UiThread annotation = element.getAnnotation(UiThread.class);
    long delay = annotation.delay();
    UiThread.Propagation propagation = annotation.propagation();
    if (delay == 0 && propagation == UiThread.Propagation.REUSE) {
        // Put in the check for the UI thread.
        addUIThreadCheck(delegatingMethod, previousBody, holder);
    }
    delegatingMethod.body().add(//
    getJClass(UiThreadExecutor.class).staticInvoke(METHOD_RUN_TASK).arg(//
    annotation.id()).arg(//
    _new(anonymousRunnableClass)).arg(lit(delay)));
}
Also used : UiThreadExecutor(org.androidannotations.api.UiThreadExecutor) JDefinedClass(com.helger.jcodemodel.JDefinedClass) UiThread(org.androidannotations.annotations.UiThread) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod)

Example 70 with JBlock

use of com.helger.jcodemodel.JBlock in project androidannotations by androidannotations.

the class UiThreadHandler method addUIThreadCheck.

/**
	 * Add the pre-check to see if we are already in the UI thread.
	 *
	 * @param delegatingMethod
	 * @param holder
	 * @throws JClassAlreadyExistsException
	 */
private void addUIThreadCheck(JMethod delegatingMethod, JBlock previousBody, EComponentHolder holder) throws JClassAlreadyExistsException {
    // Get the Thread and Looper class.
    AbstractJClass tClass = getClasses().THREAD;
    AbstractJClass lClass = getClasses().LOOPER;
    // invoke the methods.
    IJExpression lhs = tClass.staticInvoke(METHOD_CUR_THREAD);
    IJExpression rhs = lClass.staticInvoke(METHOD_MAIN_LOOPER).invoke(METHOD_GET_THREAD);
    // create the conditional and the block.
    JConditional con = delegatingMethod.body()._if(JOp.eq(lhs, rhs));
    JBlock thenBlock = con._then().add(previousBody);
    thenBlock._return();
}
Also used : IJExpression(com.helger.jcodemodel.IJExpression) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional)

Aggregations

JBlock (com.helger.jcodemodel.JBlock)148 JVar (com.helger.jcodemodel.JVar)81 JMethod (com.helger.jcodemodel.JMethod)73 JInvocation (com.helger.jcodemodel.JInvocation)37 AbstractJClass (com.helger.jcodemodel.AbstractJClass)35 IJExpression (com.helger.jcodemodel.IJExpression)31 ExecutableElement (javax.lang.model.element.ExecutableElement)25 JFieldRef (com.helger.jcodemodel.JFieldRef)19 JDefinedClass (com.helger.jcodemodel.JDefinedClass)15 VariableElement (javax.lang.model.element.VariableElement)15 TypeMirror (javax.lang.model.type.TypeMirror)13 JFieldVar (com.helger.jcodemodel.JFieldVar)11 JConditional (com.helger.jcodemodel.JConditional)10 JTryBlock (com.helger.jcodemodel.JTryBlock)9 JCatchBlock (com.helger.jcodemodel.JCatchBlock)4 TypeElement (javax.lang.model.element.TypeElement)3 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3 TextWatcherHolder (org.androidannotations.holder.TextWatcherHolder)3 AbstractJType (com.helger.jcodemodel.AbstractJType)2 IJAssignmentTarget (com.helger.jcodemodel.IJAssignmentTarget)2