Search in sources :

Example 1 with JBlock

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

the class EBeanHolder method setConstructor.

private void setConstructor() {
    constructor = generatedClass.constructor(PRIVATE);
    JVar constructorContextParam = constructor.param(getClasses().CONTEXT, "context");
    JBlock constructorBody = constructor.body();
    List<ExecutableElement> constructors = ElementFilter.constructorsIn(annotatedElement.getEnclosedElements());
    ExecutableElement superConstructor = constructors.get(0);
    if (superConstructor.getParameters().size() == 1) {
        constructorBody.invoke("super").arg(constructorContextParam);
    }
    constructorBody.assign(getContextField(), constructorContextParam);
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 2 with JBlock

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

the class EBeanHolder method createFactoryMethod.

public void createFactoryMethod(boolean hasSingletonScope) {
    AbstractJClass narrowedGeneratedClass = codeModelHelper.narrowGeneratedClass(generatedClass, annotatedElement.asType());
    JMethod factoryMethod = generatedClass.method(PUBLIC | STATIC, narrowedGeneratedClass, GET_INSTANCE_METHOD_NAME);
    codeModelHelper.generify(factoryMethod, annotatedElement);
    JVar factoryMethodContextParam = factoryMethod.param(getClasses().CONTEXT, "context");
    JBlock factoryMethodBody = factoryMethod.body();
    /*
		 * Singletons are bound to the application context
		 */
    if (hasSingletonScope) {
        JFieldVar instanceField = generatedClass.field(PRIVATE | STATIC, generatedClass, "instance" + generationSuffix());
        JBlock creationBlock = //
        factoryMethodBody._if(//
        instanceField.eq(_null()))._then();
        JVar previousNotifier = viewNotifierHelper.replacePreviousNotifierWithNull(creationBlock);
        creationBlock.assign(instanceField, _new(narrowedGeneratedClass).arg(factoryMethodContextParam.invoke("getApplicationContext")));
        creationBlock.invoke(instanceField, getInit());
        viewNotifierHelper.resetPreviousNotifier(creationBlock, previousNotifier);
        factoryMethodBody._return(instanceField);
    } else {
        factoryMethodBody._return(_new(narrowedGeneratedClass).arg(factoryMethodContextParam));
    }
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 3 with JBlock

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

the class EFragmentHolder method setFindViewById.

private void setFindViewById() {
    JMethod findViewById = generatedClass.method(PUBLIC, getClasses().VIEW, "findViewById");
    findViewById.annotate(Override.class);
    JVar idParam = findViewById.param(getCodeModel().INT, "id");
    JBlock body = findViewById.body();
    JFieldVar contentView = getContentView();
    //
    body._if(contentView.eq(_null()))._then()._return(_null());
    body._return(contentView.invoke(findViewById).arg(idParam));
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 4 with JBlock

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

the class OrmLiteHolder method injectReleaseInOnDestroy.

private void injectReleaseInOnDestroy(JFieldVar databaseHelperRef) {
    if (holder() instanceof HasLifecycleMethods) {
        JBlock destroyBody = ((HasLifecycleMethods) holder()).getOnDestroyBeforeSuperBlock();
        destroyBody.staticInvoke(getJClass(OrmLiteClasses.OPEN_HELPER_MANAGER), "releaseHelper");
        destroyBody.assign(databaseHelperRef, _null());
    }
}
Also used : HasLifecycleMethods(org.androidannotations.holder.HasLifecycleMethods) JBlock(com.helger.jcodemodel.JBlock)

Example 5 with JBlock

use of com.helger.jcodemodel.JBlock 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)

Aggregations

JBlock (com.helger.jcodemodel.JBlock)155 JVar (com.helger.jcodemodel.JVar)86 JMethod (com.helger.jcodemodel.JMethod)75 AbstractJClass (com.helger.jcodemodel.AbstractJClass)40 JInvocation (com.helger.jcodemodel.JInvocation)38 IJExpression (com.helger.jcodemodel.IJExpression)31 ExecutableElement (javax.lang.model.element.ExecutableElement)26 JFieldRef (com.helger.jcodemodel.JFieldRef)19 JDefinedClass (com.helger.jcodemodel.JDefinedClass)15 VariableElement (javax.lang.model.element.VariableElement)15 JFieldVar (com.helger.jcodemodel.JFieldVar)13 TypeMirror (javax.lang.model.type.TypeMirror)13 JConditional (com.helger.jcodemodel.JConditional)10 JTryBlock (com.helger.jcodemodel.JTryBlock)9 JCatchBlock (com.helger.jcodemodel.JCatchBlock)4 TypeElement (javax.lang.model.element.TypeElement)3 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3 AbstractJType (com.helger.jcodemodel.AbstractJType)2 IJAssignmentTarget (com.helger.jcodemodel.IJAssignmentTarget)2 IJStatement (com.helger.jcodemodel.IJStatement)2