invoke "values" coming from getAllEnums #608
Replies: 5 comments 6 replies
-
Which exception is thrown? For enums, the enum constants are available as methods, if I rember correctly. List methods and fields for an enum, and you should find what you need. If it's methods, not fields, then maybe ClassGraph should add a ClassInfo.getEnumConstants() method that strips out hashCode(), equals(), etc. -- feel free to submit a pull request for that! |
Beta Was this translation helpful? Give feedback.
-
Thanks Luke for your quick answer It gives NoSuchFieldException. by ClassInfoList allEnums = scanResult.getAllEnums(); I get all those enums and then through a for loop I try to get all values per each one so getMine variable should get one by one each enum. The code: if I run the values by using the enum directly in this way below then it works fine. If you want me to create the pull request then let me know, I wanted to share the detail before adding the pull request. Just let me know and I will create it if so. Thanks again |
Beta Was this translation helpful? Give feedback.
-
Hi, The enum constants can be listed by looking at the fields, as I mentioned:
This prints:
Just skip the one |
Beta Was this translation helpful? Give feedback.
-
I added a method to return these enum constants. It will be in the next release (but it's not worth pushing out a release just for this). You can see the code here: |
Beta Was this translation helpful? Give feedback.
-
@FranGomezVenegas Ah, I see the problem. You need not just the names of the enum constants, but also the enum constant objects themselves. The only way to do that is by loading the enum class, then using reflection to access each enum constant by name (since enum constants are stored as static fields of enum classes). I implemented a method to accomplish this: I released this change in version 4.8.138: https://github.com/classgraph/classgraph/releases/tag/classgraph-4.8.138 |
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm refactoring my code in the way to not have to add to my 'declared enums documentation' method the enums one by one any time my code requires one new enum.
The idea is to get them by reflection and it works fine by using getAllEnums therefore I can document all enum names.
My problem comes when I need to get all enum entries, what I'm trying to do is to call the enum 'values' method due to I need to document the enum names and its entries.
I tried by this way. method=getMine.getClass().getMethod(methName, paramTypes), but it raises one Exception.
Then found this alternative way, Field f = getMine.getClass().getDeclaredField("$VALUES"), but did not works, it seems to not be compatible with the ClassInfo object in some way.
Anybody tried to do something similar? How to apply reflection for both, get the enum names (this works for me) and its entries (this is where I'm stuck).
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions