001/* 002 * SonarQube 003 * Copyright (C) 2009-2018 SonarSource SA 004 * mailto:info 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.rules; 021 022import java.util.ArrayList; 023import java.util.Date; 024import java.util.List; 025import java.util.stream.Collectors; 026import javax.annotation.CheckForNull; 027import org.apache.commons.lang.StringUtils; 028import org.apache.commons.lang.builder.ToStringBuilder; 029import org.sonar.api.profiles.RulesProfile; 030 031public class ActiveRule implements Cloneable { 032 033 public static final String INHERITED = "INHERITED"; 034 public static final String OVERRIDES = "OVERRIDES"; 035 036 private Integer id; 037 private Rule rule; 038 private RulePriority severity; 039 private RulePriority overriddenSeverity; 040 private RulesProfile rulesProfile; 041 private List<ActiveRuleParam> activeRuleParams = new ArrayList<>(); 042 private String inheritance; 043 044 /** 045 * @deprecated visibility should be reduced to protected or package 046 */ 047 @Deprecated 048 public ActiveRule() { 049 } 050 051 /** 052 * @deprecated visibility should be reduced to protected or package 053 */ 054 @Deprecated 055 public ActiveRule(RulesProfile profile, Rule rule, RulePriority severity) { 056 this.rule = rule; 057 this.overriddenSeverity = severity; 058 if (severity == null && rule != null) { 059 this.severity = rule.getSeverity(); 060 } else { 061 this.severity = severity; 062 } 063 064 this.rulesProfile = profile; 065 } 066 067 public Integer getId() { 068 return id; 069 } 070 071 /** 072 * For internal use only. 073 * 074 * @since 2.5 075 */ 076 public String getInheritance() { 077 return inheritance; 078 } 079 080 /** 081 * For internal use only. 082 * 083 * @since 2.5 084 */ 085 public void setInheritance(String s) { 086 this.inheritance = s; 087 } 088 089 public boolean isInherited() { 090 return StringUtils.equals(INHERITED, inheritance); 091 } 092 093 public boolean doesOverride() { 094 return StringUtils.equals(OVERRIDES, inheritance); 095 } 096 097 /** 098 * @deprecated visibility should be decreased to protected or package 099 */ 100 @Deprecated 101 public void setId(Integer id) { 102 this.id = id; 103 } 104 105 public Rule getRule() { 106 return rule; 107 } 108 109 /** 110 * @deprecated visibility should be reduced to protected or package 111 */ 112 @Deprecated 113 public void setRule(Rule rule) { 114 this.rule = rule; 115 } 116 117 /** 118 * @since 2.5 119 */ 120 public RulePriority getSeverity() { 121 return severity; 122 } 123 124 /** 125 * For internal use 126 * @since 6.6 127 * @deprecated 128 */ 129 @Deprecated 130 public RulePriority getOverriddenSeverity() { 131 return overriddenSeverity; 132 } 133 134 /** 135 * @since 2.5 136 */ 137 public void setSeverity(RulePriority severity) { 138 this.severity = severity; 139 } 140 141 /** 142 * @deprecated since 2.5 use {@link #getSeverity()} instead. See http://jira.sonarsource.com/browse/SONAR-1829 143 */ 144 @Deprecated 145 public RulePriority getPriority() { 146 return severity; 147 } 148 149 /** 150 * @deprecated since 2.5 use {@link #setSeverity(RulePriority)} instead. See http://jira.sonarsource.com/browse/SONAR-1829 151 */ 152 @Deprecated 153 public void setPriority(RulePriority priority) { 154 this.severity = priority; 155 } 156 157 public RulesProfile getRulesProfile() { 158 return rulesProfile; 159 } 160 161 /** 162 * @deprecated visibility should be reduced to protected or package 163 */ 164 @Deprecated 165 public void setRulesProfile(RulesProfile rulesProfile) { 166 this.rulesProfile = rulesProfile; 167 } 168 169 public List<ActiveRuleParam> getActiveRuleParams() { 170 return activeRuleParams; 171 } 172 173 /** 174 * @deprecated use setParameter() 175 */ 176 @Deprecated 177 public void setActiveRuleParams(List<ActiveRuleParam> params) { 178 this.activeRuleParams = params; 179 } 180 181 public ActiveRule setParameter(String key, String value) { 182 RuleParam ruleParameter = rule.getParam(key); 183 if (ruleParameter != null) { 184 activeRuleParams.add(new ActiveRuleParam(this, ruleParameter, value)); 185 } 186 return this; 187 } 188 189 public String getParameter(String key) { 190 if (activeRuleParams != null) { 191 for (ActiveRuleParam param : activeRuleParams) { 192 if (StringUtils.equals(key, param.getKey())) { 193 return param.getValue(); 194 } 195 } 196 } 197 return null; 198 } 199 200 /** 201 * @deprecated since 2.3 use {@link #getRepositoryKey()} instead 202 */ 203 @Deprecated 204 public String getPluginName() { 205 return rule.getRepositoryKey(); 206 } 207 208 public String getRepositoryKey() { 209 return rule.getRepositoryKey(); 210 } 211 212 /** 213 * @return the config key the active rule belongs to 214 */ 215 public String getConfigKey() { 216 return rule.getConfigKey(); 217 } 218 219 /** 220 * @return the key of the active rule 221 */ 222 public String getRuleKey() { 223 return rule.getKey(); 224 } 225 226 /** 227 * @since 4.2 228 * @deprecated in 4.4. Feature dropped. 229 */ 230 @CheckForNull 231 @Deprecated 232 public String getNoteData() { 233 return null; 234 } 235 236 /** 237 * @since 4.2 238 * @deprecated in 4.4. Feature dropped. 239 */ 240 @CheckForNull 241 @Deprecated 242 public String getNoteUserLogin() { 243 return null; 244 } 245 246 /** 247 * @since 4.2 248 * @deprecated in 4.4. Feature dropped. 249 */ 250 @CheckForNull 251 @Deprecated 252 public Date getNoteCreatedAt() { 253 return null; 254 } 255 256 /** 257 * @since 4.2 258 * @deprecated in 4.4. Feature dropped. 259 */ 260 @CheckForNull 261 @Deprecated 262 public Date getNoteUpdatedAt() { 263 return null; 264 } 265 266 @Override 267 public boolean equals(Object o) { 268 if (this == o) { 269 return true; 270 } 271 if (o == null || getClass() != o.getClass()) { 272 return false; 273 } 274 275 ActiveRule that = (ActiveRule) o; 276 277 if (!rule.equals(that.rule)) { 278 return false; 279 } 280 return !((rulesProfile != null) ? !rulesProfile.equals(that.rulesProfile) : (that.rulesProfile != null)); 281 282 } 283 284 @Override 285 public int hashCode() { 286 int result = rule.hashCode(); 287 result = 31 * result + (rulesProfile != null ? rulesProfile.hashCode() : 0); 288 return result; 289 } 290 291 @Override 292 public String toString() { 293 return new ToStringBuilder(this).append("id", getId()).append("rule", rule).append("priority", severity) 294 .append("params", activeRuleParams).toString(); 295 } 296 297 @Override 298 public Object clone() { 299 final ActiveRule clone = new ActiveRule(getRulesProfile(), getRule(), getSeverity()); 300 clone.setInheritance(getInheritance()); 301 if (activeRuleParams != null && !activeRuleParams.isEmpty()) { 302 clone.setActiveRuleParams(activeRuleParams 303 .stream() 304 .map(input -> { 305 ActiveRuleParam activeRuleParamClone = (ActiveRuleParam) input.clone(); 306 activeRuleParamClone.setActiveRule(clone); 307 return activeRuleParamClone; 308 }) 309 .collect(Collectors.toList())); 310 } 311 return clone; 312 } 313 314 /** 315 * @since 2.6 316 */ 317 public boolean isEnabled() { 318 return getRule() != null && getRule().isEnabled(); 319 } 320}