Last active
August 29, 2015 14:14
-
-
Save joschuck/1d8b290945ac4ab3e27c to your computer and use it in GitHub Desktop.
Java Reflection API - array from final static Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Name { | |
| private final static String = "Alice"; | |
| private final static String = "Eve"; | |
| private final static String = "Bob"; | |
| } | |
| // somewhere else in the program | |
| Name name = new Name(); | |
| Field[] fields = Name.class.getDeclaredFields(); | |
| List<String> names = new ArrayList<String>(); | |
| for (Field field : fields) { | |
| if (java.lang.reflect.Modifier.isStatic(field.getModifiers()) && field.getType() == String.class) { | |
| field.setAccessible(true); | |
| names.add((String) field.get(edgeType)); | |
| } | |
| } | |
| // names ["Alice", "Eve", "Bob"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment