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 035import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner; 036 037/** 038 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 039 */ 040public class AnnotationScanner { 041 042 protected final Set<Class<?>> visitedClasses = new HashSet<Class<?>>(); 043 044 protected final Map<Class<?>, List<Annotation>> classes = new Hashtable<Class<?>, List<Annotation>>(); 045 046 public synchronized void scan(Class<?> clazz) { 047 if (classes.containsKey(clazz)) { 048 return; 049 } 050 collectAnnotations(clazz); 051 } 052 053 public List<? extends Annotation> getAnnotations(Class<?> clazz) { 054 if (!visitedClasses.contains(clazz)) { 055 scan(clazz); 056 } 057 return classes.get(clazz); 058 } 059 060 public <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationType) { 061 final List<T> annotations = getAnnotations(clazz, annotationType); 062 if (annotations.isEmpty()) { 063 return null; 064 } 065 return Defaults.of(annotationType, annotations); 066 } 067 068 public <T extends Annotation> T getFirstAnnotation(Class<?> clazz, Class<T> annotationType) { 069 List<T> result = getAnnotations(clazz, annotationType); 070 if (result.isEmpty()) { 071 return null; 072 } 073 return result.get(0); 074 } 075 076 @SuppressWarnings("unchecked") 077 public <T extends Annotation> List<T> getAnnotations(Class<?> clazz, Class<T> annotationType) { 078 if (!visitedClasses.contains(clazz)) { 079 scan(clazz); 080 } 081 return (List<T>) ImmutableList.copyOf(Iterables.filter(classes.get(clazz), 082 Predicates.instanceOf(annotationType))); 083 } 084 085 /** 086 * TODO when collecting annotations annotated with {@link Inherited} they will be collected twice. 087 * 088 * @param clazz 089 * @param result 090 * @param visitedClasses 091 */ 092 protected List<Annotation> collectAnnotations(Class<?> clazz) { 093 if (visitedClasses.contains(clazz)) { 094 return classes.get(clazz); 095 } 096 visitedClasses.add(clazz); 097 List<Annotation> result = new ArrayList<Annotation>(); // collect only the annotation on this class 098 try { 099 Annotation[] data = clazz.getAnnotations(); 100 result.addAll(Arrays.asList(data)); 101 } catch (ArrayStoreException cause) { 102 throw new AssertionError( 103 "Cannot load annotations of " + clazz.getName() + ", check your classpath for missing classes\n" + new FastClasspathScanner().getUniqueClasspathElements(), 104 cause); 105 } 106 // first scan interfaces 107 for (Class<?> itf : clazz.getInterfaces()) { 108 result.addAll(collectAnnotations(itf)); 109 } 110 // collect annotations from super classes 111 Class<?> superClass = clazz.getSuperclass(); 112 if (superClass != null) { 113 result.addAll(collectAnnotations(superClass)); 114 } 115 classes.put(clazz, result); 116 return result; 117 } 118 119}