org.sonar.api.utils.AnnotationUtils Java Examples

The following examples show how to use org.sonar.api.utils.AnnotationUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebDriverRulesDefinition.java    From sonar-webdriver-plugin with MIT License 6 votes vote down vote up
@VisibleForTesting
protected void newRule(Class<?> ruleClass, NewRepository repository) {

  org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
  if (ruleAnnotation == null) {
    throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
  }
  String ruleKey = ruleAnnotation.key();
  if (StringUtils.isEmpty(ruleKey)) {
    throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
  }
  NewRule rule = repository.rule(ruleKey);
  if (rule == null) {
    throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
  }
  ruleMetadata(ruleClass, rule);
}
 
Example #2
Source File: SonarDefinition.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
protected void newRule(Class<?> ruleClass, NewRepository repository) {

    org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
    if (ruleAnnotation == null) {
        throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
    }
    String ruleKey = ruleAnnotation.key();
    if (StringUtils.isEmpty(ruleKey)) {
        throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
    }
    NewRule rule = repository.rule(ruleKey);
    if (rule == null) {
        throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
    }
    ruleMetadata(ruleClass, rule);

    rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null);
    if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) {
        throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead for " + ruleClass);
    }
}
 
Example #3
Source File: SonarDefinition.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
protected void newRule(Class<?> ruleClass, NewRepository repository) {

    org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
    if (ruleAnnotation == null) {
        throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
    }
    String ruleKey = ruleAnnotation.key();
    if (StringUtils.isEmpty(ruleKey)) {
        throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
    }
    NewRule rule = repository.rule(ruleKey);
    if (rule == null) {
        throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
    }
    ruleMetadata(ruleClass, rule);

    rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null);
    if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) {
        throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead for " + ruleClass);
    }
}
 
Example #4
Source File: ApexRulesDefinition.java    From enforce-sonarqube-plugin with MIT License 6 votes vote down vote up
/**
 * Loads HTML descriptions and meta data from JSON file to checks.
 *
 * @param ruleClass Class type of given rule.
 * @param repository current repository.
 */
@VisibleForTesting
protected void newRule(Class<?> ruleClass, NewRepository repository) {
    org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
    if (ruleAnnotation == null) {
        throw new IllegalArgumentException(
                String.format("No Rule annotation was found on %s", ruleClass));
    }
    String ruleKey = ruleAnnotation.key();
    if (StringUtils.isEmpty(ruleKey)) {
        throw new IllegalArgumentException(
                String.format("No key is defined in Rule annotation of %s", ruleClass));
    }
    NewRule rule = repository.rule(ruleKey);
    if (rule == null) {
        throw new IllegalStateException(
                String.format("No rule was created for %s in %s", ruleClass, repository.key()));
    }
    addHtmlDescription(rule, rule.key());
    addMetadata(rule, rule.key());
}
 
Example #5
Source File: WebDriverRulesDefinition.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private String ruleMetadata(Class<?> ruleClass, NewRule rule) {
  String metadataKey = rule.key();
  org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.java.RspecKey.class);
  if (rspecKeyAnnotation != null) {
    metadataKey = rspecKeyAnnotation.value();
    rule.setInternalKey(metadataKey);
  }
  addHtmlDescription(rule, metadataKey);
  addMetadata(rule, metadataKey);
  return metadataKey;
}
 
Example #6
Source File: SonarDefinition.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private String ruleMetadata(Class<?> ruleClass, NewRule rule) {
    String metadataKey = rule.key();
    org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.java.RspecKey.class);
    if (rspecKeyAnnotation != null) {
        metadataKey = rspecKeyAnnotation.value();
        rule.setInternalKey(metadataKey);
    }
    addHtmlDescription(rule, metadataKey);
    addMetadata(rule, metadataKey);
    return metadataKey;
}
 
Example #7
Source File: SonarDefinition.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private String ruleMetadata(Class<?> ruleClass, NewRule rule) {
    String metadataKey = rule.key();
    org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.java.RspecKey.class);
    if (rspecKeyAnnotation != null) {
        metadataKey = rspecKeyAnnotation.value();
        rule.setInternalKey(metadataKey);
    }
    addHtmlDescription(rule, metadataKey);
    addMetadata(rule, metadataKey);
    return metadataKey;
}
 
Example #8
Source File: RulesLoader.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@CheckForNull
private RulesDefinition.NewRule loadRule(RulesDefinition.NewExtendedRepository repo, Class clazz) {
    return Optional.ofNullable(AnnotationUtils.getAnnotation(clazz, Rule.class))
        .map(annotationRule -> createRule(repo, clazz, annotationRule))
        .orElse(null);
}
 
Example #9
Source File: RulesLoader.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private void setMetadata(RulesDefinition.NewRule rule, Class clazz) {
    Optional.ofNullable(AnnotationUtils.getAnnotation(clazz, Metadata.class))
        .ifPresent(metadataAnnotation -> setTechnicalDebt(rule, metadataAnnotation));
}