Search in sources :

Example 1 with JConditional

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());
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 2 with JConditional

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;
}
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 3 with JConditional

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));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) JFieldVar(com.helger.jcodemodel.JFieldVar) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) BundleHelper(org.androidannotations.helper.BundleHelper) JVar(com.helger.jcodemodel.JVar)

Example 4 with JConditional

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);
}
Also used : IgnoreWhen(org.androidannotations.annotations.IgnoreWhen) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod)

Example 5 with JConditional

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);
}
Also used : IJStatement(com.helger.jcodemodel.IJStatement) TypeMirror(javax.lang.model.type.TypeMirror) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional) NonConfigurationInstance(org.androidannotations.annotations.NonConfigurationInstance)

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