package com.intellij.coverage;

import com.intellij.rt.coverage.instrumentation.SourceLineCounter;
import gnu.trove.TIntObjectHashMap;
import gnu.trove.TIntProcedure;
import org.objectweb.asm.ClassReader;

import java.util.List;

/**
 * User: anna
 * Date: 12/30/11
 */
public class SourceLineCounterUtil {
  public static boolean collectNonCoveredClassInfo(final PackageAnnotator.ClassCoverageInfo classCoverageInfo,
                                                   final PackageAnnotator.PackageCoverageInfo packageCoverageInfo, 
                                                   byte[] content, 
                                                   final boolean excludeLines) {
    if (content == null) return false;
    ClassReader reader = new ClassReader(content, 0, content.length);
    
    try {
      SourceLineCounter counter = new SourceLineCounter(null, excludeLines);
      reader.accept(counter, 0);
      classCoverageInfo.totalLineCount += counter.getNSourceLines();
      classCoverageInfo.totalMethodCount += counter.getNMethodsWithCode();
      packageCoverageInfo.totalLineCount += counter.getNSourceLines();
      if (!counter.isInterface()) {
        packageCoverageInfo.totalClassCount++;
      }
      return counter.getNMethodsWithCode() > 0;
    }
    catch (IncompatibleClassChangeError e) {
      //todo waiting for asm4 in idea lib 
      return true;
    }
  }

  public static void collectSrcLinesForUntouchedFiles(final List<Integer> uncoveredLines,
                                                      byte[] content, final boolean excludeLines) {
    final ClassReader reader = new ClassReader(content, 0, content.length);
    try {
      final SourceLineCounter collector = new SourceLineCounter(null, excludeLines);
      reader.accept(collector, 0);
      final TIntObjectHashMap lines = collector.getSourceLines();
      lines.forEachKey(new TIntProcedure() {
        public boolean execute(int line) {
          line--;
          uncoveredLines.add(line);
          return true;
        }
      });
    }
    catch (IncompatibleClassChangeError e) {
      //todo waiting for asm4 in idea lib
    }
  }
}

package com.intellij.coverage;

import com.intellij.rt.coverage.instrumentation.SourceLineCounter;
import gnu.trove.TIntObjectHashMap;
import gnu.trove.TIntProcedure;
import org.objectweb.asm.ClassReader;

import java.util.List;

/**
 * User: anna
 * Date: 12/30/11
 */
public class SourceLineCounterUtil {
  public static boolean collectNonCoveredClassInfo(final PackageAnnotator.ClassCoverageInfo classCoverageInfo,
                                                   final PackageAnnotator.PackageCoverageInfo packageCoverageInfo, 
                                                   byte[] content, 
                                                   final boolean excludeLines) {
    if (content == null) return false;
    ClassReader reader = new ClassReader(content, 0, content.length);
    
    try {
      SourceLineCounter counter = new SourceLineCounter(null, excludeLines);
      reader.accept(counter, 0);
      classCoverageInfo.totalLineCount += counter.getNSourceLines();
      classCoverageInfo.totalMethodCount += counter.getNMethodsWithCode();
      packageCoverageInfo.totalLineCount += counter.getNSourceLines();
      packageCoverageInfo.totalMethodCount += counter.getNMethodsWithCode();
      if (!counter.isInterface()) {
        packageCoverageInfo.totalClassCount++;
      }
      return counter.getNMethodsWithCode() > 0;
    }
    catch (IncompatibleClassChangeError e) {
      //todo waiting for asm4 in idea lib 
      return true;
    }
  }

  public static void collectSrcLinesForUntouchedFiles(final List<Integer> uncoveredLines,
                                                      byte[] content, final boolean excludeLines) {
    final ClassReader reader = new ClassReader(content, 0, content.length);
    try {
      final SourceLineCounter collector = new SourceLineCounter(null, excludeLines);
      reader.accept(collector, 0);
      final TIntObjectHashMap lines = collector.getSourceLines();
      lines.forEachKey(new TIntProcedure() {
        public boolean execute(int line) {
          line--;
          uncoveredLines.add(line);
          return true;
        }
      });
    }
    catch (IncompatibleClassChangeError e) {
      //todo waiting for asm4 in idea lib
    }
  }
}

include unloaded class methods in statistics (IDEA-80715)