Search in sources :

Example 41 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class PreferenceChangeHandler method processParameters.

@Override
protected void processParameters(HasPreferences holder, JMethod listenerMethod, JInvocation call, List<? extends VariableElement> userParameters) {
    JVar preferenceParam = listenerMethod.param(holder.getBasePreferenceClass(), "preference");
    JVar newValueParam = listenerMethod.param(getClasses().OBJECT, "newValue");
    for (VariableElement variableElement : userParameters) {
        String type = variableElement.asType().toString();
        if (isTypeOrSubclass(CanonicalNameConstants.PREFERENCE, variableElement)) {
            call.arg(castArgumentIfNecessary(holder, CanonicalNameConstants.PREFERENCE, preferenceParam, variableElement));
        } else if (isTypeOrSubclass(CanonicalNameConstants.SUPPORT_V7_PREFERENCE, variableElement)) {
            call.arg(castArgumentIfNecessary(holder, CanonicalNameConstants.SUPPORT_V7_PREFERENCE, preferenceParam, variableElement));
        } else if (type.equals(CanonicalNameConstants.OBJECT)) {
            call.arg(newValueParam);
        } else if (//
        type.equals(CanonicalNameConstants.INTEGER) || type.equals(int.class.getName()) || type.equals(CanonicalNameConstants.FLOAT) || //
        type.equals(float.class.getName()) || type.equals(CanonicalNameConstants.LONG) || type.equals(long.class.getName())) {
            AbstractJClass wrapperClass = getEnvironment().getCodeModel().parseType(type).boxify();
            call.arg(wrapperClass.staticInvoke("valueOf").arg(JExpr.cast(getClasses().STRING, newValueParam)));
        } else {
            AbstractJClass userParamClass = codeModelHelper.typeMirrorToJClass(variableElement.asType());
            call.arg(JExpr.cast(userParamClass, newValueParam));
            if (type.equals(CanonicalNameConstants.STRING_SET)) {
                codeModelHelper.addSuppressWarnings(listenerMethod, "unchecked");
            }
        }
    }
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 42 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class CheckedChangeHandler method processParameters.

@Override
protected void processParameters(EComponentWithViewSupportHolder holder, JMethod listenerMethod, JInvocation call, List<? extends VariableElement> parameters) {
    JVar btnParam = listenerMethod.param(getClasses().COMPOUND_BUTTON, "buttonView");
    JVar isCheckedParam = listenerMethod.param(getCodeModel().BOOLEAN, "isChecked");
    for (VariableElement parameter : parameters) {
        String parameterType = parameter.asType().toString();
        if (isTypeOrSubclass(CanonicalNameConstants.COMPOUND_BUTTON, parameter)) {
            call.arg(castArgumentIfNecessary(holder, CanonicalNameConstants.COMPOUND_BUTTON, btnParam, parameter));
        } else if (parameterType.equals(CanonicalNameConstants.BOOLEAN) || parameter.asType().getKind() == TypeKind.BOOLEAN) {
            call.arg(isCheckedParam);
        }
    }
}
Also used : VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 43 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class ItemSelectHandler method processParameters.

@Override
protected void processParameters(EComponentWithViewSupportHolder holder, JMethod listenerMethod, JInvocation itemSelectedCall, List<? extends VariableElement> parameters) {
    AbstractJClass narrowAdapterViewClass = getClasses().ADAPTER_VIEW.narrow(getCodeModel().wildcard());
    JVar onItemClickParentParam = listenerMethod.param(narrowAdapterViewClass, "parent");
    listenerMethod.param(getClasses().VIEW, "view");
    JVar onItemClickPositionParam = listenerMethod.param(getCodeModel().INT, "position");
    listenerMethod.param(getCodeModel().LONG, "id");
    itemSelectedCall.arg(JExpr.TRUE);
    boolean hasItemParameter = parameters.size() == 2;
    boolean secondParameterIsInt = false;
    String secondParameterQualifiedName = null;
    if (hasItemParameter) {
        VariableElement secondParameter = parameters.get(1);
        TypeMirror secondParameterType = secondParameter.asType();
        secondParameterQualifiedName = secondParameterType.toString();
        secondParameterIsInt = secondParameterType.getKind() == TypeKind.INT;
    }
    if (hasItemParameter) {
        if (secondParameterIsInt) {
            itemSelectedCall.arg(onItemClickPositionParam);
        } else {
            itemSelectedCall.arg(JExpr.cast(getJClass(secondParameterQualifiedName), invoke(onItemClickParentParam, "getAdapter").invoke("getItem").arg(onItemClickPositionParam)));
        }
    }
    onNothingSelectedMethod.param(narrowAdapterViewClass, "parent");
    IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
    JInvocation nothingSelectedCall = invoke(activityRef, getMethodName());
    onNothingSelectedMethod.body().add(nothingSelectedCall);
    nothingSelectedCall.arg(JExpr.FALSE);
    if (hasItemParameter) {
        if (secondParameterIsInt) {
            nothingSelectedCall.arg(lit(-1));
        } else {
            nothingSelectedCall.arg(_null());
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 44 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class ReceiverActionHandler method addActionInOnReceive.

private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionsField, JFieldVar dataSchemesField) {
    String actionsInvoke = getInvocationName(actionsField);
    IJExpression filterCondition = actionsField.invoke(actionsInvoke).arg(holder.getOnReceiveIntentAction());
    if (dataSchemesField != null) {
        String dataSchemesInvoke = getInvocationName(dataSchemesField);
        filterCondition = filterCondition.cand(dataSchemesField.invoke(dataSchemesInvoke).arg(holder.getOnReceiveIntentDataScheme()));
    }
    JBlock callActionBlock = holder.getOnReceiveBody()._if(filterCondition)._then();
    IJExpression receiverRef = holder.getGeneratedClass().staticRef("this");
    JInvocation callActionInvocation = receiverRef.invoke(methodName);
    JVar intent = holder.getOnReceiveIntent();
    JVar extras = null;
    List<? extends VariableElement> methodParameters = executableElement.getParameters();
    for (VariableElement param : methodParameters) {
        AbstractJClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType());
        if (extraParamClass.equals(getClasses().CONTEXT)) {
            callActionInvocation.arg(holder.getOnReceiveContext());
        } else if (extraParamClass.equals(getClasses().INTENT) && param.getAnnotation(ReceiverAction.Extra.class) == null) {
            callActionInvocation.arg(intent);
        } else if (param.getAnnotation(ReceiverAction.Extra.class) != null) {
            if (extras == null) {
                extras = callActionBlock.decl(getClasses().BUNDLE, "extras_", JOp.cond(//
                intent.invoke("getExtras").ne(_null()), intent.invoke("getExtras"), _new(getClasses().BUNDLE)));
            }
            callActionInvocation.arg(extraHandler.getExtraValue(param, extras, callActionBlock, holder));
        }
    }
    callActionBlock.add(callActionInvocation);
    callActionBlock._return();
}
Also used : IJExpression(com.helger.jcodemodel.IJExpression) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 45 with VariableElement

use of javax.lang.model.element.VariableElement 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)

Aggregations

VariableElement (javax.lang.model.element.VariableElement)248 ExecutableElement (javax.lang.model.element.ExecutableElement)75 TypeElement (javax.lang.model.element.TypeElement)75 TypeMirror (javax.lang.model.type.TypeMirror)72 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)35 Element (javax.lang.model.element.Element)35 Expression (com.google.devtools.j2objc.ast.Expression)30 SimpleName (com.google.devtools.j2objc.ast.SimpleName)28 JVar (com.helger.jcodemodel.JVar)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)23 ArrayList (java.util.ArrayList)23 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)16 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)16 JInvocation (com.helger.jcodemodel.JInvocation)16 DeclaredType (javax.lang.model.type.DeclaredType)16 JBlock (com.helger.jcodemodel.JBlock)15 AbstractJClass (com.helger.jcodemodel.AbstractJClass)14 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)13 Elements (javax.lang.model.util.Elements)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)12