Search in sources :

Example 61 with JVar

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

the class ViewInstanceStateDelegate method setRestoreStateMethod.

private void setRestoreStateMethod() {
    restoreStateMethod = getGeneratedClass().method(PUBLIC, codeModel().VOID, "onRestoreInstanceState");
    restoreStateMethod.annotate(Override.class);
    JVar state = restoreStateMethod.param(getClasses().PARCELABLE, "state");
    JBlock body = restoreStateMethod.body();
    restoreStateBundleParam = body.decl(getClasses().BUNDLE, "bundle" + generationSuffix(), cast(getClasses().BUNDLE, state));
    JVar instanceState = body.decl(getClasses().PARCELABLE, "instanceState", restoreStateBundleParam.invoke("getParcelable").arg(getInstanceStateKey()));
    restoreStateMethodBody = body.blockSimple();
    body.invoke(_super(), "onRestoreInstanceState").arg(instanceState);
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 62 with JVar

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

the class KeyEventCallbackMethodsDelegate method createOnKeyDownMethod.

private void createOnKeyDownMethod() {
    JMethod method = getGeneratedClass().method(PUBLIC, codeModel().BOOLEAN, "onKeyDown");
    method.annotate(Override.class);
    JVar keyCode = method.param(codeModel().INT, "keyCode");
    onKeyDownKeyEventParam = method.param(getClasses().KEY_EVENT, "keyEvent");
    JBlock methodBody = method.body();
    onKeyDownSwitchBody = methodBody._switch(keyCode);
    methodBody._return(_super().invoke(method).arg(keyCode).arg(onKeyDownKeyEventParam));
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 63 with JVar

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

the class KeyEventCallbackMethodsDelegate method createOnKeyMultipleMethod.

private void createOnKeyMultipleMethod() {
    JMethod method = getGeneratedClass().method(PUBLIC, codeModel().BOOLEAN, "onKeyMultiple");
    method.annotate(Override.class);
    JVar keyCode = method.param(codeModel().INT, "keyCode");
    onKeyMultipleCountParam = method.param(codeModel().INT, "count");
    onKeyMultipleKeyEventParam = method.param(getClasses().KEY_EVENT, "keyEvent");
    JBlock methodBody = method.body();
    onKeyMultipleSwitchBody = methodBody._switch(keyCode);
    methodBody._return(_super().invoke(method).arg(keyCode).arg(onKeyMultipleCountParam).arg(onKeyDownKeyEventParam));
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 64 with JVar

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

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

the class RestMethodHandler method addMethodParams.

protected SortedMap<String, JVar> addMethodParams(ExecutableElement executableElement, RestHolder restHolder, JMethod method) {
    List<? extends VariableElement> params = executableElement.getParameters();
    SortedMap<String, JVar> methodParams = new TreeMap<>();
    for (VariableElement parameter : params) {
        String paramName = parameter.getSimpleName().toString();
        String paramType = parameter.asType().toString();
        JVar param;
        if (parameter.asType().getKind().isPrimitive()) {
            param = method.param(getCodeModel().parseType(paramType), paramName);
        } else {
            AbstractJClass parameterClass = codeModelHelper.typeMirrorToJClass(parameter.asType());
            param = method.param(parameterClass, paramName);
        }
        methodParams.put(paramName, param);
    }
    return methodParams;
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) VariableElement(javax.lang.model.element.VariableElement) TreeMap(java.util.TreeMap) JVar(com.helger.jcodemodel.JVar)

Aggregations

JVar (com.helger.jcodemodel.JVar)126 JBlock (com.helger.jcodemodel.JBlock)86 JMethod (com.helger.jcodemodel.JMethod)61 AbstractJClass (com.helger.jcodemodel.AbstractJClass)53 JInvocation (com.helger.jcodemodel.JInvocation)39 IJExpression (com.helger.jcodemodel.IJExpression)33 VariableElement (javax.lang.model.element.VariableElement)25 ExecutableElement (javax.lang.model.element.ExecutableElement)19 JFieldRef (com.helger.jcodemodel.JFieldRef)16 TypeMirror (javax.lang.model.type.TypeMirror)16 JDefinedClass (com.helger.jcodemodel.JDefinedClass)15 JFieldVar (com.helger.jcodemodel.JFieldVar)15 JConditional (com.helger.jcodemodel.JConditional)9 JTryBlock (com.helger.jcodemodel.JTryBlock)7 JCatchBlock (com.helger.jcodemodel.JCatchBlock)5 AbstractJType (com.helger.jcodemodel.AbstractJType)4 ArrayList (java.util.ArrayList)4 BundleHelper (org.androidannotations.helper.BundleHelper)4 JTypeVar (com.helger.jcodemodel.JTypeVar)3 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)2