| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.config; |
| 2 | |
| 3 | import org.reflections.Reflections; |
| 4 | |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 5 | import java.lang.annotation.Annotation; |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 6 | import java.lang.reflect.ParameterizedType; |
| 7 | import java.lang.reflect.Type; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 8 | import java.util.Set; |
| 9 | |
| 10 | /** |
| 11 | * @author hanl |
| 12 | * @date 10/06/2015 |
| 13 | */ |
| 14 | public class KustvaktClassLoader { |
| 15 | |
| 16 | private static final Reflections reflections = new Reflections( |
| 17 | "de.ids_mannheim.korap"); |
| 18 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 19 | |
| 20 | private KustvaktClassLoader () {} |
| 21 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 22 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 23 | /** |
| 24 | * loads interface implementations in current classpath |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 25 | * |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 26 | * @param iface |
| 27 | * @param <T> |
| 28 | * @return |
| 29 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 30 | public static <T> Set<Class<? extends T>> loadSubTypes (Class<T> iface) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 31 | return reflections.getSubTypesOf(iface); |
| 32 | } |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 33 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 34 | |
| 35 | public static Set<Class<?>> loadFromAnnotation ( |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 36 | Class<? extends Annotation> annotation) { |
| 37 | return reflections.getTypesAnnotatedWith(annotation); |
| 38 | } |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 39 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 40 | |
| 41 | public static <T> Class<? extends T> getTypeClass (Class type, |
| 42 | Class<T> iface) { |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 43 | Set<Class<? extends T>> c = KustvaktClassLoader.loadSubTypes(iface); |
| 44 | for (Class<? extends T> o : c) { |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 45 | Type ctype = o.getGenericInterfaces()[0]; |
| 46 | if (ctype instanceof ParameterizedType) { |
| 47 | ParameterizedType ptype = (ParameterizedType) ctype; |
| 48 | Class tclass = (Class) ptype.getActualTypeArguments()[0]; |
| 49 | if (tclass.equals(type)) |
| 50 | return o; |
| 51 | } |
| 52 | } |
| 53 | return null; |
| 54 | } |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 55 | } |