1 5 package com.tc.util; 6 7 import java.lang.reflect.Method ; 8 import java.text.ParseException ; 9 10 public class ClassUtils { 11 12 private static final Class METHOD_CLASS = Method .class; 13 14 public static ClassSpec parseFullyQualifiedFieldName(String fieldName) throws ParseException { 15 ClassSpecImpl rv = new ClassSpecImpl(); 16 rv.parseFullyQualifiedFieldName(fieldName); 17 return rv; 18 } 19 20 public static int arrayDimensions(Class arrayClass) { 21 if (arrayClass == null) { throw new NullPointerException (); } 22 if (!arrayClass.isArray()) { throw new IllegalArgumentException (arrayClass + " is not an array type"); } 23 return arrayClass.getName().lastIndexOf("[") + 1; 24 } 25 26 public static Class baseComponetType(Class c) { 27 if (c == null) { throw new NullPointerException (); } 28 if (!c.isArray()) { throw new IllegalArgumentException (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 test) { 38 if (test == null) { return false; } 39 Class c = test.getClass(); 40 if (!c.isArray()) { return false; } 41 return c.getComponentType().isPrimitive(); 42 } 43 44 public static boolean isEnum(Class c) { 45 Class 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 c) { 53 return METHOD_CLASS == c; 54 } 55 56 public interface ClassSpec { 57 public String getFullyQualifiedClassName(); 58 59 public String getShortFieldName(); 60 } 61 62 private static class ClassSpecImpl implements ClassSpec { 63 64 private String fullyQualifiedClassName; 65 private String shortFieldName; 66 67 private void parseFullyQualifiedFieldName(String fieldName) throws ParseException { 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 fieldName, int position) throws ParseException { 77 throw new ParseException ("Not a fully qualified fieldname: " + fieldName, position); 78 } 79 80 public String getFullyQualifiedClassName() { 81 return this.fullyQualifiedClassName; 82 } 83 84 public String getShortFieldName() { 85 return this.shortFieldName; 86 } 87 88 public String toString() { 89 return "ClassSpec[classname=" + fullyQualifiedClassName + ", shortFieldName=" + shortFieldName + "]"; 90 } 91 } 92 93 } 94 | Popular Tags |