Search in sources :

Example 26 with IJExpression

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

the class ExtraHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EActivityHolder holder, Element element, Element param) {
    String fieldName = element.getSimpleName().toString();
    String extraKey = extractExtraKey(element, fieldName);
    TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, param);
    BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
    JFieldVar extraKeyStaticField = getOrCreateStaticExtraField(holder, extraKey, fieldName);
    if (element.getKind() != ElementKind.PARAMETER) {
        holder.getIntentBuilder().getPutExtraMethod(element, new IntentBuilder.IntentExtra(param.asType(), fieldName, extraKeyStaticField));
    }
    AbstractJClass elementClass = codeModelHelper.typeMirrorToJClass(param.asType());
    JMethod injectExtrasMethod = holder.getInjectExtrasMethod();
    JVar extras = holder.getInjectExtras();
    IJExpression restoreMethodCall = bundleHelper.getExpressionToRestoreFromBundle(elementClass, extras, extraKeyStaticField, injectExtrasMethod);
    JBlock ifContainsKey = targetBlock._if(JExpr.invoke(extras, "containsKey").arg(extraKeyStaticField))._then();
    ifContainsKey.assign(fieldRef, restoreMethodCall);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) JFieldVar(com.helger.jcodemodel.JFieldVar) IntentBuilder(org.androidannotations.internal.core.helper.IntentBuilder) IJExpression(com.helger.jcodemodel.IJExpression) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) BundleHelper(org.androidannotations.helper.BundleHelper) JVar(com.helger.jcodemodel.JVar)

Example 27 with IJExpression

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

the class AfterTextChangeHandler 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 editableParameterPosition = -1;
    int viewParameterPosition = -1;
    TypeMirror viewParameterType = null;
    for (int i = 0; i < parameters.size(); i++) {
        VariableElement parameter = parameters.get(i);
        TypeMirror parameterType = parameter.asType();
        if (CanonicalNameConstants.EDITABLE.equals(parameterType.toString())) {
            editableParameterPosition = 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.getAfterTextChangedBody();
        IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
        JInvocation textChangeCall = methodBody.invoke(activityRef, methodName);
        for (int i = 0; i < parameters.size(); i++) {
            if (i == editableParameterPosition) {
                JVar afterTextChangeEditableParam = textWatcherHolder.getAfterTextChangedEditableParam();
                textChangeCall.arg(afterTextChangeEditableParam);
            } 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 28 with IJExpression

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

the class OrmLiteHolder method setDatabaseHelperRef.

private JFieldVar setDatabaseHelperRef(TypeMirror databaseHelperTypeMirror) {
    AbstractJClass databaseHelperClass = getJClass(databaseHelperTypeMirror.toString());
    String fieldName = CaseHelper.lowerCaseFirst(databaseHelperClass.name()) + ModelConstants.generationSuffix();
    JFieldVar databaseHelperRef = getGeneratedClass().field(PRIVATE, databaseHelperClass, fieldName);
    databaseHelperRefs.put(databaseHelperTypeMirror, databaseHelperRef);
    IJExpression dbHelperClass = databaseHelperClass.dotclass();
    // 
    holder().getInitBodyInjectionBlock().assign(// 
    databaseHelperRef, getJClass(OrmLiteClasses.OPEN_HELPER_MANAGER).staticInvoke("getHelper").arg(holder().getContextRef()).arg(dbHelperClass));
    return databaseHelperRef;
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Example 29 with IJExpression

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

the class IntentBuilder method getSuperPutExtraInvocation.

public JInvocation getSuperPutExtraInvocation(TypeMirror elementType, JVar extraParam, JFieldVar extraKeyField) {
    IJExpression extraParameterArg = extraParam;
    // Cast to Parcelable, wrap with Parcels.wrap or cast Serializable if needed
    if (elementType.getKind() == TypeKind.DECLARED) {
        Elements elementUtils = environment.getProcessingEnvironment().getElementUtils();
        TypeMirror parcelableType = elementUtils.getTypeElement(PARCELABLE).asType();
        if (typeUtils.isSubtype(elementType, parcelableType)) {
            TypeMirror serializableType = elementUtils.getTypeElement(SERIALIZABLE).asType();
            if (typeUtils.isSubtype(elementType, serializableType)) {
                extraParameterArg = cast(environment.getClasses().PARCELABLE, extraParameterArg);
            }
        } else if (!BundleHelper.METHOD_SUFFIX_BY_TYPE_NAME.containsKey(elementType.toString()) && parcelerHelper.isParcelType(elementType)) {
            extraParameterArg = environment.getJClass(CanonicalNameConstants.PARCELS_UTILITY_CLASS).staticInvoke("wrap").arg(extraParameterArg);
        } else {
            TypeMirror stringType = elementUtils.getTypeElement(STRING).asType();
            if (!typeUtils.isSubtype(elementType, stringType)) {
                extraParameterArg = cast(environment.getClasses().SERIALIZABLE, extraParameterArg);
            }
        }
    }
    return _super().invoke("extra").arg(extraKeyField).arg(extraParameterArg);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) IJExpression(com.helger.jcodemodel.IJExpression) Elements(javax.lang.model.util.Elements)

Example 30 with IJExpression

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

the class OrmLiteDaoHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) {
    OrmLiteHolder ormLiteHolder = holder.getPluginHolder(new OrmLiteHolder(holder));
    AbstractJClass modelClass = getJClass(ormLiteHelper.getEntityType(param).toString());
    AbstractJClass idClass = getJClass(ormLiteHelper.getEntityIdType(param).toString());
    IJExpression modelClassDotClass = modelClass.dotclass();
    AbstractJClass daoClass = getJClass(OrmLiteClasses.DAO).narrow(modelClass, idClass);
    AbstractJClass daoImplClass = codeModelHelper.typeMirrorToJClass(param.asType());
    TypeMirror databaseHelperTypeMirror = annotationHelper.extractAnnotationParameter(element, "helper");
    JFieldVar databaseHelperRef = ormLiteHolder.getDatabaseHelperRef(databaseHelperTypeMirror);
    IJExpression injectExpr = databaseHelperRef.invoke("getDao").arg(modelClassDotClass);
    if (elementExtendsRuntimeExceptionDao(param)) {
        injectExpr = _new(daoImplClass).arg(cast(daoClass, injectExpr));
    }
    JTryBlock tryBlock = targetBlock._try();
    tryBlock.body().add(fieldRef.assign(injectExpr));
    JCatchBlock catchBlock = tryBlock._catch(getClasses().SQL_EXCEPTION);
    JVar exception = catchBlock.param("e");
    String fieldName = param.getSimpleName().toString();
    // 
    catchBlock.body().staticInvoke(getClasses().LOG, // 
    "e").arg(// 
    logTagForClassHolder(holder)).arg(// 
    "Could not create DAO " + fieldName).arg(exception);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) JFieldVar(com.helger.jcodemodel.JFieldVar) OrmLiteHolder(org.androidannotations.ormlite.holder.OrmLiteHolder) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) 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