Search in sources :

Example 26 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 27 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)

Example 28 with JVar

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

the class AbstractRestMethodWithParameterHandler method getRequestEntity.

@Override
protected IJExpression getRequestEntity(ExecutableElement element, RestHolder holder, JBlock methodBody, SortedMap<String, JVar> params) {
    JVar httpHeaders = restAnnotationHelper.declareHttpHeaders(element, holder, methodBody);
    JVar entitySentToServer = restAnnotationHelper.getEntitySentToServer(element, params);
    if (entitySentToServer == null) {
        Map<String, String> parameters = restAnnotationHelper.extractFieldAndPartParameters(element);
        if (parameters != null) {
            AbstractJClass hashMapClass = getJClass(RestSpringClasses.LINKED_MULTI_VALUE_MAP).narrow(String.class, Object.class);
            entitySentToServer = methodBody.decl(hashMapClass, "parameters", JExpr._new(hashMapClass));
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                methodBody.add(entitySentToServer.invoke("add").arg(JExpr.lit(parameter.getKey())).arg(params.get(parameter.getValue())));
            }
        }
    }
    return restAnnotationHelper.declareHttpEntity(methodBody, entitySentToServer, httpHeaders);
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) Map(java.util.Map) SortedMap(java.util.SortedMap) JVar(com.helger.jcodemodel.JVar)

Example 29 with JVar

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

the class NonConfigurationInstanceHandler method retainInOnRetain.

private void retainInOnRetain(EActivityHolder holder, String fieldName, JFieldVar ncHolderField) throws JClassAlreadyExistsException {
    JBlock onRetainNonConfigurationInstanceBindBlock = holder.getOnRetainNonConfigurationInstanceBindBlock();
    JVar onRetainNonConfigurationInstance = holder.getOnRetainNonConfigurationInstance();
    onRetainNonConfigurationInstanceBindBlock.assign(onRetainNonConfigurationInstance.ref(ncHolderField), ref(fieldName));
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 30 with JVar

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

the class NonConfigurationInstanceHandler method injectInInit.

private void injectInInit(Element element, EActivityHolder holder, String fieldName, JFieldVar ncHolderField) throws JClassAlreadyExistsException {
    JBlock initIfNonConfigurationNotNullBlock = holder.getInitIfNonConfigurationNotNullBlock();
    JVar initNonConfigurationInstance = holder.getInitNonConfigurationInstance();
    initIfNonConfigurationNotNullBlock.assign(ref(fieldName), initNonConfigurationInstance.ref(ncHolderField));
    rebindContextIfBean(element, initIfNonConfigurationNotNullBlock, ncHolderField);
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Aggregations

JVar (com.helger.jcodemodel.JVar)119 JBlock (com.helger.jcodemodel.JBlock)81 JMethod (com.helger.jcodemodel.JMethod)56 AbstractJClass (com.helger.jcodemodel.AbstractJClass)48 JInvocation (com.helger.jcodemodel.JInvocation)38 IJExpression (com.helger.jcodemodel.IJExpression)32 VariableElement (javax.lang.model.element.VariableElement)25 ExecutableElement (javax.lang.model.element.ExecutableElement)18 TypeMirror (javax.lang.model.type.TypeMirror)16 JDefinedClass (com.helger.jcodemodel.JDefinedClass)15 JFieldRef (com.helger.jcodemodel.JFieldRef)15 JFieldVar (com.helger.jcodemodel.JFieldVar)12 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 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3