Skip to content
Advertisement

Get Values of variables under an Enum

I want to write into a file/print the constants of an enum, as well as the values of their variables.

For example, here is what I thought of doing:

JavaScript

However, I am not fully sure on how to do such a thing, as I only recently began working with reflection.

This is the code that I currently have.

JavaScript

Here is the Enum (APIPerm), which I setup for a simple test:

JavaScript

However, I get an NPE in for(Object current: e.getDeclaringClass().getEnumConstants())

Thanks for your help, Sneling.

Advertisement

Answer

Thanks to @Pshemo and @MarkusFisher, I’ve come to a solution.

Note that this method DOES include other classes and methods, but they don’t affect the way this method works.

If you want to test for yourself:

  • Logger.debug can be replaced with System.out.println, LColor should be deleted
  • SnelPlugin is only needed for the Logger.debug and locating a directory.
  • SnelTextFile is just a class to make creating text files easier. Remove if you’re only printing.

Method Code:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement