1 8 14 package jfun.util; 15 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileNotFoundException ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.lang.reflect.Array ; 22 import java.lang.reflect.Field ; 23 import java.lang.reflect.Modifier ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 28 36 public class Misc { 37 40 public static final Object [] array0 = new Object [0]; 41 47 public static Class getArrayType(Class ctype){ 48 return Array.newInstance(ctype, 0).getClass(); 49 } 50 51 57 public static String getTypeName(Class c){ 58 if(c==null) return ""+c; 59 if(c.isArray()){ 60 return getTypeName(c.getComponentType())+"[]"; 61 } 62 else return c.getName(); 63 } 64 65 70 public static Class getWrapperType(Class t) { 71 final Object rt = primitives.get(t); 72 return (rt == null) ? t : (Class ) rt; 73 } 74 80 public static boolean isPrimitiveOf(Class t1, Class t2) { 81 if (t1.isPrimitive()) { 82 return t2.equals(primitives.get(t1)); 83 } else 84 return false; 85 } 86 private static final HashMap primitives = new HashMap (); 87 static { 88 primitives.put(int.class, Integer .class); 89 primitives.put(long.class, Long .class); 90 primitives.put(short.class, Short .class); 91 primitives.put(byte.class, Byte .class); 92 primitives.put(char.class, Character .class); 93 primitives.put(boolean.class, Boolean .class); 94 primitives.put(double.class, Double .class); 95 primitives.put(float.class, Float .class); 96 } 97 102 public static Map readConstants(Class c){ 103 final HashMap map = new HashMap (); 104 if(!Modifier.isPublic(c.getModifiers())) return map; 105 final Field [] flds = c.getFields(); 106 for(int i=0; i<flds.length; i++){ 107 final Field fld = flds[i]; 108 final int mod = fld.getModifiers(); 109 if(Modifier.isStatic(mod) && Modifier.isFinal(mod)){ 110 try{ 111 map.put(fld.getName(), fld.get(null)); 112 } 113 catch(IllegalAccessException e){ 114 throw new IllegalStateException (e.getMessage()); 115 } 116 } 117 } 118 return map; 119 } 120 121 126 public static int getArrayHashcode(Object [] arr){ 127 int r = 0; 128 for(int i=0; i<arr.length; i++){ 129 r = r*31+hashcode(arr[i]); 130 } 131 return r; 132 } 133 134 139 public static int hashcode(Object obj){ 140 return obj==null?0:obj.hashCode(); 141 } 142 148 public static boolean equals(Object o1, Object o2){ 149 return o1==null?o2==null:o1.equals(o2); 150 } 151 159 public static File getAbsolutePath(final File root, 160 File path)throws IOException { 161 if(root==null) 162 return path; 163 if(path.isAbsolute()){ 164 return path; 165 } 166 else{ 167 final String pathname = path.getPath(); 168 if(path==null || pathname.length()==0) 169 return root; 170 final char c = pathname.charAt(0); 171 if(c == File.separatorChar) 172 return path; 173 return new File (root.getAbsolutePath()+File.separatorChar+ 174 (c==File.separatorChar?pathname.substring(1):pathname)) 175 .getCanonicalFile(); 176 } 177 } 178 179 187 public static Properties loadResourceProperties(ClassLoader loader, String resource) 188 throws IOException { 189 final Properties props = new Properties (); 190 final InputStream in = loader.getResourceAsStream(resource); 191 if(in==null){ 192 throw new FileNotFoundException (resource); 193 } 194 try{ 195 props.load(in); 196 } 197 finally{ 198 try{ 199 in.close(); 200 } 201 catch(Exception e){} 202 } 203 return props; 204 } 205 211 public static Properties loadPropertiesFile(File file) 212 throws IOException { 213 final Properties props = new Properties (); 214 final InputStream in = new FileInputStream (file); 215 try{ 216 props.load(in); 217 } 218 finally{ 219 try{ 220 in.close(); 221 } 222 catch(Exception e){} 223 } 224 return props; 225 } 226 227 } 228 | Popular Tags |