001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
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
073    List<Field> fields = FieldUtils2.getFields(clazz, true);
074    for (Field field : fields) {
075      addRuleProperty(rule, field);
076    }
077    return rule;
078  }
079
080  private void addRuleProperty(Rule rule, Field field) {
081    org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
082    if (propertyAnnotation != null) {
083      String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
084      RuleParam param = rule.createParameter(fieldKey);
085      param.setDescription(propertyAnnotation.description());
086      param.setDefaultValue(propertyAnnotation.defaultValue());
087      if (!StringUtils.isBlank(propertyAnnotation.type())) {
088        try {
089          param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name());
090        } catch (IllegalArgumentException e) {
091          throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e);
092        }
093      } else {
094        param.setType(guessType(field.getType()).name());
095      }
096    }
097  }
098
099  private static final Function<Class<?>, PropertyType> TYPE_FOR_CLASS = Functions.forMap(
100      ImmutableMap.<Class<?>, PropertyType> builder()
101          .put(Integer.class, PropertyType.INTEGER)
102          .put(int.class, PropertyType.INTEGER)
103          .put(Float.class, PropertyType.FLOAT)
104          .put(float.class, PropertyType.FLOAT)
105          .put(Boolean.class, PropertyType.BOOLEAN)
106          .put(boolean.class, PropertyType.BOOLEAN)
107          .build(),
108      PropertyType.STRING);
109
110  @VisibleForTesting
111  static PropertyType guessType(Class<?> type) {
112    return TYPE_FOR_CLASS.apply(type);
113  }
114}