How to get the type of a variable in a pattern
Answered
Hi, i'm stuck with a pattern :
I'd like to check the type of a variable in a pattern, inside a groovy file.
For example this would be the code :
import org.apache.ofbiz.entity.GenericValue
//...
GenericValue partyAcctgPreference = from('PartyAcctgPreference').where(parameters).queryOne()
partyAcctgPreference.myAttribute = //...
I would like a pattern that allows me to detect that myAttribute is an attribute of a GenericValue.
i know there is something like what i want for methods, such as these patterns :
GroovyPatterns.groovyLiteralExpression().methodCallParameter(0,
GroovyPatterns.psiMethod().withName("findList")
.definedInClass("org.apache.ofbiz.entity.Delegator")),
// or
GroovyPatterns.groovyLiteralExpression().methodCallParameter(1,
GroovyPatterns.psiMethod().withName("addMemberEntity")
.definedInClass("org.apache.ofbiz.entity.model.DynamicViewEntity")),
Is there something similar for variable type ?
Something like
PlatformPatterns.psiElement().withTypeFromClass('the.class.i.want')
Thanks berofehand, i'm really stuck with this one
Please sign in to leave a comment.
Hi,
There is no method you need, but you can create your own pattern and use it like:
GroovyPatterns.grField().with(new FieldTypeCondition("org.apache.ofbiz.entity.GenericValue"));
The condition can be implemented based on this answer:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000404860-What-is-the-idiomatic-way-to-use-Pattern-framework-in-platform-core-api-com-intellij-patterns-?page=1#community_comment_360000087699
and Peter's comments.
Hi, thanks for your answer.
I think i get the big picture, but i have little to no kotlin skills.
I will try an keep you updated.
Thanks again for your answer !
So i tried something like this but it doesn't seem to work :
In patterns :
The FieldTypeCondition class :
Sorry to bother the forum again, but do you have any idea what's wrong with this code ?
So i managed to make it work, like so :
and
Thanks for the help !