import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.ActiveRuleParam;
+import org.sonar.api.utils.AnnotationUtils;
import org.sonar.api.utils.FieldUtils2;
import org.sonar.api.utils.SonarException;
import org.sonar.check.Check;
*/
public final class AnnotationCheckFactory extends CheckFactory {
- private Map<String, Class> checkClassesByKey = Maps.newHashMap();
+ private Map<String, Object> checksByKey = Maps.newHashMap();
- private AnnotationCheckFactory(RulesProfile profile, String repositoryKey, Collection<Class> checkClasses) {
+ private AnnotationCheckFactory(RulesProfile profile, String repositoryKey, Collection checks) {
super(profile, repositoryKey);
- groupClassesByKey(checkClasses);
+ groupByKey(checks);
}
- public static AnnotationCheckFactory create(RulesProfile profile, String repositoryKey, Collection<Class> checkClasses) {
+ public static AnnotationCheckFactory create(RulesProfile profile, String repositoryKey, Collection checkClasses) {
AnnotationCheckFactory factory = new AnnotationCheckFactory(profile, repositoryKey, checkClasses);
factory.init();
return factory;
}
- private void groupClassesByKey(Collection<Class> checkClasses) {
- for (Class checkClass : checkClasses) {
- String key = getRuleKey(checkClass);
+ private void groupByKey(Collection checks) {
+ for (Object check : checks) {
+ String key = getRuleKey(check);
if (key != null) {
- checkClassesByKey.put(key, checkClass);
+ checksByKey.put(key, check);
}
}
}
protected Object createCheck(ActiveRule activeRule) {
- Class clazz = checkClassesByKey.get(activeRule.getConfigKey());
- if (clazz != null) {
- return instantiate(activeRule, clazz);
+ Object object = checksByKey.get(activeRule.getConfigKey());
+ if (object != null) {
+ return instantiate(activeRule, object);
}
return null;
}
- private Object instantiate(ActiveRule activeRule, Class clazz) {
+ private Object instantiate(ActiveRule activeRule, Object checkClassOrInstance) {
try {
- Object check = clazz.newInstance();
+ Object check = checkClassOrInstance;
+ if (check instanceof Class) {
+ check = ((Class) checkClassOrInstance).newInstance();
+ }
configureFields(activeRule, check);
return check;
return null;
}
- private String getRuleKey(Class annotatedClass) {
+ private String getRuleKey(Object annotatedClassOrObject) {
String key = null;
- Rule ruleAnnotation = (Rule) annotatedClass.getAnnotation(Rule.class);
+ Rule ruleAnnotation = AnnotationUtils.getClassAnnotation(annotatedClassOrObject, Rule.class);
if (ruleAnnotation != null) {
key = ruleAnnotation.key();
} else {
- Check checkAnnotation = (Check) annotatedClass.getAnnotation(Check.class);
+ Check checkAnnotation = AnnotationUtils.getClassAnnotation(annotatedClassOrObject, Check.class);
if (checkAnnotation != null) {
key = checkAnnotation.key();
}
}
- return StringUtils.defaultIfEmpty(key, annotatedClass.getCanonicalName());
+ Class clazz = annotatedClassOrObject.getClass();
+ if (annotatedClassOrObject instanceof Class) {
+ clazz = (Class) annotatedClassOrObject;
+ }
+ return StringUtils.defaultIfEmpty(key, clazz.getCanonicalName());
}
}
import org.hamcrest.Matcher;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.Rule;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.*;
public class AnnotationCheckFactoryTest {
+ @org.junit.Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Test
public void createCheckWithoutProperties() {
RulesProfile profile = RulesProfile.create("repo", "java");
assertThat(((CheckWithStringProperty) check).getPattern(), is("foo"));
}
- @Test(expected = SonarException.class)
+ @Test
public void failIfMissingProperty() {
+ thrown.expect(SonarException.class);
+
RulesProfile profile = RulesProfile.create("repo", "java");
Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithStringProperty", "");
rule.createParameter("unknown");
assertThat(((ImplementedCheck) check).getMax(), is(300));
}
- @Test(expected = SonarException.class)
+ @Test
public void failIfPropertyTypeIsNotSupported() {
+ thrown.expect(SonarException.class);
+
RulesProfile profile = RulesProfile.create("repo", "java");
Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithUnsupportedPropertyType", "");
rule.createParameter("max");
assertThat(factory.getChecks(), (Matcher) not(hasItems(nullValue())));
}
+
+ /**
+ * SONAR-2900
+ */
+ @Test
+ public void create_accept_objects() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ ActiveRule activeRule = profile.activateRule(Rule.create("repo", "org.sonar.api.checks.CheckWithoutProperties", ""), null);
+ CheckWithoutProperties checkInstance = new CheckWithoutProperties();
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.asList(checkInstance));
+
+ Object check = factory.getCheck(activeRule);
+ assertNotNull(check);
+ assertSame(check, checkInstance);
+ }
+
+ @Test
+ public void create_instance_with_string_property() {
+ RulesProfile profile = RulesProfile.create("repo", "java");
+ Rule rule = Rule.create("repo", "org.sonar.api.checks.CheckWithStringProperty", "");
+ rule.createParameter("pattern");
+
+ ActiveRule activeRule = profile.activateRule(rule, null);
+ activeRule.setParameter("pattern", "foo");
+ CheckWithStringProperty checkInstance = new CheckWithStringProperty();
+ AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, "repo", Arrays.asList(checkInstance));
+
+ Object check = factory.getCheck(activeRule);
+ assertNotNull(check);
+ assertSame(check, checkInstance);
+ assertThat(checkInstance.getPattern(), is("foo"));
+ }
}