private RulesDefinition.Repository load(Class annotatedClass) { RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewExtendedRepository newRepository = context.createRepository("squid", "java"); annotationLoader.load(newRepository, annotatedClass); newRepository.done(); return context.repository("squid"); }
@Test public void should_recognize_type() { assertThat(RulesDefinitionAnnotationLoader.guessType(Integer.class)).isEqualTo(RuleParamType.INTEGER); assertThat(RulesDefinitionAnnotationLoader.guessType(int.class)).isEqualTo(RuleParamType.INTEGER); assertThat(RulesDefinitionAnnotationLoader.guessType(Float.class)).isEqualTo(RuleParamType.FLOAT); assertThat(RulesDefinitionAnnotationLoader.guessType(float.class)).isEqualTo(RuleParamType.FLOAT); assertThat(RulesDefinitionAnnotationLoader.guessType(Boolean.class)).isEqualTo(RuleParamType.BOOLEAN); assertThat(RulesDefinitionAnnotationLoader.guessType(boolean.class)).isEqualTo(RuleParamType.BOOLEAN); assertThat(RulesDefinitionAnnotationLoader.guessType(String.class)).isEqualTo(RuleParamType.STRING); assertThat(RulesDefinitionAnnotationLoader.guessType(Object.class)).isEqualTo(RuleParamType.STRING); }
public void load(RulesDefinition.NewExtendedRepository repo, Class... annotatedClasses) { for (Class annotatedClass : annotatedClasses) { loadRule(repo, annotatedClass); } }
private void defineRulesXoo(Context context) { NewRepository repo = context.createRepository(XOO_REPOSITORY, Xoo.KEY).setName("Xoo"); new RulesDefinitionAnnotationLoader().load(repo, Check.ALL);
private static RulesDefinition.NewRule loadRule(RulesDefinition.NewExtendedRepository repo, Class clazz, org.sonar.check.Rule ruleAnnotation) { String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName()); String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null); String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null); RulesDefinition.NewRule rule = repo.createRule(ruleKey); rule.setName(ruleName).setHtmlDescription(description); rule.setSeverity(ruleAnnotation.priority().name()); rule.setTemplate(ruleAnnotation.cardinality() == Cardinality.MULTIPLE); rule.setStatus(RuleStatus.valueOf(ruleAnnotation.status())); rule.setTags(ruleAnnotation.tags()); List<Field> fields = FieldUtils2.getFields(clazz, true); for (Field field : fields) { loadParameters(rule, field); } return rule; }
private static NewRule addAnnotatedRule(NewRepository repository, Class<?> ruleClass) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalStateException("No Rule annotation was found on " + ruleClass.getName()); } String ruleKey = ruleAnnotation.key(); if (ruleKey.length() == 0) { throw new IllegalStateException("Empty key"); } new RulesDefinitionAnnotationLoader().load(repository, ruleClass); NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("Rule not found: " + ruleKey); } return rule; }
private RulesDefinition.NewRule loadRule(RulesDefinition.NewExtendedRepository repo, Class clazz, org.sonar.check.Rule ruleAnnotation) { String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName()); String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null); String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null); RulesDefinition.NewRule rule = repo.createRule(ruleKey); rule.setName(ruleName).setHtmlDescription(description); rule.setSeverity(ruleAnnotation.priority().name()); rule.setTemplate(ruleAnnotation.cardinality() == Cardinality.MULTIPLE); rule.setStatus(RuleStatus.valueOf(ruleAnnotation.status())); rule.setTags(ruleAnnotation.tags()); List<Field> fields = FieldUtils2.getFields(clazz, true); for (Field field : fields) { loadParameters(rule, field); } return rule; }
@Override public void define(Context context) { NewRepository repo = context.createRepository(REPOSITORY_KEY, "java"); repo.setName(REPOSITORY_KEY); RulesDefinitionAnnotationLoader annotationLoader = new RulesDefinitionAnnotationLoader(); annotationLoader.load(repo, JavaDebuggingPluginCheckRegistrar.checkClasses()); annotationLoader.load(repo, JavaDebuggingPluginCheckRegistrar.testCheckClasses()); repo.done(); } }
@CheckForNull RulesDefinition.NewRule loadRule(RulesDefinition.NewExtendedRepository repo, Class clazz) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(clazz, org.sonar.check.Rule.class); if (ruleAnnotation != null) { return loadRule(repo, clazz, ruleAnnotation); } else { LOG.warn("The class " + clazz.getCanonicalName() + " should be annotated with " + org.sonar.check.Rule.class); return null; } }
private static void loadParameters(RulesDefinition.NewRule rule, Field field) { org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
private static RulesDefinition.NewRule loadRule(RulesDefinition.NewExtendedRepository repo, Class clazz, org.sonar.check.Rule ruleAnnotation) { String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName()); String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null); String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null); RulesDefinition.NewRule rule = repo.createRule(ruleKey); rule.setName(ruleName).setHtmlDescription(description); rule.setSeverity(ruleAnnotation.priority().name()); rule.setTemplate(ruleAnnotation.cardinality() == Cardinality.MULTIPLE); rule.setStatus(RuleStatus.valueOf(ruleAnnotation.status())); rule.setTags(ruleAnnotation.tags()); List<Field> fields = FieldUtils2.getFields(clazz, true); for (Field field : fields) { loadParameters(rule, field); } return rule; }
@Override public void define(Context context) { NewRepository repo = context.createRepository(REPOSITORY_KEY, "java"); repo.setName(REPOSITORY_KEY); // We could use a XML or JSON file to load all rule metadata, but // we prefer use annotations in order to have all information in a // single place RulesDefinitionAnnotationLoader annotationLoader = new RulesDefinitionAnnotationLoader(); // Load custom check classes annotationLoader.load(repo, JavaExtensionsCheckRegistrar.checkClasses()); // Load custom test check classes annotationLoader.load(repo, JavaExtensionsCheckRegistrar.testCheckClasses()); repo.done(); } }
@Test public void override_annotation_programmatically() { RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository newRepository = context.createRepository("squid", "java"); NewRule newRule = annotationLoader.loadRule(newRepository, RuleWithProperty.class); newRule.setName("Overridden name"); newRule.param("property").setDefaultValue("true"); newRule.param("property").setDescription("Overridden"); newRepository.done(); RulesDefinition.Repository repository = context.repository("squid"); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo("foo"); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.name()).isEqualTo("Overridden name"); assertThat(rule.htmlDescription()).isEqualTo("Foo Bar"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.params()).hasSize(1); RulesDefinition.Param prop = rule.param("property"); assertThat(prop.key()).isEqualTo("property"); assertThat(prop.description()).isEqualTo("Overridden"); assertThat(prop.defaultValue()).isEqualTo("true"); assertThat(prop.type()).isEqualTo(RuleParamType.STRING); }
private void loadParameters(RulesDefinition.NewRule rule, Field field) { org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
@Override public void define(Context context) { NewRepository repository = context .createRepository(REPOSITORY_KEY, "java") .setName("VIPAnalyzer "); List<Class> checks = SonarRulesList.getChecks(); new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class)); for (Class ruleClass : checks) { newRule(ruleClass, repository); } repository.done(); }
public void load(RulesDefinition.NewExtendedRepository repo, Class... annotatedClasses) { for (Class annotatedClass : annotatedClasses) { loadRule(repo, annotatedClass); } }
private static void loadParameters(RulesDefinition.NewRule rule, Field field) { org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
@Override public void define(Context context) { NewRepository repository = context .createRepository(REPOSITORY_KEY, Java.KEY) .setName("MyCompany Custom Repository"); List<Class> checks = RulesList.getChecks(); new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class)); for (Class ruleClass : checks) { newRule(ruleClass, repository); } repository.done(); }
public void load(RulesDefinition.NewExtendedRepository repo, Class... annotatedClasses) { for (Class annotatedClass : annotatedClasses) { loadRule(repo, annotatedClass); } }
@SuppressWarnings("rawtypes") public void addRuleClasses(boolean failIfNoExplicitKey, Iterable<Class> ruleClasses) { new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(ruleClasses, Class.class)); List<NewRule> newRules = Lists.newArrayList(); for (Class<?> ruleClass : ruleClasses) { NewRule rule = newRule(ruleClass, failIfNoExplicitKey); externalDescriptionLoader.addHtmlDescription(rule, ruleClass); rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); try { setupSqaleModel(rule, ruleClass); } catch (RuntimeException e) { throw new IllegalArgumentException("Could not setup SQALE model on " + ruleClass, e); } newRules.add(rule); } setupExternalNames(newRules); }