PsiField is enum

Hi

I need to determine if a field's type is based on an enum.
How do I do this?

Do I really need to get from PsiField => PsiClass and then use PsiClass.isEnum()?

An example:

public enum Planets { EARTH, MARS }

public class SpaceTravel {

private Planets from;
private Planets to;
private Date departure;

}

I want to know that the PsiField from is a enum.

8 comments
Comment actions Permalink

Hello Claus,

CI> I need to determine if a field's type is based on an enum. How do I
CI> do this?
CI>
CI> Do I really need to get from PsiField => PsiClass and then use
CI> PsiClass.isEnum()?

Yes, that's how it's supposed to be done.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink


They've made it easier recently. Just use

field instanceof PsiEnumConstant

.

0
Comment actions Permalink

Hello Dave,

DG> They've made it easier recently. Just use
DG>
DG> field instanceof PsiEnumConstant

As far as I understand, this is a different thing. In the example provided
by Claus, 'EARTH' is a PsiEnumConstant, but 'from' is a regular PsiField.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

You're right. I misunderstood.

--Dave Griffith

0
Comment actions Permalink

Yeah I wasn't very easy to get from PsiField -> PsiClas.

I can't show the code as I am not connected to the internet at home where I work with the plugin. Here at work I can use the internet.

I remember I basically had to (pseudo code)



As you can see it's not easy and cumbersome. You manually have to check that the type isn't a simple type (int, char etc.). And this is also quite tricky.

I assume there is some code somewhere in IDEA that Jetbrains use to determine this, but it's not open.

0
Comment actions Permalink

Here's an easier way, but not very easy to discover if you don't know the api. This is one of those things that should be documented.

0
Comment actions Permalink

if (type instanceof PsiClassType) {
return false;
}


Of course you mean

if (!(type instanceof PsiClassType)) {
return false;
}

;)

Sascha

0
Comment actions Permalink

Doh! That's what I meant.

0

Please sign in to leave a comment.