Exemplo n.º 1
0
 public void decorate(Resource resource, DecoratorContext context) {
   if (Scopes.isFile(resource) && !Qualifiers.UNIT_TEST_FILE.equals(resource.getQualifier())) {
     int classes = 0;
     for (DecoratorContext child : context.getChildren()) {
       if (Scopes.isProgramUnit(child.getResource())) {
         classes++;
       }
     }
     context.saveMeasure(new Measure(CoreMetrics.CLASSES, (double) classes));
   }
 }
Exemplo n.º 2
0
  @Override
  public void addViolation(Violation violation, boolean force) {
    Resource resource = violation.getResource();
    if (resource == null) {
      violation.setResource(currentProject);
    } else if (!Scopes.isHigherThanOrEquals(resource, Scopes.FILE)) {
      throw new IllegalArgumentException(
          "Violations are only supported on files, directories and project");
    }

    Rule rule = violation.getRule();
    if (rule == null) {
      LOG.warn("Rule is null. Ignoring violation {}", violation);
      return;
    }

    Bucket bucket = getBucket(resource);
    if (bucket == null) {
      LOG.warn("Resource is not indexed. Ignoring violation {}", violation);
      return;
    }

    // keep a limitation (bug?) of deprecated violations api : severity is always
    // set by sonar. The severity set by plugins is overridden.
    // This is not the case with issue api.
    violation.setSeverity(null);

    violation.setResource(bucket.getResource());
    moduleIssues.initAndAddViolation(violation);
  }
Exemplo n.º 3
0
  /** {@inheritDoc} */
  @Override
  public List<Violation> getViolations(ViolationQuery violationQuery) {
    Resource resource = violationQuery.getResource();
    if (resource == null) {
      throw new IllegalArgumentException(
          "A resource must be set on the ViolationQuery in order to search for violations.");
    }

    if (!Scopes.isHigherThanOrEquals(resource, Scopes.FILE)) {
      return Collections.emptyList();
    }

    Bucket bucket = buckets.get(resource);
    if (bucket == null) {
      return Collections.emptyList();
    }

    List<Violation> violations = deprecatedViolations.get(bucket.getResource().getEffectiveKey());
    if (violationQuery.getSwitchMode() == ViolationQuery.SwitchMode.BOTH) {
      return violations;
    }

    List<Violation> filteredViolations = Lists.newArrayList();
    for (Violation violation : violations) {
      if (isFiltered(violation, violationQuery.getSwitchMode())) {
        filteredViolations.add(violation);
      }
    }
    return filteredViolations;
  }
Exemplo n.º 4
0
 private Bucket checkIndexed(Resource resource) {
   Bucket bucket = getBucket(resource, true);
   if (bucket == null) {
     if (lock.isLocked()) {
       if (lock.isFailWhenLocked()) {
         throw new ResourceNotIndexedException(resource);
       }
       LOG.warn("Resource will be ignored in next Sonar versions, index is locked: " + resource);
     }
     if (Scopes.isDirectory(resource) || Scopes.isFile(resource)) {
       bucket = doIndex(resource);
     } else if (!lock.isLocked()) {
       LOG.warn(
           "Resource will be ignored in next Sonar versions, it must be indexed before adding data: "
               + resource);
     }
   }
   return bucket;
 }