This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch test/document-datamapping-core-finders in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 6bcc9600512d324ad6e8969855e1dcd839b3705b Author: Walter Duque de Estrada <[email protected]> AuthorDate: Sat Aug 15 15:52:42 2026 -0500 Fix IntelliJ warnings on SingleResultFinder - Raw Class/Closure/DetachedCriteria on the three invoke overloads. - constructFromEqualExpressions' Map.put on the raw HashMap is "unchecked", a separate category from the "rawtypes" it already suppressed. - findByBoolean/findOrCreateBy/findOrSaveBy(MappingContext) had no callers, same Datastore/MappingContext factory-pair pattern as ListResultFinder - add the missing stateless-mode tests (mirroring findBy(MappingContext)'s existing one) instead of removing them. Co-Authored-By: Claude Sonnet 5 <[email protected]> --- .../datastore/gorm/finders/SingleResultFinder.java | 35 +++++++++++----------- .../gorm/finders/SingleResultFinderSpec.groovy | 24 +++++++++++++++ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/finders/SingleResultFinder.java b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/finders/SingleResultFinder.java index a05fce66a8..57024c1710 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/finders/SingleResultFinder.java +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/finders/SingleResultFinder.java @@ -35,7 +35,6 @@ import org.springframework.core.convert.ConversionException; import grails.gorm.DetachedCriteria; import org.grails.datastore.mapping.core.Datastore; import org.grails.datastore.mapping.core.Session; -import org.grails.datastore.mapping.core.SessionCallback; import org.grails.datastore.mapping.core.exceptions.ConfigurationException; import org.grails.datastore.mapping.model.MappingContext; import org.grails.datastore.mapping.query.Query; @@ -135,7 +134,7 @@ public class SingleResultFinder implements FinderMethod, QueryBuildingFinder { * it too when {@code save} is true. The single implementation this helper provides is what * prevents the two finder kinds from ever diverging into duplicate, driftable copies. */ - @SuppressWarnings("rawtypes") + @SuppressWarnings({"rawtypes", "unchecked"}) private static Object constructFromEqualExpressions(DynamicFinderInvocation invocation, boolean save) { Map m = new HashMap(); List<MethodExpression> expressions = invocation.getExpressions(); @@ -166,11 +165,13 @@ public class SingleResultFinder implements FinderMethod, QueryBuildingFinder { } @Override + @SuppressWarnings("rawtypes") public Object invoke(Class clazz, String methodName, Object[] arguments) { return invoke(clazz, methodName, (Closure) null, arguments); } @Override + @SuppressWarnings("rawtypes") public Object invoke(Class clazz, String methodName, Closure additionalCriteria, Object[] arguments) { DynamicFinderInvocation invocation = grammar.createFinderInvocation(clazz, methodName, additionalCriteria, arguments); return doInvoke(invocation); @@ -188,6 +189,7 @@ public class SingleResultFinder implements FinderMethod, QueryBuildingFinder { * @param arguments The method call arguments * @return The result of the method call */ + @SuppressWarnings("rawtypes") public Object invoke(Class clazz, String methodName, DetachedCriteria detachedCriteria, Object[] arguments) { DynamicFinderInvocation invocation = grammar.createFinderInvocation(clazz, methodName, null, arguments); if (detachedCriteria != null) { @@ -200,25 +202,22 @@ public class SingleResultFinder implements FinderMethod, QueryBuildingFinder { if (validate != null) { validate.accept(invocation); } - return FinderSupport.execute(datastore, new SessionCallback<Object>() { - @Override - public Object doInSession(Session session) { - Object result; - if (onNullResult != null) { - try { - result = buildQuery(invocation, session).singleResult(); - } catch (ConversionException e) { - throw new MissingMethodException(invocation.getMethodName(), invocation.getJavaClass(), invocation.getArguments()); - } - if (result == null) { - result = onNullResult.apply(invocation); - } - } - else { + return FinderSupport.execute(datastore, session -> { + Object result; + if (onNullResult != null) { + try { result = buildQuery(invocation, session).singleResult(); + } catch (ConversionException e) { + throw new MissingMethodException(invocation.getMethodName(), invocation.getJavaClass(), invocation.getArguments()); + } + if (result == null) { + result = onNullResult.apply(invocation); } - return result; } + else { + result = buildQuery(invocation, session).singleResult(); + } + return result; }); } diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/finders/SingleResultFinderSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/finders/SingleResultFinderSpec.groovy index c7cbfa7dfc..d2d5e38d5b 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/finders/SingleResultFinderSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/finders/SingleResultFinderSpec.groovy @@ -156,6 +156,14 @@ class SingleResultFinderSpec extends Specification { thrown(IllegalStateException) } + void "findByBoolean invoke throws IllegalStateException when constructed in stateless mode"() { + when: + SingleResultFinder.findByBoolean(mappingContext).invoke(FinderTestEntity, 'findActiveByName', ['Bob'] as Object[]) + + then: + thrown(IllegalStateException) + } + void "findOrCreateBy rejects the Or operator without ever touching the datastore"() { given: Datastore datastoreThatMustNotBeUsed = Mock(Datastore) { @@ -212,6 +220,14 @@ class SingleResultFinderSpec extends Specification { !result.saved } + void "findOrCreateBy invoke throws IllegalStateException when constructed in stateless mode"() { + when: + SingleResultFinder.findOrCreateBy(mappingContext).invoke(FinderTestEntity, 'findOrCreateByName', ['Bob'] as Object[]) + + then: + thrown(IllegalStateException) + } + void "findOrCreateBy throws MissingMethodException on the null-result path when a non-Equal expression reached it"() { given: // validate only rejects GT/LT/GTE/LTE - Like/InList/etc slip past it, so this defensive @@ -262,6 +278,14 @@ class SingleResultFinderSpec extends Specification { result.saved } + void "findOrSaveBy invoke throws IllegalStateException when constructed in stateless mode"() { + when: + SingleResultFinder.findOrSaveBy(mappingContext).invoke(FinderTestEntity, 'findOrSaveByName', ['Bob'] as Object[]) + + then: + thrown(IllegalStateException) + } + void "findOrSaveBy throws MissingMethodException on the null-result path when a non-Equal expression reached it"() { given: Query query = Mock(Query) {
