Skip to content

Commit

Permalink
avoid a lookup by unqualified name that I *think* us unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Dec 18, 2024
1 parent 4b5d3d0 commit 0221a94
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
Expand All @@ -35,7 +34,6 @@
import static java.lang.Boolean.parseBoolean;
import static java.util.Collections.emptyList;
import static org.hibernate.internal.util.collections.ArrayHelper.EMPTY_STRING_ARRAY;
import static org.hibernate.processor.validation.ProcessorSessionFactory.findEntityByUnqualifiedName;

/**
* @author Max Andersen
Expand Down Expand Up @@ -533,27 +531,6 @@ private void addEnumValue(String qualifiedTypeName, String value) {
enumTypesByValue.computeIfAbsent( value, s -> new TreeSet<>() ).add( qualifiedTypeName );
}

public @Nullable TypeElement entityType(String entityName) {
final Elements elementUtils = getElementUtils();
final String qualifiedName = qualifiedNameForEntityName(entityName);
if ( qualifiedName != null ) {
return elementUtils.getTypeElement(qualifiedName);
}
TypeElement symbol =
findEntityByUnqualifiedName( entityName,
elementUtils.getModuleElement("") );
if ( symbol != null ) {
return symbol;
}
for ( ModuleElement module : elementUtils.getAllModuleElements() ) {
symbol = findEntityByUnqualifiedName( entityName, module );
if ( symbol != null ) {
return symbol;
}
}
return null;
}

public void setIndexing(boolean index) {
this.indexing = index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public class HibernateProcessor extends AbstractProcessor {

private static final boolean ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS = false;

// dupe of ProcessorSessionFactory.ENTITY_INDEX for reasons of modularity
public static final String ENTITY_INDEX = "entity.index";

private Context context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
final AnnotationValue annotationValue = getAnnotationValue( mirror, "resultClass" );
final String resultType = annotationValue != null
? annotationValue.getValue().toString()
: resultType( selectStatement, context );
: resultType( selectStatement );
putMember( name,
new NamedQueryMethod(
this,
Expand All @@ -128,7 +128,7 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
);
}
if ( getAnnotationValue( mirror, "resultClass" ) == null ) {
final String resultType = resultType( selectStatement, context );
final String resultType = resultType( selectStatement );
if ( resultType != null ) {
putMember( "QUERY_" + name,
new TypedMetaAttribute( this, name, "QUERY_", resultType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,33 @@
*/
package org.hibernate.processor.util;

import org.hibernate.processor.Context;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.query.criteria.JpaEntityJoin;
import org.hibernate.query.criteria.JpaRoot;
import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.sqm.tree.select.SqmSelectClause;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelectableNode;

import javax.lang.model.element.TypeElement;
import java.util.List;

public final class SqmTypeUtils {
private SqmTypeUtils() {
}

public static String resultType(SqmSelectStatement<?> selectStatement, Context context) {
final String javaTypeName = selectStatement.getSelection().getJavaTypeName();
if ( javaTypeName != null ) {
return javaTypeName;
public static String resultType(SqmSelectStatement<?> selectStatement) {
final JpaSelection<?> selection = selectStatement.getSelection();
if (selection instanceof SqmSelectClause from) {
return from.getSelectionItems().size() > 1
? "Object[]"
: from.getSelectionItems().get(0).getJavaTypeName();
}
else if (selection instanceof JpaRoot<?> root) {
return root.getJavaTypeName();
}
else if (selection instanceof JpaEntityJoin<?, ?> join) {
return join.getJavaTypeName();
}
else {
final List<SqmSelectableNode<?>> items =
selectStatement.getQuerySpec().getSelectClause().getSelectionItems();
final SqmExpressible<?> expressible;
if ( items.size() == 1 && (expressible = items.get( 0 ).getExpressible()) != null ) {
final String typeName = expressible.getTypeName();
final TypeElement entityType = context.entityType( typeName );
return entityType == null ? typeName : entityType.getQualifiedName().toString();
}
else {
return "Object[]";
}
return selection.getJavaTypeName();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ private static JdbcType enumJdbcType(Element member) {
: IntegerJdbcType.INSTANCE;
}

// dupe of HibernateProcessor.ENTITY_INDEX for reasons of modularity
public static final String ENTITY_INDEX = "entity.index";

@Override @Nullable
Expand Down Expand Up @@ -520,7 +521,7 @@ private TypeElement findEntityByUnqualifiedName(String entityName) {
}

if ( indexing ) {
final TypeElement indexedEntity = findIndexedEntityByQualifiedName( entityName );
final TypeElement indexedEntity = findIndexedEntityByUnqualifiedName( entityName );
if ( indexedEntity != null ) {
entityCache.put(entityName, indexedEntity);
return indexedEntity;
Expand All @@ -544,7 +545,7 @@ private TypeElement findEntityByUnqualifiedName(String entityName) {
return null;
}

private @Nullable TypeElement findIndexedEntityByQualifiedName(String entityName) {
private @Nullable TypeElement findIndexedEntityByUnqualifiedName(String entityName) {
final String qualifiedName = entityNameMappings.get(entityName);
if ( qualifiedName != null ) {
return elementUtil.getTypeElement(qualifiedName);
Expand All @@ -564,7 +565,7 @@ private TypeElement findEntityByUnqualifiedName(String entityName) {
return null;
}

public static TypeElement findEntityByUnqualifiedName(String entityName, ModuleElement module) {
private static @Nullable TypeElement findEntityByUnqualifiedName(String entityName, ModuleElement module) {
for (Element element: module.getEnclosedElements()) {
if (element.getKind() == ElementKind.PACKAGE) {
final PackageElement pack = (PackageElement) element;
Expand Down

0 comments on commit 0221a94

Please sign in to comment.