From ceefdd162a6bcfa054a593843659b7ae995ca65e Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 4 Apr 2024 21:12:29 +0200 Subject: [PATCH] improve 'this' emulation for @Query methods for Jakarta Data TCK work Signed-off-by: Gavin King --- .../annotation/AnnotationMetaEntity.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 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 32df57ab9e..a1b1e434e1 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 @@ -69,6 +69,7 @@ import static java.util.Collections.emptyList; import static java.util.stream.Collectors.toList; import static javax.lang.model.util.ElementFilter.fieldsIn; import static javax.lang.model.util.ElementFilter.methodsIn; +import static org.hibernate.grammars.hql.HqlLexer.AS; import static org.hibernate.grammars.hql.HqlLexer.FROM; import static org.hibernate.grammars.hql.HqlLexer.GROUP; import static org.hibernate.grammars.hql.HqlLexer.HAVING; @@ -2303,10 +2304,14 @@ public class AnnotationMetaEntity extends AnnotationMeta { break; } } - for (Token token : allTokens) { + for (int i = 0; i < allTokens.size(); i++) { + final Token token = allTokens.get(i); switch ( token.getType() ) { case FROM: - return hql; + return thisText.isEmpty() || hasAlias(i, allTokens) ? hql + : new StringBuilder(hql) + .insert(allTokens.get(i+1).getStopIndex() + 1, thisText) + .toString(); case WHERE: case HAVING: case GROUP: @@ -2320,6 +2325,17 @@ public class AnnotationMetaEntity extends AnnotationMeta { } } + private static boolean hasAlias(int i, List allTokens) { + if ( allTokens.size() <= i+2 ) { + return false; + } + else { + final int nextTokenType = allTokens.get(i+2).getType(); + return nextTokenType == IDENTIFIER + || nextTokenType == AS; + } + } + private @Nullable DeclaredType resultType( ExecutableElement method, @Nullable TypeMirror returnType, AnnotationMirror mirror, AnnotationValue value) { if ( returnType != null && returnType.getKind() == TypeKind.DECLARED ) {