Пример #1
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 = checkIndexed(resource);
    if (bucket == null || bucket.isExcluded()) {
      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());
    scanIssues.initAndAddViolation(violation);
  }
Пример #2
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;
  }
Пример #3
0
 @Override
 public <R extends Resource> R getResource(R reference) {
   Bucket bucket = buckets.get(reference);
   if (bucket != null) {
     return (R) bucket.getResource();
   }
   return null;
 }
Пример #4
0
 public List<Resource> getChildren(Resource resource, boolean acceptExcluded) {
   List<Resource> children = Lists.newLinkedList();
   Bucket bucket = getBucket(resource, acceptExcluded);
   if (bucket != null) {
     for (Bucket childBucket : bucket.getChildren()) {
       if (acceptExcluded || !childBucket.isExcluded()) {
         children.add(childBucket.getResource());
       }
     }
   }
   return children;
 }
Пример #5
0
  private Bucket doIndex(Resource resource, Resource parentReference) {
    Bucket bucket = buckets.get(resource);
    if (bucket != null) {
      return bucket;
    }

    checkLock(resource);

    Resource parent = null;
    if (!ResourceUtils.isLibrary(resource)) {
      // a library has no parent
      parent = (Resource) ObjectUtils.defaultIfNull(parentReference, currentProject);
    }

    Bucket parentBucket = getBucket(parent, true);
    if (parentBucket == null && parent != null) {
      LOG.warn("Resource ignored, parent is not indexed: " + resource);
      return null;
    }

    resource.setEffectiveKey(createUID(currentProject, resource));
    bucket = new Bucket(resource).setParent(parentBucket);
    buckets.put(resource, bucket);

    boolean excluded = checkExclusion(resource, parentBucket);
    if (!excluded) {
      Snapshot snapshot =
          persistence.saveResource(
              currentProject, resource, (parentBucket != null ? parentBucket.getResource() : null));
      if (ResourceUtils.isPersistable(resource)
          && !Qualifiers.LIBRARY.equals(resource.getQualifier())) {
        graph.addComponent(resource, snapshot);
      }
    }

    return bucket;
  }
Пример #6
0
 /** Does nothing if the resource is already registered. */
 @Override
 public Resource addResource(Resource resource) {
   Bucket bucket = doIndex(resource);
   return bucket != null ? bucket.getResource() : null;
 }