Skip to content

Commit

Permalink
HHH-18863 probably more efficient way to detect if a class is a Panac…
Browse files Browse the repository at this point in the history
…he thing
  • Loading branch information
gavinking committed Nov 22, 2024
1 parent c824e1a commit 06758f0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit 06758f0

Please sign in to comment.