001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.runtime.test.runner;
020
021import java.lang.annotation.Annotation;
022import java.lang.annotation.Inherited;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.HashSet;
026import java.util.Hashtable;
027import java.util.List;
028import java.util.Map;
029import java.util.Set;
030
031import com.google.common.base.Predicates;
032import com.google.common.collect.ImmutableList;
033import com.google.common.collect.Iterables;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038public class AnnotationScanner {
039
040    protected final Set<Class<?>> visitedClasses = new HashSet<Class<?>>();
041
042    protected final Map<Class<?>, List<Annotation>> classes = new Hashtable<Class<?>, List<Annotation>>();
043
044    public synchronized void scan(Class<?> clazz) {
045        if (classes.containsKey(clazz)) {
046            return;
047        }
048        collectAnnotations(clazz);
049    }
050
051    public List<? extends Annotation> getAnnotations(Class<?> clazz) {
052        if (!visitedClasses.contains(clazz)) {
053            scan(clazz);
054        }
055        return classes.get(clazz);
056    }
057
058    public <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationType) {
059        final List<T> annotations = getAnnotations(clazz, annotationType);
060        if (annotations.isEmpty()) {
061            return null;
062        }
063        return Defaults.of(annotationType, annotations);
064    }
065
066    public <T extends Annotation> T getFirstAnnotation(Class<?> clazz, Class<T> annotationType) {
067        List<T> result = getAnnotations(clazz, annotationType);
068        if (result.isEmpty()) {
069            return null;
070        }
071        return result.get(0);
072    }
073
074    @SuppressWarnings("unchecked")
075    public <T extends Annotation> List<T> getAnnotations(Class<?> clazz, Class<T> annotationType) {
076        if (!visitedClasses.contains(clazz)) {
077            scan(clazz);
078        }
079        return (List<T>) ImmutableList.copyOf(Iterables.filter(classes.get(clazz),
080                Predicates.instanceOf(annotationType)));
081    }
082
083    /**
084     * TODO when collecting annotations annotated with {@link Inherited} they will be collected twice.
085     *
086     * @param clazz
087     * @param result
088     * @param visitedClasses
089     */
090    protected List<Annotation> collectAnnotations(Class<?> clazz) {
091        if (visitedClasses.contains(clazz)) {
092            return classes.get(clazz);
093        }
094        visitedClasses.add(clazz);
095        List<Annotation> result = new ArrayList<Annotation>(); // collect only the annotation on this class
096        result.addAll(Arrays.asList(clazz.getAnnotations()));
097        // first scan interfaces
098        for (Class<?> itf : clazz.getInterfaces()) {
099            result.addAll(collectAnnotations(itf));
100        }
101        // collect annotations from super classes
102        Class<?> superClass = clazz.getSuperclass();
103        if (superClass != null) {
104            result.addAll(collectAnnotations(superClass));
105        }
106        classes.put(clazz, result);
107        return result;
108    }
109
110}