Search in sources :

Example 61 with JInvocation

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

the class SharedPrefHandler method extractResValue.

private IJExpression extractResValue(SharedPrefHolder holder, Element method, IRClass.Res res) {
    JFieldRef idRef = annotationHelper.extractOneAnnotationFieldRef(method, DefaultRes.class.getCanonicalName(), res, true);
    String resourceGetMethodName = null;
    switch(res) {
        case BOOL:
            resourceGetMethodName = "getBoolean";
            break;
        case INTEGER:
            resourceGetMethodName = "getInteger";
            break;
        case STRING:
            resourceGetMethodName = "getString";
            break;
        case ARRAY:
            resourceGetMethodName = "getStringArray";
            break;
        default:
            break;
    }
    JInvocation resourceInvocation = holder.getContextField().invoke("getResources").invoke(resourceGetMethodName).arg(idRef);
    if (IRClass.Res.ARRAY.equals(res)) {
        JInvocation asList = getClasses().ARRAYS.staticInvoke("asList");
        JInvocation newHashMap = JExpr._new(getClasses().HASH_SET.narrow(getClasses().STRING));
        resourceInvocation = newHashMap.arg(asList.arg(resourceInvocation));
    }
    return resourceInvocation;
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) DefaultRes(org.androidannotations.annotations.sharedpreferences.DefaultRes) JInvocation(com.helger.jcodemodel.JInvocation) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString)

Example 62 with JInvocation

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

the class SupposeBackgroundHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    AbstractJClass bgExecutor = getJClass(BackgroundExecutor.class);
    SupposeBackground annotation = element.getAnnotation(SupposeBackground.class);
    String[] serial = annotation.serial();
    JInvocation invocation = bgExecutor.staticInvoke(METHOD_CHECK_BG_THREAD);
    for (String s : serial) {
        invocation.arg(lit(s));
    }
    JBlock body = delegatingMethod.body();
    body.pos(0);
    body.add(invocation);
    body.pos(body.getContents().size());
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) SupposeBackground(org.androidannotations.annotations.SupposeBackground) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod)

Example 63 with JInvocation

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

the class SystemServiceHandler method createSpecialInjection.

@SuppressWarnings("checkstyle:parameternumber")
private IJExpression createSpecialInjection(EComponentHolder holder, String fieldTypeQualifiedName, JFieldRef serviceRef, int apiLevel, String apiLevelName, AbstractJClass serviceClass, String injectionMethodName, boolean invocationRequiresContext) {
    if (getEnvironment().getAndroidManifest().getMinSdkVersion() >= apiLevel) {
        return createNormalInjection(holder, fieldTypeQualifiedName, serviceRef);
    } else {
        JInvocation serviceClassInvocation = serviceClass.staticInvoke(injectionMethodName);
        if (invocationRequiresContext) {
            serviceClassInvocation.arg(holder.getContextRef());
        }
        if (isApiOnClasspath(apiLevelName)) {
            IJExpression condition = getClasses().BUILD_VERSION.staticRef("SDK_INT").gte(getClasses().BUILD_VERSION_CODES.staticRef(apiLevelName));
            IJExpression normalInjection = createNormalInjection(holder, fieldTypeQualifiedName, serviceRef);
            return JOp.cond(condition, normalInjection, serviceClassInvocation);
        } else {
            return serviceClassInvocation;
        }
    }
}
Also used : IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation)

Example 64 with JInvocation

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

the class TextChangeHandler method process.

@Override
public void process(Element element, EComponentWithViewSupportHolder holder) throws Exception {
    String methodName = element.getSimpleName().toString();
    ExecutableElement executableElement = (ExecutableElement) element;
    List<? extends VariableElement> parameters = executableElement.getParameters();
    int startParameterPosition = -1;
    int countParameterPosition = -1;
    int beforeParameterPosition = -1;
    int charSequenceParameterPosition = -1;
    int viewParameterPosition = -1;
    TypeMirror viewParameterType = null;
    for (int i = 0; i < parameters.size(); i++) {
        VariableElement parameter = parameters.get(i);
        String parameterName = parameter.toString();
        TypeMirror parameterType = parameter.asType();
        if (CanonicalNameConstants.CHAR_SEQUENCE.equals(parameterType.toString())) {
            charSequenceParameterPosition = i;
        } else if (parameterType.getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
            if ("start".equals(parameterName)) {
                startParameterPosition = i;
            } else if ("count".equals(parameterName)) {
                countParameterPosition = i;
            } else if ("before".equals(parameterName)) {
                beforeParameterPosition = i;
            }
        } else {
            TypeMirror textViewType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.TEXT_VIEW).asType();
            if (annotationHelper.isSubtype(parameterType, textViewType)) {
                viewParameterPosition = i;
                viewParameterType = parameterType;
            }
        }
    }
    List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.ID, true);
    for (JFieldRef idRef : idsRefs) {
        TextWatcherHolder textWatcherHolder = holder.getTextWatcherHolder(idRef, viewParameterType);
        JBlock methodBody = textWatcherHolder.getOnTextChangedBody();
        IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
        JInvocation textChangeCall = methodBody.invoke(activityRef, methodName);
        for (int i = 0; i < parameters.size(); i++) {
            if (i == startParameterPosition) {
                JVar startParameter = textWatcherHolder.getOnTextChangedStartParam();
                textChangeCall.arg(startParameter);
            } else if (i == countParameterPosition) {
                JVar countParameter = textWatcherHolder.getOnTextChangedCountParam();
                textChangeCall.arg(countParameter);
            } else if (i == beforeParameterPosition) {
                JVar beforeParameter = textWatcherHolder.getOnTextChangedBeforeParam();
                textChangeCall.arg(beforeParameter);
            } else if (i == charSequenceParameterPosition) {
                JVar charSequenceParam = textWatcherHolder.getOnTextChangedCharSequenceParam();
                textChangeCall.arg(charSequenceParam);
            } else if (i == viewParameterPosition) {
                JVar viewParameter = textWatcherHolder.getTextViewVariable();
                textChangeCall.arg(viewParameter);
            }
        }
    }
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) TextWatcherHolder(org.androidannotations.holder.TextWatcherHolder) TypeMirror(javax.lang.model.type.TypeMirror) JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 65 with JInvocation

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

the class PageScrollStateChangedHandler method process.

@Override
public void process(Element element, EComponentWithViewSupportHolder holder) {
    String methodName = element.getSimpleName().toString();
    ExecutableElement executableElement = (ExecutableElement) element;
    List<? extends VariableElement> parameters = executableElement.getParameters();
    int stateParameterPosition = -1;
    int viewPagerParameterPosition = -1;
    TypeMirror viewPagerParameterType = null;
    for (int i = 0; i < parameters.size(); i++) {
        VariableElement parameter = parameters.get(i);
        TypeMirror parameterType = parameter.asType();
        if (parameterType.getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
            stateParameterPosition = i;
        } else {
            TypeMirror viewPagerType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW_PAGER).asType();
            if (annotationHelper.isSubtype(parameterType, viewPagerType)) {
                viewPagerParameterPosition = i;
                viewPagerParameterType = parameterType;
            }
        }
    }
    List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.ID, true);
    for (JFieldRef idRef : idsRefs) {
        PageChangeHolder pageChangeHolder = holder.getPageChangeHolder(idRef, viewPagerParameterType, hasAddOnPageChangeListenerMethod());
        JBlock methodBody = pageChangeHolder.getPageScrollStateChangedBody();
        IJExpression thisRef = holder.getGeneratedClass().staticRef("this");
        JInvocation methodCall = methodBody.invoke(thisRef, methodName);
        for (int i = 0; i < parameters.size(); i++) {
            if (i == stateParameterPosition) {
                JVar stateParam = pageChangeHolder.getPageScrollStateChangedStateParam();
                methodCall.arg(stateParam);
            } else if (i == viewPagerParameterPosition) {
                JVar viewParameter = pageChangeHolder.getViewPagerVariable();
                methodCall.arg(viewParameter);
            }
        }
    }
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression) PageChangeHolder(org.androidannotations.holder.PageChangeHolder) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) TypeMirror(javax.lang.model.type.TypeMirror) JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Aggregations

JInvocation (com.helger.jcodemodel.JInvocation)71 JVar (com.helger.jcodemodel.JVar)38 JBlock (com.helger.jcodemodel.JBlock)37 AbstractJClass (com.helger.jcodemodel.AbstractJClass)31 IJExpression (com.helger.jcodemodel.IJExpression)27 JMethod (com.helger.jcodemodel.JMethod)26 ExecutableElement (javax.lang.model.element.ExecutableElement)20 VariableElement (javax.lang.model.element.VariableElement)16 JFieldRef (com.helger.jcodemodel.JFieldRef)14 TypeMirror (javax.lang.model.type.TypeMirror)14 JConditional (com.helger.jcodemodel.JConditional)11 JDefinedClass (com.helger.jcodemodel.JDefinedClass)9 AbstractJType (com.helger.jcodemodel.AbstractJType)5 JFieldVar (com.helger.jcodemodel.JFieldVar)5 JTryBlock (com.helger.jcodemodel.JTryBlock)5 ArrayList (java.util.ArrayList)4 DeclaredType (javax.lang.model.type.DeclaredType)4 JCatchBlock (com.helger.jcodemodel.JCatchBlock)3 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3 TextWatcherHolder (org.androidannotations.holder.TextWatcherHolder)3