Search in sources :

Example 16 with ElementKind

use of javax.lang.model.element.ElementKind in project hibernate-orm by hibernate.

the class XmlMetaEntity method parseAttributes.

private void parseAttributes(Attributes attributes) {
    XmlMetaSingleAttribute attribute;
    for (Id id : attributes.getId()) {
        ElementKind elementKind = getElementKind(id.getAccess());
        String type = getType(id.getName(), null, elementKind);
        if (type != null) {
            attribute = new XmlMetaSingleAttribute(this, id.getName(), type);
            members.add(attribute);
        }
    }
    if (attributes.getEmbeddedId() != null) {
        EmbeddedId embeddedId = attributes.getEmbeddedId();
        ElementKind elementKind = getElementKind(embeddedId.getAccess());
        String type = getType(embeddedId.getName(), null, elementKind);
        if (type != null) {
            attribute = new XmlMetaSingleAttribute(this, embeddedId.getName(), type);
            members.add(attribute);
        }
    }
    for (Basic basic : attributes.getBasic()) {
        parseBasic(basic);
    }
    for (ManyToOne manyToOne : attributes.getManyToOne()) {
        parseManyToOne(manyToOne);
    }
    for (OneToOne oneToOne : attributes.getOneToOne()) {
        parseOneToOne(oneToOne);
    }
    for (ManyToMany manyToMany : attributes.getManyToMany()) {
        if (parseManyToMany(manyToMany)) {
            break;
        }
    }
    for (OneToMany oneToMany : attributes.getOneToMany()) {
        if (parseOneToMany(oneToMany)) {
            break;
        }
    }
    for (ElementCollection collection : attributes.getElementCollection()) {
        if (parseElementCollection(collection)) {
            break;
        }
    }
    for (Embedded embedded : attributes.getEmbedded()) {
        parseEmbedded(embedded);
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind) Basic(org.hibernate.jpamodelgen.xml.jaxb.Basic) OneToOne(org.hibernate.jpamodelgen.xml.jaxb.OneToOne) EmbeddedId(org.hibernate.jpamodelgen.xml.jaxb.EmbeddedId) ManyToMany(org.hibernate.jpamodelgen.xml.jaxb.ManyToMany) EmbeddedId(org.hibernate.jpamodelgen.xml.jaxb.EmbeddedId) Id(org.hibernate.jpamodelgen.xml.jaxb.Id) ElementCollection(org.hibernate.jpamodelgen.xml.jaxb.ElementCollection) Embedded(org.hibernate.jpamodelgen.xml.jaxb.Embedded) OneToMany(org.hibernate.jpamodelgen.xml.jaxb.OneToMany) ManyToOne(org.hibernate.jpamodelgen.xml.jaxb.ManyToOne)

Example 17 with ElementKind

use of javax.lang.model.element.ElementKind in project hibernate-orm by hibernate.

the class XmlMetaEntity method parseOneToMany.

private boolean parseOneToMany(OneToMany oneToMany) {
    String[] types;
    XmlMetaCollection metaCollection;
    ElementKind elementKind = getElementKind(oneToMany.getAccess());
    String explicitTargetClass = determineExplicitTargetEntity(oneToMany.getTargetEntity());
    String explicitMapKey = determineExplicitMapKeyClass(oneToMany.getMapKeyClass());
    try {
        types = getCollectionTypes(oneToMany.getName(), explicitTargetClass, explicitMapKey, elementKind);
    } catch (MetaModelGenerationException e) {
        logMetaModelException(oneToMany.getName(), e);
        return true;
    }
    if (types != null) {
        if (types[2] == null) {
            metaCollection = new XmlMetaCollection(this, oneToMany.getName(), types[0], types[1]);
        } else {
            metaCollection = new XmlMetaMap(this, oneToMany.getName(), types[0], types[1], types[2]);
        }
        members.add(metaCollection);
    }
    return false;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) MetaModelGenerationException(org.hibernate.jpamodelgen.MetaModelGenerationException)

Example 18 with ElementKind

use of javax.lang.model.element.ElementKind in project hibernate-orm by hibernate.

the class XmlMetaEntity method parseOneToOne.

private void parseOneToOne(OneToOne oneToOne) {
    XmlMetaSingleAttribute attribute;
    ElementKind elementKind = getElementKind(oneToOne.getAccess());
    String type = getType(oneToOne.getName(), oneToOne.getTargetEntity(), elementKind);
    if (type != null) {
        attribute = new XmlMetaSingleAttribute(this, oneToOne.getName(), type);
        members.add(attribute);
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind)

Example 19 with ElementKind

use of javax.lang.model.element.ElementKind in project hibernate-orm by hibernate.

the class TypeUtils method getAccessTypeOfIdAnnotation.

private static AccessType getAccessTypeOfIdAnnotation(Element element) {
    AccessType accessType = null;
    final ElementKind kind = element.getKind();
    if (kind == ElementKind.FIELD || kind == ElementKind.METHOD) {
        accessType = kind == ElementKind.FIELD ? AccessType.FIELD : AccessType.PROPERTY;
    }
    return accessType;
}
Also used : ElementKind(javax.lang.model.element.ElementKind)

Example 20 with ElementKind

use of javax.lang.model.element.ElementKind in project DeepLinkDispatch by airbnb.

the class DeepLinkProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Set<Element> customAnnotations = new HashSet<>();
    for (Element annotation : annotations) {
        if (annotation.getAnnotation(DEEP_LINK_SPEC_CLASS) != null) {
            customAnnotations.add(annotation);
        }
    }
    Map<Element, String[]> prefixes = new HashMap<>();
    Set<Element> customAnnotatedElements = new HashSet<>();
    for (Element customAnnotation : customAnnotations) {
        ElementKind kind = customAnnotation.getKind();
        if (kind != ElementKind.ANNOTATION_TYPE) {
            error(customAnnotation, "Only annotation types can be annotated with @%s", DEEP_LINK_SPEC_CLASS.getSimpleName());
        }
        String[] prefix = customAnnotation.getAnnotation(DEEP_LINK_SPEC_CLASS).prefix();
        if (Utils.hasEmptyOrNullString(prefix)) {
            error(customAnnotation, "Prefix property cannot have null or empty strings");
        }
        if (prefix.length == 0) {
            error(customAnnotation, "Prefix property cannot be empty");
        }
        prefixes.put(customAnnotation, prefix);
        for (Element customAnnotatedElement : roundEnv.getElementsAnnotatedWith(MoreElements.asType(customAnnotation))) {
            customAnnotatedElements.add(customAnnotatedElement);
        }
    }
    Set<Element> elementsToProcess = new HashSet<>(customAnnotatedElements);
    elementsToProcess.addAll(roundEnv.getElementsAnnotatedWith(DEEP_LINK_CLASS));
    List<DeepLinkAnnotatedElement> deepLinkElements = new ArrayList<>();
    for (Element element : elementsToProcess) {
        ElementKind kind = element.getKind();
        if (kind != ElementKind.METHOD && kind != ElementKind.CLASS) {
            error(element, "Only classes and methods can be annotated with @%s", DEEP_LINK_CLASS.getSimpleName());
        }
        if (kind == ElementKind.METHOD) {
            Set<Modifier> methodModifiers = element.getModifiers();
            if (!methodModifiers.contains(Modifier.STATIC)) {
                error(element, "Only static methods can be annotated with @%s", DEEP_LINK_CLASS.getSimpleName());
            }
        }
        DeepLink deepLinkAnnotation = element.getAnnotation(DEEP_LINK_CLASS);
        List<String> deepLinks = new ArrayList<>();
        if (deepLinkAnnotation != null) {
            deepLinks.addAll(Arrays.asList(deepLinkAnnotation.value()));
        }
        if (customAnnotatedElements.contains(element)) {
            deepLinks.addAll(enumerateCustomDeepLinks(element, prefixes));
        }
        DeepLinkEntry.Type type = kind == ElementKind.CLASS ? DeepLinkEntry.Type.CLASS : DeepLinkEntry.Type.METHOD;
        for (String deepLink : deepLinks) {
            try {
                deepLinkElements.add(new DeepLinkAnnotatedElement(deepLink, element, type));
            } catch (MalformedURLException e) {
                messager.printMessage(Diagnostic.Kind.ERROR, "Malformed Deep Link URL " + deepLink);
            }
        }
    }
    Set<? extends Element> deepLinkHandlerElements = roundEnv.getElementsAnnotatedWith(DeepLinkHandler.class);
    for (Element deepLinkHandlerElement : deepLinkHandlerElements) {
        Optional<AnnotationMirror> annotationMirror = getAnnotationMirror(deepLinkHandlerElement, DeepLinkHandler.class);
        if (annotationMirror.isPresent()) {
            Iterable<TypeMirror> klasses = getTypeValue(annotationMirror.get(), "value");
            List<TypeElement> typeElements = FluentIterable.from(klasses).transform(new Function<TypeMirror, TypeElement>() {

                @Override
                public TypeElement apply(TypeMirror klass) {
                    return MoreTypes.asTypeElement(klass);
                }
            }).toList();
            String packageName = processingEnv.getElementUtils().getPackageOf(deepLinkHandlerElement).getQualifiedName().toString();
            try {
                generateDeepLinkDelegate(packageName, typeElements);
            } catch (IOException e) {
                messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file");
            } catch (RuntimeException e) {
                messager.printMessage(Diagnostic.Kind.ERROR, "Internal error during annotation processing: " + e.getClass().getSimpleName());
            }
        }
    }
    Set<? extends Element> deepLinkModuleElements = roundEnv.getElementsAnnotatedWith(DeepLinkModule.class);
    for (Element deepLinkModuleElement : deepLinkModuleElements) {
        String packageName = processingEnv.getElementUtils().getPackageOf(deepLinkModuleElement).getQualifiedName().toString();
        try {
            generateDeepLinkLoader(packageName, deepLinkModuleElement.getSimpleName().toString(), deepLinkElements);
        } catch (IOException e) {
            messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file");
        } catch (RuntimeException e) {
            messager.printMessage(Diagnostic.Kind.ERROR, "Internal error during annotation processing: " + e.getClass().getSimpleName());
        }
    }
    return false;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) ArrayList(java.util.ArrayList) Function(com.google.common.base.Function) TypeMirror(javax.lang.model.type.TypeMirror) Modifier(javax.lang.model.element.Modifier) HashSet(java.util.HashSet) TypeElement(javax.lang.model.element.TypeElement) IOException(java.io.IOException) MoreElements.getAnnotationMirror(com.google.auto.common.MoreElements.getAnnotationMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Aggregations

ElementKind (javax.lang.model.element.ElementKind)21 Element (javax.lang.model.element.Element)9 TypeElement (javax.lang.model.element.TypeElement)9 ExecutableElement (javax.lang.model.element.ExecutableElement)5 PackageElement (javax.lang.model.element.PackageElement)4 LinkedHashSet (java.util.LinkedHashSet)3 VariableElement (javax.lang.model.element.VariableElement)3 MetaModelGenerationException (org.hibernate.jpamodelgen.MetaModelGenerationException)3 HashSet (java.util.HashSet)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 Modifier (javax.lang.model.element.Modifier)2 TypeParameterElement (javax.lang.model.element.TypeParameterElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 TypeConverter (com.bluelinelabs.logansquare.typeconverters.TypeConverter)1 MoreElements.getAnnotationMirror (com.google.auto.common.MoreElements.getAnnotationMirror)1 Function (com.google.common.base.Function)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1