01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.logging;
05:
06: import java.lang.reflect.Field;
07: import java.lang.reflect.Modifier;
08: import java.util.ArrayList;
09: import java.util.List;
10:
11: /**
12: * Silly little class to extract all "static final String" constants (ie. name is ALL CAPS) from a given class
13: */
14: public class Keys {
15:
16: public static String[] getKeys(Class clazz) {
17: if (clazz == null) {
18: return new String[] {};
19: }
20:
21: List keys = new ArrayList();
22: Field[] fields = clazz.getDeclaredFields();
23: for (int i = 0; i < fields.length; i++) {
24: Field field = fields[i];
25: field.setAccessible(true);
26: int access = field.getModifiers();
27: if ((field.getType().equals(String.class))
28: && Modifier.isStatic(access)
29: && Modifier.isFinal(access)) {
30: String fieldName = field.getName();
31: if (fieldName.toUpperCase().equals(fieldName)) {
32: try {
33: keys.add(field.get(clazz));
34: } catch (IllegalAccessException e) {
35: e.printStackTrace();
36: }
37: }
38: }
39: }
40:
41: String[] rv = new String[keys.size()];
42: return (String[]) keys.toArray(rv);
43: }
44:
45: }
|