Search in sources :

Example 61 with JMethod

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

the class TransactionalHandler method process.

@Override
public void process(Element element, EComponentHolder holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    String returnTypeName = executableElement.getReturnType().toString();
    AbstractJClass returnType = getJClass(returnTypeName);
    JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    codeModelHelper.removeBody(method);
    JVar db = method.params().get(0);
    JBlock body = method.body();
    body.invoke(db, "beginTransaction");
    JTryBlock tryBlock = body._try();
    IJExpression activitySuper = holder.getGeneratedClass().staticRef("super");
    JInvocation superCall = JExpr.invoke(activitySuper, method);
    for (JVar param : method.params()) {
        superCall.arg(param);
    }
    JBlock tryBody = tryBlock.body();
    if (returnTypeName.equals("void")) {
        tryBody.add(superCall);
        tryBody.invoke(db, "setTransactionSuccessful");
        tryBody._return();
    } else {
        JVar result = tryBody.decl(returnType, "result_", superCall);
        tryBody.invoke(db, "setTransactionSuccessful");
        tryBody._return(result);
    }
    JCatchBlock catchBlock = tryBlock._catch(getJClass(RuntimeException.class));
    JVar exceptionParam = catchBlock.param("e");
    JBlock catchBody = catchBlock.body();
    JInvocation errorInvoke = catchBody.staticInvoke(getClasses().LOG, "e");
    errorInvoke.arg(logTagForClassHolder(holder));
    errorInvoke.arg("Error in transaction");
    errorInvoke.arg(exceptionParam);
    catchBody._throw(exceptionParam);
    tryBlock._finally().invoke(db, "endTransaction");
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) JVar(com.helger.jcodemodel.JVar)

Example 62 with JMethod

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

the class UiThreadHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousBody = codeModelHelper.removeBody(delegatingMethod);
    JDefinedClass anonymousRunnableClass = codeModelHelper.createDelegatingAnonymousRunnableClass(previousBody);
    UiThread annotation = element.getAnnotation(UiThread.class);
    long delay = annotation.delay();
    UiThread.Propagation propagation = annotation.propagation();
    if (delay == 0 && propagation == UiThread.Propagation.REUSE) {
        // Put in the check for the UI thread.
        addUIThreadCheck(delegatingMethod, previousBody, holder);
    }
    delegatingMethod.body().add(//
    getJClass(UiThreadExecutor.class).staticInvoke(METHOD_RUN_TASK).arg(//
    annotation.id()).arg(//
    _new(anonymousRunnableClass)).arg(lit(delay)));
}
Also used : UiThreadExecutor(org.androidannotations.api.UiThreadExecutor) JDefinedClass(com.helger.jcodemodel.JDefinedClass) UiThread(org.androidannotations.annotations.UiThread) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod)

Example 63 with JMethod

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

the class SupposeUiThreadHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock body = delegatingMethod.body();
    AbstractJClass bgExecutor = getJClass(BackgroundExecutor.class);
    body.pos(0);
    body.staticInvoke(bgExecutor, METHOD_CHECK_UI_THREAD);
    body.pos(body.getContents().size());
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod)

Example 64 with JMethod

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

Example 65 with JMethod

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

the class AbstractPresentationModelObjectClassGen method definePropertyNames.

/*
	 	@Override
	 	public Set<String> propertyNames() {
	 		return Sets.newHasSet("prop1", "prop2", ...);
	 	}

	 */
public void definePropertyNames() {
    JMethod method = declarePublicMethodOverride("propertyNames", setClassWithString);
    method.body()._return(newHashSetInvocation(presentationModelInfo.propertyNames()));
}
Also used : JMethod(com.helger.jcodemodel.JMethod)

Aggregations

JMethod (com.helger.jcodemodel.JMethod)122 JBlock (com.helger.jcodemodel.JBlock)73 JVar (com.helger.jcodemodel.JVar)56 AbstractJClass (com.helger.jcodemodel.AbstractJClass)36 JInvocation (com.helger.jcodemodel.JInvocation)26 JDefinedClass (com.helger.jcodemodel.JDefinedClass)20 IJExpression (com.helger.jcodemodel.IJExpression)15 ExecutableElement (javax.lang.model.element.ExecutableElement)13 JFieldVar (com.helger.jcodemodel.JFieldVar)10 JConditional (com.helger.jcodemodel.JConditional)8 JTryBlock (com.helger.jcodemodel.JTryBlock)7 JTypeVar (com.helger.jcodemodel.JTypeVar)7 TypeMirror (javax.lang.model.type.TypeMirror)7 VariableElement (javax.lang.model.element.VariableElement)6 JFieldRef (com.helger.jcodemodel.JFieldRef)5 JPrimitiveType (com.helger.jcodemodel.JPrimitiveType)5 ArrayList (java.util.ArrayList)5 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)4 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)4 AbstractJType (com.helger.jcodemodel.AbstractJType)4