improve 'this' emulation for @Query methods

for Jakarta Data TCK work

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-04-04 21:12:29 +02:00
parent 6b1b8d65ed
commit ceefdd162a
1 changed files with 18 additions and 2 deletions

View File

@ -69,6 +69,7 @@ import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static javax.lang.model.util.ElementFilter.fieldsIn; import static javax.lang.model.util.ElementFilter.fieldsIn;
import static javax.lang.model.util.ElementFilter.methodsIn; 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.FROM;
import static org.hibernate.grammars.hql.HqlLexer.GROUP; import static org.hibernate.grammars.hql.HqlLexer.GROUP;
import static org.hibernate.grammars.hql.HqlLexer.HAVING; import static org.hibernate.grammars.hql.HqlLexer.HAVING;
@ -2303,10 +2304,14 @@ public class AnnotationMetaEntity extends AnnotationMeta {
break; break;
} }
} }
for (Token token : allTokens) { for (int i = 0; i < allTokens.size(); i++) {
final Token token = allTokens.get(i);
switch ( token.getType() ) { switch ( token.getType() ) {
case FROM: 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 WHERE:
case HAVING: case HAVING:
case GROUP: case GROUP:
@ -2320,6 +2325,17 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
private static boolean hasAlias(int i, List<? extends Token> 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( private @Nullable DeclaredType resultType(
ExecutableElement method, @Nullable TypeMirror returnType, AnnotationMirror mirror, AnnotationValue value) { ExecutableElement method, @Nullable TypeMirror returnType, AnnotationMirror mirror, AnnotationValue value) {
if ( returnType != null && returnType.getKind() == TypeKind.DECLARED ) { if ( returnType != null && returnType.getKind() == TypeKind.DECLARED ) {