001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.rules;
021
022import com.google.common.annotations.VisibleForTesting;
023import com.google.common.base.Function;
024import com.google.common.base.Functions;
025import com.google.common.collect.ImmutableMap;
026import com.google.common.collect.Lists;
027import org.apache.commons.lang.StringUtils;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.sonar.api.PropertyType;
031import org.sonar.api.ServerComponent;
032import org.sonar.api.utils.AnnotationUtils;
033import org.sonar.api.utils.FieldUtils2;
034import org.sonar.api.utils.SonarException;
035
036import java.lang.reflect.Field;
037import java.util.Collection;
038import java.util.List;
039
040/**
041 * @since 2.3
042 */
043public final class AnnotationRuleParser implements ServerComponent {
044
045  private static final Logger LOG = LoggerFactory.getLogger(AnnotationRuleParser.class);
046
047  public List<Rule> parse(String repositoryKey, Collection<Class> annotatedClasses) {
048    List<Rule> rules = Lists.newArrayList();
049    for (Class annotatedClass : annotatedClasses) {
050      rules.add(create(repositoryKey, annotatedClass));
051    }
052    return rules;
053  }
054
055  private Rule create(String repositoryKey, Class annotatedClass) {
056    org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class);
057    if (ruleAnnotation != null) {
058      return toRule(repositoryKey, annotatedClass, ruleAnnotation);
059    }
060    LOG.warn("The class " + annotatedClass.getCanonicalName() + " should be annotated with " + Rule.class);
061    return null;
062  }
063
064  private Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) {
065    String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
066    String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
067    String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null);
068    Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
069    rule.setDescription(description);
070    rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
071    rule.setCardinality(ruleAnnotation.cardinality());
072    rule.setStatus(ruleAnnotation.status());
073
074    List<Field> fields = FieldUtils2.getFields(clazz, true);
075    for (Field field : fields) {
076      addRuleProperty(rule, field);
077    }
078    return rule;
079  }
080
081  private void addRuleProperty(Rule rule, Field field) {
082    org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
083    if (propertyAnnotation != null) {
084      String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
085      RuleParam param = rule.createParameter(fieldKey);
086      param.setDescription(propertyAnnotation.description());
087      param.setDefaultValue(propertyAnnotation.defaultValue());
088      if (!StringUtils.isBlank(propertyAnnotation.type())) {
089        try {
090          param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name());
091        } catch (IllegalArgumentException e) {
092          throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e);
093        }
094      } else {
095        param.setType(guessType(field.getType()).name());
096      }
097    }
098  }
099
100  private static final Function<Class<?>, PropertyType> TYPE_FOR_CLASS = Functions.forMap(
101      ImmutableMap.<Class<?>, PropertyType> builder()
102          .put(Integer.class, PropertyType.INTEGER)
103          .put(int.class, PropertyType.INTEGER)
104          .put(Float.class, PropertyType.FLOAT)
105          .put(float.class, PropertyType.FLOAT)
106          .put(Boolean.class, PropertyType.BOOLEAN)
107          .put(boolean.class, PropertyType.BOOLEAN)
108          .build(),
109      PropertyType.STRING);
110
111  @VisibleForTesting
112  static PropertyType guessType(Class<?> type) {
113    return TYPE_FOR_CLASS.apply(type);
114  }
115}