Search in sources :

Example 16 with IJExpression

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

the class SeekBarProgressChangeHandler 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 seekBarViewParameterPosition = -1;
    int progressParameterPosition = -1;
    int fromUserParameterPosition = -1;
    for (int i = 0; i < parameters.size(); i++) {
        VariableElement parameter = parameters.get(i);
        TypeMirror parameterType = parameter.asType();
        if (CanonicalNameConstants.SEEKBAR.equals(parameterType.toString())) {
            seekBarViewParameterPosition = i;
        } else if (parameterType.getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
            progressParameterPosition = i;
        } else if (parameterType.getKind() == TypeKind.BOOLEAN || CanonicalNameConstants.BOOLEAN.equals(parameterType.toString())) {
            fromUserParameterPosition = i;
        }
    }
    List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.ID, true);
    for (JFieldRef idRef : idsRefs) {
        OnSeekBarChangeListenerHolder onSeekBarChangeListenerHolder = holder.getOnSeekBarChangeListenerHolder(idRef);
        JBlock methodBody = onSeekBarChangeListenerHolder.getOnProgressChangedBody();
        IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
        JInvocation textChangeCall = methodBody.invoke(activityRef, methodName);
        for (int i = 0; i < parameters.size(); i++) {
            if (i == seekBarViewParameterPosition) {
                JVar seekBarViewParameter = onSeekBarChangeListenerHolder.getOnProgressChangedSeekBarParam();
                textChangeCall.arg(seekBarViewParameter);
            } else if (i == progressParameterPosition) {
                JVar progressParameter = onSeekBarChangeListenerHolder.getOnProgressChangedProgressParam();
                textChangeCall.arg(progressParameter);
            } else if (i == fromUserParameterPosition) {
                JVar fromUserParameter = onSeekBarChangeListenerHolder.getOnProgressChangedFromUserParam();
                textChangeCall.arg(fromUserParameter);
            }
        }
    }
}
Also used : OnSeekBarChangeListenerHolder(org.androidannotations.holder.OnSeekBarChangeListenerHolder) 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) TypeMirror(javax.lang.model.type.TypeMirror) JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 17 with IJExpression

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

the class SharedPrefHandler method generateFieldMethodAndEditorFieldMethod.

private void generateFieldMethodAndEditorFieldMethod(Element element, SharedPrefHolder sharedPrefHolder) {
    for (ExecutableElement method : getValidMethods(element)) {
        IJExpression keyExpression = generateFieldMethod(sharedPrefHolder, method);
        sharedPrefHolder.createEditorFieldMethods(method, keyExpression);
    }
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression)

Example 18 with IJExpression

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

the class ReceiverHandler method registerAndUnregisterReceiver.

private void registerAndUnregisterReceiver(HasReceiverRegistration holder, Receiver.RegisterAt registerAt, JFieldVar intentFilterField, JFieldVar receiverField, boolean local) {
    JBlock registerBlock = null;
    JBlock unregisterBlock = null;
    if (holder instanceof HasActivityLifecycleMethods) {
        HasActivityLifecycleMethods activityLifecycleMethods = (HasActivityLifecycleMethods) holder;
        switch(registerAt) {
            case OnCreateOnDestroy:
                registerBlock = activityLifecycleMethods.getOnCreateAfterSuperBlock();
                unregisterBlock = activityLifecycleMethods.getOnDestroyBeforeSuperBlock();
                break;
            case OnStartOnStop:
                registerBlock = activityLifecycleMethods.getOnStartAfterSuperBlock();
                unregisterBlock = activityLifecycleMethods.getOnStopBeforeSuperBlock();
                break;
            case OnResumeOnPause:
                registerBlock = activityLifecycleMethods.getOnResumeAfterSuperBlock();
                unregisterBlock = activityLifecycleMethods.getOnPauseBeforeSuperBlock();
                break;
        }
        if (holder instanceof EFragmentHolder && registerAt == Receiver.RegisterAt.OnAttachOnDetach) {
            EFragmentHolder fragmentHolder = (EFragmentHolder) holder;
            registerBlock = fragmentHolder.getOnAttachAfterSuperBlock();
            unregisterBlock = fragmentHolder.getOnDetachBeforeSuperBlock();
        }
    } else {
        registerBlock = holder.getStartLifecycleAfterSuperBlock();
        unregisterBlock = holder.getEndLifecycleBeforeSuperBlock();
    }
    IJExpression broadcastManager;
    if (local) {
        if (getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.LOCAL_BROADCAST_MANAGER) == null) {
            broadcastManager = getClasses().ANDROIDX_LOCAL_BROADCAST_MANAGER.staticInvoke("getInstance").arg(holder.getContextRef());
        } else {
            broadcastManager = getClasses().LOCAL_BROADCAST_MANAGER.staticInvoke("getInstance").arg(holder.getContextRef());
        }
    } else {
        broadcastManager = holder.getContextRef();
    }
    registerBlock.invoke(broadcastManager, "registerReceiver").arg(receiverField).arg(intentFilterField);
    unregisterBlock.invoke(broadcastManager, "unregisterReceiver").arg(receiverField);
}
Also used : HasActivityLifecycleMethods(org.androidannotations.holder.HasActivityLifecycleMethods) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) EFragmentHolder(org.androidannotations.holder.EFragmentHolder)

Example 19 with IJExpression

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

the class ServiceActionHandler method addActionInOnHandleIntent.

private void addActionInOnHandleIntent(EIntentServiceHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField) {
    JMethod onHandleIntentMethod = holder.getOnHandleIntentMethod();
    // If action match, call the method
    JInvocation actionCondition = actionKeyField.invoke("equals").arg(holder.getOnHandleIntentIntentAction());
    JBlock callActionBlock = holder.getOnHandleIntentBody()._if(actionCondition)._then();
    JInvocation callActionInvocation = JExpr._super().invoke(methodName);
    // For each method params, we get back value from extras and put it
    // in super calls
    List<? extends VariableElement> methodParameters = executableElement.getParameters();
    if (methodParameters.size() > 0) {
        // Extras
        JVar intent = holder.getOnHandleIntentIntent();
        JVar extras = callActionBlock.decl(getClasses().BUNDLE, "extras");
        extras.init(intent.invoke("getExtras"));
        callActionBlock = callActionBlock._if(extras.ne(_null()))._then();
        // Extras params
        for (VariableElement param : methodParameters) {
            String paramName = param.getSimpleName().toString();
            String extraParamName = paramName + "Extra";
            JFieldVar paramVar = getStaticExtraField(holder, paramName);
            AbstractJClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType());
            BundleHelper bundleHelper = new BundleHelper(getEnvironment(), param.asType());
            IJExpression getExtraExpression = bundleHelper.getExpressionToRestoreFromBundle(extraParamClass, extras, paramVar, onHandleIntentMethod);
            JVar extraField = callActionBlock.decl(extraParamClass, extraParamName, getExtraExpression);
            callActionInvocation.arg(extraField);
        }
    }
    callActionBlock.add(callActionInvocation);
    callActionBlock._return();
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar) BundleHelper(org.androidannotations.helper.BundleHelper)

Example 20 with IJExpression

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

the class OnActivityResultHandler method process.

@Override
public void process(Element element, HasOnActivityResult holder) throws Exception {
    String methodName = element.getSimpleName().toString();
    ExecutableElement executableElement = (ExecutableElement) element;
    List<? extends VariableElement> parameters = executableElement.getParameters();
    int requestCode = executableElement.getAnnotation(OnActivityResult.class).value();
    JBlock onResultBlock = holder.getOnActivityResultCaseBlock(requestCode).blockSimple();
    IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
    JInvocation onResultInvocation = JExpr.invoke(activityRef, methodName);
    JVar intent = holder.getOnActivityResultDataParam();
    JVar extras = null;
    for (VariableElement parameter : parameters) {
        TypeMirror parameterType = parameter.asType();
        if (parameter.getAnnotation(OnActivityResult.Extra.class) != null) {
            if (extras == null) {
                extras = onResultBlock.decl(getClasses().BUNDLE, "extras_", JOp.cond(intent.ne(_null()).cand(intent.invoke("getExtras").ne(_null())), intent.invoke("getExtras"), _new(getClasses().BUNDLE)));
            }
            IJExpression extraParameter = extraHandler.getExtraValue(parameter, extras, onResultBlock, holder);
            onResultInvocation.arg(extraParameter);
        } else if (CanonicalNameConstants.INTENT.equals(parameterType.toString())) {
            onResultInvocation.arg(intent);
        } else if (// 
        parameterType.getKind().equals(TypeKind.INT) || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
            onResultInvocation.arg(holder.getOnActivityResultResultCodeParam());
        }
    }
    onResultBlock.add(onResultInvocation);
}
Also used : HasOnActivityResult(org.androidannotations.holder.HasOnActivityResult) OnActivityResult(org.androidannotations.annotations.OnActivityResult) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) 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