--- old/src/share/classes/jdk/internal/org/objectweb/asm/commons/LocalVariablesSorter.java Thu Apr 25 10:09:21 2013 +++ new/src/share/classes/jdk/internal/org/objectweb/asm/commons/LocalVariablesSorter.java Thu Apr 25 10:09:20 2013 @@ -58,10 +58,12 @@ */ package jdk.internal.org.objectweb.asm.commons; +import jdk.internal.org.objectweb.asm.AnnotationVisitor; import jdk.internal.org.objectweb.asm.Label; import jdk.internal.org.objectweb.asm.MethodVisitor; import jdk.internal.org.objectweb.asm.Opcodes; import jdk.internal.org.objectweb.asm.Type; +import jdk.internal.org.objectweb.asm.TypePath; /** * A {@link MethodVisitor} that renumbers local variables in their order of @@ -77,7 +79,8 @@ */ public class LocalVariablesSorter extends MethodVisitor { - private static final Type OBJECT_TYPE = Type.getObjectType("java/lang/Object"); + private static final Type OBJECT_TYPE = Type + .getObjectType("java/lang/Object"); /** * Mapping from old to new local variable indexes. A local variable at index @@ -111,33 +114,33 @@ * this constructor. Instead, they must use the * {@link #LocalVariablesSorter(int, int, String, MethodVisitor)} version. * - * @param access access flags of the adapted method. - * @param desc the method's descriptor (see {@link Type Type}). - * @param mv the method visitor to which this adapter delegates calls. + * @param access + * access flags of the adapted method. + * @param desc + * the method's descriptor (see {@link Type Type}). + * @param mv + * the method visitor to which this adapter delegates calls. */ - public LocalVariablesSorter( - final int access, - final String desc, - final MethodVisitor mv) - { - this(Opcodes.ASM4, access, desc, mv); + public LocalVariablesSorter(final int access, final String desc, + final MethodVisitor mv) { + this(Opcodes.ASM5, access, desc, mv); } /** * Creates a new {@link LocalVariablesSorter}. * - * @param api the ASM API version implemented by this visitor. Must be one - * of {@link Opcodes#ASM4}. - * @param access access flags of the adapted method. - * @param desc the method's descriptor (see {@link Type Type}). - * @param mv the method visitor to which this adapter delegates calls. + * @param api + * the ASM API version implemented by this visitor. Must be one + * of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}. + * @param access + * access flags of the adapted method. + * @param desc + * the method's descriptor (see {@link Type Type}). + * @param mv + * the method visitor to which this adapter delegates calls. */ - protected LocalVariablesSorter( - final int api, - final int access, - final String desc, - final MethodVisitor mv) - { + protected LocalVariablesSorter(final int api, final int access, + final String desc, final MethodVisitor mv) { super(api, mv); Type[] args = Type.getArgumentTypes(desc); nextLocal = (Opcodes.ACC_STATIC & access) == 0 ? 1 : 0; @@ -151,32 +154,32 @@ public void visitVarInsn(final int opcode, final int var) { Type type; switch (opcode) { - case Opcodes.LLOAD: - case Opcodes.LSTORE: - type = Type.LONG_TYPE; - break; + case Opcodes.LLOAD: + case Opcodes.LSTORE: + type = Type.LONG_TYPE; + break; - case Opcodes.DLOAD: - case Opcodes.DSTORE: - type = Type.DOUBLE_TYPE; - break; + case Opcodes.DLOAD: + case Opcodes.DSTORE: + type = Type.DOUBLE_TYPE; + break; - case Opcodes.FLOAD: - case Opcodes.FSTORE: - type = Type.FLOAT_TYPE; - break; + case Opcodes.FLOAD: + case Opcodes.FSTORE: + type = Type.FLOAT_TYPE; + break; - case Opcodes.ILOAD: - case Opcodes.ISTORE: - type = Type.INT_TYPE; - break; + case Opcodes.ILOAD: + case Opcodes.ISTORE: + type = Type.INT_TYPE; + break; - default: + default: // case Opcodes.ALOAD: // case Opcodes.ASTORE: // case RET: - type = OBJECT_TYPE; - break; + type = OBJECT_TYPE; + break; } mv.visitVarInsn(opcode, remap(var, type)); } @@ -192,28 +195,32 @@ } @Override - public void visitLocalVariable( - final String name, - final String desc, - final String signature, - final Label start, - final Label end, - final int index) - { + public void visitLocalVariable(final String name, final String desc, + final String signature, final Label start, final Label end, + final int index) { int newIndex = remap(index, Type.getType(desc)); mv.visitLocalVariable(name, desc, signature, start, end, newIndex); } @Override - public void visitFrame( - final int type, - final int nLocal, - final Object[] local, - final int nStack, - final Object[] stack) - { + public AnnotationVisitor visitLocalVariableAnnotation(int typeRef, + TypePath typePath, Label[] start, Label[] end, int[] index, + String desc, boolean visible) { + Type t = Type.getType(desc); + int[] newIndex = new int[index.length]; + for (int i = 0; i < newIndex.length; ++i) { + newIndex[i] = remap(index[i], t); + } + return mv.visitLocalVariableAnnotation(typeRef, typePath, start, end, + newIndex, desc, visible); + } + + @Override + public void visitFrame(final int type, final int nLocal, + final Object[] local, final int nStack, final Object[] stack) { if (type != Opcodes.F_NEW) { // uncompressed frame - throw new IllegalStateException("ClassReader.accept() should be called with EXPAND_FRAMES flag"); + throw new IllegalStateException( + "ClassReader.accept() should be called with EXPAND_FRAMES flag"); } if (!changed) { // optimization for the case where mapping = identity @@ -225,6 +232,8 @@ Object[] oldLocals = new Object[newLocals.length]; System.arraycopy(newLocals, 0, oldLocals, 0, oldLocals.length); + updateNewLocals(newLocals); + // copies types from 'local' to 'newLocals' // 'newLocals' already contains the variables added with 'newLocal' @@ -280,38 +289,38 @@ /** * Creates a new local variable of the given type. * - * @param type the type of the local variable to be created. + * @param type + * the type of the local variable to be created. * @return the identifier of the newly created local variable. */ public int newLocal(final Type type) { Object t; switch (type.getSort()) { - case Type.BOOLEAN: - case Type.CHAR: - case Type.BYTE: - case Type.SHORT: - case Type.INT: - t = Opcodes.INTEGER; - break; - case Type.FLOAT: - t = Opcodes.FLOAT; - break; - case Type.LONG: - t = Opcodes.LONG; - break; - case Type.DOUBLE: - t = Opcodes.DOUBLE; - break; - case Type.ARRAY: - t = type.getDescriptor(); - break; - // case Type.OBJECT: - default: - t = type.getInternalName(); - break; + case Type.BOOLEAN: + case Type.CHAR: + case Type.BYTE: + case Type.SHORT: + case Type.INT: + t = Opcodes.INTEGER; + break; + case Type.FLOAT: + t = Opcodes.FLOAT; + break; + case Type.LONG: + t = Opcodes.LONG; + break; + case Type.DOUBLE: + t = Opcodes.DOUBLE; + break; + case Type.ARRAY: + t = type.getDescriptor(); + break; + // case Type.OBJECT: + default: + t = type.getInternalName(); + break; } - int local = nextLocal; - nextLocal += type.getSize(); + int local = newLocalMapping(type); setLocalType(local, type); setFrameLocal(local, t); return local; @@ -318,13 +327,37 @@ } /** - * Sets the current type of the given local variable. The default - * implementation of this method does nothing. + * Notifies subclasses that a new stack map frame is being visited. The + * array argument contains the stack map frame types corresponding to the + * local variables added with {@link #newLocal}. This method can update + * these types in place for the stack map frame being visited. The default + * implementation of this method does nothing, i.e. a local variable added + * with {@link #newLocal} will have the same type in all stack map frames. + * But this behavior is not always the desired one, for instance if a local + * variable is added in the middle of a try/catch block: the frame for the + * exception handler should have a TOP type for this new local. * - * @param local a local variable identifier, as returned by {@link #newLocal - * newLocal()}. - * @param type the type of the value being stored in the local variable + * @param newLocals + * the stack map frame types corresponding to the local variables + * added with {@link #newLocal} (and null for the others). The + * format of this array is the same as in + * {@link MethodVisitor#visitFrame}, except that long and double + * types use two slots. The types for the current stack map frame + * must be updated in place in this array. */ + protected void updateNewLocals(Object[] newLocals) { + } + + /** + * Notifies subclasses that a local variable has been added or remapped. The + * default implementation of this method does nothing. + * + * @param local + * a local variable identifier, as returned by {@link #newLocal + * newLocal()}. + * @param type + * the type of the value being stored in the local variable. + */ protected void setLocalType(final int local, final Type type) { }