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

0
4 comments

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.

0

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 !

0

So i tried something like this but it doesn't seem to work :

In patterns :

public static final PsiElementPattern GENERIC_VALUE_ATTRIBUTE = PlatformPatterns.psiElement().andOr(
PlatformPatterns.psiElement().afterLeafSkipping(
StandardPatterns.string().oneOf("."),
// grField() doesn't works either
GroovyPatterns.groovyLiteralExpression().with(new FieldTypeCondition(
"GenericValueTypePattern",
"org.apache.ofbiz.entity.GenericValue"))
)
)
public static final PsiElementPattern GENERIC_VALUE_ATTRIBUTE_COMPL = PlatformPatterns.psiElement()
.inside(GENERIC_VALUE_ATTRIBUTE)

The FieldTypeCondition class :

class FieldTypeCondition extends PatternCondition<PsiElement> {
String expectedType
FieldTypeCondition(@Nullable String debugMethodName, String expectedType) {
super(debugMethodName)
this.expectedType = expectedType
}
@Override
boolean accepts(@NotNull PsiElement psiElement, ProcessingContext context) {
boolean isMatch = false
if (psiElement instanceof PsiVariable) {
PsiType elType = TypeConversionUtil.erasure((psiElement as PsiVariable).getType())
isMatch = elType.getCanonicalText() == expectedType
}
return isMatch
}
}

Sorry to bother the forum again, but do you have any idea what's wrong with this code ?

0

So i managed to make it work, like so :

public static final PsiElementPattern GENERIC_VALUE_ATTRIBUTE = PlatformPatterns.psiElement().andOr(
PlatformPatterns.psiElement().afterLeafSkipping(
PlatformPatterns.psiElement().withText("."),
PlatformPatterns.psiElement().withParent(
PlatformPatterns.psiElement().with( new FieldTypeCondition(
"GenericValueTypePattern",
"org.apache.ofbiz.entity.GenericValue"))
)
)
)

and

class FieldTypeCondition extends PatternCondition<PsiElement> {
String expectedType

FieldTypeCondition(@Nullable String debugMethodName, String expectedType) {
super(debugMethodName)
this.expectedType = expectedType
}

@Override
boolean accepts(@NotNull PsiElement element, ProcessingContext context) {
boolean isMatch = false
if (element instanceof GrReferenceExpression) {
PsiType myType = (element as GrReferenceExpression).getType()
isMatch = myType.getCanonicalText() == expectedType
}
return isMatch
}
}

Thanks for the help !

0

Please sign in to leave a comment.