1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import java.io.BufferedWriter ; 14 import java.io.File ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.ObjectStreamClass ; 18 import java.io.OutputStreamWriter ; 19 import java.io.PrintWriter ; 20 import java.io.Writer ; 21 import java.text.DateFormat ; import java.text.MessageFormat ; import java.util.Date ; 24 25 42 public final class SerialVersionComputer { 43 44 private static final String NON_RESOLVABLE_CLASS= "The class {0} could not be resolved."; private static final String NON_SERIALIZABLE_CLASS= "The class {0} does not implement ''java.io.Serializable'' or ''java.io.Externalizable'' or has already an id"; 47 51 private static final boolean DEBUG= false; 52 53 54 private static final String TEMP_FILE_ENCODING= "utf-8"; 56 57 private static final String TEMP_FILE_NAME= "serials.tmp"; 59 65 public static void main(final String [] arguments) { 66 BufferedWriter logger= null; 67 if (DEBUG) { 68 try { 69 logger= new BufferedWriter (new OutputStreamWriter (new FileOutputStream ("C:\\serial.log"))); final Date date= new Date (System.currentTimeMillis()); 71 logger.write("Begin Session: " + DateFormat.getDateInstance().format(date) + " at " + DateFormat.getTimeInstance().format(date) + "\r\n"); logger.write("Argument Count: " + arguments.length + "\r\n"); } catch (IOException exception) { 74 } 76 } 77 if (arguments.length > 0) { 78 final String directory= System.getProperty("java.io.tmpdir"); if (directory != null && !"".equals(directory)) { final String separator= System.getProperty("file.separator"); if (separator != null && !"".equals(separator)) { final File file= new File (directory + separator + TEMP_FILE_NAME); 83 if (DEBUG) { 84 try { 85 logger.write("Created file: " + file.getCanonicalPath() + "\r\n"); } catch (IOException exception) { 87 } 89 } 90 Writer writer= null; 91 try { 92 file.delete(); 93 file.createNewFile(); 94 writer= new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file), TEMP_FILE_ENCODING)); 95 for (int i= 0; i < arguments.length; i++) { 96 try { 97 final ObjectStreamClass clazz= ObjectStreamClass.lookup(Class.forName(arguments[i])); 98 if (clazz != null) { 99 writer.write(new Long (clazz.getSerialVersionUID()).toString()); 100 writer.write('\n'); 101 } else { 102 writer.write(format(NON_SERIALIZABLE_CLASS, arguments[i])); 103 writer.write('\n'); 104 } 105 } catch (ClassNotFoundException exception) { 106 writer.write(format(NON_RESOLVABLE_CLASS, arguments[i])); 107 writer.write('\n'); 108 } 109 } 110 } catch (Throwable throwable) { 111 if (DEBUG) { 112 PrintWriter printer= null; 113 try { 114 logger.write("Exception occurred: " + throwable.getLocalizedMessage() + "\r\n"); printer= new PrintWriter (logger); 116 throwable.printStackTrace(printer); 117 } catch (IOException exc) { 118 } finally { 120 if (printer != null) { 121 printer.close(); 122 } 123 } 124 } 125 } finally { 126 if (writer != null) { 127 try { 128 writer.close(); 129 } catch (IOException exception) { 130 } 132 } 133 } 134 } 135 } 136 } 137 if (DEBUG) { 138 try { 139 logger.write("End Session\r\n"); logger.close(); 141 } catch (IOException exception) { 142 } 144 } 145 } 146 147 private static String format(String message, Object object) { 148 return MessageFormat.format(message, new Object [] { object}); 149 } 150 } 151 | Popular Tags |