You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActiveRule.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.api.rules;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.stream.Collectors;
  24. import javax.annotation.Nullable;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.apache.commons.lang.builder.ToStringBuilder;
  27. import org.sonar.api.profiles.RulesProfile;
  28. public class ActiveRule implements Cloneable {
  29. public static final String INHERITED = "INHERITED";
  30. public static final String OVERRIDES = "OVERRIDES";
  31. private Integer id;
  32. private Rule rule;
  33. private RulePriority severity;
  34. private RulePriority overriddenSeverity;
  35. private RulesProfile rulesProfile;
  36. private List<ActiveRuleParam> activeRuleParams = new ArrayList<>();
  37. private String inheritance;
  38. /**
  39. * @deprecated visibility should be reduced to protected or package
  40. */
  41. @Deprecated
  42. public ActiveRule() {
  43. }
  44. /**
  45. * @deprecated visibility should be reduced to protected or package
  46. */
  47. @Deprecated
  48. public ActiveRule(RulesProfile profile, Rule rule, @Nullable RulePriority severity) {
  49. this.rule = rule;
  50. this.overriddenSeverity = severity;
  51. if (severity == null && rule != null) {
  52. this.severity = rule.getSeverity();
  53. } else {
  54. this.severity = severity;
  55. }
  56. this.rulesProfile = profile;
  57. }
  58. public Integer getId() {
  59. return id;
  60. }
  61. /**
  62. * For internal use only.
  63. *
  64. * @since 2.5
  65. */
  66. public String getInheritance() {
  67. return inheritance;
  68. }
  69. /**
  70. * For internal use only.
  71. *
  72. * @since 2.5
  73. */
  74. public void setInheritance(String s) {
  75. this.inheritance = s;
  76. }
  77. public boolean isInherited() {
  78. return StringUtils.equals(INHERITED, inheritance);
  79. }
  80. public boolean doesOverride() {
  81. return StringUtils.equals(OVERRIDES, inheritance);
  82. }
  83. /**
  84. * @deprecated visibility should be decreased to protected or package
  85. */
  86. @Deprecated
  87. public void setId(Integer id) {
  88. this.id = id;
  89. }
  90. public Rule getRule() {
  91. return rule;
  92. }
  93. /**
  94. * @deprecated visibility should be reduced to protected or package
  95. */
  96. @Deprecated
  97. public void setRule(Rule rule) {
  98. this.rule = rule;
  99. }
  100. /**
  101. * @since 2.5
  102. */
  103. public RulePriority getSeverity() {
  104. return severity;
  105. }
  106. /**
  107. * For internal use
  108. * @since 6.6
  109. * @deprecated
  110. */
  111. @Deprecated
  112. public RulePriority getOverriddenSeverity() {
  113. return overriddenSeverity;
  114. }
  115. /**
  116. * @since 2.5
  117. */
  118. public void setSeverity(RulePriority severity) {
  119. this.severity = severity;
  120. }
  121. public RulesProfile getRulesProfile() {
  122. return rulesProfile;
  123. }
  124. /**
  125. * @deprecated visibility should be reduced to protected or package
  126. */
  127. @Deprecated
  128. public void setRulesProfile(RulesProfile rulesProfile) {
  129. this.rulesProfile = rulesProfile;
  130. }
  131. public List<ActiveRuleParam> getActiveRuleParams() {
  132. return activeRuleParams;
  133. }
  134. /**
  135. * @deprecated use setParameter()
  136. */
  137. @Deprecated
  138. public void setActiveRuleParams(List<ActiveRuleParam> params) {
  139. this.activeRuleParams = params;
  140. }
  141. public ActiveRule setParameter(String key, String value) {
  142. RuleParam ruleParameter = rule.getParam(key);
  143. if (ruleParameter != null) {
  144. activeRuleParams.add(new ActiveRuleParam(this, ruleParameter, value));
  145. }
  146. return this;
  147. }
  148. public String getParameter(String key) {
  149. if (activeRuleParams != null) {
  150. for (ActiveRuleParam param : activeRuleParams) {
  151. if (StringUtils.equals(key, param.getKey())) {
  152. return param.getValue();
  153. }
  154. }
  155. }
  156. return null;
  157. }
  158. /**
  159. * @deprecated since 2.3 use {@link #getRepositoryKey()} instead
  160. */
  161. @Deprecated
  162. public String getPluginName() {
  163. return rule.getRepositoryKey();
  164. }
  165. public String getRepositoryKey() {
  166. return rule.getRepositoryKey();
  167. }
  168. /**
  169. * @return the config key the active rule belongs to
  170. */
  171. public String getConfigKey() {
  172. return rule.getConfigKey();
  173. }
  174. /**
  175. * @return the key of the active rule
  176. */
  177. public String getRuleKey() {
  178. return rule.getKey();
  179. }
  180. @Override
  181. public boolean equals(Object o) {
  182. if (this == o) {
  183. return true;
  184. }
  185. if (o == null || getClass() != o.getClass()) {
  186. return false;
  187. }
  188. ActiveRule that = (ActiveRule) o;
  189. if (!rule.equals(that.rule)) {
  190. return false;
  191. }
  192. return !((rulesProfile != null) ? !rulesProfile.equals(that.rulesProfile) : (that.rulesProfile != null));
  193. }
  194. @Override
  195. public int hashCode() {
  196. int result = rule.hashCode();
  197. result = 31 * result + (rulesProfile != null ? rulesProfile.hashCode() : 0);
  198. return result;
  199. }
  200. @Override
  201. public String toString() {
  202. return new ToStringBuilder(this).append("id", getId()).append("rule", rule).append("priority", severity)
  203. .append("params", activeRuleParams).toString();
  204. }
  205. @Override
  206. public Object clone() {
  207. final ActiveRule clone = new ActiveRule(getRulesProfile(), getRule(), getSeverity());
  208. clone.setInheritance(getInheritance());
  209. if (activeRuleParams != null && !activeRuleParams.isEmpty()) {
  210. clone.setActiveRuleParams(activeRuleParams
  211. .stream()
  212. .map(input -> {
  213. ActiveRuleParam activeRuleParamClone = (ActiveRuleParam) input.clone();
  214. activeRuleParamClone.setActiveRule(clone);
  215. return activeRuleParamClone;
  216. })
  217. .collect(Collectors.toList()));
  218. }
  219. return clone;
  220. }
  221. /**
  222. * @since 2.6
  223. */
  224. public boolean isEnabled() {
  225. return getRule() != null && getRule().isEnabled();
  226. }
  227. }