jdaugherty commented on code in PR #17: URL: https://github.com/apache/grails-intellij-plugin/pull/17#discussion_r3684736575
########## plugin/src/main/java/org/apache/grails/intellij/plugin/references/domain/criteria/BuildableCriteriaImplicitMemberContributor.java: ########## @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.references.domain.criteria; + +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiType; +import com.intellij.psi.ResolveState; +import com.intellij.psi.scope.DelegatingScopeProcessor; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.psi.util.InheritanceUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.groovy.lang.resolve.NonCodeMembersContributor; +import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; +import org.jetbrains.plugins.groovy.util.dynamicMembers.DynamicMemberUtils; + +import java.util.Set; + +/** + * Since GORM 4 {@code createCriteria()} comes from the {@code GormEntity} trait and is declared to return + * {@link #BUILDABLE_CRITERIA_CLASS}, not {@link CriteriaBuilderUtil#CRITERIA_BUILDER_CLASS}. That interface + * declares {@code get}, {@code list}, {@code listDistinct} and {@code scroll}, which is why those forms + * resolve out of the box, but the remaining closure-terminal calls only exist in + * {@code AbstractHibernateCriteriaBuilder.invokeMethod(...)} and are therefore invisible to resolution: + * {@code Ddd.createCriteria().count { ... }} used to resolve to nothing, which in turn left + * {@link CriteriaBuilderUtil#checkCriteriaClosure} unable to find the domain class, so no property inside + * the closure could be navigated either. + */ +final class BuildableCriteriaImplicitMemberContributor extends NonCodeMembersContributor { + public static final String BUILDABLE_CRITERIA_CLASS = "org.grails.datastore.mapping.query.api.BuildableCriteria"; + + /** + * Members of {@link CriteriaBuilderImplicitMemberContributor#CLASS_SOURCE} that + * {@link #BUILDABLE_CRITERIA_CLASS} (or its {@code Criteria} supertype) does not declare itself. + * Contributing the rest would duplicate real methods. + */ + // #CHECK# org.grails.datastore.mapping.query.api.BuildableCriteria against org.grails.orm.hibernate.query.AbstractHibernateCriteriaBuilder#invokeMethod(...) + private static final Set<String> MEMBERS_MISSING_FROM_BUILDABLE_CRITERIA = Set.of("count", "call", "doCall"); + + @Override + protected String getParentClassName() { + return BUILDABLE_CRITERIA_CLASS; + } + + @Override + public void processDynamicElements(@NotNull PsiType qualifierType, + @Nullable PsiClass aClass, + @NotNull PsiScopeProcessor processor, + @NotNull PsiElement place, + @NotNull ResolveState state) { + if (aClass == null) return; + + // HibernateCriteriaBuilder implements BuildableCriteria since GORM 4, and a qualifier typed as the + // builder is already served by CriteriaBuilderImplicitMemberContributor. + if (InheritanceUtil.isInheritor(aClass, CriteriaBuilderUtil.CRITERIA_BUILDER_CLASS)) return; + + String nameHint = ResolveUtil.getNameHint(processor); + if (nameHint != null && !MEMBERS_MISSING_FROM_BUILDABLE_CRITERIA.contains(nameHint)) return; Review Comment: Two things about this pair of guards. **1. The guard doesn't appear to be load-bearing.** I removed it and re-ran; results were identical in every qualifier shape: | qualifier typed as | with guard | without guard | |---|---|---| | `HibernateCriteriaBuilder` | 1 candidate, `count:Integer` (synthetic) | identical | | `grails.gorm.CriteriaBuilder` | 1 candidate, `count:Number` (real method wins) | identical | | `BuildableCriteria` (via `createCriteria()`) | 1 candidate, `count:Integer` (synthetic) | identical | Completion counts for `count`/`get` were `1` in all six runs, and the whole criteria suite stayed green. There's a principled reason it can't duplicate: both contributors hand the *same* `CLASS_SOURCE` string to `DynamicMemberUtils.process`, which caches its synthetic class per source string — so the "duplicate" is literally the same `PsiMethod` instance contributed twice, and both resolution and completion dedupe by element. I'd lean toward *keeping* it as insurance against a future contributor that doesn't share `CLASS_SOURCE`, but then it needs a test to pin it — as it stands nothing in the suite notices if it's deleted. **2. Regardless of the above, the cheap check should run first.** `processDynamicElements` fires on every member resolve against any `BuildableCriteria`-typed qualifier, so today every `eq`/`ge`/`order`/`list`/`get` resolve pays a full supertype walk before bailing on the name. Swapping them is free: ```suggestion String nameHint = ResolveUtil.getNameHint(processor); if (nameHint != null && !MEMBERS_MISSING_FROM_BUILDABLE_CRITERIA.contains(nameHint)) return; // HibernateCriteriaBuilder implements BuildableCriteria since GORM 4, and a qualifier typed as the // builder is already served by CriteriaBuilderImplicitMemberContributor. if (InheritanceUtil.isInheritor(aClass, CriteriaBuilderUtil.CRITERIA_BUILDER_CLASS)) return; ``` This also feels in keeping with a9d13dc ("Fix expensive method calls from being invoked"). ########## plugin/src/main/java/org/apache/grails/intellij/plugin/references/domain/criteria/BuildableCriteriaImplicitMemberContributor.java: ########## @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.references.domain.criteria; + +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiType; +import com.intellij.psi.ResolveState; +import com.intellij.psi.scope.DelegatingScopeProcessor; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.psi.util.InheritanceUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.groovy.lang.resolve.NonCodeMembersContributor; +import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; +import org.jetbrains.plugins.groovy.util.dynamicMembers.DynamicMemberUtils; + +import java.util.Set; + +/** + * Since GORM 4 {@code createCriteria()} comes from the {@code GormEntity} trait and is declared to return + * {@link #BUILDABLE_CRITERIA_CLASS}, not {@link CriteriaBuilderUtil#CRITERIA_BUILDER_CLASS}. That interface Review Comment: Small doc nits: * The opening sentence is a fragment — "Since GORM 4 `createCriteria()` comes from the `GormEntity` trait and is declared to return X, not Y." has no main clause. Dropping "Since" (or adding one) fixes it. * `{@link #BUILDABLE_CRITERIA_CLASS}` and `{@link CriteriaBuilderUtil#CRITERIA_BUILDER_CLASS}` link to `String` constants but are used in prose as though they name types, so they render oddly. `{@code BuildableCriteria}` / `{@code HibernateCriteriaBuilder}` read better here. ########## plugin/src/test/java/org/apache/grails/intellij/plugin/domain/GrailsBuildableCriteriaTest.java: ########## @@ -0,0 +1,314 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.domain; + +import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiReference; +import com.intellij.testFramework.UsefulTestCase; +import org.apache.grails.intellij.lib.testFramework.GrailsTestCase; +import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Since GORM 4, {@code createCriteria()} returns {@code BuildableCriteria} instead of + * {@code HibernateCriteriaBuilder}, so out of the box only the closure-terminal calls that interface + * declares itself ({@code get}, {@code list}, ...) resolve. The GORM libraries the other tests run against + * are older than that, hence the stubs below for the pieces of a GORM 5 classpath the plugin looks at. + */ +public class GrailsBuildableCriteriaTest extends GrailsTestCase { + /** + * The bundled GORM is older than 4, and so is its {@code grails.orm.HibernateCriteriaBuilder}: it does not + * implement {@code BuildableCriteria}. That hierarchy is what CriteriaBuilderUtil.isCriteriaBuilderMethod() + * keys off, so the builder is stubbed below instead of taken from the library. + */ + @Override + protected boolean needGormLibrary() { + return false; + } + + /** GormTraitContributor only picks GormEntity when {@code org.hibernate.Hibernate} is on the classpath. */ + @Override + protected boolean needHibernate() { + return true; + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + myFixture.addFileToProject("src/java/org/grails/datastore/mapping/query/api/Criteria.java", """ + package org.grails.datastore.mapping.query.api; + + import groovy.lang.Closure; + + public interface Criteria { + Criteria eq(String propertyName, Object value); + Criteria ge(String propertyName, Object value); + Criteria order(String propertyName, String direction); + Criteria and(Closure callable); + } + """); + + myFixture.addFileToProject("src/java/org/grails/datastore/mapping/query/api/BuildableCriteria.java", """ + package org.grails.datastore.mapping.query.api; + + import groovy.lang.Closure; + + public interface BuildableCriteria extends Criteria { + Object get(Closure callable); + Object list(Closure callable); + Object listDistinct(Closure callable); + Object scroll(Closure callable); + } + """); + + myFixture.addFileToProject("src/java/grails/orm/HibernateCriteriaBuilder.java", """ + package grails.orm; + + import groovy.lang.Closure; + import org.grails.datastore.mapping.query.api.BuildableCriteria; + import org.grails.datastore.mapping.query.api.Criteria; + + public class HibernateCriteriaBuilder implements BuildableCriteria { + public Criteria eq(String propertyName, Object value) { return this; } + public Criteria ge(String propertyName, Object value) { return this; } + public Criteria order(String propertyName, String direction) { return this; } + public Criteria and(Closure callable) { return this; } + public Object get(Closure callable) { return null; } + public Object list(Closure callable) { return null; } + public Object listDistinct(Closure callable) { return null; } + public Object scroll(Closure callable) { return null; } + } + """); + + // GormVersion.IS_5 is the lowest version GormTraitContributor injects the trait for. + myFixture.addFileToProject("src/java/grails/gorm/annotation/Entity.java", """ + package grails.gorm.annotation; + + public @interface Entity { + } + """); + + myFixture.addFileToProject("src/groovy/org/grails/datastore/gorm/GormEntity.groovy", """ + package org.grails.datastore.gorm + + import org.grails.datastore.mapping.query.api.BuildableCriteria + + trait GormEntity<D> { + static BuildableCriteria createCriteria() { null } + static Object withCriteria(Closure callable) { null } + } + """); + + addDomain(""" + + class Ddd { + String aaa + String bbb + } + """); + } + + /** + * {@code count} exists only in {@code AbstractHibernateCriteriaBuilder.invokeMethod(...)}, so it has to be + * contributed to {@code BuildableCriteria} explicitly. + */ + public void testResolveCountCall() { + PsiFile file = myFixture.addFileToProject("src/groovy/Ggg.groovy", """ + + class Ggg { + void someMethod() { + Ddd.createCriteria().cou<caret>nt { + ge('aaa', 'bbb') + } + } + } + """); + + myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); + + PsiElement elementAtCaret = myFixture.getElementAtCaret(); + UsefulTestCase.assertInstanceOf(elementAtCaret, PsiMethod.class); + assertEquals("count", ((PsiMethod)elementAtCaret).getName()); + } + + /** + * Resolving {@code count} is what lets CriteriaBuilderUtil.checkCriteriaClosure() find the domain class of + * the closure, which in turn is what makes the properties inside it navigable. + */ + public void testNavigateToPropertyInsideCountClosure() { + PsiFile file = myFixture.addFileToProject("src/groovy/Ggg.groovy", """ + + class Ggg { + void someMethod() { + Ddd.createCriteria().count { + ge('aa<caret>a', 'bbb') + } + } + } + """); + + myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); + + PsiElement elementAtCaret = myFixture.getElementAtCaret(); + UsefulTestCase.assertInstanceOf(elementAtCaret, GrField.class); + assertEquals("aaa", ((GrField)elementAtCaret).getName()); + } + + /** + * Every closure-terminal form has to end up with the same delegate, whether the method is contributed + * ({@code count}, {@code call}) or declared by BuildableCriteria itself ({@code get}, {@code list}, ...). + */ + public void testNavigateToPropertyInsideEveryTerminalClosure() { + PsiFile file = myFixture.addFileToProject("src/groovy/Ggg.groovy", """ + + class Ggg { + void someMethod() { + def c = Ddd.createCriteria() + + Ddd.createCriteria().count { ge('aaa', 'x') } + Ddd.createCriteria().get { ge('aaa', 'x') } + Ddd.createCriteria().list { ge('aaa', 'x') } + Ddd.createCriteria().listDistinct { ge('aaa', 'x') } + Ddd.createCriteria().scroll { ge('aaa', 'x') } + c { ge('aaa', 'x') } + c({ ge('aaa', 'x') }) + } + } + """); + + String text = file.getText(); + List<String> unresolvedForms = new ArrayList<>(); + int propertyCount = 0; + + for (int i = text.indexOf("'aaa'"); i >= 0; i = text.indexOf("'aaa'", i + 1)) { + PsiReference reference = file.findReferenceAt(i + 1); + PsiElement resolved = reference == null ? null : reference.resolve(); + + if (!(resolved instanceof GrField field) || !"aaa".equals(field.getName())) { + int lineStart = text.lastIndexOf('\n', i) + 1; + int lineEnd = text.indexOf('\n', i); + unresolvedForms.add(text.substring(lineStart, lineEnd < 0 ? text.length() : lineEnd).trim()); + } + + propertyCount++; + } + + assertEquals("Not every terminal form was checked", 7, propertyCount); + UsefulTestCase.assertEmpty("The property does not resolve in these forms", unresolvedForms); Review Comment: The `propertyCount == 7` assertion is a nice touch — it stops the loop from silently checking fewer forms than intended. One gap though: this only asserts the `'aaa'` property references resolve. It never checks that the terminal calls themselves resolve, so `listDistinct` and `scroll` aren't actually covered anywhere in the class (`count` has `testResolveCountCall`, `get` has `testDeclaredMemberStillResolvesUnambiguously`). Adding `GrailsTestCase.checkResolve(file);` on this same file would cover all seven terminal calls for free, since it walks every reference in the file. ########## plugin/src/main/java/org/apache/grails/intellij/plugin/references/domain/criteria/BuildableCriteriaImplicitMemberContributor.java: ########## @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.references.domain.criteria; + +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiType; +import com.intellij.psi.ResolveState; +import com.intellij.psi.scope.DelegatingScopeProcessor; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.psi.util.InheritanceUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.groovy.lang.resolve.NonCodeMembersContributor; +import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; +import org.jetbrains.plugins.groovy.util.dynamicMembers.DynamicMemberUtils; + +import java.util.Set; + +/** + * Since GORM 4 {@code createCriteria()} comes from the {@code GormEntity} trait and is declared to return + * {@link #BUILDABLE_CRITERIA_CLASS}, not {@link CriteriaBuilderUtil#CRITERIA_BUILDER_CLASS}. That interface + * declares {@code get}, {@code list}, {@code listDistinct} and {@code scroll}, which is why those forms + * resolve out of the box, but the remaining closure-terminal calls only exist in + * {@code AbstractHibernateCriteriaBuilder.invokeMethod(...)} and are therefore invisible to resolution: + * {@code Ddd.createCriteria().count { ... }} used to resolve to nothing, which in turn left + * {@link CriteriaBuilderUtil#checkCriteriaClosure} unable to find the domain class, so no property inside + * the closure could be navigated either. + */ +final class BuildableCriteriaImplicitMemberContributor extends NonCodeMembersContributor { + public static final String BUILDABLE_CRITERIA_CLASS = "org.grails.datastore.mapping.query.api.BuildableCriteria"; Review Comment: `public` on a member of a package-private class is effectively package-private, which reads as a wider contract than it is. More to the point, this constant's sibling `CRITERIA_BUILDER_CLASS` lives in the public `CriteriaBuilderUtil` — which this class already references two lines into the guard — and `GormClassNames` already centralises GORM FQNs (`ENTITY_TRAIT`, `ENTITY_ANNO`). Either would be a more consistent home than a private constant on the contributor. ########## plugin/src/main/java/org/apache/grails/intellij/plugin/references/domain/criteria/BuildableCriteriaImplicitMemberContributor.java: ########## @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.references.domain.criteria; + +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiType; +import com.intellij.psi.ResolveState; +import com.intellij.psi.scope.DelegatingScopeProcessor; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.psi.util.InheritanceUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.groovy.lang.resolve.NonCodeMembersContributor; +import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; +import org.jetbrains.plugins.groovy.util.dynamicMembers.DynamicMemberUtils; + +import java.util.Set; + +/** + * Since GORM 4 {@code createCriteria()} comes from the {@code GormEntity} trait and is declared to return + * {@link #BUILDABLE_CRITERIA_CLASS}, not {@link CriteriaBuilderUtil#CRITERIA_BUILDER_CLASS}. That interface + * declares {@code get}, {@code list}, {@code listDistinct} and {@code scroll}, which is why those forms + * resolve out of the box, but the remaining closure-terminal calls only exist in + * {@code AbstractHibernateCriteriaBuilder.invokeMethod(...)} and are therefore invisible to resolution: + * {@code Ddd.createCriteria().count { ... }} used to resolve to nothing, which in turn left + * {@link CriteriaBuilderUtil#checkCriteriaClosure} unable to find the domain class, so no property inside + * the closure could be navigated either. + */ +final class BuildableCriteriaImplicitMemberContributor extends NonCodeMembersContributor { + public static final String BUILDABLE_CRITERIA_CLASS = "org.grails.datastore.mapping.query.api.BuildableCriteria"; + + /** + * Members of {@link CriteriaBuilderImplicitMemberContributor#CLASS_SOURCE} that + * {@link #BUILDABLE_CRITERIA_CLASS} (or its {@code Criteria} supertype) does not declare itself. + * Contributing the rest would duplicate real methods. + */ + // #CHECK# org.grails.datastore.mapping.query.api.BuildableCriteria against org.grails.orm.hibernate.query.AbstractHibernateCriteriaBuilder#invokeMethod(...) + private static final Set<String> MEMBERS_MISSING_FROM_BUILDABLE_CRITERIA = Set.of("count", "call", "doCall"); Review Comment: The stated rationale doesn't match the actual set, which matters because this comment is the maintenance contract behind the `#CHECK#` marker. `projections` is declared by **neither** `BuildableCriteria` nor `Criteria` (I grepped both interfaces in 7.1.99), yet it's excluded from the set. The exclusion is *correct* — `projections` is a closure-**body** member, supplied inside the closure by `CriteriaClosureMemberContributor` → `CriteriaBuilderImplicitMemberContributor.process` — but by the letter of this comment it looks like an oversight, so the next person auditing the `#CHECK#` will "fix" it by adding `projections` and get a bogus completion entry on every `createCriteria()` qualifier. Suggest rewording to say the set is the missing **qualifier-level terminal** calls, not simply the members the interface doesn't declare. Separately on `doCall`: Groovy's implicit-call resolution (`c { }` → `c.call(closure)`) looks for `call`, not `doCall` — `doCall` is closure protocol. So it isn't what makes the shorthand work (only `call` is), and it does add a completion entry on every qualifier. Keeping it for `CLASS_SOURCE` parity is defensible; I'd just not credit it to the shorthand form. ########## plugin/src/test/java/org/apache/grails/intellij/plugin/domain/GrailsBuildableCriteriaTest.java: ########## @@ -0,0 +1,314 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.domain; + +import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiReference; +import com.intellij.testFramework.UsefulTestCase; +import org.apache.grails.intellij.lib.testFramework.GrailsTestCase; +import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Since GORM 4, {@code createCriteria()} returns {@code BuildableCriteria} instead of + * {@code HibernateCriteriaBuilder}, so out of the box only the closure-terminal calls that interface + * declares itself ({@code get}, {@code list}, ...) resolve. The GORM libraries the other tests run against + * are older than that, hence the stubs below for the pieces of a GORM 5 classpath the plugin looks at. + */ +public class GrailsBuildableCriteriaTest extends GrailsTestCase { + /** + * The bundled GORM is older than 4, and so is its {@code grails.orm.HibernateCriteriaBuilder}: it does not + * implement {@code BuildableCriteria}. That hierarchy is what CriteriaBuilderUtil.isCriteriaBuilderMethod() + * keys off, so the builder is stubbed below instead of taken from the library. + */ + @Override + protected boolean needGormLibrary() { + return false; + } + + /** GormTraitContributor only picks GormEntity when {@code org.hibernate.Hibernate} is on the classpath. */ + @Override + protected boolean needHibernate() { + return true; + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + myFixture.addFileToProject("src/java/org/grails/datastore/mapping/query/api/Criteria.java", """ + package org.grails.datastore.mapping.query.api; + + import groovy.lang.Closure; + + public interface Criteria { + Criteria eq(String propertyName, Object value); + Criteria ge(String propertyName, Object value); + Criteria order(String propertyName, String direction); + Criteria and(Closure callable); + } + """); + + myFixture.addFileToProject("src/java/org/grails/datastore/mapping/query/api/BuildableCriteria.java", """ + package org.grails.datastore.mapping.query.api; + + import groovy.lang.Closure; + + public interface BuildableCriteria extends Criteria { + Object get(Closure callable); + Object list(Closure callable); + Object listDistinct(Closure callable); + Object scroll(Closure callable); Review Comment: Two fidelity gaps against the real interface, worth closing so the stub doesn't drift misleadingly: * **Missing `Object list(Map params, Closure closure)`.** It's genuinely declared on real `BuildableCriteria`, so `createCriteria().list(max: 10) { }` should already work — but nothing here pins it, and its absence from the stub makes it look like a member the interface lacks. * **Missing `@DelegatesTo(Criteria.class)` on every closure param.** This is the one I'd prioritise: in real GORM those annotations give the closure a `Criteria` delegate, i.e. a competing candidate source for everything inside `get { }` / `list { }` that this test doesn't model at all. Since the whole point of the class is which members resolve inside those closures, the stub is quietly easier than reality. (For what it's worth, the `Criteria` stub's `order(String, String)` is faithful — real `Criteria` declares `order(String)`, `order(Query.Order)` and `order(String, String)`.) Worth noting more generally: this is the first test in the repo to stub a modern GORM classpath instead of using the bundled jars. That's a reasonable call given `testdata/mockGrails14` only ships GORM 1.x, and the Javadoc explains it well — but these stubs will silently drift from real GORM, so the `#CHECK#` convention is doing a lot of work here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
