+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.check;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * @since 2.1 (experimental)
- * @deprecated since 2.3. Use @Rule
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.TYPE)
-@Deprecated
-public @interface Check {
-
- /**
- * The default key is the class name.
- */
- String key() default "";
-
- /**
- * The path to resource bundles (optional). If not set, then it equals the class name.
- */
- String bundle() default "";
-
- /**
- * The check title. If not defined, then the title is the key
- */
- String title() default "";
-
- /**
- * The check description, optional.
- */
- String description() default "";
-
- /**
- * Default priority.
- */
- Priority priority() default Priority.MAJOR;
-
- /**
- * Will probably be deprecated and replaced by tags
- *
- * @deprecated since 2.5. See http://jira.codehaus.org/browse/SONAR-2007
- */
- @Deprecated
- IsoCategory isoCategory();
-}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.check;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * @since 2.1 (experimental)
- * @deprecated since 2.3. Use @RuleProperty
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.FIELD)
-@Deprecated
-public @interface CheckProperty {
-
- /**
- * The default key is the field name, read by reflection. Overriding this key can be useful when
- * obfuscating the code.
- */
- String key() default "";
-
- String title() default "";
-
- /**
- * Optional description
- */
- String description() default "";
-}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.check;
-
-
-@Check(isoCategory = IsoCategory.Portability, priority = Priority.CRITICAL)
-class SimplestCheck {
-
-}
-
-@Check(key = "overridenKey", isoCategory = IsoCategory.Portability, priority = Priority.CRITICAL)
-class CheckWithOverridenKey {
-
-}
-
-@Check(isoCategory = IsoCategory.Portability, priority = Priority.CRITICAL)
-class CheckWithProperties {
-
- @CheckProperty
- private boolean active = false;
-
- @CheckProperty(key = "Maximum")
- private int max = 50;
-
- private String nonProperty;
-}
\ No newline at end of file
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.check;
-
-import org.junit.Test;
-
-import java.lang.reflect.Field;
-
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
-
-public class CheckTest {
- @Test
- public void getCheckMetadata() {
- Check check = FakeCheck.class.getAnnotation(Check.class);
- assertThat(check.title(), is("Detailed Check"));
- assertThat(check.description(), is("Detailed description"));
- }
-
- @Test
- public void getCheckProperty() throws NoSuchFieldException {
- Field field = FakeCheck.class.getDeclaredField("max");
- CheckProperty propertyAnnotation = field.getAnnotation(CheckProperty.class);
- assertThat(propertyAnnotation.description(), is("Maximum value"));
- }
-}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.check;
-
-@Check(title ="Detailed Check", description = "Detailed description", isoCategory = IsoCategory.Reliability)
-public class FakeCheck {
-
- @CheckProperty(description = "Maximum value")
- private String max;
-
- @CheckProperty(description ="Minimum value")
- protected String min;
-
- private int nonConfigurableProperty;
-
- public String getMax() {
- return max;
- }
-
- public void setMax(String max) {
- this.max = max;
- }
-}
import org.sonar.api.utils.AnnotationUtils;
import org.sonar.api.utils.FieldUtils2;
import org.sonar.api.utils.SonarException;
-import org.sonar.check.Check;
-import org.sonar.check.CheckProperty;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
if (StringUtils.equals(key, field.getName()) || StringUtils.equals(key, propertyAnnotation.key())) {
return field;
}
- } else {
- CheckProperty checkAnnotation = field.getAnnotation(CheckProperty.class);
- if (checkAnnotation != null) {
- if (StringUtils.equals(key, field.getName()) || StringUtils.equals(key, checkAnnotation.key())) {
- return field;
- }
- }
}
}
return null;
Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClassOrObject, Rule.class);
if (ruleAnnotation != null) {
key = ruleAnnotation.key();
- } else {
- Check checkAnnotation = AnnotationUtils.getAnnotation(annotatedClassOrObject, Check.class);
- if (checkAnnotation != null) {
- key = checkAnnotation.key();
-
- }
}
Class clazz = annotatedClassOrObject.getClass();
if (annotatedClassOrObject instanceof Class) {
import org.sonar.api.utils.AnnotationUtils;
import org.sonar.api.utils.FieldUtils2;
import org.sonar.api.utils.SonarException;
-import org.sonar.check.Check;
import java.lang.reflect.Field;
import java.util.Collection;
if (ruleAnnotation != null) {
return toRule(repositoryKey, annotatedClass, ruleAnnotation);
}
- Check checkAnnotation = AnnotationUtils.getAnnotation(annotatedClass, Check.class);
- if (checkAnnotation != null) {
- return toRule(repositoryKey, annotatedClass, checkAnnotation);
- }
LOG.warn("The class " + annotatedClass.getCanonicalName() + " should be annotated with " + Rule.class);
return null;
}
return rule;
}
- private Rule toRule(String repositoryKey, Class clazz, Check checkAnnotation) {
- String ruleKey = StringUtils.defaultIfEmpty(checkAnnotation.key(), clazz.getCanonicalName());
- String ruleName = StringUtils.defaultIfEmpty(checkAnnotation.title(), ruleKey);
- Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
- rule.setDescription(checkAnnotation.description());
- rule.setSeverity(RulePriority.fromCheckPriority(checkAnnotation.priority()));
-
- List<Field> fields = FieldUtils2.getFields(clazz, true);
- for (Field field : fields) {
- addCheckProperty(rule, field);
- }
- return rule;
- }
-
private void addRuleProperty(Rule rule, Field field) {
org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
if (propertyAnnotation != null) {
}
}
- private void addCheckProperty(Rule rule, Field field) {
- org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class);
- if (propertyAnnotation != null) {
- String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
- RuleParam param = rule.createParameter(fieldKey);
- param.setDescription(propertyAnnotation.description());
- }
- }
-
@VisibleForTesting
static PropertyType guessType(Class<?> type) {
if ((type == Integer.class) || (type == int.class)) {
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.AnnotationUtils;
-import org.sonar.check.Check;
/**
* @since 2.3
org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class);
if (ruleAnnotation != null) {
key = ruleAnnotation.key();
- } else {
- Check checkAnnotation = AnnotationUtils.getAnnotation(annotatedClass, Check.class);
- if (checkAnnotation != null) {
- key = checkAnnotation.key();
- }
}
return StringUtils.defaultIfEmpty(key, annotatedClass.getCanonicalName());
}
import org.junit.rules.ExpectedException;
import org.sonar.api.PropertyType;
import org.sonar.api.utils.SonarException;
-import org.sonar.check.IsoCategory;
import org.sonar.check.Priority;
import java.util.Collections;
assertThat(rule.getParams()).hasSize(2);
}
- @Test
- public void supportDeprecatedAnnotations() {
- List<Rule> rules = parseAnnotatedClass(Check.class);
- assertThat(rules).hasSize(1);
- Rule rule = rules.get(0);
- assertThat(rule.getKey()).isEqualTo(Check.class.getCanonicalName());
- assertThat(rule.getName()).isEqualTo(Check.class.getCanonicalName());
- assertThat(rule.getDescription()).isEqualTo("Deprecated check");
- assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
- }
-
private List<Rule> parseAnnotatedClass(Class annotatedClass) {
return new AnnotationRuleParser().parse("repo", Collections.singleton(annotatedClass));
}
@org.sonar.check.RuleProperty(description = "text", defaultValue = "Long text", type = "INVALID")
public String property;
}
-
- @org.sonar.check.Check(description = "Deprecated check", priority = Priority.BLOCKER, isoCategory = IsoCategory.Maintainability)
- static class Check {
- }
}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.api.rules;
-
-import org.sonar.check.Check;
-import org.sonar.check.IsoCategory;
-
-@Check(title ="Annotated Check", description = "Description", isoCategory = IsoCategory.Reliability)
-public class DeprecatedAnnotatedCheck {
-}
assertThat(key, is(AnnotatedCheck.class.getName()));
}
- @Test
- public void shouldGetKeyFromDeprecatedCheckAnnotation() {
- String key = RuleAnnotationUtils.getRuleKey(DeprecatedAnnotatedCheck.class);
- assertThat(key, is(DeprecatedAnnotatedCheck.class.getName()));
- }
-
@Test
public void shouldGetKey() {
String key = RuleAnnotationUtils.getRuleKey(AnnotatedCheckWithParameters.class);