Skip to content

Commit 06758f0

Browse files
committed
HHH-18863 probably more efficient way to detect if a class is a Panache thing
1 parent c824e1a commit 06758f0

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.hibernate.query.sqm.tree.expression.SqmParameter;
3434
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
3535

36-
import javax.annotation.processing.ProcessingEnvironment;
3736
import javax.lang.model.element.AnnotationMirror;
3837
import javax.lang.model.element.AnnotationValue;
3938
import javax.lang.model.element.Element;
@@ -51,7 +50,6 @@
5150
import javax.lang.model.type.TypeMirror;
5251
import javax.lang.model.type.TypeVariable;
5352
import javax.lang.model.type.WildcardType;
54-
import javax.lang.model.util.Elements;
5553
import javax.lang.model.util.Types;
5654
import javax.tools.Diagnostic;
5755
import java.util.ArrayList;
@@ -86,10 +84,12 @@
8684
import static org.hibernate.processor.util.TypeUtils.containsAnnotation;
8785
import static org.hibernate.processor.util.TypeUtils.determineAccessTypeForHierarchy;
8886
import static org.hibernate.processor.util.TypeUtils.determineAnnotationSpecifiedAccessType;
87+
import static org.hibernate.processor.util.TypeUtils.extendsClass;
8988
import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass;
9089
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
9190
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
9291
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
92+
import static org.hibernate.processor.util.TypeUtils.implementsInterface;
9393
import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind;
9494
import static org.hibernate.processor.util.TypeUtils.propertyName;
9595

@@ -660,41 +660,18 @@ boolean needsDefaultConstructor() {
660660
}
661661

662662
private boolean isPanacheType(TypeElement type) {
663-
return isOrmPanacheType( type )
664-
|| isReactivePanacheType( type );
663+
return context.usesQuarkusOrm() && isOrmPanacheType( type )
664+
|| context.usesQuarkusReactive() && isReactivePanacheType( type );
665665
}
666666

667667
private boolean isOrmPanacheType(TypeElement type) {
668-
final ProcessingEnvironment processingEnvironment = context.getProcessingEnvironment();
669-
final Elements elements = processingEnvironment.getElementUtils();
670-
final TypeElement panacheRepositorySuperType = elements.getTypeElement( PANACHE_ORM_REPOSITORY_BASE );
671-
final TypeElement panacheEntitySuperType = elements.getTypeElement( PANACHE_ORM_ENTITY_BASE );
672-
if ( panacheRepositorySuperType == null || panacheEntitySuperType == null ) {
673-
return false;
674-
}
675-
else {
676-
final Types types = processingEnvironment.getTypeUtils();
677-
// check against a raw supertype of PanacheRepositoryBase, which .asType() is not
678-
return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) )
679-
|| types.isSubtype( type.asType(), panacheEntitySuperType.asType() );
680-
}
668+
return implementsInterface( type, PANACHE_ORM_REPOSITORY_BASE )
669+
|| extendsClass( type, PANACHE_ORM_ENTITY_BASE );
681670
}
682671

683672
private boolean isReactivePanacheType(TypeElement type) {
684-
final ProcessingEnvironment processingEnvironment = context.getProcessingEnvironment();
685-
final Elements elements = processingEnvironment.getElementUtils();
686-
final TypeElement panacheRepositorySuperType = elements.getTypeElement( PANACHE_REACTIVE_REPOSITORY_BASE );
687-
final TypeElement panacheEntitySuperType = elements.getTypeElement( PANACHE_REACTIVE_ENTITY_BASE );
688-
689-
if ( panacheRepositorySuperType == null || panacheEntitySuperType == null ) {
690-
return false;
691-
}
692-
else {
693-
final Types types = processingEnvironment.getTypeUtils();
694-
// check against a raw supertype of PanacheRepositoryBase, which .asType() is not
695-
return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) )
696-
|| types.isSubtype( type.asType(), panacheEntitySuperType.asType() );
697-
}
673+
return implementsInterface( type, PANACHE_REACTIVE_REPOSITORY_BASE )
674+
|| extendsClass( type, PANACHE_REACTIVE_ENTITY_BASE );
698675
}
699676

700677
/**

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,11 @@ protected TypeMirror defaultAction(TypeMirror e, Void aVoid) {
129129
}
130130

131131
public static @Nullable TypeElement getSuperclassTypeElement(TypeElement element) {
132-
final TypeMirror superClass = element.getSuperclass();
132+
final TypeMirror superclass = element.getSuperclass();
133133
//superclass of Object is of NoType which returns some other kind
134-
if ( superClass.getKind() == TypeKind.DECLARED ) {
135-
//F..king Ch...t Have those people used their horrible APIs even once?
136-
final Element superClassElement = ( (DeclaredType) superClass ).asElement();
137-
return (TypeElement) superClassElement;
134+
if ( superclass.getKind() == TypeKind.DECLARED ) {
135+
final DeclaredType declaredType = (DeclaredType) superclass;
136+
return (TypeElement) declaredType.asElement();
138137
}
139138
else {
140139
return null;
@@ -602,7 +601,7 @@ else if ( element.getKind() == ElementKind.METHOD ) {
602601
return elementsUtil.getName(decapitalize(name.substring(3))).toString();
603602
}
604603
else if ( name.startsWith( "is" ) ) {
605-
return (elementsUtil.getName(decapitalize(name.substring(2)))).toString();
604+
return elementsUtil.getName(decapitalize(name.substring(2))).toString();
606605
}
607606
return elementsUtil.getName(decapitalize(name)).toString();
608607
}
@@ -654,6 +653,33 @@ private static boolean extendsSuperMetaModel(Element superClassElement, boolean
654653
|| !entityMetaComplete && containsAnnotation( superClassElement, ENTITY, MAPPED_SUPERCLASS );
655654
}
656655

656+
public static boolean implementsInterface(TypeElement type, String interfaceName) {
657+
for ( TypeMirror iface : type.getInterfaces() ) {
658+
if ( iface.getKind() == TypeKind.DECLARED ) {
659+
final DeclaredType declaredType = (DeclaredType) iface;
660+
final TypeElement typeElement = (TypeElement) declaredType.asElement();
661+
if ( typeElement.getQualifiedName().contentEquals( interfaceName )
662+
|| implementsInterface( typeElement, interfaceName ) ) {
663+
return true;
664+
}
665+
}
666+
}
667+
return false;
668+
}
669+
670+
public static boolean extendsClass(TypeElement type, String className) {
671+
TypeMirror superclass = type.getSuperclass();
672+
while ( superclass != null && superclass.getKind() == TypeKind.DECLARED ) {
673+
final DeclaredType declaredType = (DeclaredType) superclass;
674+
final TypeElement typeElement = (TypeElement) declaredType.asElement();
675+
if ( typeElement.getQualifiedName().contentEquals( className ) ) {
676+
return true;
677+
}
678+
superclass = typeElement.getSuperclass();
679+
}
680+
return false;
681+
}
682+
657683
static class EmbeddedAttributeVisitor extends SimpleTypeVisitor8<@Nullable TypeElement, Element> {
658684
private final Context context;
659685

@@ -665,7 +691,7 @@ static class EmbeddedAttributeVisitor extends SimpleTypeVisitor8<@Nullable TypeE
665691
public @Nullable TypeElement visitDeclared(DeclaredType declaredType, Element element) {
666692
final TypeElement returnedElement = (TypeElement)
667693
context.getTypeUtils().asElement( declaredType );
668-
return containsAnnotation( NullnessUtil.castNonNull( returnedElement ), EMBEDDABLE ) ? returnedElement : null;
694+
return containsAnnotation( castNonNull( returnedElement ), EMBEDDABLE ) ? returnedElement : null;
669695
}
670696

671697
@Override

0 commit comments

Comments
 (0)