Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting enum values from shadowed enum type #1791

Closed
Egor18 opened this issue Dec 31, 2017 · 1 comment
Closed

Getting enum values from shadowed enum type #1791

Egor18 opened this issue Dec 31, 2017 · 1 comment
Labels

Comments

@Egor18
Copy link
Contributor

Egor18 commented Dec 31, 2017

Hi. I'm trying to get all elements of some enum.
For the enum, which declaration is present in the source code this works fine:

...
CtTypeReference typeRef = expr.getType();
if (typeRef.isEnum())
{
  CtType type = typeRef.getTypeDeclaration();
  if (type instanceof CtEnum)
  {
    System.out.println(((CtEnum) type).getEnumValues());
  }
}

But once the enum is not present in the source (i.e. it is located in some .jar), this approach does not work.
For shadowed type reference .isEnum() always returns false, and the actual type of such enum is CtClass (why not CtEnum?).
So, it is impossible to cast it to CtEnum and call .getEnumValues().

Obviously, to get enum values from enum which is located in .jar, I can check if it's parent is java.lang.Enum and then get all the fields, excluding .ENUM$VALUES, name and ordinal.
But it seems to be quite overcomplicated, because I need to use two different approaches for shadowed and non-shadowed enums.

So can you please explain, why is this happening? Is it possible to get shadowed enum values easier?
Furthermore, I'm interested, are there any similar problems with other data types?

P.S. This partially concerns incremental build.

Thank you.

@monperrus
Copy link
Collaborator

monperrus commented Dec 31, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants