1 /*
2 * Copyright 2010-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.testdoc.collect.extractor;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.lang.model.element.AnnotationMirror;
23 import javax.lang.model.element.AnnotationValue;
24 import javax.lang.model.element.Element;
25 import javax.lang.model.element.ExecutableElement;
26 import javax.lang.model.element.TypeElement;
27
28 import org.junit.experimental.categories.Category;
29
30 /**
31 * Extracts category information.
32 *
33 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34 * @version $Revision:591 $
35 */
36 class CategoryExtractor
37 {
38 // ********************************* Fields *********************************
39
40 // --- constants ------------------------------------------------------------
41
42 // --- members --------------------------------------------------------------
43
44 /**
45 * The type element of the enclosing unit test class.
46 */
47 private final TypeElement unitTestType;
48
49 // ****************************** Initializer *******************************
50
51 // ****************************** Constructors ******************************
52
53 /**
54 * Default constructor.
55 *
56 * @param unitTestType the type element of the enclosing unit test class.
57 */
58 CategoryExtractor(final TypeElement unitTestType)
59 {
60 this.unitTestType = unitTestType;
61 }
62
63 // ****************************** Inner Classes *****************************
64
65 // ********************************* Methods ********************************
66
67 // --- init -----------------------------------------------------------------
68
69 // --- get&set --------------------------------------------------------------
70
71 // --- business -------------------------------------------------------------
72
73 List<String> calcCategories(final Element element)
74 {
75 final List<String> categories = new ArrayList<String>();
76
77 if (unitTestType != null)
78 {
79 for (final AnnotationMirror annotation : unitTestType
80 .getAnnotationMirrors())
81 {
82 addCategories(categories, annotation);
83 }
84 }
85 for (final AnnotationMirror annotation : element.getAnnotationMirrors())
86 {
87 addCategories(categories, annotation);
88 }
89
90 return categories;
91 }
92
93 private static void addCategories(final List<String> categories,
94 final AnnotationMirror annotation)
95 {
96 if (Category.class.getName().equals(
97 annotation.getAnnotationType().toString()))
98 {
99 final Map<? extends ExecutableElement, ? extends AnnotationValue> map =
100 annotation.getElementValues();
101 for (final Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : map
102 .entrySet())
103 {
104 addCategories(categories, entry);
105 }
106 }
107 }
108
109 private static void addCategories(
110 final List<String> categories,
111 final Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry)
112 {
113 final String key = entry.getKey().toString();
114 if (key != null && key.trim().endsWith("value()"))
115 {
116 final Object values = entry.getValue().getValue();
117 if (values instanceof Iterable)
118 {
119 for (final Object value : (Iterable<?>) values)
120 {
121 final String valueString = value.toString();
122 final String name = TestDocAnnotationExtractor.typeNameFix(valueString);
123 categories.add(name);
124 }
125 }
126 }
127 }
128
129 // --- object basics --------------------------------------------------------
130
131 }