diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-01-13 20:03:49 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-01-13 20:03:49 +0100 |
commit | 52ad7a6332ba164a12f19e33358e8e2f6f15d925 (patch) | |
tree | 43c476205bbaf0aacd55af27ebcc212f2b5d3dc3 | |
parent | 17af80b7687436a3ac3ff0930bfcbea52d8cf663 (diff) | |
download | sonarqube-52ad7a6332ba164a12f19e33358e8e2f6f15d925.tar.gz sonarqube-52ad7a6332ba164a12f19e33358e8e2f6f15d925.zip |
SONAR-3164 AnnotationCheckFactory does not detect properties declared in inherited classes
4 files changed, 90 insertions, 14 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java index 2340bfe8f93..9088e89454c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java @@ -19,20 +19,21 @@ */ package org.sonar.api.checks; +import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.ActiveRuleParam; +import org.sonar.api.utils.FieldUtils; 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; -import com.google.common.collect.Maps; - import java.lang.reflect.Field; import java.util.Collection; +import java.util.List; import java.util.Map; /** @@ -143,7 +144,7 @@ public final class AnnotationCheckFactory extends CheckFactory { } private Field getField(Object check, String key) { - Field[] fields = check.getClass().getDeclaredFields(); + List<Field> fields = FieldUtils.getFields(check.getClass(), true); for (Field field : fields) { RuleProperty propertyAnnotation = field.getAnnotation(RuleProperty.class); if (propertyAnnotation != null) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/AbstractCheck.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/AbstractCheck.java new file mode 100644 index 00000000000..3f354a0c023 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/AbstractCheck.java @@ -0,0 +1,32 @@ +/* + * 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.checks; + +import org.sonar.check.RuleProperty; + +abstract class AbstractCheck { + + @RuleProperty + private Integer max; + + public Integer getMax() { + return max; + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/AnnotationCheckFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/AnnotationCheckFactoryTest.java index 6be436d32f7..5c2832f3f14 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/checks/AnnotationCheckFactoryTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/AnnotationCheckFactoryTest.java @@ -28,9 +28,7 @@ import org.sonar.api.utils.SonarException; import java.util.Arrays; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.*; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; @@ -41,7 +39,7 @@ public class AnnotationCheckFactoryTest { public void createCheckWithoutProperties() { RulesProfile profile = RulesProfile.create("repo", "java"); ActiveRule activeRule = profile.activateRule(Rule.create("repo", "org.sonar.api.checks.CheckWithoutProperties", ""), null); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class> asList(CheckWithoutProperties.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithoutProperties.class)); Object check = factory.getCheck(activeRule); assertNotNull(check); @@ -56,7 +54,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("pattern", "foo"); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class> asList(CheckWithStringProperty.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithStringProperty.class)); Object check = factory.getCheck(activeRule); assertNotNull(check); @@ -72,7 +70,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("unknown", "bar"); - AnnotationCheckFactory.create(profile, "repo", Arrays.<Class> asList(CheckWithStringProperty.class)); + AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithStringProperty.class)); } @Test @@ -85,7 +83,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("max", "300"); activeRule.setParameter("ignore", "true"); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class> asList(CheckWithPrimitiveProperties.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithPrimitiveProperties.class)); Object check = factory.getCheck(activeRule); assertThat(((CheckWithPrimitiveProperties) check).getMax(), is(300)); @@ -100,12 +98,29 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("max", "300"); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class> asList(CheckWithIntegerProperty.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithIntegerProperty.class)); Object check = factory.getCheck(activeRule); assertThat(((CheckWithIntegerProperty) check).getMax(), is(300)); } + /** + * SONAR-3164 + */ + @Test + public void setValueOfInheritedField() { + RulesProfile profile = RulesProfile.create("repo", "java"); + Rule rule = Rule.create("repo", "org.sonar.api.checks.ImplementedCheck", ""); + rule.createParameter("max"); + + ActiveRule activeRule = profile.activateRule(rule, null); + activeRule.setParameter("max", "300"); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(ImplementedCheck.class)); + + Object check = factory.getCheck(activeRule); + assertThat(((ImplementedCheck) check).getMax(), is(300)); + } + @Test(expected = SonarException.class) public void failIfPropertyTypeIsNotSupported() { RulesProfile profile = RulesProfile.create("repo", "java"); @@ -114,7 +129,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("max", "300"); - AnnotationCheckFactory.create(profile, "repo", Arrays.<Class> asList(CheckWithUnsupportedPropertyType.class)); + AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithUnsupportedPropertyType.class)); } @Test @@ -125,7 +140,7 @@ public class AnnotationCheckFactoryTest { ActiveRule activeRule = profile.activateRule(rule, null); activeRule.setParameter("maximum", "300"); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class> asList(CheckWithOverriddenPropertyKey.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithOverriddenPropertyKey.class)); Object check = factory.getCheck(activeRule); assertThat(((CheckWithOverriddenPropertyKey) check).getMax(), is(300)); @@ -139,7 +154,7 @@ public class AnnotationCheckFactoryTest { profile.activateRule(rule, null); profile.activateRule(clonedRule, null); - AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class> asList(CheckWithKey.class)); + AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.<Class>asList(CheckWithKey.class)); assertThat(factory.getChecks(), (Matcher) not(hasItems(nullValue()))); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/ImplementedCheck.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/ImplementedCheck.java new file mode 100644 index 00000000000..66974111777 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/ImplementedCheck.java @@ -0,0 +1,28 @@ +/* + * 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.checks; + +import org.sonar.check.Priority; +import org.sonar.check.Rule; + +@Rule(priority = Priority.CRITICAL) +class ImplementedCheck extends AbstractCheck { + +} |