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.pass1;
027
028import java.util.HashSet;
029import java.util.Set;
030import java.util.concurrent.atomic.AtomicInteger;
031
032import net.sourceforge.cobertura.instrument.AbstractFindTouchPointsClassInstrumenter;
033
034import org.objectweb.asm.ClassVisitor;
035import org.objectweb.asm.MethodVisitor;
036import org.objectweb.asm.Opcodes;
037
038public class DetectIgnoredCodeClassVisitor extends ClassVisitor {
039        /**
040         *  set of ignored line IDs
041         */ 
042        private Set<Integer> ignoredLineIds = new HashSet<Integer>();
043        
044        /**
045         * Set of concatenated methodName and methodSignature that should be ignored
046         */
047        private Set<String> ignoredMethodNamesAndSignatures = new HashSet<String>(); 
048                
049        /**
050         * Name (internal asm) of currently processed class  
051         **/
052        private String className;
053        
054        /**
055         * Name (internal asm) of parent of processed class  
056         **/
057        private String superName;
058        
059        /**
060         * Every LINENUMBER instruction will have generated it's lineId. 
061         * 
062         * The generated ids must be the same as those generated by ( {@link AbstractFindTouchPointsClassInstrumenter#lineIdGenerator} ) 
063         */
064        private final AtomicInteger lineIdGenerator = new AtomicInteger(0);
065        
066        private final boolean ignoreTrivial;
067        private final Set<String> ignoreAnnotations;
068                
069        public DetectIgnoredCodeClassVisitor(ClassVisitor cv, 
070                        boolean ignoreTrivial, Set<String> ignoreAnnotations) {
071                super(Opcodes.ASM4, cv);
072                this.ignoreTrivial = ignoreTrivial;
073                this.ignoreAnnotations = ignoreAnnotations;
074        }
075        
076        @Override
077        public void visit(int version, int access,
078                        String name, String signature, String superName, String[] interfaces)  {        
079                super.visit(version, access, name, signature, superName, interfaces);
080                this.className = name;
081                this.superName = superName;
082        }
083        
084        @Override
085        public MethodVisitor visitMethod(int access, String methodName, String description, 
086                    String signature, String[] exceptions) {
087                MethodVisitor nestedVisitor = super.visitMethod(access, methodName, description, 
088                    signature, exceptions);
089                return new DetectIgnoredCodeMethodVisitor(nestedVisitor, 
090                    ignoredLineIds, ignoredMethodNamesAndSignatures,
091                    ignoreTrivial, ignoreAnnotations,
092                    className, superName, methodName, description, lineIdGenerator);
093        }
094        
095        public Set<Integer> getIgnoredLineIds() {
096                return ignoredLineIds;
097        }
098        
099        public Set<String> getIgnoredMethodNamesAndSignatures() {
100                return ignoredMethodNamesAndSignatures;
101        }
102}