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;
}
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;
}
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);
}
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));
}
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);
}
Aggregations