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.classgraph.ClassGraph; 036 037/** 038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 039 */ 040public class AnnotationScanner { 041 042 protected final Set<Class<?>> visitedClasses = new HashSet<>(); 043 044 protected final Map<Class<?>, List<Annotation>> classes = new Hashtable<>(); 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 protected List<Annotation> collectAnnotations(Class<?> clazz) { 089 if (visitedClasses.contains(clazz)) { 090 return classes.get(clazz); 091 } 092 visitedClasses.add(clazz); 093 List<Annotation> result = new ArrayList<>(); // collect only the annotation on this class 094 try { 095 Annotation[] data = clazz.getAnnotations(); 096 result.addAll(Arrays.asList(data)); 097 } catch (ArrayStoreException cause) { 098 String classpathFiles = new ClassGraph().getClasspathFiles().toString(); 099 throw new AssertionError("Cannot load annotations of " + clazz.getName() 100 + ", check your classpath for missing classes\n" + classpathFiles, cause); 101 } 102 // first scan interfaces 103 for (Class<?> itf : clazz.getInterfaces()) { 104 result.addAll(collectAnnotations(itf)); 105 } 106 // collect annotations from super classes 107 Class<?> superClass = clazz.getSuperclass(); 108 if (superClass != null) { 109 result.addAll(collectAnnotations(superClass)); 110 } 111 classes.put(clazz, result); 112 return result; 113 } 114 115}