Search in sources :

Example 1 with Entity

use of com.datastax.oss.driver.api.mapper.annotations.Entity in project java-driver by datastax.

the class EntityUtils method asEntityElement.

/**
 * If this mirror is an {@link Entity}-annotated class, return that class's element, otherwise
 * {@code null}.
 */
public static TypeElement asEntityElement(TypeMirror mirror, Map<Name, TypeElement> typeParameters) {
    Element element;
    if (mirror.getKind() == TypeKind.TYPEVAR) {
        // extract concrete implementation for type variable.
        TypeVariable typeVariable = ((TypeVariable) mirror);
        Name name = typeVariable.asElement().getSimpleName();
        element = typeParameters.get(name);
        if (element == null) {
            return null;
        }
    } else if (mirror.getKind() == TypeKind.DECLARED) {
        element = ((DeclaredType) mirror).asElement();
    } else {
        return null;
    }
    if (element.getKind() != ElementKind.CLASS && // Hack to support Java 14 records without having to compile against JDK 14
    !element.getKind().name().equals("RECORD")) {
        return null;
    }
    TypeElement typeElement = (TypeElement) element;
    if (typeElement.getAnnotation(Entity.class) == null) {
        return null;
    }
    return typeElement;
}
Also used : Entity(com.datastax.oss.driver.api.mapper.annotations.Entity) TypeVariable(javax.lang.model.type.TypeVariable) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) TypeName(com.squareup.javapoet.TypeName) Name(javax.lang.model.element.Name) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

Entity (com.datastax.oss.driver.api.mapper.annotations.Entity)1 TypeName (com.squareup.javapoet.TypeName)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 Name (javax.lang.model.element.Name)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1 TypeVariable (javax.lang.model.type.TypeVariable)1