Mercurial > lasercutter
view src/clojure/asm/commons/SerialVersionUIDAdder.java @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 /***2 * ASM: a very small and fast Java bytecode manipulation framework3 * Copyright (c) 2000-2005 INRIA, France Telecom4 * All rights reserved.5 *6 * Redistribution and use in source and binary forms, with or without7 * modification, are permitted provided that the following conditions8 * are met:9 * 1. Redistributions of source code must retain the above copyright10 * notice, this list of conditions and the following disclaimer.11 * 2. Redistributions in binary form must reproduce the above copyright12 * notice, this list of conditions and the following disclaimer in the13 * documentation and/or other materials provided with the distribution.14 * 3. Neither the name of the copyright holders nor the names of its15 * contributors may be used to endorse or promote products derived from16 * this software without specific prior written permission.17 *18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF28 * THE POSSIBILITY OF SUCH DAMAGE.29 */30 package clojure.asm.commons;32 import java.io.ByteArrayOutputStream;33 import java.io.DataOutputStream;34 import java.io.IOException;35 import java.security.MessageDigest;36 import java.util.ArrayList;37 import java.util.Arrays;38 import java.util.Collection;40 import clojure.asm.ClassAdapter;41 import clojure.asm.ClassVisitor;42 import clojure.asm.FieldVisitor;43 import clojure.asm.MethodVisitor;44 import clojure.asm.Opcodes;46 /**47 * A {@link ClassAdapter} that adds a serial version unique identifier to a48 * class if missing. Here is typical usage of this class:49 * <p/>50 * <pre>51 * ClassWriter cw = new ClassWriter(...);52 * ClassVisitor sv = new SerialVersionUIDAdder(cw);53 * ClassVisitor ca = new MyClassAdapter(sv);54 * new ClassReader(orginalClass).accept(ca, false);55 * </pre>56 * <p/>57 * The SVUID algorithm can be found <a href=58 * "http://java.sun.com/j2se/1.4.2/docs/guide/serialization/spec/class.html"59 * >http://java.sun.com/j2se/1.4.2/docs/guide/serialization/spec/class.html</a>:60 * <p/>61 * <pre>62 * The serialVersionUID is computed using the signature of a stream of bytes63 * that reflect the class definition. The National Institute of Standards and64 * Technology (NIST) Secure Hash Algorithm (SHA-1) is used to compute a65 * signature for the stream. The first two 32-bit quantities are used to form a66 * 64-bit hash. A java.lang.DataOutputStream is used to convert primitive data67 * types to a sequence of bytes. The values input to the stream are defined by68 * the Java Virtual Machine (VM) specification for classes.69 * <p/>70 * The sequence of items in the stream is as follows:71 * <p/>72 * 1. The class name written using UTF encoding.73 * 2. The class modifiers written as a 32-bit integer.74 * 3. The name of each interface sorted by name written using UTF encoding.75 * 4. For each field of the class sorted by field name (except private static76 * and private transient fields):77 * 1. The name of the field in UTF encoding.78 * 2. The modifiers of the field written as a 32-bit integer.79 * 3. The descriptor of the field in UTF encoding80 * 5. If a class initializer exists, write out the following:81 * 1. The name of the method, <clinit>, in UTF encoding.82 * 2. The modifier of the method, java.lang.reflect.Modifier.STATIC,83 * written as a 32-bit integer.84 * 3. The descriptor of the method, ()V, in UTF encoding.85 * 6. For each non-private constructor sorted by method name and signature:86 * 1. The name of the method, <init>, in UTF encoding.87 * 2. The modifiers of the method written as a 32-bit integer.88 * 3. The descriptor of the method in UTF encoding.89 * 7. For each non-private method sorted by method name and signature:90 * 1. The name of the method in UTF encoding.91 * 2. The modifiers of the method written as a 32-bit integer.92 * 3. The descriptor of the method in UTF encoding.93 * 8. The SHA-1 algorithm is executed on the stream of bytes produced by94 * DataOutputStream and produces five 32-bit values sha[0..4].95 * <p/>96 * 9. The hash value is assembled from the first and second 32-bit values of97 * the SHA-1 message digest. If the result of the message digest, the five98 * 32-bit words H0 H1 H2 H3 H4, is in an array of five int values named99 * sha, the hash value would be computed as follows:100 * <p/>101 * long hash = ((sha[0] >>> 24) & 0xFF) |102 * ((sha[0] >>> 16) & 0xFF) << 8 |103 * ((sha[0] >>> 8) & 0xFF) << 16 |104 * ((sha[0] >>> 0) & 0xFF) << 24 |105 * ((sha[1] >>> 24) & 0xFF) << 32 |106 * ((sha[1] >>> 16) & 0xFF) << 40 |107 * ((sha[1] >>> 8) & 0xFF) << 48 |108 * ((sha[1] >>> 0) & 0xFF) << 56;109 * </pre>110 *111 * @author Rajendra Inamdar, Vishal Vishnoi112 */113 public class SerialVersionUIDAdder extends ClassAdapter{115 /**116 * Flag that indicates if we need to compute SVUID.117 */118 protected boolean computeSVUID;120 /**121 * Set to true if the class already has SVUID.122 */123 protected boolean hasSVUID;125 /**126 * Classes access flags.127 */128 protected int access;130 /**131 * Internal name of the class132 */133 protected String name;135 /**136 * Interfaces implemented by the class.137 */138 protected String[] interfaces;140 /**141 * Collection of fields. (except private static and private transient142 * fields)143 */144 protected Collection svuidFields;146 /**147 * Set to true if the class has static initializer.148 */149 protected boolean hasStaticInitializer;151 /**152 * Collection of non-private constructors.153 */154 protected Collection svuidConstructors;156 /**157 * Collection of non-private methods.158 */159 protected Collection svuidMethods;161 /**162 * Creates a new {@link SerialVersionUIDAdder}.163 *164 * @param cv a {@link ClassVisitor} to which this visitor will delegate165 * calls.166 */167 public SerialVersionUIDAdder(final ClassVisitor cv){168 super(cv);169 svuidFields = new ArrayList();170 svuidConstructors = new ArrayList();171 svuidMethods = new ArrayList();172 }174 // ------------------------------------------------------------------------175 // Overriden methods176 // ------------------------------------------------------------------------178 /*179 * Visit class header and get class name, access , and intefraces180 * informatoin (step 1,2, and 3) for SVUID computation.181 */183 public void visit(184 final int version,185 final int access,186 final String name,187 final String signature,188 final String superName,189 final String[] interfaces){190 computeSVUID = (access & Opcodes.ACC_INTERFACE) == 0;192 if(computeSVUID)193 {194 this.name = name;195 this.access = access;196 this.interfaces = interfaces;197 }199 super.visit(version, access, name, signature, superName, interfaces);200 }202 /*203 * Visit the methods and get constructor and method information (step 5 and204 * 7). Also determince if there is a class initializer (step 6).205 */206 public MethodVisitor visitMethod(207 final int access,208 final String name,209 final String desc,210 final String signature,211 final String[] exceptions){212 if(computeSVUID)213 {214 if(name.equals("<clinit>"))215 {216 hasStaticInitializer = true;217 }218 /*219 * Remembers non private constructors and methods for SVUID220 * computation For constructor and method modifiers, only the221 * ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,222 * ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT and ACC_STRICT flags223 * are used.224 */225 int mods = access226 & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PRIVATE227 | Opcodes.ACC_PROTECTED | Opcodes.ACC_STATIC228 | Opcodes.ACC_FINAL | Opcodes.ACC_SYNCHRONIZED229 | Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_STRICT);231 // all non private methods232 if((access & Opcodes.ACC_PRIVATE) == 0)233 {234 if(name.equals("<init>"))235 {236 svuidConstructors.add(new Item(name, mods, desc));237 }238 else if(!name.equals("<clinit>"))239 {240 svuidMethods.add(new Item(name, mods, desc));241 }242 }243 }245 return cv.visitMethod(access, name, desc, signature, exceptions);246 }248 /*249 * Gets class field information for step 4 of the alogrithm. Also determines250 * if the class already has a SVUID.251 */252 public FieldVisitor visitField(253 final int access,254 final String name,255 final String desc,256 final String signature,257 final Object value){258 if(computeSVUID)259 {260 if(name.equals("serialVersionUID"))261 {262 // since the class already has SVUID, we won't be computing it.263 computeSVUID = false;264 hasSVUID = true;265 }266 /*267 * Remember field for SVUID computation For field modifiers, only268 * the ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC,269 * ACC_FINAL, ACC_VOLATILE, and ACC_TRANSIENT flags are used when270 * computing serialVersionUID values.271 */272 int mods = access273 & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PRIVATE274 | Opcodes.ACC_PROTECTED | Opcodes.ACC_STATIC275 | Opcodes.ACC_FINAL | Opcodes.ACC_VOLATILE | Opcodes.ACC_TRANSIENT);277 if((access & Opcodes.ACC_PRIVATE) == 0278 || (access & (Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT)) == 0)279 {280 svuidFields.add(new Item(name, mods, desc));281 }282 }284 return super.visitField(access, name, desc, signature, value);285 }287 /*288 * Add the SVUID if class doesn't have one289 */290 public void visitEnd(){291 // compute SVUID and add it to the class292 if(computeSVUID && !hasSVUID)293 {294 try295 {296 cv.visitField(Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,297 "serialVersionUID",298 "J",299 null,300 new Long(computeSVUID()));301 }302 catch(Throwable e)303 {304 throw new RuntimeException("Error while computing SVUID for "305 + name, e);306 }307 }309 super.visitEnd();310 }312 // ------------------------------------------------------------------------313 // Utility methods314 // ------------------------------------------------------------------------316 /**317 * Returns the value of SVUID if the class doesn't have one already. Please318 * note that 0 is returned if the class already has SVUID, thus use319 * <code>isHasSVUID</code> to determine if the class already had an SVUID.320 *321 * @return Returns the serial version UID322 * @throws IOException323 */324 protected long computeSVUID() throws IOException{325 ByteArrayOutputStream bos = null;326 DataOutputStream dos = null;327 long svuid = 0;329 try330 {331 bos = new ByteArrayOutputStream();332 dos = new DataOutputStream(bos);334 /*335 * 1. The class name written using UTF encoding.336 */337 dos.writeUTF(name.replace('/', '.'));339 /*340 * 2. The class modifiers written as a 32-bit integer.341 */342 dos.writeInt(access343 & (Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL344 | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT));346 /*347 * 3. The name of each interface sorted by name written using UTF348 * encoding.349 */350 Arrays.sort(interfaces);351 for(int i = 0; i < interfaces.length; i++)352 {353 dos.writeUTF(interfaces[i].replace('/', '.'));354 }356 /*357 * 4. For each field of the class sorted by field name (except358 * private static and private transient fields):359 *360 * 1. The name of the field in UTF encoding. 2. The modifiers of the361 * field written as a 32-bit integer. 3. The descriptor of the field362 * in UTF encoding363 *364 * Note that field signatutes are not dot separated. Method and365 * constructor signatures are dot separated. Go figure...366 */367 writeItems(svuidFields, dos, false);369 /*370 * 5. If a class initializer exists, write out the following: 1. The371 * name of the method, <clinit>, in UTF encoding. 2. The modifier of372 * the method, java.lang.reflect.Modifier.STATIC, written as a373 * 32-bit integer. 3. The descriptor of the method, ()V, in UTF374 * encoding.375 */376 if(hasStaticInitializer)377 {378 dos.writeUTF("<clinit>");379 dos.writeInt(Opcodes.ACC_STATIC);380 dos.writeUTF("()V");381 } // if..383 /*384 * 6. For each non-private constructor sorted by method name and385 * signature: 1. The name of the method, <init>, in UTF encoding. 2.386 * The modifiers of the method written as a 32-bit integer. 3. The387 * descriptor of the method in UTF encoding.388 */389 writeItems(svuidConstructors, dos, true);391 /*392 * 7. For each non-private method sorted by method name and393 * signature: 1. The name of the method in UTF encoding. 2. The394 * modifiers of the method written as a 32-bit integer. 3. The395 * descriptor of the method in UTF encoding.396 */397 writeItems(svuidMethods, dos, true);399 dos.flush();401 /*402 * 8. The SHA-1 algorithm is executed on the stream of bytes403 * produced by DataOutputStream and produces five 32-bit values404 * sha[0..4].405 */406 byte[] hashBytes = computeSHAdigest(bos.toByteArray());408 /*409 * 9. The hash value is assembled from the first and second 32-bit410 * values of the SHA-1 message digest. If the result of the message411 * digest, the five 32-bit words H0 H1 H2 H3 H4, is in an array of412 * five int values named sha, the hash value would be computed as413 * follows:414 *415 * long hash = ((sha[0] >>> 24) & 0xFF) | ((sha[0] >>> 16) & 0xFF) <<416 * 8 | ((sha[0] >>> 8) & 0xFF) << 16 | ((sha[0] >>> 0) & 0xFF) <<417 * 24 | ((sha[1] >>> 24) & 0xFF) << 32 | ((sha[1] >>> 16) & 0xFF) <<418 * 40 | ((sha[1] >>> 8) & 0xFF) << 48 | ((sha[1] >>> 0) & 0xFF) <<419 * 56;420 */421 for(int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--)422 {423 svuid = (svuid << 8) | (hashBytes[i] & 0xFF);424 }425 }426 finally427 {428 // close the stream (if open)429 if(dos != null)430 {431 dos.close();432 }433 }435 return svuid;436 }438 /**439 * Returns the SHA-1 message digest of the given value.440 *441 * @param value the value whose SHA message digest must be computed.442 * @return the SHA-1 message digest of the given value.443 */444 protected byte[] computeSHAdigest(final byte[] value){445 try446 {447 return MessageDigest.getInstance("SHA").digest(value);448 }449 catch(Exception e)450 {451 throw new UnsupportedOperationException(e);452 }453 }455 /**456 * Sorts the items in the collection and writes it to the data output stream457 *458 * @param itemCollection collection of items459 * @param dos a <code>DataOutputStream</code> value460 * @param dotted a <code>boolean</code> value461 * @throws IOException if an error occurs462 */463 private void writeItems(464 final Collection itemCollection,465 final DataOutputStream dos,466 final boolean dotted) throws IOException{467 int size = itemCollection.size();468 Item items[] = (Item[]) itemCollection.toArray(new Item[size]);469 Arrays.sort(items);470 for(int i = 0; i < size; i++)471 {472 dos.writeUTF(items[i].name);473 dos.writeInt(items[i].access);474 dos.writeUTF(dotted475 ? items[i].desc.replace('/', '.')476 : items[i].desc);477 }478 }480 // ------------------------------------------------------------------------481 // Inner classes482 // ------------------------------------------------------------------------484 static class Item implements Comparable{486 String name;488 int access;490 String desc;492 Item(final String name, final int access, final String desc){493 this.name = name;494 this.access = access;495 this.desc = desc;496 }498 public int compareTo(final Object o){499 Item other = (Item) o;500 int retVal = name.compareTo(other.name);501 if(retVal == 0)502 {503 retVal = desc.compareTo(other.desc);504 }505 return retVal;506 }507 }508 }