Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertySetClassesLoader |
|
|
2.5;2.5 |
1 | /* |
|
2 | * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package de.smartics.properties.spi.core.classpath; |
|
17 | ||
18 | import java.net.URL; |
|
19 | import java.net.URLClassLoader; |
|
20 | import java.util.Collection; |
|
21 | import java.util.Collections; |
|
22 | import java.util.Set; |
|
23 | ||
24 | import org.reflections.Reflections; |
|
25 | import org.reflections.scanners.TypeAnnotationsScanner; |
|
26 | import org.reflections.util.ConfigurationBuilder; |
|
27 | ||
28 | import de.smartics.properties.api.core.annotations.PropertySet; |
|
29 | ||
30 | /** |
|
31 | * Loads classes that have a {@link PropertySet} annotation. |
|
32 | */ |
|
33 | 0 | public final class PropertySetClassesLoader |
34 | { |
|
35 | // ********************************* Fields ********************************* |
|
36 | ||
37 | // --- constants ------------------------------------------------------------ |
|
38 | ||
39 | /** |
|
40 | * Default scanner to use. |
|
41 | */ |
|
42 | 0 | private static final TypeAnnotationsScanner SCANNER = |
43 | new TypeAnnotationsScanner(); |
|
44 | ||
45 | // --- members -------------------------------------------------------------- |
|
46 | ||
47 | // ****************************** Initializer ******************************* |
|
48 | ||
49 | // ****************************** Constructors ****************************** |
|
50 | ||
51 | // ****************************** Inner Classes ***************************** |
|
52 | ||
53 | // ********************************* Methods ******************************** |
|
54 | ||
55 | // --- init ----------------------------------------------------------------- |
|
56 | ||
57 | private static Reflections buildReflections(final Collection<URL> urls) |
|
58 | { |
|
59 | 0 | final ClassLoader classLoader = |
60 | new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread |
|
61 | .currentThread().getContextClassLoader()); |
|
62 | ||
63 | 0 | final ConfigurationBuilder builder = new ConfigurationBuilder(); |
64 | 0 | builder.setScanners(SCANNER).addClassLoader(classLoader).addUrls(urls); |
65 | 0 | final Reflections reflections = new Reflections(builder); |
66 | 0 | return reflections; |
67 | } |
|
68 | ||
69 | // --- get&set -------------------------------------------------------------- |
|
70 | ||
71 | // --- business ------------------------------------------------------------- |
|
72 | ||
73 | /** |
|
74 | * Returns all types that are annotated as property set. |
|
75 | * |
|
76 | * @param urls the class roots to search for types that have the required |
|
77 | * annotation. |
|
78 | * @return the set of types that are annotated with {@link PropertySet}. |
|
79 | */ |
|
80 | public Set<Class<?>> getPropertySetTypes(final Collection<URL> urls) |
|
81 | { |
|
82 | 0 | if (urls == null || urls.isEmpty()) |
83 | { |
|
84 | 0 | return Collections.emptySet(); |
85 | } |
|
86 | ||
87 | 0 | final Reflections reflections = buildReflections(urls); |
88 | 0 | final Set<Class<?>> propertySetTypes = |
89 | reflections.getTypesAnnotatedWith(PropertySet.class); |
|
90 | ||
91 | 0 | return propertySetTypes; |
92 | } |
|
93 | ||
94 | // --- object basics -------------------------------------------------------- |
|
95 | ||
96 | } |