Skip to content

Commit 9e2ffa6

Browse files
committed
HHH-18705 Moved resultType to TypeUtils
1 parent 8f11b24 commit 9e2ffa6

File tree

4 files changed

+31
-48
lines changed

4 files changed

+31
-48
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
import static org.hibernate.processor.util.TypeUtils.containsAnnotation;
3030
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
3131
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
32+
import static org.hibernate.processor.util.TypeUtils.resultType;
3233

33-
public abstract class AnnotationMeta implements Metamodel, ResultTypeSupplier {
34+
public abstract class AnnotationMeta implements Metamodel {
3435

3536
void addAuxiliaryMembers() {
3637
addAuxiliaryMembersForAnnotation( NAMED_QUERY, "QUERY_" );
@@ -122,7 +123,7 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
122123
);
123124
}
124125
if ( getAnnotationValue( mirror, "resultClass" ) == null ) {
125-
final String resultType = resultType( selectStatement );
126+
final String resultType = resultType( selectStatement, context );
126127
if ( resultType != null ) {
127128
putMember( "QUERY_" + name,
128129
new TypedMetaAttribute( this, name, "QUERY_", resultType,

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.hibernate.processor.annotation;
66

77
import org.checkerframework.checker.nullness.qual.Nullable;
8-
import org.hibernate.processor.Context;
98
import org.hibernate.processor.model.MetaAttribute;
109
import org.hibernate.processor.model.Metamodel;
1110
import org.hibernate.processor.util.Constants;
@@ -16,11 +15,12 @@
1615
import java.util.TreeSet;
1716

1817
import static org.hibernate.processor.util.StringUtil.nameToFieldName;
18+
import static org.hibernate.processor.util.TypeUtils.resultType;
1919

2020
/**
2121
* @author Gavin King
2222
*/
23-
class NamedQueryMethod implements MetaAttribute, ResultTypeSupplier {
23+
class NamedQueryMethod implements MetaAttribute {
2424
private final AnnotationMeta annotationMeta;
2525
private final SqmSelectStatement<?> select;
2626
private final String name;
@@ -46,11 +46,6 @@ public NamedQueryMethod(
4646
this.addNonnullAnnotation = addNonnullAnnotation;
4747
}
4848

49-
@Override
50-
public Context getContext() {
51-
return annotationMeta.getContext();
52-
}
53-
5449
@Override
5550
public boolean hasTypedAttribute() {
5651
return true;
@@ -126,7 +121,7 @@ private void returnType(StringBuilder declaration) {
126121
declaration
127122
.append(annotationMeta.importType(Constants.LIST))
128123
.append('<')
129-
.append(annotationMeta.importType(resultType( select )))
124+
.append( annotationMeta.importType( resultType( select, annotationMeta.getContext() ) ) )
130125
.append("> ")
131126
.append(name);
132127
if ( reactive ) {

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
import javax.tools.Diagnostic;
3232

3333
import jakarta.persistence.AccessType;
34+
import org.hibernate.query.sqm.SqmExpressible;
35+
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
36+
import org.hibernate.query.sqm.tree.select.SqmSelectableNode;
37+
3438
import java.util.HashMap;
3539
import java.util.HashSet;
3640
import java.util.List;
@@ -682,6 +686,27 @@ static class EmbeddedAttributeVisitor extends SimpleTypeVisitor8<@Nullable TypeE
682686
}
683687
}
684688

689+
public static String resultType(SqmSelectStatement<?> selectStatement, Context context) {
690+
final String javaTypeName = selectStatement.getSelection().getJavaTypeName();
691+
if ( javaTypeName != null ) {
692+
return javaTypeName;
693+
}
694+
else {
695+
final List<SqmSelectableNode<?>> items =
696+
selectStatement.getQuerySpec().getSelectClause().getSelectionItems();
697+
final SqmExpressible<?> expressible;
698+
if ( items.size() == 1 && (expressible = items.getFirst().getExpressible()) != null ) {
699+
final String typeName = expressible.getTypeName();
700+
final TypeElement entityType = context.entityType( typeName );
701+
return entityType == null ? typeName : entityType.getQualifiedName().toString();
702+
}
703+
else {
704+
return "Object[]";
705+
}
706+
}
707+
}
708+
709+
685710
public static boolean isPrimitive(String paramType) {
686711
return PRIMITIVE_TYPES.contains( paramType );
687712
}

0 commit comments

Comments
 (0)