use of com.helger.jcodemodel.JConditional in project RoboBinding by RoboBinding.
the class AbstractPresentationModelObjectClassGen method defineTryToCreateFunction.
/*
@Override
public Function tryToCreateFunction(MethodDescriptor methodDescriptor) {
if(methodDescriptor.equals(createMethodDescriptor(ON_CLICK))) {
return new Function() {
@Override
public Object call(Object... args) {
presentationModel.onClick();
return null;
}
};
}
if(methodDescriptor.equals(createMethodDescriptor(ON_CLICK_WITH_EVENT, AbstractViewEvent.class))){
return new Function() {
@Override
public Object call(Object... args) {
boolean result = presentationModel.onLongClickWithEvent((AbstractViewEvent)args[0]);
return (Boolean)result;
}
};
}
return null;
}
*/
public void defineTryToCreateFunction() {
JMethod method = declarePublicMethodOverride("tryToCreateFunction", Function.class);
JVar methodDescriptorParam = method.param(MethodDescriptor.class, "methodDescriptor");
JBlock body = method.body();
for (EventMethodInfo eventMethodInfo : presentationModelInfo.eventMethods()) {
JInvocation getMethod = JExpr.invoke("createMethodDescriptor").arg(eventMethodInfo.name());
if (eventMethodInfo.hasEventArg()) {
getMethod.arg(codeModel.ref(eventMethodInfo.eventArgType()).dotclass());
}
JConditional conditional = body._if(methodDescriptorParam.invoke("equals").arg(getMethod));
JBlock conditionalBody = conditional._then();
//create Function.
JDefinedClass anonymousFunction = codeModel.anonymousClass(Function.class);
JMethod call = declarePublicMethodOverride(anonymousFunction, "call", Object.class);
JVar argsVar = call.varParam(Object.class, "args");
JBlock callBody = call.body();
//call event method.
JInvocation onEvent = presentationModelFieldWithoutThis.invoke(eventMethodInfo.name());
if (eventMethodInfo.hasEventArg()) {
AbstractJClass eventArgClass = codeModel.ref(eventMethodInfo.eventArgType());
onEvent.arg(JExpr.cast(eventArgClass, argsVar.component(JExpr.lit(0))));
}
//call return.
if (eventMethodInfo.hasReturn()) {
JVar returnVar = callBody.decl(codeModel.ref(eventMethodInfo.nonPrimitiveReturnType()), "result", onEvent);
callBody._return(returnVar);
} else {
callBody.add(onEvent);
callBody._return(JExpr._null());
}
//return Function.
conditionalBody._return(JExpr._new(anonymousFunction));
}
body._return(JExpr._null());
}
use of com.helger.jcodemodel.JConditional 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.JConditional in project androidannotations by androidannotations.
the class FragmentArgHandler method assignValue.
@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EFragmentHolder holder, Element element, Element param) {
String fieldName = element.getSimpleName().toString();
String argKey = extractArgKey(element, fieldName);
if (element.getKind() != ElementKind.PARAMETER) {
createBuilderInjectionMethod(holder, element, new ArgHelper(param, argKey));
}
TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, param);
AbstractJClass elementClass = codeModelHelper.typeMirrorToJClass(actualType);
BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
JVar bundle = holder.getInjectBundleArgs();
JMethod injectExtrasMethod = holder.getInjectArgsMethod();
JFieldVar extraKeyStaticField = getOrCreateStaticArgField(holder, argKey, fieldName);
IJExpression restoreMethodCall = bundleHelper.getExpressionToRestoreFromBundle(elementClass, bundle, extraKeyStaticField, injectExtrasMethod);
JConditional conditional = targetBlock._if(JExpr.invoke(bundle, "containsKey").arg(extraKeyStaticField));
conditional._then().add(fieldRef.assign(restoreMethodCall));
}
use of com.helger.jcodemodel.JConditional in project androidannotations by androidannotations.
the class IgnoreWhenHandler method process.
@Override
public void process(Element element, EFragmentHolder holder) throws Exception {
ExecutableElement executableElement = (ExecutableElement) element;
JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
JBlock previousMethodBody = codeModelHelper.removeBody(delegatingMethod);
IgnoreWhen ignoreWhen = element.getAnnotation(IgnoreWhen.class);
JBlock methodBody = delegatingMethod.body();
JConditional conditional = null;
switch(ignoreWhen.value()) {
case VIEW_DESTROYED:
conditional = methodBody._if(holder.getViewDestroyedField().not());
break;
case DETACHED:
conditional = methodBody._if(invoke(holder.getGeneratedClass().staticRef("this"), "getActivity").ne(_null()));
break;
}
conditional._then().add(previousMethodBody);
}
use of com.helger.jcodemodel.JConditional in project androidannotations by androidannotations.
the class BeanHandler method assignValue.
@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) {
TypeMirror typeMirror = annotationHelper.extractAnnotationClassParameter(element);
if (typeMirror == null) {
typeMirror = param.asType();
typeMirror = getProcessingEnvironment().getTypeUtils().erasure(typeMirror);
}
String typeQualifiedName = typeMirror.toString();
AbstractJClass injectedClass = getJClass(annotationHelper.generatedClassQualifiedNameFromQualifiedName(typeQualifiedName));
JInvocation beanInstance = injectedClass.staticInvoke(EBeanHolder.GET_INSTANCE_METHOD_NAME).arg(holder.getContextRef());
IJStatement assignment = fieldRef.assign(beanInstance);
if (param.getKind() == ElementKind.FIELD) {
boolean hasNonConfigurationInstanceAnnotation = element.getAnnotation(NonConfigurationInstance.class) != null;
if (hasNonConfigurationInstanceAnnotation) {
JConditional conditional = targetBlock._if(fieldRef.eq(_null()));
conditional._then().add(assignment);
assignment = conditional;
}
}
targetBlock.add(assignment);
}
Aggregations