Skip to content

Commit 393d3f5

Browse files
committed
HSEARCH-5358 Fail faster if an enclosing instance parameter is encountered in a projection constructor on JDK 25+
see https://bugs.openjdk.org/browse/JDK-8164714
1 parent f05b717 commit 393d3f5

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

mapper/pojo-base/src/main/java/org/hibernate/search/mapper/pojo/logging/impl/PojoMapperLog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public interface PojoMapperLog
5555
* here to the next value.
5656
*/
5757
@LogMessage(level = TRACE)
58-
@Message(id = ID_OFFSET + 179, value = "")
58+
@Message(id = ID_OFFSET + 180, value = "")
5959
void nextLoggerIdForConvenience();
6060

6161
}

mapper/pojo-base/src/main/java/org/hibernate/search/mapper/pojo/logging/impl/ProjectionLog.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,9 @@ SearchException invalidParameterTypeForDistanceProjectionInProjectionConstructor
162162
SearchException cannotBindSortedSetWithNonComparableElements(@FormatWith(ClassFormatter.class) Class<?> elementType,
163163
@Param EventContext eventContext);
164164

165+
@Message(id = ID_OFFSET + 179,
166+
value = "An enclosing instance constructor parameter on a projection constructor is not allowed. " +
167+
"Make the projection class either static, or a top-level class to remove the enclosing instance constructor parameter.")
168+
SearchException nullEnclosingParameterInProjectionConstructorNotAllowed(@Param EventContext eventContext);
169+
165170
}

mapper/pojo-base/src/main/java/org/hibernate/search/mapper/pojo/model/spi/PojoMethodParameterModel.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.stream.Stream;
1010

1111
import org.hibernate.search.engine.mapper.model.spi.MappableTypeModel;
12+
import org.hibernate.search.util.common.annotation.Incubating;
1213

1314
public interface PojoMethodParameterModel<T> {
1415

@@ -26,6 +27,17 @@ public interface PojoMethodParameterModel<T> {
2627
*/
2728
boolean isEnclosingInstance();
2829

30+
/**
31+
* Starting with JDk 25, there is an additional check within the constructor itself,
32+
* that by default prevents passing {@code null} as a value for an enclosing instance.
33+
*
34+
* @return Whether {@code null} can be passed as an enclosing instance.
35+
*/
36+
@Incubating
37+
default boolean enclosingInstanceCanBeNull() {
38+
return Runtime.version().feature() < 25;
39+
}
40+
2941
/**
3042
* @return {@code true} if {@code obj} is a {@link MappableTypeModel} referencing the exact same type
3143
* with the exact same exposed metadata.

mapper/pojo-base/src/main/java/org/hibernate/search/mapper/pojo/search/definition/binding/impl/ProjectionConstructorParameterBinder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ BeanHolder<? extends ProjectionDefinition<?>> bind() {
5656
// and it's often useful to be able to declare a method-local type for projections
5757
// (those types have a "surrounding instance" parameter in their constructor
5858
// even if they don't use it).
59-
return ConstantProjectionDefinition.nullValue();
59+
60+
// NOTE: with JDK 25+ this is no longer the case,
61+
// and will fail further at runtime when such projection is created.
62+
// Hence, let's fails faster instead;
63+
if ( parameter.enclosingInstanceCanBeNull() ) {
64+
return ConstantProjectionDefinition.nullValue();
65+
}
66+
else {
67+
throw ProjectionLog.INSTANCE.nullEnclosingParameterInProjectionConstructorNotAllowed( eventContext() );
68+
}
6069
}
6170

6271
BeanHolder<? extends ProjectionDefinition<?>> result = null;

0 commit comments

Comments
 (0)