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    rule.setTags(ruleAnnotation.tags());
074
075    List<Field> fields = FieldUtils2.getFields(clazz, true);
076    for (Field field : fields) {
077      addRuleProperty(rule, field);
078    }
079    return rule;
080  }
081
082  private void addRuleProperty(Rule rule, Field field) {
083    org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
084    if (propertyAnnotation != null) {
085      String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
086      RuleParam param = rule.createParameter(fieldKey);
087      param.setDescription(propertyAnnotation.description());
088      param.setDefaultValue(propertyAnnotation.defaultValue());
089      if (!StringUtils.isBlank(propertyAnnotation.type())) {
090        try {
091          param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name());
092        } catch (IllegalArgumentException e) {
093          throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e);
094        }
095      } else {
096        param.setType(guessType(field.getType()).name());
097      }
098    }
099  }
100
101  private static final Function<Class<?>, PropertyType> TYPE_FOR_CLASS = Functions.forMap(
102      ImmutableMap.<Class<?>, PropertyType> builder()
103          .put(Integer.class, PropertyType.INTEGER)
104          .put(int.class, PropertyType.INTEGER)
105          .put(Float.class, PropertyType.FLOAT)
106          .put(float.class, PropertyType.FLOAT)
107          .put(Boolean.class, PropertyType.BOOLEAN)
108          .put(boolean.class, PropertyType.BOOLEAN)
109          .build(),
110      PropertyType.STRING);
111
112  @VisibleForTesting
113  static PropertyType guessType(Class<?> type) {
114    return TYPE_FOR_CLASS.apply(type);
115  }
116}