Search in sources :

Example 36 with VariableElement

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

Example 37 with VariableElement

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

the class EditorActionHandler method processParameters.

@Override
protected void processParameters(EComponentWithViewSupportHolder holder, JMethod listenerMethod, JInvocation call, List<? extends VariableElement> userParameters) {
    JVar textView = listenerMethod.param(getClasses().TEXT_VIEW, "textView");
    JVar actionId = listenerMethod.param(getCodeModel().INT, "actionId");
    JVar event = listenerMethod.param(getClasses().KEY_EVENT, "event");
    for (VariableElement param : userParameters) {
        String paramClassQualifiedName = param.asType().toString();
        if (isTypeOrSubclass(CanonicalNameConstants.TEXT_VIEW, param)) {
            call.arg(castArgumentIfNecessary(holder, CanonicalNameConstants.TEXT_VIEW, textView, param));
        } else if (paramClassQualifiedName.equals(CanonicalNameConstants.INTEGER) || paramClassQualifiedName.equals(getCodeModel().INT.fullName())) {
            call.arg(actionId);
        } else if (paramClassQualifiedName.equals(CanonicalNameConstants.KEY_EVENT)) {
            call.arg(event);
        }
    }
}
Also used : VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 38 with VariableElement

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

the class AbstractResHandler method validate.

@Override
public final void validate(Element element, ElementValidation validation) {
    injectHelper.validate(androidRes.getAnnotationClass(), element, validation);
    if (!validation.isValid()) {
        return;
    }
    validatorHelper.allowedType(element, androidRes.getAllowedTypes(), validation);
    validatorHelper.resIdsExist(element, androidRes.getRInnerClass(), IdValidatorHelper.FallbackStrategy.USE_ELEMENT_NAME, validation);
    Element enclosingElement = element.getEnclosingElement();
    if (element instanceof VariableElement && enclosingElement instanceof ExecutableElement) {
        validatorHelper.isNotPrivate(enclosingElement, validation);
    } else {
        validatorHelper.isNotPrivate(element, validation);
    }
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 39 with VariableElement

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

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

the class PageScrolledHandler 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 positionParameterPosition = -1;
    int positionOffsetParameterPosition = -1;
    int positionOffsetPixelsParameterPosition = -1;
    int viewPagerParameterPosition = -1;
    int numberOfIntParameters = 0;
    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())) {
            if (numberOfIntParameters == 0 && positionOffsetParameterPosition == -1) {
                positionParameterPosition = i;
            } else {
                positionOffsetPixelsParameterPosition = i;
            }
            numberOfIntParameters++;
        } else if (parameterType.getKind() == TypeKind.FLOAT || CanonicalNameConstants.FLOAT.equals(parameterType.toString())) {
            positionOffsetParameterPosition = i;
        } else {
            TypeMirror viewPagerType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW_PAGER).asType();
            if (annotationHelper.isSubtype(parameterType, viewPagerType)) {
                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.getPageScrolledBody();
        IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
        JInvocation pageScrollCall = methodBody.invoke(activityRef, methodName);
        for (int i = 0; i < parameters.size(); i++) {
            if (i == positionParameterPosition) {
                JVar positionParam = pageChangeHolder.getPageScrolledPositionParam();
                pageScrollCall.arg(positionParam);
            } else if (i == positionOffsetParameterPosition) {
                JVar pageScrolledPositionOffsetParam = pageChangeHolder.getPageScrolledPositionOffsetParam();
                pageScrollCall.arg(pageScrolledPositionOffsetParam);
            } else if (i == positionOffsetPixelsParameterPosition) {
                JVar pageScrolledPositionOffsetPixelsParam = pageChangeHolder.getPageScrolledPositionOffsetPixelsParam();
                pageScrollCall.arg(pageScrolledPositionOffsetPixelsParam);
            } else if (i == viewPagerParameterPosition) {
                JVar viewParameter = pageChangeHolder.getViewPagerVariable();
                pageScrollCall.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

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