001/*
002 * Cobertura - http://cobertura.sourceforge.net/
003 *
004 * Copyright (C) 2011 Piotr Tabor
005 *
006 * Note: This file is dual licensed under the GPL and the Apache
007 * Source License (so that it can be used from both the main
008 * Cobertura classes and the ant tasks).
009 *
010 * Cobertura is free software; you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License as published
012 * by the Free Software Foundation; either version 2 of the License,
013 * or (at your option) any later version.
014 *
015 * Cobertura is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 * General Public License for more details.
019 *
020 * You should have received a copy of the GNU General Public License
021 * along with Cobertura; if not, write to the Free Software
022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023 * USA
024 */
025
026package net.sourceforge.cobertura.instrument;
027
028import java.util.concurrent.atomic.AtomicInteger;
029
030import org.objectweb.asm.Label;
031import org.objectweb.asm.MethodVisitor;
032import org.objectweb.asm.Opcodes;
033
034/**
035 * Abstract implementation of {@link MethodAdapter} that:
036 * <ul>
037 *    <li>provides information about {@link #className},{@link #methodName} and {@link #methodSignature} of method currently being instrumented/analyzed</li>
038 *    <li>Assign line identifiers (see {@link AbstractFindTouchPointsClassInstrumenter#lineIdGenerator} to every LINENUMBER asm instruction found</li> 
039 * </ul>
040 * 
041 * @author ptab
042 *
043 */
044public abstract class ContextMethodAwareMethodAdapter extends MethodVisitor{
045    protected final String className;
046        protected final String methodName;
047        protected final String methodSignature;
048        
049        /**
050         * What was the last lineId assigned. We can read this field to know which line (by identifier) we are currently analyzing 
051         */
052        protected int lastLineId;
053        
054        /**
055         * Generator that assigns unique (in scope of single class) identifiers to every LINENUMBER asm derective. 
056         * 
057         * <p>We will use this 'generator' to provide this identifiers. Remember to acquire identifiers using {@link AtomicInteger#incrementAndGet()} (not {@link AtomicInteger#getAndIncrement()}!!!)</p> 
058         */
059        protected final AtomicInteger lineIdGenerator; 
060        
061        public ContextMethodAwareMethodAdapter(MethodVisitor mv, String className, String methodName, String methodSignature,AtomicInteger lineIdGenerator) {
062                super(Opcodes.ASM4, mv);
063                this.className=className;
064                this.methodName=methodName;
065                this.methodSignature=methodSignature;
066                lastLineId=0;
067                this.lineIdGenerator=lineIdGenerator;
068        }
069        
070        @Override
071        public void visitLineNumber(int number, Label label) {  
072                lastLineId=lineIdGenerator.incrementAndGet();
073                super.visitLineNumber(number, label);           
074        }
075
076}