Search in sources :

Example 11 with JConditional

use of com.helger.jcodemodel.JConditional in project adt4j by sviperll.

the class EqualsMethod method appendNonnullPrimitive.

private void appendNonnullPrimitive(AbstractJType type, IJExpression value1, IJExpression value2, boolean isLast) {
    IJExpression equalsCondition;
    IJExpression notEqualsCondition;
    if (!type.name().equals("float") && !type.name().equals("doable")) {
        equalsCondition = value1.eq(value2);
        notEqualsCondition = value1.ne(value2);
    } else {
        IJExpression epsilon = type.name().equals("float") ? JExpr.lit(floatCustomization.floatEpsilon()) : JExpr.lit(floatCustomization.doubleEpsilon());
        JInvocation invocation = types._Math.staticInvoke("abs");
        invocation.arg(value1.minus(value2));
        equalsCondition = invocation.lte(epsilon);
        notEqualsCondition = invocation.gt(epsilon);
    }
    if (isLast) {
        body._return(equalsCondition);
    } else {
        JConditional _if = body._if(notEqualsCondition);
        _if._then()._return(JExpr.FALSE);
    }
}
Also used : IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional)

Example 12 with JConditional

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

the class RootContextHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EBeanHolder holder, Element element, Element param) {
    TypeMirror elementType = param.asType();
    String typeQualifiedName = elementType.toString();
    IJExpression contextRef = holder.getContextRef();
    if (CanonicalNameConstants.CONTEXT.equals(typeQualifiedName)) {
        targetBlock.add(fieldRef.assign(contextRef));
    } else {
        AbstractJClass extendingContextClass = getEnvironment().getJClass(typeQualifiedName);
        JConditional cond = getInvocationBlock(holder)._if(holder.getContextRef()._instanceof(extendingContextClass));
        cond._then().add(fieldRef.assign(cast(extendingContextClass, holder.getContextRef())));
        JInvocation warningInvoke = getClasses().LOG.staticInvoke("w");
        warningInvoke.arg(logTagForClassHolder(holder));
        warningInvoke.arg(lit("Due to Context class ").plus(holder.getContextRef().invoke("getClass").invoke("getSimpleName")).plus(lit(", the @RootContext " + extendingContextClass.name() + " won't be populated")));
        cond._else().add(warningInvoke);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JInvocation(com.helger.jcodemodel.JInvocation)

Example 13 with JConditional

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

the class ActivityIntentBuilder method overrideStartForResultMethod.

private void overrideStartForResultMethod() {
    AbstractJClass postActivityStarterClass = environment.getJClass(PostActivityStarter.class);
    JMethod method = holder.getIntentBuilderClass().method(PUBLIC, postActivityStarterClass, "startForResult");
    method.annotate(Override.class);
    JVar requestCode = method.param(environment.getCodeModel().INT, "requestCode");
    JBlock body = method.body();
    JConditional condition = null;
    if (fragmentSupportField != null) {
        condition = body._if(fragmentSupportField.ne(JExpr._null()));
        //
        condition._then().invoke(fragmentSupportField, "startActivityForResult").arg(intentField).arg(requestCode);
    }
    if (fragmentField != null) {
        if (condition == null) {
            condition = body._if(fragmentField.ne(JExpr._null()));
        } else {
            condition = condition._elseif(fragmentField.ne(JExpr._null()));
        }
        JBlock fragmentStartForResultInvocationBlock;
        if (hasActivityOptionsInFragment() && shouldGuardActivityOptions()) {
            fragmentStartForResultInvocationBlock = createCallWithIfGuard(requestCode, condition._then(), fragmentField);
        } else {
            fragmentStartForResultInvocationBlock = condition._then();
        }
        JInvocation invocation = //
        fragmentStartForResultInvocationBlock.invoke(fragmentField, "startActivityForResult").arg(intentField).arg(requestCode);
        if (hasActivityOptionsInFragment()) {
            invocation.arg(optionsField);
        }
    }
    JBlock activityStartInvocationBlock = null;
    if (condition != null) {
        activityStartInvocationBlock = condition._else();
    } else {
        activityStartInvocationBlock = method.body();
    }
    JConditional activityCondition = activityStartInvocationBlock._if(contextField._instanceof(getClasses().ACTIVITY));
    JBlock thenBlock = activityCondition._then();
    JVar activityVar = thenBlock.decl(getClasses().ACTIVITY, "activity", JExpr.cast(getClasses().ACTIVITY, contextField));
    if (hasActivityCompatInClasspath() && hasActivityOptionsInActivityCompat()) {
        //
        thenBlock.staticInvoke(getClasses().ACTIVITY_COMPAT, "startActivityForResult").arg(activityVar).arg(intentField).arg(requestCode).arg(optionsField);
    } else if (hasActivityOptionsInFragment()) {
        JBlock startForResultInvocationBlock;
        if (shouldGuardActivityOptions()) {
            startForResultInvocationBlock = createCallWithIfGuard(requestCode, thenBlock, activityVar);
        } else {
            startForResultInvocationBlock = thenBlock;
        }
        //
        startForResultInvocationBlock.invoke(activityVar, "startActivityForResult").arg(intentField).arg(requestCode).arg(optionsField);
    } else {
        thenBlock.invoke(activityVar, "startActivityForResult").arg(intentField).arg(requestCode);
    }
    if (hasActivityOptionsInFragment()) {
        JBlock startInvocationBlock;
        if (shouldGuardActivityOptions()) {
            startInvocationBlock = createCallWithIfGuard(null, activityCondition._else(), contextField);
        } else {
            startInvocationBlock = activityCondition._else();
        }
        startInvocationBlock.invoke(contextField, "startActivity").arg(intentField).arg(optionsField);
    } else {
        activityCondition._else().invoke(contextField, "startActivity").arg(intentField);
    }
    body._return(_new(postActivityStarterClass).arg(contextField));
}
Also used : JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 14 with JConditional

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

the class ContextCompatAwareResHandler method createCallWithIfGuard.

private IJExpression createCallWithIfGuard(EComponentHolder holder, JFieldRef idRef, IJAssignmentTarget fieldRef, JBlock targetBlock) {
    JVar resourcesRef = holder.getResourcesRef();
    IJExpression buildVersionCondition = getClasses().BUILD_VERSION.staticRef("SDK_INT").gte(getClasses().BUILD_VERSION_CODES.staticRef(minSdkPlatformName));
    JConditional conditional = targetBlock._if(buildVersionCondition);
    conditional._then().add(fieldRef.assign(holder.getContextRef().invoke(androidRes.getResourceMethodName()).arg(idRef)));
    conditional._else().add(fieldRef.assign(resourcesRef.invoke(androidRes.getResourceMethodName()).arg(idRef)));
    return null;
}
Also used : IJExpression(com.helger.jcodemodel.IJExpression) JConditional(com.helger.jcodemodel.JConditional) JVar(com.helger.jcodemodel.JVar)

Example 15 with JConditional

use of com.helger.jcodemodel.JConditional in project RoboBinding by RoboBinding.

the class AbstractPresentationModelObjectClassGen method defineTryToCreateProperty.

/*
	@Override
	public SimpleProperty tryToCreateProperty(String name) {
		if(name.equals(PROP1)) {
			PropertyDescriptor descriptor = createPropertyDescriptor(String.class, name, true, true);
			
			AbstractGetSet<?> getSet = new AbstractGetSet<String>(descriptor) {
				@Override
				public String getValue() {
					return presentationModel.getProp1();
				}
				
				@Override
				public void setValue(String newValue) {
					presentationModel.setProp1(newValue);
				}
			};
			
			return new SimpleProperty(this, descriptor, getSet);
		}
		
		if(name.equals(PROP2)) {
			PropertyDescriptor descriptor = createPropertyDescriptor(Integer.class, name, true, true);
			
			AbstractGetSet<?> getSet = new AbstractGetSet<Integer>(descriptor) {
				@Override
				public Integer getValue() {
					return presentationModel.getProp2();
				}
				
				@Override
				public void setValue(Integer newValue) {
					presentationModel.setProp2(newValue);
				}
			};
			
			return new SimpleProperty(this, descriptor, getSet);
		} 
		
		return null;
	}
	 */
public void defineTryToCreateProperty() {
    try {
        JMethod method = declarePublicMethodOverride("tryToCreateProperty", SimpleProperty.class);
        JVar nameParam = method.param(String.class, "name");
        JBlock body = method.body();
        for (PropertyInfo propertyInfo : presentationModelInfo.properties()) {
            JConditional conditional = body._if(nameParam.invoke("equals").arg(propertyInfo.name()));
            JBlock conditionalBody = conditional._then();
            //create PropertyDescriptor.
            AbstractJClass propertyClass = codeModel.ref(propertyInfo.typeName());
            JInvocation createPropertyDescriptor = JExpr.invoke("createPropertyDescriptor").arg(propertyClass.dotclass()).arg(nameParam).arg(JExpr.lit(propertyInfo.isReadable())).arg(JExpr.lit(propertyInfo.isWritable()));
            JVar descriptorVar = conditionalBody.decl(propertyDescriptorClass, "descriptor", createPropertyDescriptor);
            //create AbstractGetSet.
            //JClass narrowedGetSet = getSetClass.narrow(codeModel.ref(propertyInfo.typeName()));
            AbstractJClass narrowedGetSet = getSetClass.narrow(propertyClass);
            JDefinedClass anonymousGetSet = codeModel.anonymousClass(narrowedGetSet);
            if (propertyInfo.isReadable()) {
                JMethod getter = declarePublicMethodOverride(anonymousGetSet, "getValue", propertyClass);
                getter.body()._return(presentationModelFieldWithoutThis.invoke(propertyInfo.getter()));
            }
            if (propertyInfo.isWritable()) {
                JMethod setter = declarePublicMethodOverride(anonymousGetSet, "setValue", Void.TYPE);
                JVar newValueParam = setter.param(propertyClass, "newValue");
                setter.body().add(presentationModelFieldWithoutThis.invoke(propertyInfo.setter()).arg(newValueParam));
            }
            JVar getSetVar = conditionalBody.decl(wildcardGetSetClass, "getSet", JExpr._new(anonymousGetSet).arg(descriptorVar));
            //return SimpleProperty.
            conditionalBody._return(JExpr._new(simplePropertyClass).arg(JExpr._this()).arg(descriptorVar).arg(getSetVar));
        }
        body._return(JExpr._null());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) IOException(java.io.IOException) JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) JVar(com.helger.jcodemodel.JVar)

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