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.checkers;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.sonar.api.checks.profiles.Check;
024    import org.sonar.api.checks.profiles.CheckProfile;
025    import org.sonar.check.AnnotationIntrospector;
026    import org.sonar.check.CheckProperty;
027    
028    import java.lang.reflect.Field;
029    import java.util.Collection;
030    import java.util.HashMap;
031    import java.util.IdentityHashMap;
032    import java.util.Map;
033    
034    /**
035     * @since 2.1
036     */
037    public class AnnotationCheckerFactory<CHECKER> extends CheckerFactory<CHECKER> {
038    
039      private CheckProfile profile;
040      private String repositoryKey;
041      private Collection<Class<CHECKER>> checkerClasses;
042    
043      public AnnotationCheckerFactory(CheckProfile profile, String repositoryKey, Collection<Class<CHECKER>> checkerClasses) {
044        this.profile = profile;
045        this.repositoryKey = repositoryKey;
046        this.checkerClasses = checkerClasses;
047      }
048    
049      public Map<Check, CHECKER> create() {
050        Map<String, Class<CHECKER>> classesByKey = getClassesByKey(checkerClasses);
051    
052        Map<Check, CHECKER> map = new IdentityHashMap<Check, CHECKER>();
053        for (Check check : profile.getChecks(repositoryKey)) {
054          Class<CHECKER> clazz = classesByKey.get(check.getTemplateKey());
055          if (clazz != null) {
056            CHECKER checker = instantiate(check, clazz);
057            if (checker != null) {
058              map.put(check, checker);
059            }
060          }
061        }
062        return map;
063      }
064    
065      CHECKER instantiate(Check check, Class<CHECKER> clazz) {
066        try {
067          CHECKER checker = clazz.newInstance();
068          configureFields(check, checker);
069          return checker;
070    
071        } catch (UnvalidCheckerException e) {
072          throw e;
073    
074        } catch (Exception e) {
075          throw new UnvalidCheckerException("The checker " + clazz.getCanonicalName() + " can not be created", e);
076        }
077      }
078    
079      private void configureFields(Check check, CHECKER checker) throws IllegalAccessException {
080        for (Map.Entry<String, String> entry : check.getProperties().entrySet()) {
081          Field field = getField(checker, entry.getKey());
082          if (field == null) {
083            throw new UnvalidCheckerException("The field " + entry.getKey() + " does not exist or is not annotated with @CheckProperty");
084          }
085          if (StringUtils.isNotBlank(entry.getValue())) {
086            configureField(checker, field, entry);
087          }      
088        }
089    
090      }
091    
092      private void configureField(Object checker, Field field, Map.Entry<String, String> parameter) throws IllegalAccessException {
093        field.setAccessible(true);
094    
095        if (field.getType().equals(String.class)) {
096          field.set(checker, parameter.getValue());
097    
098        } else if (field.getType().getSimpleName().equals("int")) {
099          field.setInt(checker, Integer.parseInt(parameter.getValue()));
100    
101        } else if (field.getType().getSimpleName().equals("short")) {
102          field.setShort(checker, Short.parseShort(parameter.getValue()));
103    
104        } else if (field.getType().getSimpleName().equals("long")) {
105          field.setLong(checker, Long.parseLong(parameter.getValue()));
106    
107        } else if (field.getType().getSimpleName().equals("double")) {
108          field.setDouble(checker, Double.parseDouble(parameter.getValue()));
109    
110        } else if (field.getType().getSimpleName().equals("boolean")) {
111          field.setBoolean(checker, Boolean.parseBoolean(parameter.getValue()));
112    
113        } else if (field.getType().getSimpleName().equals("byte")) {
114          field.setByte(checker, Byte.parseByte(parameter.getValue()));
115    
116        } else if (field.getType().equals(Integer.class)) {
117          field.set(checker, new Integer(Integer.parseInt(parameter.getValue())));
118    
119        } else if (field.getType().equals(Long.class)) {
120          field.set(checker, new Long(Long.parseLong(parameter.getValue())));
121    
122        } else if (field.getType().equals(Double.class)) {
123          field.set(checker, new Double(Double.parseDouble(parameter.getValue())));
124    
125        } else if (field.getType().equals(Boolean.class)) {
126          field.set(checker, Boolean.valueOf(Boolean.parseBoolean(parameter.getValue())));
127    
128        } else {
129          throw new UnvalidCheckerException("The type of the field " + field + " is not supported: " + field.getType());
130        }
131      }
132    
133      private Field getField(Object checker, String key) {
134        Field[] fields = checker.getClass().getDeclaredFields();
135        for (Field field : fields) {
136          CheckProperty annotation = field.getAnnotation(CheckProperty.class);
137          if (annotation != null) {
138            if (key.equals(field.getName()) || key.equals(annotation.key())) {
139              return field;
140            }
141          }
142        }
143        return null;
144      }
145    
146      private Map<String, Class<CHECKER>> getClassesByKey(Collection<Class<CHECKER>> checkerClasses) {
147        Map<String, Class<CHECKER>> result = new HashMap<String, Class<CHECKER>>();
148        for (Class<CHECKER> checkerClass : checkerClasses) {
149          String key = AnnotationIntrospector.getCheckKey(checkerClass);
150          if (key != null) {
151            result.put(key, checkerClass);
152          }
153        }
154        return result;
155      }
156    
157    }