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