Search in sources :

Example 51 with VariableElement

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

the class SpringAnnotationProcessor method processElement.

private void processElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, XmlElement element, XmlElementRef elementRef, VariableElement fieldElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    String fieldName;
    fieldName = fieldElement.getSimpleName().toString();
    if (element != null || elementRef != null) {
        String kind = "element";
        String name = element != null ? element.name() : elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
        String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
        if (isNullOrEmpty(docComment)) {
            Metadata metadata = fieldElement.getAnnotation(Metadata.class);
            docComment = metadata != null ? metadata.description() : null;
        }
        boolean required = element != null ? element.required() : elementRef.required();
        // metadata may overrule element required
        required = findRequired(fieldElement, required);
        // gather enums
        Set<String> enums = new LinkedHashSet<String>();
        boolean isEnum = fieldTypeElement != null && fieldTypeElement.getKind() == ElementKind.ENUM;
        if (isEnum) {
            TypeElement enumClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
            // 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);
                }
            }
        }
        // is it a definition/factory-bean type then its a oneOf
        TreeSet oneOfTypes = new TreeSet<String>();
        if (fieldTypeName.endsWith("Definition") || fieldTypeName.endsWith("FactoryBean")) {
            TypeElement definitionClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
            if (definitionClass != null) {
                XmlRootElement rootElement = definitionClass.getAnnotation(XmlRootElement.class);
                if (rootElement != null) {
                    String childName = rootElement.name();
                    if (childName != null) {
                        oneOfTypes.add(childName);
                    }
                }
            }
        } else if (fieldTypeName.endsWith("Definition>") || fieldTypeName.endsWith("FactoryBean>")) {
            // its a list so we need to load the generic type
            String typeName = Strings.between(fieldTypeName, "<", ">");
            TypeElement definitionClass = findTypeElement(processingEnv, roundEnv, typeName);
            if (definitionClass != null) {
                XmlRootElement rootElement = definitionClass.getAnnotation(XmlRootElement.class);
                if (rootElement != null) {
                    String childName = rootElement.name();
                    if (childName != null) {
                        oneOfTypes.add(childName);
                    }
                }
            }
        }
        boolean oneOf = !oneOfTypes.isEmpty();
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        boolean asPredicate = false;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, oneOf, oneOfTypes, asPredicate);
        eipOptions.add(ep);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet)

Example 52 with VariableElement

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

the class CoreEipAnnotationProcessor 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;
                }
            }
            XmlValue value = fieldElement.getAnnotation(XmlValue.class);
            if (value != null) {
                processValue(processingEnv, roundEnv, originalClassType, classElement, fieldElement, fieldName, value, eipOptions, prefix, modelName);
            }
            XmlElements elements = fieldElement.getAnnotation(XmlElements.class);
            if (elements != null) {
                processElements(processingEnv, roundEnv, classElement, elements, fieldElement, eipOptions, prefix);
            }
            XmlElement element = fieldElement.getAnnotation(XmlElement.class);
            if (element != null) {
                processElement(processingEnv, roundEnv, classElement, element, fieldElement, eipOptions, prefix);
            }
            // special for eips which has outputs or requires an expressions
            XmlElementRef elementRef = fieldElement.getAnnotation(XmlElementRef.class);
            if (elementRef != null) {
                // special for routes
                processRoutes(roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for outputs
                processOutputs(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for when clauses (choice eip)
                processRefWhenClauses(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for rests (rest-dsl)
                processRests(roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for verbs (rest-dsl)
                processVerbs(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for expression
                processRefExpression(processingEnv, roundEnv, originalClassType, classElement, elementRef, fieldElement, fieldName, eipOptions, prefix);
            }
        }
        // special when we process these nodes as they do not use JAXB annotations on fields, but on methods
        if ("OptionalIdentifiedDefinition".equals(classElement.getSimpleName().toString())) {
            processIdentified(processingEnv, roundEnv, originalClassType, classElement, eipOptions, prefix);
        } else if ("RouteDefinition".equals(classElement.getSimpleName().toString())) {
            processRoute(processingEnv, roundEnv, originalClassType, classElement, 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) XmlValue(javax.xml.bind.annotation.XmlValue) XmlElement(javax.xml.bind.annotation.XmlElement) VariableElement(javax.lang.model.element.VariableElement)

Example 53 with VariableElement

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

the class CoreEipAnnotationProcessor method processAttribute.

private boolean processAttribute(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, VariableElement fieldElement, String fieldName, XmlAttribute attribute, Set<EipOption> eipOptions, String prefix, String modelName) {
    Elements elementUtils = processingEnv.getElementUtils();
    String name = attribute.name();
    if (isNullOrEmpty(name) || "##default".equals(name)) {
        name = fieldName;
    }
    // lets skip some unwanted attributes
    if (skipUnwanted) {
        // we want to skip inheritErrorHandler which is only applicable for the load-balancer
        boolean loadBalancer = "LoadBalanceDefinition".equals(originalClassType.getSimpleName().toString());
        if (!loadBalancer && "inheritErrorHandler".equals(name)) {
            return true;
        }
    }
    name = prefix + name;
    TypeMirror fieldType = fieldElement.asType();
    String fieldTypeName = fieldType.toString();
    TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
    String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
    String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
    boolean required = attribute.required();
    // metadata may overrule element required
    required = findRequired(fieldElement, required);
    // gather enums
    Set<String> enums = new TreeSet<String>();
    boolean 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);
                }
            }
        }
    }
    boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
    String displayName = null;
    Metadata metadata = fieldElement.getAnnotation(Metadata.class);
    if (metadata != null) {
        displayName = metadata.displayName();
    }
    EipOption ep = new EipOption(name, displayName, "attribute", fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, false, null, false);
    eipOptions.add(ep);
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) TreeSet(java.util.TreeSet) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Example 54 with VariableElement

use of javax.lang.model.element.VariableElement in project dagger by square.

the class InjectAdapterProcessor method getMethod.

private MethodSpec getMethod(ExecutableElement constructor, boolean disambiguateFields, boolean injectMembers, ClassName injectedClassName) {
    MethodSpec.Builder result = MethodSpec.methodBuilder("get").addJavadoc(AdapterJavadocs.GET_METHOD, injectedClassName).addAnnotation(Override.class).returns(injectedClassName).addModifiers(PUBLIC);
    result.addCode("$T result = new $T(", injectedClassName, injectedClassName);
    boolean first = true;
    for (VariableElement parameter : constructor.getParameters()) {
        if (!first)
            result.addCode(", ");
        else
            first = false;
        result.addCode("$N.get()", parameterName(disambiguateFields, parameter));
    }
    result.addCode(");\n");
    if (injectMembers) {
        result.addStatement("injectMembers(result)");
    }
    result.addStatement("return result");
    return result.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) VariableElement(javax.lang.model.element.VariableElement)

Example 55 with VariableElement

use of javax.lang.model.element.VariableElement in project dagger by square.

the class InjectAdapterProcessor method generateInjectAdapter.

/**
   * Write a companion class for {@code type} that extends {@link Binding}.
   *
   * @param constructor the injectable constructor, or null if this binding
   *     supports members injection only.
   */
private void generateInjectAdapter(TypeElement type, ExecutableElement constructor, List<Element> fields) throws IOException {
    String packageName = getPackage(type).getQualifiedName().toString();
    TypeMirror supertype = getApplicationSupertype(type);
    if (supertype != null) {
        supertype = processingEnv.getTypeUtils().erasure(supertype);
    }
    ClassName injectedClassName = ClassName.get(type);
    ClassName adapterClassName = adapterName(injectedClassName, INJECT_ADAPTER_SUFFIX);
    boolean isAbstract = type.getModifiers().contains(ABSTRACT);
    boolean injectMembers = !fields.isEmpty() || supertype != null;
    boolean disambiguateFields = !fields.isEmpty() && (constructor != null) && !constructor.getParameters().isEmpty();
    boolean dependent = injectMembers || ((constructor != null) && !constructor.getParameters().isEmpty());
    TypeSpec.Builder result = TypeSpec.classBuilder(adapterClassName.simpleName()).addOriginatingElement(type).addModifiers(PUBLIC, FINAL).superclass(ParameterizedTypeName.get(ClassName.get(Binding.class), injectedClassName)).addJavadoc("$L", bindingTypeDocs(injectableType(type.asType()), isAbstract, injectMembers, dependent).toString());
    for (Element field : fields) {
        result.addField(memberBindingField(disambiguateFields, field));
    }
    if (constructor != null) {
        for (VariableElement parameter : constructor.getParameters()) {
            result.addField(parameterBindingField(disambiguateFields, parameter));
        }
    }
    if (supertype != null) {
        result.addField(supertypeBindingField(supertype));
    }
    result.addMethod(writeInjectAdapterConstructor(constructor, type, injectedClassName));
    if (dependent) {
        result.addMethod(attachMethod(constructor, fields, disambiguateFields, injectedClassName, supertype, true));
        result.addMethod(getDependenciesMethod(constructor, fields, disambiguateFields, supertype, true));
    }
    if (constructor != null) {
        result.addMethod(getMethod(constructor, disambiguateFields, injectMembers, injectedClassName));
    }
    if (injectMembers) {
        result.addMethod(membersInjectMethod(fields, disambiguateFields, injectedClassName, supertype));
    }
    JavaFile javaFile = JavaFile.builder(packageName, result.build()).addFileComment(AdapterJavadocs.GENERATED_BY_DAGGER).build();
    javaFile.writeTo(processingEnv.getFiler());
}
Also used : Binding(dagger.internal.Binding) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) Util.rawTypeToString(dagger.internal.codegen.Util.rawTypeToString) Util.elementToString(dagger.internal.codegen.Util.elementToString) VariableElement(javax.lang.model.element.VariableElement) TypeSpec(com.squareup.javapoet.TypeSpec)

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