1 7 package org.ejtools.util; 8 9 import java.util.ArrayList ; 10 11 12 18 public class ClassTools 19 { 20 21 private static ArrayList numericClasses; 22 23 24 30 public static String classDisplay(String className) 31 { 32 String result = className; 33 34 if (className == null) 35 { 36 return null; 37 } 38 39 if (className.startsWith("[Z")) 40 { 41 result = "boolean[]"; 42 } 43 if (className.startsWith("[C")) 44 { 45 result = "char[]"; 46 } 47 if (className.startsWith("[D")) 48 { 49 result = "double[]"; 50 } 51 if (className.startsWith("[F")) 52 { 53 result = "float[]"; 54 } 55 if (className.startsWith("[I")) 56 { 57 result = "int[]"; 58 } 59 if (className.startsWith("[S")) 60 { 61 result = "short[]"; 62 } 63 if (className.startsWith("[J")) 64 { 65 result = "long[]"; 66 } 67 if (className.startsWith("[B")) 68 { 69 result = "byte[]"; 70 } 71 if (className.startsWith("[L")) 72 { 73 result = className.substring(2, className.length() - 1) + "[]"; 74 } 75 76 return result; 77 } 78 79 80 86 public static Class getClass(String fullPathClassName) 87 { 88 if (fullPathClassName.equals("void")) 90 { 91 return Void.TYPE; 92 } 93 if (fullPathClassName.equals("int")) 94 { 95 return Integer.TYPE; 96 } 97 if (fullPathClassName.equals("short")) 98 { 99 return Short.TYPE; 100 } 101 if (fullPathClassName.equals("long")) 102 { 103 return Long.TYPE; 104 } 105 if (fullPathClassName.equals("byte")) 106 { 107 return Byte.TYPE; 108 } 109 if (fullPathClassName.equals("char")) 110 { 111 return Character.TYPE; 112 } 113 if (fullPathClassName.equals("float")) 114 { 115 return Float.TYPE; 116 } 117 if (fullPathClassName.equals("double")) 118 { 119 return Double.TYPE; 120 } 121 if (fullPathClassName.equals("boolean")) 122 { 123 return Boolean.TYPE; 124 } 125 126 Class c = null; 128 try 129 { 130 c = Class.forName(fullPathClassName); 131 } 132 catch (Throwable e) 133 { 134 } 136 137 return c; 138 } 139 140 141 147 public static double getValue(Object o) 148 { 149 if (isNumeric(o.getClass())) 150 { 151 if (o instanceof Number ) 152 { 153 return ((Number ) o).doubleValue(); 154 } 155 return (new Double (o.toString())).doubleValue(); 156 } 157 return 0; 158 } 159 160 161 167 public static boolean isNumeric(Class clazz) 168 { 169 return numericClasses.contains(clazz); 170 } 171 172 173 static 174 { 175 numericClasses = new ArrayList (); 176 numericClasses.add(Byte .class); 177 numericClasses.add(Double .class); 178 numericClasses.add(Float .class); 179 numericClasses.add(Integer .class); 180 numericClasses.add(Long .class); 181 numericClasses.add(Short .class); 182 numericClasses.add(Byte.TYPE); 183 numericClasses.add(Double.TYPE); 184 numericClasses.add(Float.TYPE); 185 numericClasses.add(Integer.TYPE); 186 numericClasses.add(Long.TYPE); 187 numericClasses.add(Short.TYPE); 188 } 189 } 190 | Popular Tags |