Search in sources :

Example 36 with IJExpression

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

the class ViewsByIdHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentWithViewSupportHolder holder, Element element, Element param) {
    TypeMirror viewType = extractViewClass(param);
    AbstractJClass viewClass = codeModelHelper.typeMirrorToJClass(viewType);
    String listName = getListName(element, param);
    IJExpression arrayList = instantiateArrayList(viewType, holder, "list_" + listName);
    List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.ID, true);
    for (JFieldRef idRef : idsRefs) {
        addViewToListIfNotNull(arrayList, viewClass, idRef, holder);
    }
    targetBlock.add(fieldRef.assign(arrayList));
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) TypeMirror(javax.lang.model.type.TypeMirror) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Example 37 with IJExpression

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

the class RootContextHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EBeanHolder holder, Element element, Element param) {
    TypeMirror elementType = param.asType();
    String typeQualifiedName = elementType.toString();
    IJExpression contextRef = holder.getContextRef();
    if (CanonicalNameConstants.CONTEXT.equals(typeQualifiedName)) {
        targetBlock.add(fieldRef.assign(contextRef));
    } else {
        AbstractJClass extendingContextClass = getEnvironment().getJClass(typeQualifiedName);
        JConditional cond = targetBlock._if(contextRef._instanceof(extendingContextClass));
        cond._then().add(fieldRef.assign(cast(extendingContextClass, contextRef)));
        JInvocation warningInvoke = getClasses().LOG.staticInvoke("w");
        warningInvoke.arg(logTagForClassHolder(holder));
        warningInvoke.arg(lit("Due to Context class ").plus(contextRef.invoke("getClass").invoke("getSimpleName")).plus(lit(", the @RootContext " + extendingContextClass.name() + " won't be populated")));
        cond._else().add(warningInvoke);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JInvocation(com.helger.jcodemodel.JInvocation)

Example 38 with IJExpression

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

the class SharedPrefHandler method getLocalClassName.

private JMethod getLocalClassName(SharedPrefHolder holder) {
    AbstractJClass stringClass = getClasses().STRING;
    JMethod getLocalClassName = holder.getGeneratedClass().method(PRIVATE | STATIC, stringClass, "getLocalClassName");
    AbstractJClass contextClass = getClasses().CONTEXT;
    JVar contextParam = getLocalClassName.param(contextClass, "context");
    JBlock body = getLocalClassName.body();
    JVar packageName = body.decl(stringClass, "packageName", contextParam.invoke("getPackageName"));
    JVar className = body.decl(stringClass, "className", contextParam.invoke("getClass").invoke("getName"));
    JVar packageLen = body.decl(getCodeModel().INT, "packageLen", packageName.invoke("length"));
    IJExpression condition = // 
    className.invoke("startsWith").arg(packageName).not().cor(// 
    className.invoke("length").lte(packageLen)).cor(className.invoke("charAt").arg(packageLen).ne(lit('.')));
    body._if(condition)._then()._return(className);
    body._return(className.invoke("substring").arg(packageLen.plus(lit(1))));
    return getLocalClassName;
}
Also used : JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 39 with IJExpression

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

the class SharedPrefHandler method createFieldMethod.

private IJExpression createFieldMethod(SharedPrefHolder holder, ExecutableElement method, Class<? extends Annotation> annotationClass, Class<? extends AbstractPrefField<?>> prefFieldClass, Object defaultValue, Res resType, String fieldHelperMethodName) {
    Annotation annotation = method.getAnnotation(annotationClass);
    IJExpression defaultValueExpr;
    Object value = null;
    if (annotation != null) {
        value = annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "value");
    }
    if (annotation != null && method.getAnnotation(DefaultStringSet.class) == null) {
        defaultValueExpr = codeModelHelper.litObject(value);
    } else if (method.getAnnotation(DefaultRes.class) != null) {
        defaultValueExpr = extractResValue(holder, method, resType);
        annotationClass = DefaultRes.class;
    } else if (method.getAnnotation(DefaultStringSet.class) != null) {
        if (value != null) {
            Set<String> arrayValues = new HashSet<>(Arrays.asList((String[]) value));
            value = arrayValues;
            if (arrayValues.isEmpty()) {
                defaultValueExpr = newEmptyStringHashSet();
            } else {
                JInvocation arrayAsList = getClasses().ARRAYS.staticInvoke("asList");
                for (String arrayValue : arrayValues) {
                    arrayAsList.arg(lit(arrayValue));
                }
                defaultValueExpr = JExpr._new(getClasses().HASH_SET.narrow(getClasses().STRING)).arg(arrayAsList);
            }
        } else {
            defaultValueExpr = newEmptyStringHashSet();
        }
        annotationClass = DefaultStringSet.class;
    } else {
        defaultValueExpr = defaultValue != null ? codeModelHelper.litObject(defaultValue) : newEmptyStringHashSet();
        annotationClass = null;
    }
    Integer keyResId = ResId.DEFAULT_VALUE;
    if (annotationClass != null) {
        keyResId = annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "keyRes");
    }
    IJExpression keyExpression;
    String fieldName = method.getSimpleName().toString();
    if (keyResId == ResId.DEFAULT_VALUE) {
        keyExpression = lit(fieldName);
    } else {
        IRInnerClass idClass = getEnvironment().getRClass().get(IRClass.Res.STRING);
        JFieldRef idRef = idClass.getIdStaticRef(keyResId, getEnvironment());
        keyExpression = holder.getEditorContextField().invoke("getString").arg(idRef);
    }
    String docComment = getProcessingEnvironment().getElementUtils().getDocComment(method);
    String defaultValueStr = value == null ? null : value.toString();
    if (defaultValueStr == null) {
        defaultValueStr = defaultValue == null ? null : defaultValue.toString();
    }
    holder.createFieldMethod(prefFieldClass, keyExpression, fieldName, fieldHelperMethodName, defaultValueExpr, docComment, defaultValueStr);
    return keyExpression;
}
Also used : DefaultStringSet(org.androidannotations.annotations.sharedpreferences.DefaultStringSet) JFieldRef(com.helger.jcodemodel.JFieldRef) IJExpression(com.helger.jcodemodel.IJExpression) DefaultRes(org.androidannotations.annotations.sharedpreferences.DefaultRes) JInvocation(com.helger.jcodemodel.JInvocation) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet) IRInnerClass(org.androidannotations.rclass.IRInnerClass)

Example 40 with IJExpression

use of com.helger.jcodemodel.IJExpression 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 {
            if (isViewPagerParameter(parameterType)) {
                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

IJExpression (com.helger.jcodemodel.IJExpression)56 JVar (com.helger.jcodemodel.JVar)33 JBlock (com.helger.jcodemodel.JBlock)31 JInvocation (com.helger.jcodemodel.JInvocation)28 AbstractJClass (com.helger.jcodemodel.AbstractJClass)24 TypeMirror (javax.lang.model.type.TypeMirror)21 JMethod (com.helger.jcodemodel.JMethod)16 ExecutableElement (javax.lang.model.element.ExecutableElement)16 JFieldRef (com.helger.jcodemodel.JFieldRef)15 VariableElement (javax.lang.model.element.VariableElement)12 JConditional (com.helger.jcodemodel.JConditional)7 JFieldVar (com.helger.jcodemodel.JFieldVar)7 JDefinedClass (com.helger.jcodemodel.JDefinedClass)5 BundleHelper (org.androidannotations.helper.BundleHelper)5 JTryBlock (com.helger.jcodemodel.JTryBlock)4 JCatchBlock (com.helger.jcodemodel.JCatchBlock)3 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3 TextWatcherHolder (org.androidannotations.holder.TextWatcherHolder)3 IJAssignmentTarget (com.helger.jcodemodel.IJAssignmentTarget)2 OnSeekBarChangeListenerHolder (org.androidannotations.holder.OnSeekBarChangeListenerHolder)2