KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > ClassUtils


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

5 package com.tc.util;
6
7 import java.lang.reflect.Method JavaDoc;
8 import java.text.ParseException JavaDoc;
9
10 public class ClassUtils {
11
12   private static final Class JavaDoc METHOD_CLASS = Method JavaDoc.class;
13
14   public static ClassSpec parseFullyQualifiedFieldName(String JavaDoc fieldName) throws ParseException JavaDoc {
15     ClassSpecImpl rv = new ClassSpecImpl();
16     rv.parseFullyQualifiedFieldName(fieldName);
17     return rv;
18   }
19
20   public static int arrayDimensions(Class JavaDoc arrayClass) {
21     if (arrayClass == null) { throw new NullPointerException JavaDoc(); }
22     if (!arrayClass.isArray()) { throw new IllegalArgumentException JavaDoc(arrayClass + " is not an array type"); }
23     return arrayClass.getName().lastIndexOf("[") + 1;
24   }
25
26   public static Class JavaDoc baseComponetType(Class JavaDoc c) {
27     if (c == null) { throw new NullPointerException JavaDoc(); }
28     if (!c.isArray()) { throw new IllegalArgumentException JavaDoc(c + " is not an array type"); }
29
30     while (c.isArray()) {
31       c = c.getComponentType();
32     }
33
34     return c;
35   }
36
37   public static boolean isPrimitiveArray(Object JavaDoc test) {
38     if (test == null) { return false; }
39     Class JavaDoc c = test.getClass();
40     if (!c.isArray()) { return false; }
41     return c.getComponentType().isPrimitive();
42   }
43
44   public static boolean isEnum(Class JavaDoc c) {
45     // a jdk1.4 friendly (but still fast) check for enums
46
Class JavaDoc superClass = c.getSuperclass();
47     if (superClass == null) return false;
48     if (((c.getModifiers() & 0x00004000) != 0) && "java.lang.Enum".equals(superClass.getName())) { return true; }
49     return false;
50   }
51
52   public static boolean isPortableReflectionClass(Class JavaDoc c) {
53     return METHOD_CLASS == c;
54   }
55
56   public interface ClassSpec {
57     public String JavaDoc getFullyQualifiedClassName();
58
59     public String JavaDoc getShortFieldName();
60   }
61
62   private static class ClassSpecImpl implements ClassSpec {
63
64     private String JavaDoc fullyQualifiedClassName;
65     private String JavaDoc shortFieldName;
66
67     private void parseFullyQualifiedFieldName(String JavaDoc fieldName) throws ParseException JavaDoc {
68       if (fieldName == null) throwNotFullyQualifiedFieldName(fieldName, 0);
69       int lastDot = fieldName.lastIndexOf('.');
70       if (lastDot <= 0) throwNotFullyQualifiedFieldName(fieldName, 0);
71       if (lastDot + 1 == fieldName.length()) throwNotFullyQualifiedFieldName(fieldName, lastDot);
72       fullyQualifiedClassName = fieldName.substring(0, lastDot);
73       shortFieldName = fieldName.substring(lastDot + 1);
74     }
75
76     private void throwNotFullyQualifiedFieldName(String JavaDoc fieldName, int position) throws ParseException JavaDoc {
77       throw new ParseException JavaDoc("Not a fully qualified fieldname: " + fieldName, position);
78     }
79
80     public String JavaDoc getFullyQualifiedClassName() {
81       return this.fullyQualifiedClassName;
82     }
83
84     public String JavaDoc getShortFieldName() {
85       return this.shortFieldName;
86     }
87
88     public String JavaDoc toString() {
89       return "ClassSpec[classname=" + fullyQualifiedClassName + ", shortFieldName=" + shortFieldName + "]";
90     }
91   }
92
93 }
94
Popular Tags