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.check; 021 022 import java.lang.reflect.Field; 023 import java.util.ArrayList; 024 import java.util.List; 025 026 public final class AnnotationIntrospector { 027 028 private AnnotationIntrospector() { 029 // only static methods 030 } 031 032 public static String getCheckKey(Class annotatedClass) { 033 Check checkAnnotation = getCheckAnnotation(annotatedClass); 034 if (checkAnnotation == null) { 035 return null; 036 } 037 038 String key = checkAnnotation.key(); 039 if (key == null || "".equals(key.trim())) { 040 key = annotatedClass.getCanonicalName(); 041 } 042 return key; 043 } 044 045 public static Check getCheckAnnotation(Class annotatedClass) { 046 return (Check) annotatedClass.getAnnotation(Check.class); 047 } 048 049 public static List<Field> getPropertyFields(Class annotatedClass) { 050 List<Field> fields = new ArrayList<Field>(); 051 for (Field field : annotatedClass.getDeclaredFields()) { 052 org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class); 053 if (propertyAnnotation != null) { 054 fields.add(field); 055 } 056 } 057 return fields; 058 } 059 060 public static String getPropertyFieldKey(Field field) { 061 String key = null; 062 org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class); 063 if (propertyAnnotation != null) { 064 key = propertyAnnotation.key(); 065 if (key == null || "".equals(key)) { 066 key = field.getName(); 067 } 068 } 069 return key; 070 } 071 }