Skip to content

Commit 4a974fe

Browse files
committed
fix up warnings in BasicDotIdentifierConsumer + genericize FullyQualifiedReflectivePathTerminal
1 parent 0dedc62 commit 4a974fe

File tree

5 files changed

+62
-51
lines changed

5 files changed

+62
-51
lines changed

hibernate-core/src/main/java/org/hibernate/query/hql/internal/BasicDotIdentifierConsumer.java

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@
3030
import org.hibernate.type.descriptor.java.JavaType;
3131

3232
/**
33-
* @asciidoc
34-
*
35-
* DotIdentifierHandler used to interpret paths outside of any specific
36-
* context. This is the handler used at the root of the handler stack.
37-
*
38-
* It can recognize any number of types of paths -
39-
*
40-
* * fully-qualified class names (entity or otherwise)
41-
* * static field references, e.g. `MyClass.SOME_FIELD`
42-
* * enum value references, e.g. `Sex.MALE`
43-
* * navigable-path
44-
* * others?
33+
* A {@link DotIdentifierConsumer} used to interpret paths outside any
34+
* specific context. This is the handler used at the root of the handler
35+
* stack.
36+
* <p>
37+
* It can recognize any number of types of paths:
38+
* <ul>
39+
* <li>fully-qualified class names (entity or otherwise)
40+
* <li>static field references, e.g. {@code MyClass.SOME_FIELD}
41+
* <li>enum value references, e.g. {@code Sex.MALE}
42+
* <li>navigable-path
43+
* </ul>
4544
*
4645
* @author Steve Ebersole
4746
*/
@@ -189,11 +188,11 @@ public SemanticPathPart resolvePathPart(
189188
final NodeBuilder nodeBuilder = creationContext.getNodeBuilder();
190189
if ( importableName != null ) {
191190
final ManagedDomainType<?> managedType = jpaMetamodel.managedType( importableName );
192-
if ( managedType instanceof EntityDomainType<?> ) {
193-
return new SqmLiteralEntityType<>( (EntityDomainType<?>) managedType, nodeBuilder );
191+
if ( managedType instanceof EntityDomainType<?> entityDomainType ) {
192+
return new SqmLiteralEntityType<>( entityDomainType, nodeBuilder );
194193
}
195-
else if ( managedType instanceof EmbeddableDomainType<?> ) {
196-
return new SqmLiteralEmbeddableType<>( (EmbeddableDomainType<?>) managedType, nodeBuilder );
194+
else if ( managedType instanceof EmbeddableDomainType<?> embeddableDomainType ) {
195+
return new SqmLiteralEmbeddableType<>( embeddableDomainType, nodeBuilder );
197196
}
198197
}
199198

@@ -217,35 +216,49 @@ else if ( managedType instanceof EmbeddableDomainType<?> ) {
217216
try {
218217
final EnumJavaType<?> enumType = jpaMetamodel.getEnumType( prefix );
219218
if ( enumType != null ) {
220-
return new SqmEnumLiteral(
221-
jpaMetamodel.enumValue( enumType, terminal ),
222-
enumType,
223-
terminal,
224-
nodeBuilder
225-
);
219+
return sqmEnumLiteral( jpaMetamodel, enumType, terminal, nodeBuilder );
226220
}
227221

228222
final JavaType<?> fieldJtdTest = jpaMetamodel.getJavaConstantType( prefix, terminal );
229223
if ( fieldJtdTest != null ) {
230-
final Object constantValue = jpaMetamodel.getJavaConstant( prefix, terminal );
231-
return new SqmFieldLiteral( constantValue, fieldJtdTest, terminal, nodeBuilder );
232-
224+
return sqmFieldLiteral( jpaMetamodel, prefix, terminal, fieldJtdTest, nodeBuilder );
233225
}
234226
}
235227
catch (Exception ignore) {
236228
}
237229
}
238230

239-
throw new SemanticException(
240-
String.format(
241-
"Could not interpret path expression '%s'",
242-
path
243-
)
231+
throw new SemanticException( "Could not interpret path expression '" + path + "'" );
232+
}
233+
234+
private static <E> SqmFieldLiteral<E> sqmFieldLiteral(
235+
JpaMetamodelImplementor jpaMetamodel,
236+
String prefix,
237+
String terminal,
238+
JavaType<E> fieldJtdTest,
239+
NodeBuilder nodeBuilder) {
240+
return new SqmFieldLiteral<>(
241+
jpaMetamodel.getJavaConstant( prefix, terminal ),
242+
fieldJtdTest,
243+
terminal,
244+
nodeBuilder
244245
);
245246
}
246247

247-
protected void validateAsRoot(SqmFrom<?, ?> pathRoot) {
248+
private static <E extends Enum<E>> SqmEnumLiteral<E> sqmEnumLiteral(
249+
JpaMetamodelImplementor jpaMetamodel,
250+
EnumJavaType<E> enumType,
251+
String terminal,
252+
NodeBuilder nodeBuilder) {
253+
return new SqmEnumLiteral<>(
254+
jpaMetamodel.enumValue( enumType, terminal ),
255+
enumType,
256+
terminal,
257+
nodeBuilder
258+
);
259+
}
248260

261+
protected void validateAsRoot(SqmFrom<?, ?> pathRoot) {
249262
}
250263

251264
@Override

hibernate-core/src/main/java/org/hibernate/query/hql/internal/FullyQualifiedReflectivePath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public FullyQualifiedReflectivePath resolvePathPart(
3131
boolean isTerminal,
3232
SqmCreationState creationState) {
3333
if ( isTerminal ) {
34-
return new FullyQualifiedReflectivePathTerminal( this, name, creationState );
34+
return new FullyQualifiedReflectivePathTerminal<>( this, name, creationState );
3535
}
3636
else {
3737
return new FullyQualifiedReflectivePath( this, name );

hibernate-core/src/main/java/org/hibernate/query/hql/internal/FullyQualifiedReflectivePathTerminal.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,15 @@
3535
import jakarta.persistence.criteria.Expression;
3636
import jakarta.persistence.criteria.Predicate;
3737

38-
import org.checkerframework.checker.nullness.qual.Nullable;
39-
4038
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
4139

4240
/**
4341
* @author Steve Ebersole
4442
*/
45-
public class FullyQualifiedReflectivePathTerminal
43+
public class FullyQualifiedReflectivePathTerminal<E>
4644
extends FullyQualifiedReflectivePath
47-
implements SqmExpression {
48-
private final @Nullable SqmExpressible<?> expressibleType;
45+
implements SqmExpression<E> {
46+
private final @Nullable SqmExpressible<E> expressibleType;
4947
private final SqmCreationState creationState;
5048

5149
private final Function<SemanticQueryWalker<?>,?> handler;
@@ -64,7 +62,7 @@ public FullyQualifiedReflectivePathTerminal(
6462
}
6563

6664
@Override
67-
public FullyQualifiedReflectivePathTerminal copy(SqmCopyContext context) {
65+
public FullyQualifiedReflectivePathTerminal<E> copy(SqmCopyContext context) {
6866
return this;
6967
}
7068

@@ -139,23 +137,23 @@ private JavaTypeRegistry javaTypeRegistry() {
139137
}
140138

141139
@Override
142-
public @Nullable SqmExpressible<?> getNodeType() {
140+
public @Nullable SqmExpressible<E> getNodeType() {
143141
return expressibleType;
144142
}
145143

146144
@Override
147-
public Object accept(SemanticQueryWalker walker) {
148-
return handler.apply( walker );
145+
public <X> X accept(SemanticQueryWalker<X> walker) {
146+
return (X) handler.apply( walker );
149147
}
150148

151149
@Override
152-
public JavaType<?> getJavaTypeDescriptor() {
150+
public JavaType<E> getJavaTypeDescriptor() {
153151
return expressibleType == null ? null : expressibleType.getExpressibleJavaType();
154152
}
155153

156154

157155
@Override
158-
public void applyInferableType(@Nullable SqmExpressible type) {
156+
public void applyInferableType(@Nullable SqmExpressible<?> type) {
159157
}
160158

161159
@Override
@@ -201,7 +199,7 @@ public SqmExpression<String> asString() {
201199
}
202200

203201
@Override
204-
public SqmExpression<?> as(Class type) {
202+
public <X> SqmExpression<X> as(Class<X> type) {
205203
return null;
206204
}
207205

@@ -236,7 +234,7 @@ public Predicate notEqualTo(Object value) {
236234
}
237235

238236
@Override
239-
public SqmExpression cast(Class type) {
237+
public <X> SqmExpression<X> cast(Class<X> type) {
240238
return null;
241239
}
242240

@@ -266,7 +264,7 @@ public List<? extends JpaSelection<?>> getSelectionItems() {
266264
}
267265

268266
@Override
269-
public JpaSelection<?> alias(String name) {
267+
public JpaSelection<E> alias(String name) {
270268
return null;
271269
}
272270

hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public ExpectingEntityJoinDelegate(
337337

338338
@Override
339339
public void consumeIdentifier(String identifier, boolean isTerminal, boolean allowReuse) {
340-
if ( path.length() != 0 ) {
340+
if ( !path.isEmpty() ) {
341341
path.append( '.' );
342342
}
343343
path.append( identifier );

hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPredicatePathConsumer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ protected void validateAsRoot(SqmFrom<?, ?> pathRoot) {
5353
final SqmCreationProcessingState processingState = getCreationState().getCurrentProcessingState();
5454
// First, we need to find out if the current join is part of current processing query
5555
final SqmQuery<?> currentProcessingQuery = processingState.getProcessingQuery();
56-
if ( currentProcessingQuery instanceof SqmSelectQuery<?> ) {
57-
final SqmQuerySpec<?> querySpec = ( (SqmSelectQuery<?>) currentProcessingQuery ).getQuerySpec();
56+
if ( currentProcessingQuery instanceof SqmSelectQuery<?> selectQuery ) {
57+
final SqmQuerySpec<?> querySpec = selectQuery.getQuerySpec();
5858
final SqmFromClause fromClause = querySpec.getFromClause();
5959
// If the current processing query contains the root of the current join,
6060
// then the root of the processing path must be a root of one of the parent queries
@@ -96,8 +96,8 @@ private void validateAsRootOnParentQueryClosure(
9696
SqmCreationProcessingState processingState) {
9797
while ( processingState != null ) {
9898
final SqmQuery<?> processingQuery = processingState.getProcessingQuery();
99-
if ( processingQuery instanceof SqmSelectQuery<?> ) {
100-
final SqmQuerySpec<?> querySpec = ( (SqmSelectQuery<?>) processingQuery ).getQuerySpec();
99+
if ( processingQuery instanceof SqmSelectQuery<?> selectQuery ) {
100+
final SqmQuerySpec<?> querySpec = selectQuery.getQuerySpec();
101101
final SqmFromClause fromClause = querySpec.getFromClause();
102102
// If we are in a subquery, the "foreign" from element could be one of the subquery roots,
103103
// which is totally fine. The aim of this check is to prevent uses of different "spaces"

0 commit comments

Comments
 (0)