I need to iterate through a list of static fields of a class (say, MyClass). These fields are all of the type java.util.regex.Pattern. Using reflection, I can get all the static fields as follows:
MyClass mc = new MyClass();
List<Pattern> patternList = new ArrayList<Pattern>();
for (Field f : Commands.class.getDeclaredFields()) {
if (Modifier.isStatic(f.getModifiers())) {
// add the Pattern corresponding to the field f to the list patternList
}
}
Now, since I know that all the fields f are of the type java.util.regex.Pattern, I want to create a List<Pattern> containing all of them. How can I do that?
I haven't found any question that matches mine, although there are several questions on SO about reflection. I apologize if my question is a repetition.