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