001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
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.check;
021    
022    import java.lang.reflect.Field;
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    /**
027     * @since 2.1 (experimental)
028     * @deprecated since 2.3
029     */
030    @Deprecated
031    public final class AnnotationIntrospector {
032    
033      private AnnotationIntrospector() {
034        // only static methods
035      }
036    
037      public static String getCheckKey(Class annotatedClass) {
038        Check checkAnnotation = getCheckAnnotation(annotatedClass);
039        if (checkAnnotation == null) {
040          return null;
041        }
042    
043        String key = checkAnnotation.key();
044        if (key == null || "".equals(key.trim())) {
045          key = annotatedClass.getCanonicalName();
046        }
047        return key;
048      }
049    
050      public static Check getCheckAnnotation(Class annotatedClass) {
051        return (Check) annotatedClass.getAnnotation(Check.class);
052      }
053    
054      public static List<Field> getPropertyFields(Class annotatedClass) {
055        List<Field> fields = new ArrayList<Field>();
056        for (Field field : annotatedClass.getDeclaredFields()) {
057          org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class);
058          if (propertyAnnotation != null) {
059            fields.add(field);
060          }
061        }
062        return fields;
063      }
064    
065      public static String getPropertyFieldKey(Field field) {
066        String key = null;
067        org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class);
068        if (propertyAnnotation != null) {
069          key = propertyAnnotation.key();
070          if (key == null || "".equals(key)) {
071            key = field.getName();
072          }
073        }
074        return key;
075      }
076    }