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