From 06758f09dfd3775d13bccf3e8a94e4bdbf373ff7 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Wed, 20 Nov 2024 16:23:57 +0100 Subject: [PATCH] HHH-18863 probably more efficient way to detect if a class is a Panache thing --- .../annotation/AnnotationMetaEntity.java | 39 ++++-------------- .../hibernate/processor/util/TypeUtils.java | 40 +++++++++++++++---- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java index 7b3422f43c56..dcd8c1482225 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java @@ -33,7 +33,6 @@ import org.hibernate.query.sqm.tree.expression.SqmParameter; import org.hibernate.query.sqm.tree.select.SqmSelectStatement; -import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; @@ -51,7 +50,6 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVariable; import javax.lang.model.type.WildcardType; -import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import javax.tools.Diagnostic; import java.util.ArrayList; @@ -86,10 +84,12 @@ import static org.hibernate.processor.util.TypeUtils.containsAnnotation; import static org.hibernate.processor.util.TypeUtils.determineAccessTypeForHierarchy; import static org.hibernate.processor.util.TypeUtils.determineAnnotationSpecifiedAccessType; +import static org.hibernate.processor.util.TypeUtils.extendsClass; import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass; import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror; import static org.hibernate.processor.util.TypeUtils.getAnnotationValue; import static org.hibernate.processor.util.TypeUtils.hasAnnotation; +import static org.hibernate.processor.util.TypeUtils.implementsInterface; import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind; import static org.hibernate.processor.util.TypeUtils.propertyName; @@ -660,41 +660,18 @@ boolean needsDefaultConstructor() { } private boolean isPanacheType(TypeElement type) { - return isOrmPanacheType( type ) - || isReactivePanacheType( type ); + return context.usesQuarkusOrm() && isOrmPanacheType( type ) + || context.usesQuarkusReactive() && isReactivePanacheType( type ); } private boolean isOrmPanacheType(TypeElement type) { - final ProcessingEnvironment processingEnvironment = context.getProcessingEnvironment(); - final Elements elements = processingEnvironment.getElementUtils(); - final TypeElement panacheRepositorySuperType = elements.getTypeElement( PANACHE_ORM_REPOSITORY_BASE ); - final TypeElement panacheEntitySuperType = elements.getTypeElement( PANACHE_ORM_ENTITY_BASE ); - if ( panacheRepositorySuperType == null || panacheEntitySuperType == null ) { - return false; - } - else { - final Types types = processingEnvironment.getTypeUtils(); - // check against a raw supertype of PanacheRepositoryBase, which .asType() is not - return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) ) - || types.isSubtype( type.asType(), panacheEntitySuperType.asType() ); - } + return implementsInterface( type, PANACHE_ORM_REPOSITORY_BASE ) + || extendsClass( type, PANACHE_ORM_ENTITY_BASE ); } private boolean isReactivePanacheType(TypeElement type) { - final ProcessingEnvironment processingEnvironment = context.getProcessingEnvironment(); - final Elements elements = processingEnvironment.getElementUtils(); - final TypeElement panacheRepositorySuperType = elements.getTypeElement( PANACHE_REACTIVE_REPOSITORY_BASE ); - final TypeElement panacheEntitySuperType = elements.getTypeElement( PANACHE_REACTIVE_ENTITY_BASE ); - - if ( panacheRepositorySuperType == null || panacheEntitySuperType == null ) { - return false; - } - else { - final Types types = processingEnvironment.getTypeUtils(); - // check against a raw supertype of PanacheRepositoryBase, which .asType() is not - return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) ) - || types.isSubtype( type.asType(), panacheEntitySuperType.asType() ); - } + return implementsInterface( type, PANACHE_REACTIVE_REPOSITORY_BASE ) + || extendsClass( type, PANACHE_REACTIVE_ENTITY_BASE ); } /** diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java index 724dd3c8668f..567cc886ed3b 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java @@ -129,12 +129,11 @@ protected TypeMirror defaultAction(TypeMirror e, Void aVoid) { } public static @Nullable TypeElement getSuperclassTypeElement(TypeElement element) { - final TypeMirror superClass = element.getSuperclass(); + final TypeMirror superclass = element.getSuperclass(); //superclass of Object is of NoType which returns some other kind - if ( superClass.getKind() == TypeKind.DECLARED ) { - //F..king Ch...t Have those people used their horrible APIs even once? - final Element superClassElement = ( (DeclaredType) superClass ).asElement(); - return (TypeElement) superClassElement; + if ( superclass.getKind() == TypeKind.DECLARED ) { + final DeclaredType declaredType = (DeclaredType) superclass; + return (TypeElement) declaredType.asElement(); } else { return null; @@ -602,7 +601,7 @@ else if ( element.getKind() == ElementKind.METHOD ) { return elementsUtil.getName(decapitalize(name.substring(3))).toString(); } else if ( name.startsWith( "is" ) ) { - return (elementsUtil.getName(decapitalize(name.substring(2)))).toString(); + return elementsUtil.getName(decapitalize(name.substring(2))).toString(); } return elementsUtil.getName(decapitalize(name)).toString(); } @@ -654,6 +653,33 @@ private static boolean extendsSuperMetaModel(Element superClassElement, boolean || !entityMetaComplete && containsAnnotation( superClassElement, ENTITY, MAPPED_SUPERCLASS ); } + public static boolean implementsInterface(TypeElement type, String interfaceName) { + for ( TypeMirror iface : type.getInterfaces() ) { + if ( iface.getKind() == TypeKind.DECLARED ) { + final DeclaredType declaredType = (DeclaredType) iface; + final TypeElement typeElement = (TypeElement) declaredType.asElement(); + if ( typeElement.getQualifiedName().contentEquals( interfaceName ) + || implementsInterface( typeElement, interfaceName ) ) { + return true; + } + } + } + return false; + } + + public static boolean extendsClass(TypeElement type, String className) { + TypeMirror superclass = type.getSuperclass(); + while ( superclass != null && superclass.getKind() == TypeKind.DECLARED ) { + final DeclaredType declaredType = (DeclaredType) superclass; + final TypeElement typeElement = (TypeElement) declaredType.asElement(); + if ( typeElement.getQualifiedName().contentEquals( className ) ) { + return true; + } + superclass = typeElement.getSuperclass(); + } + return false; + } + static class EmbeddedAttributeVisitor extends SimpleTypeVisitor8<@Nullable TypeElement, Element> { private final Context context; @@ -665,7 +691,7 @@ static class EmbeddedAttributeVisitor extends SimpleTypeVisitor8<@Nullable TypeE public @Nullable TypeElement visitDeclared(DeclaredType declaredType, Element element) { final TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType ); - return containsAnnotation( NullnessUtil.castNonNull( returnedElement ), EMBEDDABLE ) ? returnedElement : null; + return containsAnnotation( castNonNull( returnedElement ), EMBEDDABLE ) ? returnedElement : null; } @Override