Search in sources :

Example 21 with JFieldRef

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

the class EActivityHandler method process.

@Override
public void process(Element element, EActivityHolder holder) {
    List<JFieldRef> fieldRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.LAYOUT, false);
    JFieldRef contentViewId = null;
    if (fieldRefs.size() == 1) {
        contentViewId = fieldRefs.get(0);
    }
    if (contentViewId != null) {
        JBlock onCreateBody = holder.getOnCreate().body();
        JMethod setContentView = holder.getSetContentViewLayout();
        onCreateBody.invoke(setContentView).arg(contentViewId);
    }
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod)

Example 22 with JFieldRef

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

the class EFragmentHandler method process.

@Override
public void process(Element element, EFragmentHolder holder) {
    JFieldRef contentViewId = annotationHelper.extractOneAnnotationFieldRef(element, IRClass.Res.LAYOUT, false);
    if (contentViewId != null) {
        JBlock block = holder.getSetContentViewBlock();
        JVar inflater = holder.getInflater();
        JVar container = holder.getContainer();
        JFieldVar contentView = holder.getContentView();
        boolean forceInjection = element.getAnnotation(EFragment.class).forceLayoutInjection();
        if (!forceInjection) {
            //
            block._if(contentView.eq(_null()))._then().assign(contentView, inflater.invoke("inflate").arg(contentViewId).arg(container).arg(FALSE));
        } else {
            block.assign(contentView, inflater.invoke("inflate").arg(contentViewId).arg(container).arg(FALSE));
        }
    }
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) JFieldVar(com.helger.jcodemodel.JFieldVar) JBlock(com.helger.jcodemodel.JBlock) EFragment(org.androidannotations.annotations.EFragment) JVar(com.helger.jcodemodel.JVar)

Example 23 with JFieldRef

use of com.helger.jcodemodel.JFieldRef 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 24 with JFieldRef

use of com.helger.jcodemodel.JFieldRef 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 25 with JFieldRef

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

the class SystemServiceHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) {
    TypeMirror serviceType = param.asType();
    String fieldTypeQualifiedName = serviceType.toString();
    JFieldRef serviceRef = new AndroidSystemServices(getEnvironment()).getServiceConstantRef(serviceType);
    if (CanonicalNameConstants.APP_WIDGET_MANAGER.equals(fieldTypeQualifiedName)) {
        targetBlock.add(fieldRef.assign(createSpecialInjection(holder, fieldTypeQualifiedName, serviceRef, 21, "LOLLIPOP", getClasses().APP_WIDGET_MANAGER, "getInstance", true)));
    } else {
        targetBlock.add(fieldRef.assign(createNormalInjection(holder, fieldTypeQualifiedName, serviceRef)));
    }
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) TypeMirror(javax.lang.model.type.TypeMirror) AndroidSystemServices(org.androidannotations.internal.core.model.AndroidSystemServices)

Aggregations

JFieldRef (com.helger.jcodemodel.JFieldRef)33 JBlock (com.helger.jcodemodel.JBlock)19 IJExpression (com.helger.jcodemodel.IJExpression)15 JVar (com.helger.jcodemodel.JVar)15 TypeMirror (javax.lang.model.type.TypeMirror)15 JInvocation (com.helger.jcodemodel.JInvocation)14 ExecutableElement (javax.lang.model.element.ExecutableElement)10 VariableElement (javax.lang.model.element.VariableElement)7 AbstractJClass (com.helger.jcodemodel.AbstractJClass)6 JMethod (com.helger.jcodemodel.JMethod)5 JDefinedClass (com.helger.jcodemodel.JDefinedClass)3 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3 TextWatcherHolder (org.androidannotations.holder.TextWatcherHolder)3 IJAssignmentTarget (com.helger.jcodemodel.IJAssignmentTarget)2 JFieldVar (com.helger.jcodemodel.JFieldVar)2 DefaultRes (org.androidannotations.annotations.sharedpreferences.DefaultRes)2 DefaultString (org.androidannotations.annotations.sharedpreferences.DefaultString)2 BundleHelper (org.androidannotations.helper.BundleHelper)2 FoundPreferenceHolder (org.androidannotations.holder.FoundPreferenceHolder)2 FoundViewHolder (org.androidannotations.holder.FoundViewHolder)2