Search in sources :

Example 1 with IJStatement

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

the class APTCodeModelHelper method replaceSuperCall.

public void replaceSuperCall(JMethod method, JBlock replacement) {
    String superCallStart = "super." + method.name() + "(";
    JBlock oldBody = removeBody(method);
    JBlock newBody = method.body();
    for (Object content : oldBody.getContents()) {
        StringWriter writer = new StringWriter();
        JFormatter formatter = new JFormatter(writer);
        IJStatement statement = (IJStatement) content;
        statement.state(formatter);
        String statementString = writer.getBuffer().toString();
        if (statementString.startsWith(superCallStart)) {
            newBody.add(replacement);
        } else {
            newBody.add(statement);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) IJStatement(com.helger.jcodemodel.IJStatement) JFormatter(com.helger.jcodemodel.JFormatter) JBlock(com.helger.jcodemodel.JBlock)

Example 2 with IJStatement

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

the class BeanHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) {
    TypeMirror typeMirror = annotationHelper.extractAnnotationClassParameter(element);
    if (typeMirror == null) {
        typeMirror = param.asType();
        typeMirror = getProcessingEnvironment().getTypeUtils().erasure(typeMirror);
    }
    String typeQualifiedName = typeMirror.toString();
    AbstractJClass injectedClass = getJClass(annotationHelper.generatedClassQualifiedNameFromQualifiedName(typeQualifiedName));
    JInvocation beanInstance = injectedClass.staticInvoke(EBeanHolder.GET_INSTANCE_METHOD_NAME).arg(holder.getContextRef());
    TypeElement declaredEBean = getProcessingEnvironment().getElementUtils().getTypeElement(typeQualifiedName);
    if (declaredEBean != null) {
        EBean annotation = declaredEBean.getAnnotation(EBean.class);
        if (annotation.scope() != EBean.Scope.Singleton && annotation.scope() != EBean.Scope.Activity) {
            beanInstance.arg(holder.getRootFragmentRef());
        }
    }
    IJStatement assignment = fieldRef.assign(beanInstance);
    if (param.getKind() == ElementKind.FIELD) {
        boolean hasNonConfigurationInstanceAnnotation = element.getAnnotation(NonConfigurationInstance.class) != null;
        if (hasNonConfigurationInstanceAnnotation) {
            JConditional conditional = targetBlock._if(fieldRef.eq(_null()));
            conditional._then().add(assignment);
            assignment = conditional;
        }
    }
    targetBlock.add(assignment);
}
Also used : IJStatement(com.helger.jcodemodel.IJStatement) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional) NonConfigurationInstance(org.androidannotations.annotations.NonConfigurationInstance) EBean(org.androidannotations.annotations.EBean)

Example 3 with IJStatement

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

Aggregations

IJStatement (com.helger.jcodemodel.IJStatement)3 AbstractJClass (com.helger.jcodemodel.AbstractJClass)2 JBlock (com.helger.jcodemodel.JBlock)2 JInvocation (com.helger.jcodemodel.JInvocation)2 JCatchBlock (com.helger.jcodemodel.JCatchBlock)1 JConditional (com.helger.jcodemodel.JConditional)1 JDefinedClass (com.helger.jcodemodel.JDefinedClass)1 JFormatter (com.helger.jcodemodel.JFormatter)1 JMethod (com.helger.jcodemodel.JMethod)1 JTryBlock (com.helger.jcodemodel.JTryBlock)1 JVar (com.helger.jcodemodel.JVar)1 StringWriter (java.io.StringWriter)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Background (org.androidannotations.annotations.Background)1 EBean (org.androidannotations.annotations.EBean)1 NonConfigurationInstance (org.androidannotations.annotations.NonConfigurationInstance)1 BackgroundExecutor (org.androidannotations.api.BackgroundExecutor)1