001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 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.checks;
021    
022    import com.google.common.collect.Maps;
023    import org.apache.commons.lang.StringUtils;
024    import org.sonar.api.profiles.RulesProfile;
025    import org.sonar.api.rules.ActiveRule;
026    import org.sonar.api.rules.ActiveRuleParam;
027    import org.sonar.api.utils.AnnotationUtils;
028    import org.sonar.api.utils.FieldUtils2;
029    import org.sonar.api.utils.SonarException;
030    import org.sonar.check.Rule;
031    import org.sonar.check.RuleProperty;
032    
033    import java.lang.reflect.Field;
034    import java.util.Collection;
035    import java.util.List;
036    import java.util.Map;
037    
038    /**
039     * @since 2.3
040     */
041    public final class AnnotationCheckFactory extends CheckFactory {
042    
043      private static final String CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE = "Can not instantiate the check related to the rule ";
044      private Map<String, Object> checksByKey = Maps.newHashMap();
045    
046      private AnnotationCheckFactory(RulesProfile profile, String repositoryKey, Collection checks) {
047        super(profile, repositoryKey);
048        groupByKey(checks);
049      }
050    
051      public static AnnotationCheckFactory create(RulesProfile profile, String repositoryKey, Collection checkClasses) {
052        AnnotationCheckFactory factory = new AnnotationCheckFactory(profile, repositoryKey, checkClasses);
053        factory.init();
054        return factory;
055      }
056    
057      private void groupByKey(Collection checks) {
058        for (Object check : checks) {
059          String key = getRuleKey(check);
060          if (key != null) {
061            checksByKey.put(key, check);
062          }
063        }
064      }
065    
066      @Override
067      protected Object createCheck(ActiveRule activeRule) {
068        Object object = checksByKey.get(activeRule.getConfigKey());
069        if (object != null) {
070          return instantiate(activeRule, object);
071        }
072        return null;
073      }
074    
075      private Object instantiate(ActiveRule activeRule, Object checkClassOrInstance) {
076        try {
077          Object check = checkClassOrInstance;
078          if (check instanceof Class) {
079            check = ((Class) checkClassOrInstance).newInstance();
080          }
081          configureFields(activeRule, check);
082          return check;
083    
084        } catch (InstantiationException e) {
085          throw new SonarException(CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE + activeRule, e);
086    
087        } catch (IllegalAccessException e) {
088          throw new SonarException(CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE + activeRule, e);
089        }
090      }
091    
092      private void configureFields(ActiveRule activeRule, Object check) {
093        for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
094          Field field = getField(check, param.getKey());
095          if (field == null) {
096            throw new SonarException("The field " + param.getKey() + " does not exist or is not annotated with @RuleProperty in the class " + check.getClass().getName());
097          }
098          if (StringUtils.isNotBlank(param.getValue())) {
099            configureField(check, field, param.getValue());
100          }
101        }
102    
103      }
104    
105      private void configureField(Object check, Field field, String value) {
106        try {
107          field.setAccessible(true);
108    
109          if (field.getType().equals(String.class)) {
110            field.set(check, value);
111    
112          } else if ("int".equals(field.getType().getSimpleName())) {
113            field.setInt(check, Integer.parseInt(value));
114    
115          } else if ("short".equals(field.getType().getSimpleName())) {
116            field.setShort(check, Short.parseShort(value));
117    
118          } else if ("long".equals(field.getType().getSimpleName())) {
119            field.setLong(check, Long.parseLong(value));
120    
121          } else if ("double".equals(field.getType().getSimpleName())) {
122            field.setDouble(check, Double.parseDouble(value));
123    
124          } else if ("boolean".equals(field.getType().getSimpleName())) {
125            field.setBoolean(check, Boolean.parseBoolean(value));
126    
127          } else if ("byte".equals(field.getType().getSimpleName())) {
128            field.setByte(check, Byte.parseByte(value));
129    
130          } else if (field.getType().equals(Integer.class)) {
131            field.set(check, Integer.parseInt(value));
132    
133          } else if (field.getType().equals(Long.class)) {
134            field.set(check, Long.parseLong(value));
135    
136          } else if (field.getType().equals(Double.class)) {
137            field.set(check, Double.parseDouble(value));
138    
139          } else if (field.getType().equals(Boolean.class)) {
140            field.set(check, Boolean.parseBoolean(value));
141    
142          } else {
143            throw new SonarException("The type of the field " + field + " is not supported: " + field.getType());
144          }
145        } catch (IllegalAccessException e) {
146          throw new SonarException("Can not set the value of the field " + field + " in the class: " + check.getClass().getName(), e);
147        }
148      }
149    
150      private Field getField(Object check, String key) {
151        List<Field> fields = FieldUtils2.getFields(check.getClass(), true);
152        for (Field field : fields) {
153          RuleProperty propertyAnnotation = field.getAnnotation(RuleProperty.class);
154          if (propertyAnnotation != null && (StringUtils.equals(key, field.getName()) || StringUtils.equals(key, propertyAnnotation.key()))) {
155            return field;
156          }
157        }
158        return null;
159      }
160    
161      private String getRuleKey(Object annotatedClassOrObject) {
162        String key = null;
163        Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClassOrObject, Rule.class);
164        if (ruleAnnotation != null) {
165          key = ruleAnnotation.key();
166        }
167        Class clazz = annotatedClassOrObject.getClass();
168        if (annotatedClassOrObject instanceof Class) {
169          clazz = (Class) annotatedClassOrObject;
170        }
171        return StringUtils.defaultIfEmpty(key, clazz.getCanonicalName());
172      }
173    }