All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.coverage.instrumentation.CoverageTypePredicate Maven / Gradle / Ivy

package com.undefinedlabs.scope.coverage.instrumentation;

import com.undefinedlabs.scope.annotations.NotInstrument;
import com.undefinedlabs.scope.jdk.Predicate;
import com.undefinedlabs.scope.utils.sourcecode.SourceCodeUtils;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.type.TypeDescription;

public class CoverageTypePredicate implements Predicate {

  protected static final String DEPS_PACKAGE = "com.undefinedlabs.scope.deps";

  private final SourceCodeUtils sourceCodeUtils;
  private final CoveragePackageContainer coveragePackageContainer;

  public CoverageTypePredicate(
      final SourceCodeUtils sourceCodeUtils,
      final CoveragePackageContainer coveragePackageContainer) {
    this.sourceCodeUtils = sourceCodeUtils;
    this.coveragePackageContainer = coveragePackageContainer;
  }

  @Override
  public boolean filter(final TypeDescription target) {
    if ("com.undefinedlabs.scope.annotations.NotInstrument".equalsIgnoreCase(target.getName())) {
      return false;
    }

    final String packageName = target.getPackage().getName();
    if (packageName.startsWith(DEPS_PACKAGE)) {
      return false;
    }

    final AnnotationList packageAnnotations = target.getPackage().getDeclaredAnnotations();
    final boolean hasNotInstrumentAnnotation =
        packageAnnotations.isAnnotationPresent(NotInstrument.class);
    if (hasNotInstrumentAnnotation) {
      return false;
    }

    if (coveragePackageContainer.isEmpty()) {
      return sourceCodeUtils.getSourceCode(target.getName()).hasSourceCodeFilePath();
    }

    return coveragePackageContainer.contains(packageName);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy