Search in sources :

Example 6 with JConditional

use of com.helger.jcodemodel.JConditional 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 7 with JConditional

use of com.helger.jcodemodel.JConditional 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 8 with JConditional

use of com.helger.jcodemodel.JConditional 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 9 with JConditional

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

Example 10 with JConditional

use of com.helger.jcodemodel.JConditional in project adt4j by sviperll.

the class EqualsMethod method appendNullableValue.

void appendNullableValue(AbstractJType type, IJExpression value1, IJExpression value2) {
    if (!type.isReference()) {
        throw new AssertionError("appendNullableValue called for non-reference type");
    } else {
        JConditional _if = body._if(value1.eq(JExpr._null()));
        JConditional _if1 = _if._then()._if(value2.ne(JExpr._null()));
        _if1._then()._return(JExpr.FALSE);
        EqualsMethod innerBody = new EqualsMethod(types, _if._else(), nameSource, floatCustomization);
        innerBody.appendNotNullValue(type, value1, value2);
    }
}
Also used : JConditional(com.helger.jcodemodel.JConditional)

Aggregations

JConditional (com.helger.jcodemodel.JConditional)18 JInvocation (com.helger.jcodemodel.JInvocation)11 JBlock (com.helger.jcodemodel.JBlock)10 JVar (com.helger.jcodemodel.JVar)9 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 JMethod (com.helger.jcodemodel.JMethod)8 IJExpression (com.helger.jcodemodel.IJExpression)6 JDefinedClass (com.helger.jcodemodel.JDefinedClass)3 JTryBlock (com.helger.jcodemodel.JTryBlock)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 IJStatement (com.helger.jcodemodel.IJStatement)1 JCatchBlock (com.helger.jcodemodel.JCatchBlock)1 JClassAlreadyExistsException (com.helger.jcodemodel.JClassAlreadyExistsException)1 JFieldVar (com.helger.jcodemodel.JFieldVar)1 IOException (java.io.IOException)1 IgnoreWhen (org.androidannotations.annotations.IgnoreWhen)1 NonConfigurationInstance (org.androidannotations.annotations.NonConfigurationInstance)1 Trace (org.androidannotations.annotations.Trace)1 WakeLock (org.androidannotations.annotations.WakeLock)1