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.

AnnotationProfileParser.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.profiles;
  21. import java.util.Collection;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.sonar.api.ce.ComputeEngineSide;
  24. import org.sonar.api.rules.Rule;
  25. import org.sonar.api.rules.RuleAnnotationUtils;
  26. import org.sonar.api.rules.RuleFinder;
  27. import org.sonar.api.rules.RulePriority;
  28. import org.sonar.api.server.ServerSide;
  29. import org.sonar.api.server.profile.BuiltInQualityProfileAnnotationLoader;
  30. import org.sonar.api.utils.ValidationMessages;
  31. import org.sonar.check.BelongsToProfile;
  32. /**
  33. * @since 2.3
  34. * @deprecated since 6.6 use {@link BuiltInQualityProfileAnnotationLoader}
  35. */
  36. @ServerSide
  37. @ComputeEngineSide
  38. @Deprecated
  39. public final class AnnotationProfileParser {
  40. private final RuleFinder ruleFinder;
  41. public AnnotationProfileParser(RuleFinder ruleFinder) {
  42. this.ruleFinder = ruleFinder;
  43. }
  44. public RulesProfile parse(String repositoryKey, String profileName, String language, Collection<Class> annotatedClasses, ValidationMessages messages) {
  45. RulesProfile profile = RulesProfile.create(profileName, language);
  46. for (Class<?> aClass : annotatedClasses) {
  47. BelongsToProfile belongsToProfile = aClass.getAnnotation(BelongsToProfile.class);
  48. addRule(aClass, belongsToProfile, profile, repositoryKey, messages);
  49. }
  50. return profile;
  51. }
  52. private void addRule(Class aClass, BelongsToProfile annotation, RulesProfile profile, String repositoryKey, ValidationMessages messages) {
  53. if ((annotation != null) && StringUtils.equals(annotation.title(), profile.getName())) {
  54. String ruleKey = RuleAnnotationUtils.getRuleKey(aClass);
  55. Rule rule = ruleFinder.findByKey(repositoryKey, ruleKey);
  56. if (rule == null) {
  57. messages.addWarningText("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]");
  58. } else {
  59. RulePriority priority = null;
  60. if (annotation.priority() != null) {
  61. priority = RulePriority.fromCheckPriority(annotation.priority());
  62. }
  63. profile.activateRule(rule, priority);
  64. }
  65. }
  66. }
  67. }