From 36805421f21c3dbc2b0638d27ff9ece3d2cf3156 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 27 Jan 2014 23:27:26 +0100 Subject: Move AnnotationCheckFactory to sonar-deprecated --- .../sonar/api/checks/AnnotationCheckFactory.java | 173 --------------------- .../java/org/sonar/api/checks/CheckFactory.java | 71 --------- 2 files changed, 244 deletions(-) delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/checks/CheckFactory.java (limited to 'sonar-plugin-api/src/main/java/org/sonar/api/checks') diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java deleted file mode 100644 index e706da53015..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.checks; - -import com.google.common.collect.Maps; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.ActiveRuleParam; -import org.sonar.api.utils.AnnotationUtils; -import org.sonar.api.utils.FieldUtils2; -import org.sonar.api.utils.SonarException; -import org.sonar.check.Rule; -import org.sonar.check.RuleProperty; - -import java.lang.reflect.Field; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -/** - * @since 2.3 - */ -public final class AnnotationCheckFactory extends CheckFactory { - - private static final String CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE = "Can not instantiate the check related to the rule "; - private Map checksByKey = Maps.newHashMap(); - - private AnnotationCheckFactory(RulesProfile profile, String repositoryKey, Collection checks) { - super(profile, repositoryKey); - groupByKey(checks); - } - - public static AnnotationCheckFactory create(RulesProfile profile, String repositoryKey, Collection checkClasses) { - AnnotationCheckFactory factory = new AnnotationCheckFactory(profile, repositoryKey, checkClasses); - factory.init(); - return factory; - } - - private void groupByKey(Collection checks) { - for (Object check : checks) { - String key = getRuleKey(check); - if (key != null) { - checksByKey.put(key, check); - } - } - } - - @Override - protected Object createCheck(ActiveRule activeRule) { - Object object = checksByKey.get(activeRule.getConfigKey()); - if (object != null) { - return instantiate(activeRule, object); - } - return null; - } - - private Object instantiate(ActiveRule activeRule, Object checkClassOrInstance) { - try { - Object check = checkClassOrInstance; - if (check instanceof Class) { - check = ((Class) checkClassOrInstance).newInstance(); - } - configureFields(activeRule, check); - return check; - - } catch (InstantiationException e) { - throw new SonarException(CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE + activeRule, e); - - } catch (IllegalAccessException e) { - throw new SonarException(CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE + activeRule, e); - } - } - - private void configureFields(ActiveRule activeRule, Object check) { - for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { - Field field = getField(check, param.getKey()); - if (field == null) { - throw new SonarException("The field " + param.getKey() + " does not exist or is not annotated with @RuleProperty in the class " + check.getClass().getName()); - } - if (StringUtils.isNotBlank(param.getValue())) { - configureField(check, field, param.getValue()); - } - } - - } - - private void configureField(Object check, Field field, String value) { - try { - field.setAccessible(true); - - if (field.getType().equals(String.class)) { - field.set(check, value); - - } else if ("int".equals(field.getType().getSimpleName())) { - field.setInt(check, Integer.parseInt(value)); - - } else if ("short".equals(field.getType().getSimpleName())) { - field.setShort(check, Short.parseShort(value)); - - } else if ("long".equals(field.getType().getSimpleName())) { - field.setLong(check, Long.parseLong(value)); - - } else if ("double".equals(field.getType().getSimpleName())) { - field.setDouble(check, Double.parseDouble(value)); - - } else if ("boolean".equals(field.getType().getSimpleName())) { - field.setBoolean(check, Boolean.parseBoolean(value)); - - } else if ("byte".equals(field.getType().getSimpleName())) { - field.setByte(check, Byte.parseByte(value)); - - } else if (field.getType().equals(Integer.class)) { - field.set(check, Integer.parseInt(value)); - - } else if (field.getType().equals(Long.class)) { - field.set(check, Long.parseLong(value)); - - } else if (field.getType().equals(Double.class)) { - field.set(check, Double.parseDouble(value)); - - } else if (field.getType().equals(Boolean.class)) { - field.set(check, Boolean.parseBoolean(value)); - - } else { - throw new SonarException("The type of the field " + field + " is not supported: " + field.getType()); - } - } catch (IllegalAccessException e) { - throw new SonarException("Can not set the value of the field " + field + " in the class: " + check.getClass().getName(), e); - } - } - - private Field getField(Object check, String key) { - List fields = FieldUtils2.getFields(check.getClass(), true); - for (Field field : fields) { - RuleProperty propertyAnnotation = field.getAnnotation(RuleProperty.class); - if (propertyAnnotation != null && (StringUtils.equals(key, field.getName()) || StringUtils.equals(key, propertyAnnotation.key()))) { - return field; - } - } - return null; - } - - private String getRuleKey(Object annotatedClassOrObject) { - String key = null; - Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClassOrObject, Rule.class); - if (ruleAnnotation != null) { - key = ruleAnnotation.key(); - } - Class clazz = annotatedClassOrObject.getClass(); - if (annotatedClassOrObject instanceof Class) { - clazz = (Class) annotatedClassOrObject; - } - return StringUtils.defaultIfEmpty(key, clazz.getCanonicalName()); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/checks/CheckFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/checks/CheckFactory.java deleted file mode 100644 index a819b3c4918..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/checks/CheckFactory.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.checks; - -import com.google.common.collect.Maps; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; - -import java.util.Collection; -import java.util.Map; - -/** - * @since 2.3 - */ -public abstract class CheckFactory { - - private Map checkByActiveRule = Maps.newIdentityHashMap(); - private Map activeRuleByCheck = Maps.newIdentityHashMap(); - private RulesProfile profile; - private String repositoryKey; - - protected CheckFactory(RulesProfile profile, String repositoryKey) { - this.repositoryKey = repositoryKey; - this.profile = profile; - } - - protected void init() { - checkByActiveRule.clear(); - activeRuleByCheck.clear(); - for (ActiveRule activeRule : profile.getActiveRulesByRepository(repositoryKey)) { - C check = createCheck(activeRule); - checkByActiveRule.put(activeRule, check); - activeRuleByCheck.put(check, activeRule); - } - } - - abstract C createCheck(ActiveRule activeRule); - - public final String getRepositoryKey() { - return repositoryKey; - } - - public final Collection getChecks() { - return checkByActiveRule.values(); - } - - public final C getCheck(ActiveRule activeRule) { - return checkByActiveRule.get(activeRule); - } - - public final ActiveRule getActiveRule(C check) { - return activeRuleByCheck.get(check); - } -} -- cgit v1.2.3