1 16 17 package org.springframework.core.enums; 18 19 import java.lang.reflect.Field ; 20 import java.lang.reflect.Modifier ; 21 import java.util.Set ; 22 import java.util.TreeSet ; 23 24 import org.springframework.util.Assert; 25 26 35 public class StaticLabeledEnumResolver extends AbstractCachingLabeledEnumResolver { 36 37 40 private static final StaticLabeledEnumResolver INSTANCE = new StaticLabeledEnumResolver(); 41 42 43 48 public static StaticLabeledEnumResolver instance() { 49 return INSTANCE; 50 } 51 52 53 protected Set findLabeledEnums(Class type) { 54 Set typeEnums = new TreeSet (); 55 Field [] fields = type.getFields(); 56 for (int i = 0; i < fields.length; i++) { 57 Field field = fields[i]; 58 if (Modifier.isStatic(field.getModifiers()) && Modifier.isPublic(field.getModifiers())) { 59 if (type.isAssignableFrom(field.getType())) { 60 try { 61 Object value = field.get(null); 62 Assert.isTrue(value instanceof LabeledEnum, "Field value must be a LabeledEnum instance"); 63 typeEnums.add(value); 64 } 65 catch (IllegalAccessException e) { 66 logger.warn("Unable to access field value " + field, e); 67 } 68 } 69 } 70 } 71 return typeEnums; 72 } 73 74 } 75 | Popular Tags |