您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RuleFinderCompatibility.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.batch.rule;
  21. import org.sonar.api.batch.rule.Rules;
  22. import com.google.common.base.Function;
  23. import com.google.common.collect.Collections2;
  24. import org.apache.commons.lang.builder.ReflectionToStringBuilder;
  25. import org.apache.commons.lang.builder.ToStringStyle;
  26. import org.sonar.api.rule.RuleKey;
  27. import org.sonar.api.rules.Rule;
  28. import org.sonar.api.rules.RuleFinder;
  29. import org.sonar.api.rules.RuleQuery;
  30. import javax.annotation.CheckForNull;
  31. import javax.annotation.Nonnull;
  32. import javax.annotation.Nullable;
  33. import java.util.Arrays;
  34. import java.util.Collection;
  35. import java.util.Collections;
  36. /**
  37. * FIXME Waiting for the list of all server rules on batch side this is implemented by redirecting on ActiveRules. This is not correct
  38. * since there is a difference between a rule that doesn't exists and a rule that is not activated in project quality profile.
  39. *
  40. */
  41. public class RuleFinderCompatibility implements RuleFinder {
  42. private final Rules rules;
  43. public RuleFinderCompatibility(Rules rules) {
  44. this.rules = rules;
  45. }
  46. @Override
  47. public Rule findById(int ruleId) {
  48. throw new UnsupportedOperationException("Unable to find rule by id");
  49. }
  50. @Override
  51. public Rule findByKey(String repositoryKey, String key) {
  52. return findByKey(RuleKey.of(repositoryKey, key));
  53. }
  54. @Override
  55. public Rule findByKey(RuleKey key) {
  56. return toRule(rules.find(key));
  57. }
  58. @Override
  59. public Rule find(RuleQuery query) {
  60. Collection<Rule> all = findAll(query);
  61. if (all.size() > 1) {
  62. throw new IllegalArgumentException("Non unique result for rule query: " + ReflectionToStringBuilder.toString(query, ToStringStyle.SHORT_PREFIX_STYLE));
  63. } else if (all.isEmpty()) {
  64. return null;
  65. } else {
  66. return all.iterator().next();
  67. }
  68. }
  69. @Override
  70. public Collection<Rule> findAll(RuleQuery query) {
  71. if (query.getConfigKey() != null) {
  72. if (query.getRepositoryKey() != null && query.getKey() == null) {
  73. return byInternalKey(query);
  74. }
  75. } else if (query.getRepositoryKey() != null) {
  76. if (query.getKey() != null) {
  77. return byKey(query);
  78. } else {
  79. return byRepository(query);
  80. }
  81. }
  82. throw new UnsupportedOperationException("Unable to find rule by query");
  83. }
  84. private Collection<Rule> byRepository(RuleQuery query) {
  85. return Collections2.transform(rules.findByRepository(query.getRepositoryKey()), ruleTransformer);
  86. }
  87. private static Function<org.sonar.api.batch.rule.Rule, Rule> ruleTransformer = new Function<org.sonar.api.batch.rule.Rule, Rule>() {
  88. @Override
  89. public Rule apply(@Nonnull org.sonar.api.batch.rule.Rule input) {
  90. return toRule(input);
  91. }
  92. };
  93. private Collection<Rule> byKey(RuleQuery query) {
  94. Rule rule = toRule(rules.find(RuleKey.of(query.getRepositoryKey(), query.getKey())));
  95. return rule != null ? Arrays.asList(rule) : Collections.<Rule>emptyList();
  96. }
  97. private Collection<Rule> byInternalKey(RuleQuery query) {
  98. return Collections2.transform(rules.findByInternalKey(query.getRepositoryKey(), query.getConfigKey()), ruleTransformer);
  99. }
  100. @CheckForNull
  101. private static Rule toRule(@Nullable org.sonar.api.batch.rule.Rule ar) {
  102. return ar == null ? null : Rule.create(ar.key().repository(), ar.key().rule()).setName(ar.name()).setConfigKey(ar.internalKey());
  103. }
  104. }