Search in sources :

Example 1 with JVar

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

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

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

use of com.helger.jcodemodel.JVar 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 5 with JVar

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

the class ViewBindingObjectClassGen method defineSimpleOneWayPropertyClasses.

/**
 *    	private static class ImageAlphaAttribute implements OneWayPropertyViewAttribute<ImageView, Integer> {
 *    		@Override
 *    		public void updateView(ImageView view, Integer newValue) {
 *    			view.setImageAlpha(newValue);
 *    		}
 *    	}
 */
public void defineSimpleOneWayPropertyClasses() {
    for (SimpleOneWayPropertyInfo propInfo : simpleOneWayPropertyInfoList) {
        JDefinedClass definedBindingAttributeClass = propInfo.defineBindingClass(new ClassDefinitionCallback() {

            @Override
            public JDefinedClass define(String typeName) {
                try {
                    return definedClass._class(JMod.PUBLIC | JMod.STATIC, typeName);
                } catch (JClassAlreadyExistsException e) {
                    throw new RuntimeException("Class '" + typeName + "' already exists", e);
                }
            }
        });
        AbstractJClass propertyClass = codeModel.ref(propInfo.propertyType());
        AbstractJClass oneWayPropertyInterface = codeModel.ref(OneWayPropertyViewAttribute.class).narrow(viewClass, propertyClass);
        definedBindingAttributeClass._implements(oneWayPropertyInterface);
        JMethod method = definedBindingAttributeClass.method(JMod.PUBLIC, codeModel.VOID, "updateView");
        method.annotate(Override.class);
        JVar viewParam = method.param(viewClass, "view");
        JVar newValueParam = method.param(propertyClass, "newValue");
        JBlock body = method.body();
        body.invoke(viewParam, propInfo.propertySetter()).arg(newValueParam);
    }
}
Also used : JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) OneWayPropertyViewAttribute(org.robobinding.viewattribute.property.OneWayPropertyViewAttribute) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Aggregations

JVar (com.helger.jcodemodel.JVar)126 JBlock (com.helger.jcodemodel.JBlock)86 JMethod (com.helger.jcodemodel.JMethod)61 AbstractJClass (com.helger.jcodemodel.AbstractJClass)53 JInvocation (com.helger.jcodemodel.JInvocation)39 IJExpression (com.helger.jcodemodel.IJExpression)33 VariableElement (javax.lang.model.element.VariableElement)25 ExecutableElement (javax.lang.model.element.ExecutableElement)19 JFieldRef (com.helger.jcodemodel.JFieldRef)16 TypeMirror (javax.lang.model.type.TypeMirror)16 JDefinedClass (com.helger.jcodemodel.JDefinedClass)15 JFieldVar (com.helger.jcodemodel.JFieldVar)15 JConditional (com.helger.jcodemodel.JConditional)9 JTryBlock (com.helger.jcodemodel.JTryBlock)7 JCatchBlock (com.helger.jcodemodel.JCatchBlock)5 AbstractJType (com.helger.jcodemodel.AbstractJType)4 ArrayList (java.util.ArrayList)4 BundleHelper (org.androidannotations.helper.BundleHelper)4 JTypeVar (com.helger.jcodemodel.JTypeVar)3 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)2