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 */ 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.sonar.api.PropertyType; 029import org.sonar.api.server.ServerSide; 030import org.sonar.api.utils.AnnotationUtils; 031import org.sonar.api.utils.FieldUtils2; 032import org.sonar.api.utils.SonarException; 033import org.sonar.api.utils.log.Logger; 034import org.sonar.api.utils.log.Loggers; 035 036import java.lang.reflect.Field; 037import java.util.Collection; 038import 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@ServerSide 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 = Lists.newArrayList(); 052 for (Class annotatedClass : annotatedClasses) { 053 rules.add(create(repositoryKey, annotatedClass)); 054 } 055 return rules; 056 } 057 058 private 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 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 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 @VisibleForTesting 116 static PropertyType guessType(Class<?> type) { 117 return TYPE_FOR_CLASS.apply(type); 118 } 119}