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.checks;
021
022import com.google.common.collect.Maps;
023import java.lang.reflect.Field;
024import java.util.Collection;
025import java.util.List;
026import java.util.Map;
027import org.apache.commons.lang.StringUtils;
028import org.sonar.api.batch.rule.Checks;
029import org.sonar.api.profiles.RulesProfile;
030import org.sonar.api.rules.ActiveRule;
031import org.sonar.api.rules.ActiveRuleParam;
032import org.sonar.api.utils.AnnotationUtils;
033import org.sonar.api.utils.FieldUtils2;
034import org.sonar.api.utils.SonarException;
035import org.sonar.check.Rule;
036import org.sonar.check.RuleProperty;
037
038/**
039 * @since 2.3
040 * @deprecated since 4.2 use {@link Checks}
041 */
042@Deprecated
043public final class AnnotationCheckFactory extends CheckFactory {
044
045  private static final String CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE = "Can not instantiate the check related to the rule ";
046  private Map<String, Object> checksByKey = Maps.newHashMap();
047
048  private AnnotationCheckFactory(RulesProfile profile, String repositoryKey, Collection checks) {
049    super(profile, repositoryKey);
050    groupByKey(checks);
051  }
052
053  public static AnnotationCheckFactory create(RulesProfile profile, String repositoryKey, Collection checkClasses) {
054    AnnotationCheckFactory factory = new AnnotationCheckFactory(profile, repositoryKey, checkClasses);
055    factory.init();
056    return factory;
057  }
058
059  private void groupByKey(Collection checks) {
060    for (Object check : checks) {
061      String key = getRuleKey(check);
062      if (key != null) {
063        checksByKey.put(key, check);
064      }
065    }
066  }
067
068  @Override
069  public Object createCheck(ActiveRule activeRule) {
070    Object object = checksByKey.get(activeRule.getConfigKey());
071    if (object != null) {
072      return instantiate(activeRule, object);
073    }
074    return null;
075  }
076
077  private static Object instantiate(ActiveRule activeRule, Object checkClassOrInstance) {
078    try {
079      Object check = checkClassOrInstance;
080      if (check instanceof Class) {
081        check = ((Class) checkClassOrInstance).newInstance();
082      }
083      configureFields(activeRule, check);
084      return check;
085
086    } catch (InstantiationException | IllegalAccessException e) {
087      throw new SonarException(CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE + activeRule, e);
088
089    }
090  }
091
092  private static 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 static 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.class == field.getType()) {
113        field.setInt(check, Integer.parseInt(value));
114
115      } else if (short.class == field.getType()) {
116        field.setShort(check, Short.parseShort(value));
117
118      } else if (long.class == field.getType()) {
119        field.setLong(check, Long.parseLong(value));
120
121      } else if (double.class == field.getType()) {
122        field.setDouble(check, Double.parseDouble(value));
123
124      } else if (boolean.class == field.getType()) {
125        field.setBoolean(check, Boolean.parseBoolean(value));
126
127      } else if (byte.class == field.getType()) {
128        field.setByte(check, Byte.parseByte(value));
129
130      } else if (Integer.class == field.getType()) {
131        field.set(check, Integer.parseInt(value));
132
133      } else if (Long.class == field.getType()) {
134        field.set(check, Long.parseLong(value));
135
136      } else if (Double.class == field.getType()) {
137        field.set(check, Double.parseDouble(value));
138
139      } else if (Boolean.class == field.getType()) {
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 static 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 static 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}