Search in sources :

Example 26 with Element

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

the class JPAMetaModelEntityProcessor method modelGenerationNeedsToBeDeferred.

private boolean modelGenerationNeedsToBeDeferred(Collection<MetaEntity> entities, MetaEntity containedEntity) {
    ContainsAttributeTypeVisitor visitor = new ContainsAttributeTypeVisitor(containedEntity.getTypeElement(), context);
    for (MetaEntity entity : entities) {
        if (entity.equals(containedEntity)) {
            continue;
        }
        for (Element subElement : ElementFilter.fieldsIn(entity.getTypeElement().getEnclosedElements())) {
            TypeMirror mirror = subElement.asType();
            if (!TypeKind.DECLARED.equals(mirror.getKind())) {
                continue;
            }
            boolean contains = mirror.accept(visitor, subElement);
            if (contains) {
                return true;
            }
        }
        for (Element subElement : ElementFilter.methodsIn(entity.getTypeElement().getEnclosedElements())) {
            TypeMirror mirror = subElement.asType();
            if (!TypeKind.DECLARED.equals(mirror.getKind())) {
                continue;
            }
            boolean contains = mirror.accept(visitor, subElement);
            if (contains) {
                return true;
            }
        }
    }
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) MetaEntity(org.hibernate.jpamodelgen.model.MetaEntity) AnnotationMetaEntity(org.hibernate.jpamodelgen.annotation.AnnotationMetaEntity) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element)

Example 27 with Element

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

the class AnnotationMetaEntity method addPersistentMembers.

private void addPersistentMembers(List<? extends Element> membersOfClass, AccessType membersKind) {
    for (Element memberOfClass : membersOfClass) {
        AccessType forcedAccessType = TypeUtils.determineAnnotationSpecifiedAccessType(memberOfClass);
        if (entityAccessTypeInfo.getAccessType() != membersKind && forcedAccessType == null) {
            continue;
        }
        if (TypeUtils.containsAnnotation(memberOfClass, Constants.TRANSIENT) || memberOfClass.getModifiers().contains(Modifier.TRANSIENT) || memberOfClass.getModifiers().contains(Modifier.STATIC)) {
            continue;
        }
        MetaAttributeGenerationVisitor visitor = new MetaAttributeGenerationVisitor(this, context);
        AnnotationMetaAttribute result = memberOfClass.asType().accept(visitor, memberOfClass);
        if (result != null) {
            members.put(result.getPropertyName(), result);
        }
    }
}
Also used : PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AccessType(org.hibernate.jpamodelgen.util.AccessType)

Example 28 with Element

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

the class XmlMetaEntity method getCollectionTypes.

private String[] getCollectionTypes(String propertyName, String explicitTargetEntity, String explicitMapKeyClass, ElementKind expectedElementKind) {
    for (Element elem : element.getEnclosedElements()) {
        if (!expectedElementKind.equals(elem.getKind())) {
            continue;
        }
        String elementPropertyName = elem.getSimpleName().toString();
        if (elem.getKind().equals(ElementKind.METHOD)) {
            elementPropertyName = StringUtil.getPropertyName(elementPropertyName);
        }
        if (!propertyName.equals(elementPropertyName)) {
            continue;
        }
        DeclaredType type = determineDeclaredType(elem);
        if (type == null) {
            continue;
        }
        return determineTypes(propertyName, explicitTargetEntity, explicitMapKeyClass, type);
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) DeclaredType(javax.lang.model.type.DeclaredType)

Example 29 with Element

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

the class XmlMetaEntity method getType.

/**
	 * Returns the entity type for a property.
	 *
	 * @param propertyName The property name
	 * @param explicitTargetEntity The explicitly specified target entity type or {@code null}.
	 * @param expectedElementKind Determines property vs field access type
	 *
	 * @return The entity type for this property  or {@code null} if the property with the name and the matching access
	 *         type does not exist.
	 */
private String getType(String propertyName, String explicitTargetEntity, ElementKind expectedElementKind) {
    for (Element elem : element.getEnclosedElements()) {
        if (!expectedElementKind.equals(elem.getKind())) {
            continue;
        }
        TypeMirror mirror;
        String name = elem.getSimpleName().toString();
        if (ElementKind.METHOD.equals(elem.getKind())) {
            name = StringUtil.getPropertyName(name);
            mirror = ((ExecutableElement) elem).getReturnType();
        } else {
            mirror = elem.asType();
        }
        if (name == null || !name.equals(propertyName)) {
            continue;
        }
        if (explicitTargetEntity != null) {
            // TODO should there be a check of the target entity class and if it is loadable?
            return explicitTargetEntity;
        }
        switch(mirror.getKind()) {
            case INT:
                {
                    return "java.lang.Integer";
                }
            case LONG:
                {
                    return "java.lang.Long";
                }
            case BOOLEAN:
                {
                    return "java.lang.Boolean";
                }
            case BYTE:
                {
                    return "java.lang.Byte";
                }
            case SHORT:
                {
                    return "java.lang.Short";
                }
            case CHAR:
                {
                    return "java.lang.Char";
                }
            case FLOAT:
                {
                    return "java.lang.Float";
                }
            case DOUBLE:
                {
                    return "java.lang.Double";
                }
            case DECLARED:
                {
                    return mirror.toString();
                }
            case TYPEVAR:
                {
                    return mirror.toString();
                }
            default:
                {
                }
        }
    }
    context.logMessage(Diagnostic.Kind.WARNING, "Unable to determine type for property " + propertyName + " of class " + getQualifiedName() + " using access type " + accessTypeInfo.getDefaultAccessType());
    return null;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Example 30 with Element

use of javax.lang.model.element.Element in project neo4j by neo4j.

the class UserFunctionVisitorTest method functions_with_specified_value_cannot_be_in_root_namespace.

@Test
public void functions_with_specified_value_cannot_be_in_root_namespace() {
    Element function = elementTestUtils.findMethodElement(UserFunctionsExamples.class, "functionWithValue");
    Stream<CompilationMessage> errors = visitor.visit(function);
    assertThat(errors).hasSize(1).extracting(CompilationMessage::getCategory, CompilationMessage::getElement, CompilationMessage::getContents).contains(tuple(Diagnostic.Kind.ERROR, function, "Function <in_root_namespace_again> cannot be defined in the root namespace. Valid name example: com.acme.my_function"));
}
Also used : Element(javax.lang.model.element.Element) CompilationMessage(org.neo4j.tooling.procedure.messages.CompilationMessage) Test(org.junit.Test)

Aggregations

Element (javax.lang.model.element.Element)286 TypeElement (javax.lang.model.element.TypeElement)227 ExecutableElement (javax.lang.model.element.ExecutableElement)148 VariableElement (javax.lang.model.element.VariableElement)96 TypeMirror (javax.lang.model.type.TypeMirror)68 PackageElement (javax.lang.model.element.PackageElement)48 ArrayList (java.util.ArrayList)39 DeclaredType (javax.lang.model.type.DeclaredType)30 IOException (java.io.IOException)29 Map (java.util.Map)26 HashSet (java.util.HashSet)23 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)22 Set (java.util.Set)22 Test (org.junit.Test)21 ElementKind (javax.lang.model.element.ElementKind)20 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Elements (javax.lang.model.util.Elements)18 HashMap (java.util.HashMap)16 LinkedHashMap (java.util.LinkedHashMap)15