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    /**
031     * @since 2.1 (experimental)
032     * @deprecated since 2.3
033     */
034    @Deprecated
035    public final class AnnotationCheckProfileFactory {
036    
037      private AnnotationCheckProfileFactory() {
038      }
039    
040      public static Collection<CheckProfile> create(String repositoryKey, String language, Collection<Class> checkClasses) {
041        Map<String, CheckProfile> profilesByTitle = new HashMap<String, CheckProfile>();
042    
043        if (checkClasses != null) {
044          for (Class aClass : checkClasses) {
045            BelongsToProfiles belongsToProfiles = (BelongsToProfiles) aClass.getAnnotation(BelongsToProfiles.class);
046            if (belongsToProfiles != null) {
047              for (BelongsToProfile belongsToProfile : belongsToProfiles.value()) {
048                registerProfile(profilesByTitle, aClass, belongsToProfile, repositoryKey, language);
049              }
050            }
051            BelongsToProfile belongsToProfile = (BelongsToProfile) aClass.getAnnotation(BelongsToProfile.class);
052            registerProfile(profilesByTitle, aClass, belongsToProfile, repositoryKey, language);
053          }
054        }
055    
056        return profilesByTitle.values();
057      }
058    
059      private static void registerProfile(Map<String, CheckProfile> profilesByTitle, Class aClass, BelongsToProfile belongsToProfile, String repositoryKey, String language) {
060        if (belongsToProfile != null) {
061          String title = belongsToProfile.title();
062          CheckProfile profile = profilesByTitle.get(title);
063          if (profile == null) {
064            profile = new CheckProfile(title, language);
065            profilesByTitle.put(title, profile);
066          }
067          Check check = new Check(repositoryKey, AnnotationIntrospector.getCheckKey(aClass));
068          check.setPriority(belongsToProfile.priority());
069          profile.addCheck(check);
070        }
071      }
072    }