Search in sources :

Example 71 with Elements

use of javax.lang.model.util.Elements in project kie-wb-common by kiegroup.

the class SVGShapeProcessor method processWithExceptions.

@Override
protected boolean processWithExceptions(Set<? extends TypeElement> set, RoundEnvironment roundEnv) throws Exception {
    if (roundEnv.processingOver()) {
        return processLastRound(set, roundEnv);
    }
    // If prior processing threw an error exit
    if (roundEnv.errorRaised()) {
        return false;
    }
    // Initialize the generator
    generator = SVGGeneratorFactory.newGenerator();
    // Process SVG Shape View Factories for the annotated types.
    final Elements elementUtils = processingEnv.getElementUtils();
    for (Element e : roundEnv.getElementsAnnotatedWith(elementUtils.getTypeElement(ANNOTATION_SVGSHAPE_VIEW_FACTORY))) {
        processSvgShapeViewFactory(set, e, roundEnv);
    }
    return true;
}
Also used : PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements)

Example 72 with Elements

use of javax.lang.model.util.Elements in project cxf by apache.

the class DumpJavaDoc method run.

@Override
public boolean run(DocletEnvironment docEnv) {
    final Elements utils = docEnv.getElementUtils();
    final DocTrees docTrees = docEnv.getDocTrees();
    try (OutputStream os = Files.newOutputStream(Paths.get(dumpFileName))) {
        final Properties javaDocMap = new Properties();
        for (Element element : docEnv.getIncludedElements()) {
            if (element.getKind() == ElementKind.CLASS) {
                final TypeElement classDoc = (TypeElement) element;
                final DocCommentTree classCommentTree = docTrees.getDocCommentTree(classDoc);
                if (classCommentTree != null) {
                    javaDocMap.put(classDoc.toString(), getAllComments(classCommentTree.getFullBody()));
                }
                for (Element member : classDoc.getEnclosedElements()) {
                    // Skip all non-public methods
                    if (!member.getModifiers().contains(Modifier.PUBLIC)) {
                        continue;
                    }
                    if (member.getKind() == ElementKind.METHOD) {
                        final ExecutableElement method = (ExecutableElement) member;
                        final DocCommentTree methodCommentTree = docTrees.getDocCommentTree(method);
                        final String qualifiedName = utils.getBinaryName(classDoc) + "." + method.getSimpleName();
                        if (methodCommentTree == null) {
                            javaDocMap.put(qualifiedName, "");
                        } else {
                            javaDocMap.put(qualifiedName, getAllComments(methodCommentTree.getFullBody()));
                            for (DocTree tree : methodCommentTree.getBlockTags()) {
                                if (tree.getKind() == DocTree.Kind.RETURN) {
                                    final ReturnTree returnTree = (ReturnTree) tree;
                                    javaDocMap.put(qualifiedName + ".returnCommentTag", getAllComments(returnTree.getDescription()));
                                } else if (tree.getKind() == DocTree.Kind.PARAM) {
                                    final ParamTree paramTree = (ParamTree) tree;
                                    final int index = getParamIndex(method, paramTree);
                                    if (index >= 0) {
                                        javaDocMap.put(qualifiedName + ".paramCommentTag." + index, getAllComments(paramTree.getDescription()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        javaDocMap.store(os, "");
        os.flush();
    } catch (final IOException ex) {
        reporter.print(Diagnostic.Kind.ERROR, ex.getMessage());
    }
    return true;
}
Also used : ParamTree(com.sun.source.doctree.ParamTree) TypeElement(javax.lang.model.element.TypeElement) OutputStream(java.io.OutputStream) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnTree(com.sun.source.doctree.ReturnTree) IOException(java.io.IOException) Elements(javax.lang.model.util.Elements) Properties(java.util.Properties) DocCommentTree(com.sun.source.doctree.DocCommentTree) DocTrees(com.sun.source.util.DocTrees) DocTree(com.sun.source.doctree.DocTree)

Example 73 with Elements

use of javax.lang.model.util.Elements in project hibernate-orm by hibernate.

the class TypeUtils method getCollectionElementType.

public static TypeMirror getCollectionElementType(DeclaredType t, String fqNameOfReturnedType, String explicitTargetEntityName, Context context) {
    TypeMirror collectionElementType;
    if (explicitTargetEntityName != null) {
        Elements elements = context.getElementUtils();
        TypeElement element = elements.getTypeElement(explicitTargetEntityName);
        collectionElementType = element.asType();
    } else {
        List<? extends TypeMirror> typeArguments = t.getTypeArguments();
        if (typeArguments.size() == 0) {
            throw new MetaModelGenerationException("Unable to determine collection type");
        } else if (Map.class.getCanonicalName().equals(fqNameOfReturnedType)) {
            collectionElementType = t.getTypeArguments().get(1);
        } else {
            collectionElementType = t.getTypeArguments().get(0);
        }
    }
    return collectionElementType;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) MetaModelGenerationException(org.hibernate.jpamodelgen.MetaModelGenerationException) Elements(javax.lang.model.util.Elements)

Example 74 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class EndpointAnnotationProcessor method findClassProperties.

protected void findClassProperties(PrintWriter writer, RoundEnvironment roundEnv, ComponentModel componentModel, Set<EndpointPath> endpointPaths, Set<EndpointOption> endpointOptions, TypeElement classElement, String prefix, String excludeProperties) {
    Elements elementUtils = processingEnv.getElementUtils();
    while (true) {
        List<VariableElement> fieldElements = ElementFilter.fieldsIn(classElement.getEnclosedElements());
        for (VariableElement fieldElement : fieldElements) {
            Metadata metadata = fieldElement.getAnnotation(Metadata.class);
            boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
            Boolean secret = metadata != null ? metadata.secret() : null;
            UriPath path = fieldElement.getAnnotation(UriPath.class);
            String fieldName = fieldElement.getSimpleName().toString();
            if (path != null) {
                String name = path.name();
                if (isNullOrEmpty(name)) {
                    name = fieldName;
                }
                name = prefix + name;
                // should we exclude the name?
                if (excludeProperty(excludeProperties, name)) {
                    continue;
                }
                String defaultValue = path.defaultValue();
                if (Strings.isNullOrEmpty(defaultValue) && metadata != null) {
                    defaultValue = metadata.defaultValue();
                }
                String defaultValueNote = path.defaultValueNote();
                String required = metadata != null ? metadata.required() : null;
                String label = path.label();
                if (Strings.isNullOrEmpty(label) && metadata != null) {
                    label = metadata.label();
                }
                String displayName = path.displayName();
                if (Strings.isNullOrEmpty(displayName)) {
                    displayName = metadata != null ? metadata.displayName() : null;
                }
                TypeMirror fieldType = fieldElement.asType();
                String fieldTypeName = fieldType.toString();
                TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
                String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, false);
                if (isNullOrEmpty(docComment)) {
                    docComment = path.description();
                }
                // gather enums
                Set<String> enums = new LinkedHashSet<String>();
                boolean isEnum;
                if (!Strings.isNullOrEmpty(path.enums())) {
                    isEnum = true;
                    String[] values = path.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());
                        // find all the enum constants which has the possible enum value that can be used
                        if (enumClass != null) {
                            List<VariableElement> fields = ElementFilter.fieldsIn(enumClass.getEnclosedElements());
                            for (VariableElement var : fields) {
                                if (var.getKind() == ElementKind.ENUM_CONSTANT) {
                                    String val = var.toString();
                                    enums.add(val);
                                }
                            }
                        }
                    }
                }
                // the field type may be overloaded by another type
                if (!Strings.isNullOrEmpty(path.javaType())) {
                    fieldTypeName = path.javaType();
                }
                String group = EndpointHelper.labelAsGroupName(label, componentModel.isConsumerOnly(), componentModel.isProducerOnly());
                boolean isSecret = secret != null ? secret : false;
                EndpointPath ep = new EndpointPath(name, displayName, fieldTypeName, required, defaultValue, docComment, deprecated, isSecret, group, label, isEnum, enums);
                endpointPaths.add(ep);
            }
            UriParam param = fieldElement.getAnnotation(UriParam.class);
            fieldName = fieldElement.getSimpleName().toString();
            if (param != null) {
                String name = param.name();
                if (isNullOrEmpty(name)) {
                    name = fieldName;
                }
                name = prefix + name;
                // should we exclude the name?
                if (excludeProperty(excludeProperties, name)) {
                    continue;
                }
                String paramOptionalPrefix = param.optionalPrefix();
                String paramPrefix = param.prefix();
                boolean multiValue = param.multiValue();
                String defaultValue = param.defaultValue();
                if (defaultValue == null && metadata != null) {
                    defaultValue = metadata.defaultValue();
                }
                String defaultValueNote = param.defaultValueNote();
                String required = metadata != null ? metadata.required() : null;
                String label = param.label();
                if (Strings.isNullOrEmpty(label) && metadata != null) {
                    label = metadata.label();
                }
                String displayName = param.displayName();
                if (Strings.isNullOrEmpty(displayName)) {
                    displayName = metadata != null ? metadata.displayName() : null;
                }
                // if the field type is a nested parameter then iterate through its fields
                TypeMirror fieldType = fieldElement.asType();
                String fieldTypeName = fieldType.toString();
                TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
                UriParams fieldParams = null;
                if (fieldTypeElement != null) {
                    fieldParams = fieldTypeElement.getAnnotation(UriParams.class);
                }
                if (fieldParams != null) {
                    String nestedPrefix = prefix;
                    String extraPrefix = fieldParams.prefix();
                    if (!isNullOrEmpty(extraPrefix)) {
                        nestedPrefix += extraPrefix;
                    }
                    findClassProperties(writer, roundEnv, componentModel, endpointPaths, endpointOptions, fieldTypeElement, nestedPrefix, excludeProperties);
                } else {
                    String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, false);
                    if (isNullOrEmpty(docComment)) {
                        docComment = param.description();
                    }
                    if (isNullOrEmpty(docComment)) {
                        docComment = "";
                    }
                    // gather enums
                    Set<String> enums = new LinkedHashSet<String>();
                    boolean isEnum;
                    if (!Strings.isNullOrEmpty(param.enums())) {
                        isEnum = true;
                        String[] values = param.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);
                                    }
                                }
                            }
                        }
                    }
                    // the field type may be overloaded by another type
                    if (!Strings.isNullOrEmpty(param.javaType())) {
                        fieldTypeName = param.javaType();
                    }
                    boolean isSecret = secret != null ? secret : param.secret();
                    String group = EndpointHelper.labelAsGroupName(label, componentModel.isConsumerOnly(), componentModel.isProducerOnly());
                    EndpointOption option = new EndpointOption(name, displayName, fieldTypeName, required, defaultValue, defaultValueNote, docComment.trim(), paramOptionalPrefix, paramPrefix, multiValue, deprecated, isSecret, group, label, isEnum, enums);
                    endpointOptions.add(option);
                }
            }
        }
        // check super classes which may also have @UriParam 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) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) UriParam(org.apache.camel.spi.UriParam) EndpointPath(org.apache.camel.tools.apt.model.EndpointPath) UriParams(org.apache.camel.spi.UriParams) TypeMirror(javax.lang.model.type.TypeMirror) UriPath(org.apache.camel.spi.UriPath) EndpointOption(org.apache.camel.tools.apt.model.EndpointOption)

Example 75 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class SpringAnnotationProcessor 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;
    }
    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 = 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());
        // 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();
    }
    // special for id as its inherited from camel-core
    if ("id".equals(name) && isNullOrEmpty(docComment)) {
        if ("CamelContextFactoryBean".equals(originalClassType.getSimpleName().toString())) {
            docComment = "Sets the id (name) of this CamelContext";
        } else {
            docComment = "Sets the id of this node";
        }
    }
    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)

Aggregations

Elements (javax.lang.model.util.Elements)146 TypeElement (javax.lang.model.element.TypeElement)90 TypeMirror (javax.lang.model.type.TypeMirror)52 Element (javax.lang.model.element.Element)46 Types (javax.lang.model.util.Types)40 VariableElement (javax.lang.model.element.VariableElement)35 ExecutableElement (javax.lang.model.element.ExecutableElement)32 Map (java.util.Map)18 ArrayList (java.util.ArrayList)17 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)17 PackageElement (javax.lang.model.element.PackageElement)17 LinkedHashSet (java.util.LinkedHashSet)15 AnnotationMirror (javax.lang.model.element.AnnotationMirror)15 Test (org.junit.Test)15 List (java.util.List)14 XmlElements (javax.xml.bind.annotation.XmlElements)14 Metadata (org.apache.camel.spi.Metadata)14 Set (java.util.Set)12 DeclaredType (javax.lang.model.type.DeclaredType)12 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)12