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.

AnnotationProfileDefinition.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar 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. * Sonar 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
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.api.profiles;
  21. import org.sonar.api.rules.Rule;
  22. import org.sonar.api.rules.RuleAnnotationUtils;
  23. import org.sonar.api.rules.RuleFinder;
  24. import org.sonar.api.rules.RulePriority;
  25. import org.sonar.api.utils.ValidationMessages;
  26. import org.sonar.check.BelongsToProfile;
  27. import java.util.Collection;
  28. /**
  29. * @since 2.3
  30. */
  31. public abstract class AnnotationProfileDefinition extends ProfileDefinition {
  32. private String name;
  33. private String language;
  34. private String repositoryKey;
  35. private Collection<Class> annotatedClasses;
  36. private RuleFinder ruleFinder;
  37. protected AnnotationProfileDefinition(String repositoryKey, String profileName, String language, Collection<Class> annotatedClasses, RuleFinder ruleFinder) {
  38. this.name = profileName;
  39. this.language = language;
  40. this.repositoryKey = repositoryKey;
  41. this.annotatedClasses = annotatedClasses;
  42. this.ruleFinder = ruleFinder;
  43. }
  44. @Override
  45. public RulesProfile createProfile(ValidationMessages validation) {
  46. RulesProfile profile = RulesProfile.create(name, language);
  47. if (annotatedClasses != null) {
  48. for (Class aClass : annotatedClasses) {
  49. BelongsToProfile belongsToProfile = (BelongsToProfile) aClass.getAnnotation(BelongsToProfile.class);
  50. registerRule(aClass, belongsToProfile, profile, validation);
  51. }
  52. }
  53. return profile;
  54. }
  55. private void registerRule(Class aClass, BelongsToProfile belongsToProfile, RulesProfile profile, ValidationMessages validation) {
  56. if (belongsToProfile != null) {
  57. String ruleKey = RuleAnnotationUtils.getRuleKey(aClass);
  58. Rule rule = ruleFinder.findByKey(repositoryKey, ruleKey);
  59. if (rule == null) {
  60. validation.addErrorText("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]");
  61. } else {
  62. RulePriority priority = null;
  63. if (belongsToProfile.priority() != null) {
  64. priority = RulePriority.fromCheckPriority(belongsToProfile.priority());
  65. }
  66. profile.activateRule(rule, priority);
  67. }
  68. }
  69. }
  70. }