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.templates; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.slf4j.Logger; 024 import org.slf4j.LoggerFactory; 025 import org.sonar.check.AnnotationIntrospector; 026 027 import java.lang.reflect.Field; 028 import java.util.ArrayList; 029 import java.util.Collection; 030 import java.util.List; 031 032 /** 033 * @since 2.1 (experimental) 034 * @deprecated since 2.3 035 */ 036 @Deprecated 037 public class AnnotationCheckTemplateFactory { 038 039 private static final Logger LOG = LoggerFactory.getLogger(AnnotationCheckTemplateFactory.class); 040 041 private Collection<Class> annotatedClasses; 042 043 public AnnotationCheckTemplateFactory(Collection<Class> annotatedClasses) { 044 this.annotatedClasses = annotatedClasses; 045 } 046 047 public List<CheckTemplate> create() { 048 List<CheckTemplate> templates = new ArrayList<CheckTemplate>(); 049 for (Class annotatedClass : annotatedClasses) { 050 BundleCheckTemplate template = create(annotatedClass); 051 if (template != null) { 052 templates.add(template); 053 } 054 } 055 return templates; 056 } 057 058 059 protected BundleCheckTemplate create(Class annotatedClass) { 060 org.sonar.check.Check checkAnnotation = AnnotationIntrospector.getCheckAnnotation(annotatedClass); 061 if (checkAnnotation == null) { 062 LOG.warn("The class " + annotatedClass.getCanonicalName() + " is not a check template. It should be annotated with " + CheckTemplate.class); 063 return null; 064 } 065 066 BundleCheckTemplate check = toTemplate(annotatedClass, checkAnnotation); 067 Field[] fields = annotatedClass.getDeclaredFields(); 068 if (fields != null) { 069 for (Field field : fields) { 070 BundleCheckTemplateProperty property = toProperty(check, field); 071 if (property != null) { 072 check.addProperty(property); 073 } 074 } 075 } 076 return check; 077 } 078 079 private static BundleCheckTemplate toTemplate(Class annotatedClass, org.sonar.check.Check checkAnnotation) { 080 String key = AnnotationIntrospector.getCheckKey(annotatedClass); 081 String bundle = getBundleBaseName(checkAnnotation, annotatedClass); 082 083 BundleCheckTemplate check = new BundleCheckTemplate(key, bundle); 084 check.setDefaultDescription(checkAnnotation.description()); 085 check.setDefaultTitle(checkAnnotation.title()); 086 check.setIsoCategory(checkAnnotation.isoCategory()); 087 check.setPriority(checkAnnotation.priority()); 088 089 return check; 090 } 091 092 private static String getBundleBaseName(org.sonar.check.Check checkAnnotation, Class annotatedClass) { 093 String bundle = checkAnnotation.bundle(); 094 if (StringUtils.isBlank(bundle)) { 095 bundle = annotatedClass.getCanonicalName(); 096 } 097 return bundle; 098 } 099 100 private static BundleCheckTemplateProperty toProperty(BundleCheckTemplate check, Field field) { 101 org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class); 102 if (propertyAnnotation != null) { 103 String fieldKey = propertyAnnotation.key(); 104 if (fieldKey==null || "".equals(fieldKey)) { 105 fieldKey = field.getName(); 106 } 107 BundleCheckTemplateProperty property = new BundleCheckTemplateProperty(check, fieldKey); 108 property.setDefaultTitle(propertyAnnotation.title()); 109 property.setDefaultDescription(propertyAnnotation.description()); 110 return property; 111 } 112 return null; 113 } 114 }