Search in sources :

Example 26 with JDefinedClass

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

the class RestHolder method implementSetBearerAuth.

private void implementSetBearerAuth(List<ExecutableElement> methods) {
    JMethod setBearerMethod = codeModelHelper.implementMethod(this, methods, "setBearerAuth", TypeKind.VOID.toString(), true, STRING);
    if (setBearerMethod != null) {
        JVar tokenParamVar = setBearerMethod.params().get(0);
        IJExpression tokenExpr = lit("Bearer ").plus(tokenParamVar);
        AbstractJClass authClass = getJClass(HTTP_AUTHENTICATION);
        JDefinedClass anonymousHttpAuthClass = getCodeModel().anonymousClass(authClass);
        JMethod getHeaderValueMethod = anonymousHttpAuthClass.method(JMod.PUBLIC, String.class, "getHeaderValue");
        getHeaderValueMethod.annotate(Override.class);
        JBlock getHeaderValueMethodBody = getHeaderValueMethod.body();
        getHeaderValueMethodBody._return(tokenExpr);
        JBlock setBearerBody = setBearerMethod.body();
        setBearerBody.assign(_this().ref(getAuthenticationField()), _new(anonymousHttpAuthClass));
    }
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) IJExpression(com.helger.jcodemodel.IJExpression) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 27 with JDefinedClass

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

the class BackgroundHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousMethodBody = codeModelHelper.removeBody(delegatingMethod);
    JDefinedClass anonymousTaskClass = getCodeModel().anonymousClass(BackgroundExecutor.Task.class);
    JMethod executeMethod = anonymousTaskClass.method(JMod.PUBLIC, getCodeModel().VOID, "execute");
    executeMethod.annotate(Override.class);
    // Catch exception in user code
    JTryBlock tryBlock = executeMethod.body()._try();
    tryBlock.body().add(previousMethodBody);
    JCatchBlock catchBlock = tryBlock._catch(getClasses().THROWABLE);
    JVar caughtException = catchBlock.param("e");
    IJStatement uncaughtExceptionCall = // 
    getClasses().THREAD.staticInvoke(// 
    "getDefaultUncaughtExceptionHandler").invoke(// 
    "uncaughtException").arg(// 
    getClasses().THREAD.staticInvoke("currentThread")).arg(caughtException);
    catchBlock.body().add(uncaughtExceptionCall);
    Background annotation = element.getAnnotation(Background.class);
    String id = annotation.id();
    long delay = annotation.delay();
    String serial = annotation.serial();
    AbstractJClass backgroundExecutorClass = getJClass(BackgroundExecutor.class);
    JInvocation newTask = _new(anonymousTaskClass).arg(lit(id)).arg(lit(delay)).arg(lit(serial));
    JInvocation executeCall = backgroundExecutorClass.staticInvoke("execute").arg(newTask);
    delegatingMethod.body().add(executeCall);
}
Also used : BackgroundExecutor(org.androidannotations.api.BackgroundExecutor) Background(org.androidannotations.annotations.Background) JDefinedClass(com.helger.jcodemodel.JDefinedClass) ExecutableElement(javax.lang.model.element.ExecutableElement) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) IJStatement(com.helger.jcodemodel.IJStatement) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) JVar(com.helger.jcodemodel.JVar)

Example 28 with JDefinedClass

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

the class EActivityHolder method setInitNonConfigurationInstance.

private void setInitNonConfigurationInstance() throws JClassAlreadyExistsException {
    JBlock initBody = getInitBodyInjectionBlock();
    JDefinedClass ncHolderClass = getNonConfigurationHolder().getGeneratedClass();
    initNonConfigurationInstance = initBody.decl(ncHolderClass, "nonConfigurationInstance", cast(ncHolderClass, _super().invoke(getGetLastNonConfigurationInstance())));
    initIfNonConfigurationNotNullBlock = initBody._if(initNonConfigurationInstance.ne(_null()))._then();
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock)

Example 29 with JDefinedClass

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

the class EActivityHolder method setGetLastNonConfigurationInstance.

private void setGetLastNonConfigurationInstance() throws JClassAlreadyExistsException {
    AnnotationHelper annotationHelper = new AnnotationHelper(getEnvironment());
    TypeElement fragmentActivityTypeElement = getFragmentActivity(annotationHelper);
    TypeElement typeElement = annotationHelper.typeElementFromQualifiedName(generatedClass._extends().fullName());
    String getLastNonConfigurationInstanceName = "getLastNonConfigurationInstance";
    if (fragmentActivityTypeElement != null && annotationHelper.isSubtype(typeElement.asType(), fragmentActivityTypeElement.asType())) {
        getLastNonConfigurationInstanceName = "getLastCustomNonConfigurationInstance";
    }
    NonConfigurationHolder ncHolder = getNonConfigurationHolder();
    JDefinedClass ncHolderClass = ncHolder.getGeneratedClass();
    JFieldVar superNonConfigurationInstanceField = ncHolder.getSuperNonConfigurationInstanceField();
    getLastNonConfigurationInstance = generatedClass.method(PUBLIC, Object.class, getLastNonConfigurationInstanceName);
    getLastNonConfigurationInstance.annotate(Override.class);
    JBlock body = getLastNonConfigurationInstance.body();
    JVar nonConfigurationInstance = body.decl(ncHolderClass, "nonConfigurationInstance", cast(ncHolderClass, _super().invoke(getLastNonConfigurationInstance)));
    body._if(nonConfigurationInstance.eq(_null()))._then()._return(_null());
    body._return(nonConfigurationInstance.ref(superNonConfigurationInstanceField));
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JFieldVar(com.helger.jcodemodel.JFieldVar) AnnotationHelper(org.androidannotations.helper.AnnotationHelper) TypeElement(javax.lang.model.element.TypeElement) JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 30 with JDefinedClass

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

the class EComponentWithViewSupportHolder method createPageChangeHolder.

private PageChangeHolder createPageChangeHolder(JFieldRef idRef, TypeMirror viewParameterType, boolean hasAddOnPageChangeListenerMethod) {
    AbstractJClass viewClass;
    JDefinedClass onPageChangeListenerClass;
    if (getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.ANDROIDX_VIEW_PAGER) == null) {
        viewClass = getClasses().VIEW_PAGER;
        onPageChangeListenerClass = getCodeModel().anonymousClass(getClasses().PAGE_CHANGE_LISTENER);
    } else {
        viewClass = getClasses().ANDROIDX_VIEW_PAGER;
        onPageChangeListenerClass = getCodeModel().anonymousClass(getClasses().ANDROIDX_PAGE_CHANGE_LISTENER);
    }
    if (viewParameterType != null) {
        viewClass = getJClass(viewParameterType.toString());
    }
    JBlock onViewChangedBody = getOnViewChangedBodyInjectionBlock().blockSimple();
    JVar viewVariable = onViewChangedBody.decl(FINAL, viewClass, "view", cast(viewClass, findViewById(idRef)));
    JBlock block = onViewChangedBody._if(viewVariable.ne(JExpr._null()))._then();
    if (hasAddOnPageChangeListenerMethod) {
        block.invoke(viewVariable, "addOnPageChangeListener").arg(_new(onPageChangeListenerClass));
    } else {
        block.invoke(viewVariable, "setOnPageChangeListener").arg(_new(onPageChangeListenerClass));
    }
    return new PageChangeHolder(this, viewVariable, onPageChangeListenerClass);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JVar(com.helger.jcodemodel.JVar)

Aggregations

JDefinedClass (com.helger.jcodemodel.JDefinedClass)31 JMethod (com.helger.jcodemodel.JMethod)20 AbstractJClass (com.helger.jcodemodel.AbstractJClass)19 JBlock (com.helger.jcodemodel.JBlock)15 JVar (com.helger.jcodemodel.JVar)15 JInvocation (com.helger.jcodemodel.JInvocation)9 IJExpression (com.helger.jcodemodel.IJExpression)5 JClassAlreadyExistsException (com.helger.jcodemodel.JClassAlreadyExistsException)5 JFieldVar (com.helger.jcodemodel.JFieldVar)5 JTypeVar (com.helger.jcodemodel.JTypeVar)5 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)4 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)3 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)3 JConditional (com.helger.jcodemodel.JConditional)3 JFieldRef (com.helger.jcodemodel.JFieldRef)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)2 MethodUsage (com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage)2 JCatchBlock (com.helger.jcodemodel.JCatchBlock)2