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.profiles; 021 022import java.util.Collection; 023import org.apache.commons.lang.StringUtils; 024import org.sonar.api.ce.ComputeEngineSide; 025import org.sonar.api.rules.Rule; 026import org.sonar.api.rules.RuleAnnotationUtils; 027import org.sonar.api.rules.RuleFinder; 028import org.sonar.api.rules.RulePriority; 029import org.sonar.api.server.ServerSide; 030import org.sonar.api.server.profile.BuiltInQualityProfileAnnotationLoader; 031import org.sonar.api.utils.ValidationMessages; 032import org.sonar.check.BelongsToProfile; 033 034/** 035 * @since 2.3 036 * @deprecated since 6.6 use {@link BuiltInQualityProfileAnnotationLoader} 037 */ 038@ServerSide 039@ComputeEngineSide 040@Deprecated 041public final class AnnotationProfileParser { 042 043 private final RuleFinder ruleFinder; 044 045 public AnnotationProfileParser(RuleFinder ruleFinder) { 046 this.ruleFinder = ruleFinder; 047 } 048 049 public RulesProfile parse(String repositoryKey, String profileName, String language, Collection<Class> annotatedClasses, ValidationMessages messages) { 050 RulesProfile profile = RulesProfile.create(profileName, language); 051 for (Class<?> aClass : annotatedClasses) { 052 BelongsToProfile belongsToProfile = aClass.getAnnotation(BelongsToProfile.class); 053 addRule(aClass, belongsToProfile, profile, repositoryKey, messages); 054 } 055 return profile; 056 } 057 058 private void addRule(Class aClass, BelongsToProfile annotation, RulesProfile profile, String repositoryKey, ValidationMessages messages) { 059 if ((annotation != null) && StringUtils.equals(annotation.title(), profile.getName())) { 060 String ruleKey = RuleAnnotationUtils.getRuleKey(aClass); 061 Rule rule = ruleFinder.findByKey(repositoryKey, ruleKey); 062 if (rule == null) { 063 messages.addWarningText("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]"); 064 065 } else { 066 RulePriority priority = null; 067 if (annotation.priority() != null) { 068 priority = RulePriority.fromCheckPriority(annotation.priority()); 069 } 070 profile.activateRule(rule, priority); 071 } 072 } 073 } 074}