001    /*
002     * Cobertura - http://cobertura.sourceforge.net/
003     *
004     * Copyright (C) 2006 Jiri Mares
005     *
006     * Cobertura is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published
008     * by the Free Software Foundation; either version 2 of the License,
009     * or (at your option) any later version.
010     *
011     * Cobertura is distributed in the hope that it will be useful, but
012     * WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014     * General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with Cobertura; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019     * USA
020     */
021    
022    package net.sourceforge.cobertura.instrument;
023    
024    import org.objectweb.asm.Label;
025    import org.objectweb.asm.MethodAdapter;
026    import org.objectweb.asm.MethodVisitor;
027    import org.objectweb.asm.Opcodes;
028    import org.objectweb.asm.Type;
029    
030    
031    /**
032     * Expects that the visitMaxs is calculated for me .... 
033     */
034    public class NewLocalVariableMethodAdapter extends MethodAdapter implements Opcodes
035    {
036            protected int firstStackVariable;
037            protected int addedStackWords;
038    
039            public NewLocalVariableMethodAdapter(MethodVisitor mv, int access, String desc, int addedStackWords)
040            {
041                    super(mv);
042                    Type[] args = Type.getArgumentTypes(desc);
043                    firstStackVariable = ((ACC_STATIC & access) != 0) ? 0 : 1;
044                    for (int i = 0; i < args.length; i++) {
045                            firstStackVariable += args[i].getSize();
046                    }
047                    this.addedStackWords = addedStackWords;
048            }
049            
050            public void visitVarInsn(int opcode, int var) 
051            {
052                    mv.visitVarInsn(opcode, (var >= firstStackVariable) ? var + addedStackWords : var);
053            }
054    
055            public void visitIincInsn(int var, int increment) {
056                    mv.visitIincInsn((var >= firstStackVariable) ? var + addedStackWords : var, increment);
057            }
058    
059            public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index)
060            {
061                    mv.visitLocalVariable(name, desc, signature, start, end, (index >= firstStackVariable) ? index + addedStackWords : index);
062            }
063    
064            public int getAddedStackWords()
065            {
066                    return addedStackWords;
067            }
068    
069            public int getFirstStackVariable()
070            {
071                    return firstStackVariable;
072            }
073    
074    }