KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > logging > Keys


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.logging;
5
6 import java.lang.reflect.Field JavaDoc;
7 import java.lang.reflect.Modifier JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
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 JavaDoc[] getKeys(Class JavaDoc clazz) {
17     if (clazz == null) { return new String JavaDoc[] {}; }
18
19     List JavaDoc keys = new ArrayList JavaDoc();
20     Field JavaDoc[] fields = clazz.getDeclaredFields();
21     for (int i = 0; i < fields.length; i++) {
22       Field JavaDoc field = fields[i];
23       field.setAccessible(true);
24       int access = field.getModifiers();
25       if ((field.getType().equals(String JavaDoc.class)) && Modifier.isStatic(access) && Modifier.isFinal(access)) {
26         String JavaDoc fieldName = field.getName();
27         if (fieldName.toUpperCase().equals(fieldName)) {
28           try {
29             keys.add(field.get(clazz));
30           } catch (IllegalAccessException JavaDoc e) {
31             e.printStackTrace();
32           }
33         }
34       }
35     }
36
37     String JavaDoc[] rv = new String JavaDoc[keys.size()];
38     return (String JavaDoc[]) keys.toArray(rv);
39   }
40
41 }
42
Popular Tags