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     */
020    package org.sonar.api.rules;
021    
022    import java.lang.reflect.Field;
023    import java.util.Collection;
024    import java.util.List;
025    
026    import org.apache.commons.lang.StringUtils;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    import org.sonar.api.ServerComponent;
030    import org.sonar.api.utils.AnnotationUtils;
031    import org.sonar.check.Check;
032    
033    import com.google.common.collect.Lists;
034    
035    /**
036     * @since 2.3
037     */
038    public final class AnnotationRuleParser implements ServerComponent {
039    
040      private static final Logger LOG = LoggerFactory.getLogger(AnnotationRuleParser.class);
041    
042      public List<Rule> parse(String repositoryKey, Collection<Class> annotatedClasses) {
043        List<Rule> rules = Lists.newArrayList();
044        for (Class annotatedClass : annotatedClasses) {
045          rules.add(create(repositoryKey, annotatedClass));
046        }
047        return rules;
048      }
049    
050      private Rule create(String repositoryKey, Class annotatedClass) {
051        org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, org.sonar.check.Rule.class);
052        if (ruleAnnotation != null) {
053          return toRule(repositoryKey, annotatedClass, ruleAnnotation);
054        }
055        Check checkAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, Check.class);
056        if (checkAnnotation != null) {
057          return toRule(repositoryKey, annotatedClass, checkAnnotation);
058        }
059        LOG.warn("The class " + annotatedClass.getCanonicalName() + " should be annotated with " + Rule.class);
060        return null;
061      }
062    
063      private Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) {
064        String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
065        String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
066        String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null);
067        Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
068        rule.setDescription(description);
069        rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
070        rule.setCardinality(ruleAnnotation.cardinality());
071    
072        Field[] fields = clazz.getDeclaredFields();
073        if (fields != null) {
074          for (Field field : fields) {
075            addRuleProperty(rule, field);
076          }
077        }
078    
079        return rule;
080      }
081    
082      private Rule toRule(String repositoryKey, Class clazz, Check checkAnnotation) {
083        String ruleKey = StringUtils.defaultIfEmpty(checkAnnotation.key(), clazz.getCanonicalName());
084        String ruleName = StringUtils.defaultIfEmpty(checkAnnotation.title(), ruleKey);
085        Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
086        rule.setDescription(checkAnnotation.description());
087        rule.setSeverity(RulePriority.fromCheckPriority(checkAnnotation.priority()));
088    
089        Field[] fields = clazz.getDeclaredFields();
090        if (fields != null) {
091          for (Field field : fields) {
092            addCheckProperty(rule, field);
093          }
094        }
095        return rule;
096      }
097    
098      private void addRuleProperty(Rule rule, Field field) {
099        org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
100        if (propertyAnnotation != null) {
101          String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
102          RuleParam param = rule.createParameter(fieldKey);
103          param.setDescription(propertyAnnotation.description());
104          param.setDefaultValue(propertyAnnotation.defaultValue());
105        }
106      }
107    
108      private void addCheckProperty(Rule rule, Field field) {
109        org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class);
110        if (propertyAnnotation != null) {
111          String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
112          RuleParam param = rule.createParameter(fieldKey);
113          param.setDescription(propertyAnnotation.description());
114        }
115      }
116    }