001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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.base.Function;
023import com.google.common.base.Functions;
024import com.google.common.collect.ImmutableMap;
025import java.lang.reflect.Field;
026import java.util.ArrayList;
027import java.util.Collection;
028import java.util.List;
029import org.apache.commons.lang.StringUtils;
030import org.sonar.api.PropertyType;
031import org.sonar.api.ce.ComputeEngineSide;
032import org.sonar.api.server.ServerSide;
033import org.sonar.api.utils.AnnotationUtils;
034import org.sonar.api.utils.FieldUtils2;
035import org.sonar.api.utils.SonarException;
036import org.sonar.api.utils.log.Logger;
037import org.sonar.api.utils.log.Loggers;
038
039/**
040 * @since 2.3
041 * @deprecated in 4.2. Replaced by {@link org.sonar.api.server.rule.RulesDefinitionAnnotationLoader}
042 */
043@Deprecated
044@ServerSide
045@ComputeEngineSide
046public final class AnnotationRuleParser {
047
048  private static final Logger LOG = Loggers.get(AnnotationRuleParser.class);
049
050  public List<Rule> parse(String repositoryKey, Collection<Class> annotatedClasses) {
051    List<Rule> rules = new ArrayList<>();
052    for (Class annotatedClass : annotatedClasses) {
053      rules.add(create(repositoryKey, annotatedClass));
054    }
055    return rules;
056  }
057
058  private static Rule create(String repositoryKey, Class annotatedClass) {
059    org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class);
060    if (ruleAnnotation != null) {
061      return toRule(repositoryKey, annotatedClass, ruleAnnotation);
062    }
063    LOG.warn("The class " + annotatedClass.getCanonicalName() + " should be annotated with " + Rule.class);
064    return null;
065  }
066
067  private static Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) {
068    String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
069    String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
070    String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null);
071    Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
072    rule.setDescription(description);
073    rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
074    rule.setCardinality(ruleAnnotation.cardinality());
075    rule.setStatus(ruleAnnotation.status());
076    rule.setTags(ruleAnnotation.tags());
077
078    List<Field> fields = FieldUtils2.getFields(clazz, true);
079    for (Field field : fields) {
080      addRuleProperty(rule, field);
081    }
082    return rule;
083  }
084
085  private static void addRuleProperty(Rule rule, Field field) {
086    org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
087    if (propertyAnnotation != null) {
088      String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
089      RuleParam param = rule.createParameter(fieldKey);
090      param.setDescription(propertyAnnotation.description());
091      param.setDefaultValue(propertyAnnotation.defaultValue());
092      if (!StringUtils.isBlank(propertyAnnotation.type())) {
093        try {
094          param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name());
095        } catch (IllegalArgumentException e) {
096          throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e);
097        }
098      } else {
099        param.setType(guessType(field.getType()).name());
100      }
101    }
102  }
103
104  private static final Function<Class<?>, PropertyType> TYPE_FOR_CLASS = Functions.forMap(
105    ImmutableMap.<Class<?>, PropertyType>builder()
106      .put(Integer.class, PropertyType.INTEGER)
107      .put(int.class, PropertyType.INTEGER)
108      .put(Float.class, PropertyType.FLOAT)
109      .put(float.class, PropertyType.FLOAT)
110      .put(Boolean.class, PropertyType.BOOLEAN)
111      .put(boolean.class, PropertyType.BOOLEAN)
112      .build(),
113    PropertyType.STRING);
114
115  static PropertyType guessType(Class<?> type) {
116    return TYPE_FOR_CLASS.apply(type);
117  }
118}