1 15 16 package org.objectweb.jac.core.utils; 17 18 import java.io.ByteArrayInputStream; 19 import java.io.ByteArrayOutputStream; 20 import java.io.BufferedInputStream; 21 import java.io.File; 22 import java.io.FileInputStream; 23 import java.io.InputStream; 24 import java.io.ObjectInputStream; 25 import java.io.ObjectOutputStream; 26 import java.io.OutputStream; 27 import java.lang.reflect.Field; 28 29 import java.util.Enumeration; 30 import java.util.Hashtable; 31 import java.util.StringTokenizer; 32 import java.util.Vector; 33 34 35 41 42 43 public class Lib { 44 45 50 51 public static void exec(String command) { 52 53 Runtime runtime = Runtime.getRuntime(); 54 55 try { 56 57 Process p = runtime.exec(command); 58 59 byte[] buf = new byte[1024]; 60 int len; 61 62 63 64 65 InputStream in = new BufferedInputStream( p.getInputStream() ); 66 for ( len=in.read(buf) ; len != -1 ; len=in.read(buf) ) 67 System.out.write( buf, 0, len ); 68 69 70 71 72 in = new BufferedInputStream( p.getErrorStream() ); 73 for ( len=in.read(buf) ; len != -1 ; len=in.read(buf) ) 74 System.err.write( buf, 0, len ); 75 76 77 78 79 p.waitFor(); 80 81 } 82 catch ( Exception e ) { 83 e.printStackTrace(); 84 System.exit(1); 85 } 86 } 87 88 89 97 98 public static String stringsEnumToString(Enumeration stringsEnum) { 99 100 String str = ""; 101 102 while ( stringsEnum.hasMoreElements() ) { 103 str += (String) stringsEnum.nextElement() + " "; 104 } 105 106 return str; 107 } 108 109 110 118 119 public static String[] stringsEnumToStringArray(Enumeration stringsEnum) { 120 121 String str = stringsEnumToString(stringsEnum); 122 StringTokenizer strTok = new StringTokenizer(str); 123 int len = strTok.countTokens(); 124 125 String[] strArray = new String[len]; 126 127 for (int i=0; i<len ; i++) { 128 strArray[i] = new String(strTok.nextToken()); 129 } 130 131 return strArray; 132 } 133 134 135 143 144 public static Object[] enumToArray(Enumeration enum) { 145 146 Vector elements = new Vector(); 147 148 while ( enum.hasMoreElements() ) 149 elements.add( enum.nextElement() ); 150 151 return elements.toArray(); 152 } 153 154 155 162 163 public static String[] stringToStringArray(String str) { 164 165 StringTokenizer strTok = new StringTokenizer(str); 166 int len = strTok.countTokens(); 167 168 String[] strArray = new String[len]; 169 170 for (int i=0; i<len ; i++) { 171 strArray[i] = new String(strTok.nextToken()); 172 } 173 174 return strArray; 175 } 176 177 178 184 185 public static Hashtable stringArrayToHashtable(String[] strs) { 186 187 Hashtable ret = new Hashtable(); 188 189 for (int i=0; i<strs.length; i++) { 190 ret.put(strs[i], ""); 191 } 192 193 return ret; 194 } 195 196 197 205 206 public static byte[] loadByteCodeFromFile(String dirName, String fileName) 207 { 208 209 byte[] byteCode = null; 210 211 try { 212 213 String fullFileName = 214 dirName + new String(fileName).replace('.','/') + ".class"; 215 216 File file = new File( fullFileName ); 217 long fileSize = file.length(); 218 if ( fileSize == 0 ) return null; 219 220 byteCode = new byte[ (int) fileSize ]; 221 InputStream in = new BufferedInputStream( new FileInputStream(file) ); 222 if ( in.read(byteCode) != fileSize ) return null; 223 224 } 225 catch( Exception e ) { e.printStackTrace(); } 226 227 return byteCode; 228 } 229 230 231 237 238 public static byte[] serialize(Object src) { 239 240 return serialize(src, ObjectOutputStream.class); 241 } 242 243 244 252 253 public static byte[] serialize(Object src, Class oosClass) { 254 255 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 256 257 ObjectOutputStream oos = null; 258 byte[] ret = null; 259 260 try { 261 262 oos = 263 (ObjectOutputStream) 264 oosClass. 265 getConstructor( new Class[]{OutputStream.class} ). 266 newInstance( new Object[]{baos} ); 267 268 oos.writeObject( src ); 269 oos.close(); 270 271 ret = baos.toByteArray(); 272 baos.close(); 273 274 } 275 catch( Exception e ) { e.printStackTrace(); } 276 277 return ret; 278 } 279 280 281 287 288 public static Object deserialize(byte[] buf) { 289 290 return deserialize( buf, ObjectInputStream.class ); 291 } 292 293 294 302 303 public static Object deserialize( byte[] data, Class oisClass ) { 304 305 ByteArrayInputStream bais = new ByteArrayInputStream(data); 306 307 ObjectInputStream ois = null; 308 Object ret = null; 309 310 try { 311 312 ois = 313 (ObjectInputStream) 314 oisClass. 315 getConstructor( new Class[]{InputStream.class} ). 316 newInstance( new Object[]{bais} ); 317 318 ret = ois.readObject(); 319 ois.close(); 320 bais.close(); 321 322 } 323 catch( Exception e ) { e.printStackTrace(); } 324 325 return ret; 326 } 327 328 329 335 336 public static String[] getFieldsName( Object src ) { 337 338 String[] fieldsName = null; 339 340 try { 341 342 Field[] fields = src.getClass().getFields(); 343 fieldsName = new String[ fields.length ]; 344 345 for ( int i=0 ; i < fields.length ; i++ ) { 346 fieldsName[i] = fields[i].getName(); 347 } 348 349 } 350 catch( Exception e ) { e.printStackTrace(); } 351 352 return fieldsName; 353 } 354 355 356 362 363 public static Object[] getFieldsValue( Object src ) { 364 365 Object[] fieldsValue = null; 366 367 try { 368 369 Field[] fields = src.getClass().getFields(); 370 fieldsValue = new Object[ fields.length ]; 371 372 for ( int i=0 ; i < fields.length ; i++ ) { 373 fieldsValue[i] = fields[i].get( src ); 374 } 375 376 } 377 catch( Exception e ) { e.printStackTrace(); } 378 379 return fieldsValue; 380 } 381 382 383 390 391 public static Object[] getFieldsValue( Object src, String[] fieldsName ) { 392 393 Object[] fieldsValue = null; 394 395 try { 396 397 Class cl = src.getClass(); 398 fieldsValue = new Object[ fieldsName.length ]; 399 400 for ( int i=0 ; i < fieldsName.length ; i++ ) { 401 fieldsValue[i] = cl.getField(fieldsName[i]).get( src ); 402 } 403 404 } 405 catch( Exception e ) { e.printStackTrace(); } 406 407 return fieldsValue; 408 } 409 410 411 418 419 public static void setFieldsValue(Object src, String[] fieldsName, Object[] fieldsValue) { 420 try { 421 Class cl = src.getClass(); 422 for (int i=0; i<fieldsName.length; i++) { 423 cl.getField(fieldsName[i]).set(src, fieldsValue[i]); 424 } 425 } catch (Exception e) { 426 e.printStackTrace(); 427 } 428 } 429 430 437 438 public static Class[] getClasses( Object[] objs ) { 439 440 Class cl; 441 Field fieldTYPE; 442 Class[] cls = new Class[ objs.length ]; 443 444 for ( int i=0 ; i < objs.length ; i++ ) { 445 446 if( objs[i] != null ) { 447 cl = objs[i].getClass(); 448 449 453 454 try { 455 fieldTYPE = cl.getField("TYPE"); 456 cls[i] = (Class) fieldTYPE.get( objs[i] ); 457 } 458 catch( NoSuchFieldException e ) { cls[i] = cl; } 459 catch( IllegalAccessException e ) { cls[i] = cl; } 460 } else { 461 cls[i] = Object.class; 462 } 463 } 464 return cls; 465 } 466 467 468 473 474 public static void printArray( Object o ) { 475 476 if ( o == null ) { 477 System.out.print ( " <null> " ); 478 return; 479 } 480 481 if ( o.getClass().isArray() ) { 482 483 System.out.print ( "[ " ); 484 485 for ( int i = 0; i < ((Object[])o).length; i++ ) { 486 printArray( ((Object[])o)[i] ); 487 } 488 489 System.out.print ( "]" ); 490 491 } else { 492 System.out.print ( o + " " ); 493 } 494 495 } 496 497 } 498 | Popular Tags |