Search in sources :

Example 46 with VariableElement

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

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

the class CoreValidatorHelper method hasBeforeTextChangedMethodParameters.

public void hasBeforeTextChangedMethodParameters(ExecutableElement executableElement, ElementValidation valid) {
    List<? extends VariableElement> parameters = executableElement.getParameters();
    boolean charSequenceParameterFound = false;
    boolean textViewParameterFound = false;
    for (VariableElement parameter : parameters) {
        String parameterType = parameter.asType().toString();
        if (parameterType.equals(CanonicalNameConstants.CHAR_SEQUENCE)) {
            if (charSequenceParameterFound) {
                valid.addError("Unrecognized parameter declaration. you can declare only one parameter of type java.lang.CharSequence");
            }
            charSequenceParameterFound = true;
            continue;
        }
        if (parameterType.equals(CanonicalNameConstants.TEXT_VIEW)) {
            if (textViewParameterFound) {
                valid.addError("Unrecognized parameter declaration. you can declare only one parameter of type android.widget.TextView");
            }
            textViewParameterFound = true;
            continue;
        }
        if (parameter.asType().getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType)) {
            String parameterName = parameter.toString();
            if ("start".equals(parameterName) || "count".equals(parameterName) || "after".equals(parameterName)) {
                continue;
            }
            valid.addError("Unrecognized parameter name. You can only have start, before, or count parameter name." + " Try to pick a parameter from android.text.TextWatcher.beforeTextChanged() method.");
            continue;
        }
        valid.addError("Unrecognized parameter (" + parameter.toString() + "). %s can only have a android.widget.TextView parameter and/or parameters from android.text.TextWatcher.beforeTextChanged() method.");
    }
}
Also used : VariableElement(javax.lang.model.element.VariableElement) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString)

Example 48 with VariableElement

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

the class CoreValidatorHelper method hasTextChangedMethodParameters.

public void hasTextChangedMethodParameters(ExecutableElement executableElement, ElementValidation valid) {
    List<? extends VariableElement> parameters = executableElement.getParameters();
    boolean charSequenceParameterFound = false;
    boolean textViewParameterFound = false;
    for (VariableElement parameter : parameters) {
        String parameterType = parameter.asType().toString();
        if (parameterType.equals(CanonicalNameConstants.CHAR_SEQUENCE)) {
            if (charSequenceParameterFound) {
                valid.addError("Unrecognized parameter declaration. you can declare only one parameter of type java.lang.CharSequence");
            }
            charSequenceParameterFound = true;
            continue;
        }
        if (parameterType.equals(CanonicalNameConstants.TEXT_VIEW)) {
            if (textViewParameterFound) {
                valid.addError("Unrecognized parameter declaration. you can declare only one parameter of type android.widget.TextView");
            }
            textViewParameterFound = true;
            continue;
        }
        if (parameter.asType().getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType)) {
            String parameterName = parameter.toString();
            if ("start".equals(parameterName) || "before".equals(parameterName) || "count".equals(parameterName)) {
                continue;
            }
            valid.addError("Unrecognized parameter name. You can only have start, before, or count parameter name." + " Try to pick a parameter from the android.text.TextWatcher.onTextChanged() method.");
            continue;
        }
        valid.addError("Unrecognized parameter (" + parameter.toString() + "). %s can only have a android.widget.TextView parameter and/or parameters from android.text.TextWatcher.onTextChanged() method.");
    }
}
Also used : VariableElement(javax.lang.model.element.VariableElement) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString)

Example 49 with VariableElement

use of javax.lang.model.element.VariableElement in project camel by apache.

the class EndpointAnnotationProcessor method findComponentClassProperties.

protected void findComponentClassProperties(PrintWriter writer, RoundEnvironment roundEnv, ComponentModel componentModel, Set<ComponentOption> componentOptions, TypeElement classElement, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    while (true) {
        Metadata componentAnnotation = classElement.getAnnotation(Metadata.class);
        if (componentAnnotation != null && Objects.equals("verifiers", componentAnnotation.label())) {
            componentModel.setVerifiers(componentAnnotation.enums());
        }
        List<ExecutableElement> methods = ElementFilter.methodsIn(classElement.getEnclosedElements());
        for (ExecutableElement method : methods) {
            String methodName = method.getSimpleName().toString();
            boolean deprecated = method.getAnnotation(Deprecated.class) != null;
            Metadata metadata = method.getAnnotation(Metadata.class);
            // must be the setter
            boolean isSetter = methodName.startsWith("set") && method.getParameters().size() == 1 & method.getReturnType().getKind().equals(TypeKind.VOID);
            if (!isSetter) {
                continue;
            }
            // skip unwanted methods as they are inherited from default component and are not intended for end users to configure
            if ("setEndpointClass".equals(methodName) || "setCamelContext".equals(methodName) || "setEndpointHeaderFilterStrategy".equals(methodName) || "setApplicationContext".equals(methodName)) {
                continue;
            }
            // must be a getter/setter pair
            String fieldName = methodName.substring(3);
            fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);
            // we usually favor putting the @Metadata annotation on the field instead of the setter, so try to use it if its there
            VariableElement field = findFieldElement(classElement, fieldName);
            if (field != null && metadata == null) {
                metadata = field.getAnnotation(Metadata.class);
            }
            String required = metadata != null ? metadata.required() : null;
            String label = metadata != null ? metadata.label() : null;
            boolean secret = metadata != null && metadata.secret();
            String displayName = metadata != null ? metadata.displayName() : null;
            // we do not yet have default values / notes / as no annotation support yet
            // String defaultValueNote = param.defaultValueNote();
            String defaultValue = metadata != null ? metadata.defaultValue() : null;
            String defaultValueNote = null;
            ExecutableElement setter = method;
            String name = fieldName;
            name = prefix + name;
            TypeMirror fieldType = setter.getParameters().get(0).asType();
            String fieldTypeName = fieldType.toString();
            TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
            String docComment = findJavaDoc(elementUtils, method, fieldName, name, classElement, false);
            if (isNullOrEmpty(docComment)) {
                docComment = metadata != null ? metadata.description() : null;
            }
            if (isNullOrEmpty(docComment)) {
                // apt cannot grab javadoc from camel-core, only from annotations
                if ("setHeaderFilterStrategy".equals(methodName)) {
                    docComment = HEADER_FILTER_STRATEGY_JAVADOC;
                } else {
                    docComment = "";
                }
            }
            // gather enums
            Set<String> enums = new LinkedHashSet<String>();
            boolean isEnum;
            if (metadata != null && !Strings.isNullOrEmpty(metadata.enums())) {
                isEnum = true;
                String[] values = metadata.enums().split(",");
                for (String val : values) {
                    enums.add(val);
                }
            } else {
                isEnum = fieldTypeElement != null && fieldTypeElement.getKind() == ElementKind.ENUM;
                if (isEnum) {
                    TypeElement enumClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
                    if (enumClass != null) {
                        // find all the enum constants which has the possible enum value that can be used
                        List<VariableElement> fields = ElementFilter.fieldsIn(enumClass.getEnclosedElements());
                        for (VariableElement var : fields) {
                            if (var.getKind() == ElementKind.ENUM_CONSTANT) {
                                String val = var.toString();
                                enums.add(val);
                            }
                        }
                    }
                }
            }
            String group = EndpointHelper.labelAsGroupName(label, componentModel.isConsumerOnly(), componentModel.isProducerOnly());
            ComponentOption option = new ComponentOption(name, displayName, fieldTypeName, required, defaultValue, defaultValueNote, docComment.trim(), deprecated, secret, group, label, isEnum, enums);
            componentOptions.add(option);
        }
        // check super classes which may also have fields
        TypeElement baseTypeElement = null;
        TypeMirror superclass = classElement.getSuperclass();
        if (superclass != null) {
            String superClassName = canonicalClassName(superclass.toString());
            baseTypeElement = findTypeElement(processingEnv, roundEnv, superClassName);
        }
        if (baseTypeElement != null) {
            classElement = baseTypeElement;
        } else {
            break;
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) ComponentOption(org.apache.camel.tools.apt.model.ComponentOption) ExecutableElement(javax.lang.model.element.ExecutableElement) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) TypeMirror(javax.lang.model.type.TypeMirror)

Example 50 with VariableElement

use of javax.lang.model.element.VariableElement in project camel by apache.

the class SpringAnnotationProcessor method findClassProperties.

protected void findClassProperties(ProcessingEnvironment processingEnv, PrintWriter writer, RoundEnvironment roundEnv, Set<EipOption> eipOptions, TypeElement originalClassType, TypeElement classElement, String prefix, String modelName) {
    while (true) {
        List<VariableElement> fieldElements = ElementFilter.fieldsIn(classElement.getEnclosedElements());
        for (VariableElement fieldElement : fieldElements) {
            String fieldName = fieldElement.getSimpleName().toString();
            XmlAttribute attribute = fieldElement.getAnnotation(XmlAttribute.class);
            if (attribute != null) {
                boolean skip = processAttribute(processingEnv, roundEnv, originalClassType, classElement, fieldElement, fieldName, attribute, eipOptions, prefix, modelName);
                if (skip) {
                    continue;
                }
            }
            XmlElements elements = fieldElement.getAnnotation(XmlElements.class);
            if (elements != null) {
                processElements(processingEnv, roundEnv, classElement, elements, fieldElement, eipOptions, prefix);
            }
            XmlElementRef elementRef = fieldElement.getAnnotation(XmlElementRef.class);
            if (elementRef != null) {
                processElement(processingEnv, roundEnv, classElement, null, elementRef, fieldElement, eipOptions, prefix);
            }
            XmlElement element = fieldElement.getAnnotation(XmlElement.class);
            if (element != null) {
                if ("rests".equals(fieldName)) {
                    processRests(roundEnv, classElement, element, fieldElement, fieldName, eipOptions, prefix);
                } else if ("routes".equals(fieldName)) {
                    processRoutes(roundEnv, classElement, element, fieldElement, fieldName, eipOptions, prefix);
                } else {
                    processElement(processingEnv, roundEnv, classElement, element, null, fieldElement, eipOptions, prefix);
                }
            }
        }
        // check super classes which may also have fields
        TypeElement baseTypeElement = null;
        TypeMirror superclass = classElement.getSuperclass();
        if (superclass != null) {
            String superClassName = canonicalClassName(superclass.toString());
            baseTypeElement = findTypeElement(processingEnv, roundEnv, superClassName);
        }
        if (baseTypeElement != null) {
            classElement = baseTypeElement;
        } else {
            break;
        }
    }
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) XmlElements(javax.xml.bind.annotation.XmlElements) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) XmlElement(javax.xml.bind.annotation.XmlElement) VariableElement(javax.lang.model.element.VariableElement)

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