001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.checks.profiles;
021
022 import org.sonar.check.AnnotationIntrospector;
023 import org.sonar.check.BelongsToProfile;
024 import org.sonar.check.BelongsToProfiles;
025
026 import java.util.Collection;
027 import java.util.HashMap;
028 import java.util.Map;
029
030 public final class AnnotationCheckProfileFactory {
031
032 private AnnotationCheckProfileFactory() {
033 }
034
035 public static Collection<CheckProfile> create(String repositoryKey, String language, Collection<Class> checkClasses) {
036 Map<String, CheckProfile> profilesByTitle = new HashMap<String, CheckProfile>();
037
038 if (checkClasses != null) {
039 for (Class aClass : checkClasses) {
040 BelongsToProfiles belongsToProfiles = (BelongsToProfiles) aClass.getAnnotation(BelongsToProfiles.class);
041 if (belongsToProfiles != null) {
042 for (BelongsToProfile belongsToProfile : belongsToProfiles.value()) {
043 registerProfile(profilesByTitle, aClass, belongsToProfile, repositoryKey, language);
044 }
045 }
046 BelongsToProfile belongsToProfile = (BelongsToProfile) aClass.getAnnotation(BelongsToProfile.class);
047 registerProfile(profilesByTitle, aClass, belongsToProfile, repositoryKey, language);
048 }
049 }
050
051 return profilesByTitle.values();
052 }
053
054 private static void registerProfile(Map<String, CheckProfile> profilesByTitle, Class aClass, BelongsToProfile belongsToProfile, String repositoryKey, String language) {
055 if (belongsToProfile != null) {
056 String title = belongsToProfile.title();
057 CheckProfile profile = profilesByTitle.get(title);
058 if (profile == null) {
059 profile = new CheckProfile(title, language);
060 profilesByTitle.put(title, profile);
061 }
062 Check check = new Check(repositoryKey, AnnotationIntrospector.getCheckKey(aClass));
063 check.setPriority(belongsToProfile.priority());
064 profile.addCheck(check);
065 }
066 }
067 }