forked from mapstruct/tools-gem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGemValue.java
More file actions
193 lines (163 loc) · 7.36 KB
/
GemValue.java
File metadata and controls
193 lines (163 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.tools.gem;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.SimpleAnnotationValueVisitor8;
/**
* Class representing a annotation value
*
* @param <T> the type represented by this annotation value
*/
public class GemValue<T> {
public static <V> GemValue<V> create(AnnotationValue annotationValue,
AnnotationValue annotationDefaultValue, Class<V> valueClass) {
ValueAnnotationValueVisitor<V, V> visitor = new ValueAnnotationValueVisitor<>( Function.identity() );
V value = visit( annotationValue, visitor, valueClass );
V defaultValue = visit( annotationDefaultValue, visitor, valueClass );
return new GemValue<>( value, defaultValue, annotationValue );
}
public static <V> GemValue<List<V>> createArray(AnnotationValue annotationValue,
AnnotationValue annotationDefaultValue, Class<V> valueClass) {
ValueAnnotationValueListVisitor<V, V> visitor = new ValueAnnotationValueListVisitor<>( Function.identity() );
List<V> value = visitList( annotationValue, visitor, valueClass );
List<V> defaultValue = visitList( annotationDefaultValue, visitor, valueClass );
return new GemValue<>( value, defaultValue, annotationValue );
}
public static GemValue<String> createEnum(AnnotationValue annotationValue,
AnnotationValue annotationDefaultValue) {
ValueAnnotationValueVisitor<VariableElement, String> visitor = new ValueAnnotationValueVisitor<>(
variableElement -> variableElement.getSimpleName().toString() );
String value = visit( annotationValue, visitor, VariableElement.class );
String defaultValue = visit( annotationDefaultValue, visitor, VariableElement.class );
return new GemValue<>( value, defaultValue, annotationValue );
}
public static GemValue<List<String>> createEnumArray(AnnotationValue annotationValue,
AnnotationValue annotationDefaultValue) {
ValueAnnotationValueListVisitor<VariableElement, String> visitor = new ValueAnnotationValueListVisitor<>(
variableElement -> variableElement.getSimpleName().toString() );
List<String> value = visitList( annotationValue, visitor, VariableElement.class );
List<String> defaultValue = visitList( annotationDefaultValue, visitor, VariableElement.class );
return new GemValue<>( value, defaultValue, annotationValue );
}
public static <V> GemValue<V> create(AnnotationValue annotationValue, AnnotationValue annotationDefaultValue,
Function<AnnotationMirror, V> creator) {
ValueAnnotationValueVisitor<AnnotationMirror, V> visitor = new ValueAnnotationValueVisitor<>( creator );
V value = visit( annotationValue, visitor, AnnotationMirror.class );
V defaultValue = visit( annotationDefaultValue, visitor, AnnotationMirror.class );
return new GemValue<>( value, defaultValue, annotationValue );
}
public static <V> GemValue<List<V>> createArray(AnnotationValue annotationValue,
AnnotationValue annotationDefaultValue,
Function<AnnotationMirror, V> creator) {
ValueAnnotationValueListVisitor<AnnotationMirror, V> visitor = new ValueAnnotationValueListVisitor<>( creator );
List<V> value = visitList( annotationValue, visitor, AnnotationMirror.class );
List<V> defaultValue = visitList( annotationDefaultValue, visitor, AnnotationMirror.class );
return new GemValue<>( value, defaultValue, annotationValue );
}
private static <V, R> R visit(AnnotationValue annotationValue,
AnnotationValueVisitor<R, Class<V>> visitor, Class<V> vClass) {
return annotationValue == null ? null : annotationValue.accept( visitor, vClass );
}
private static <V, R> List<R> visitList(AnnotationValue annotationValue,
AnnotationValueVisitor<List<R>, Class<V>> visitor,
Class<V> vClass) {
return annotationValue == null ? null : annotationValue.accept( visitor, vClass );
}
private final T value;
private final T defaultValue;
private final AnnotationValue annotationValue;
private GemValue(T value, T defaultValue, AnnotationValue annotationValue) {
this.value = value;
this.defaultValue = defaultValue;
this.annotationValue = annotationValue;
}
/**
* The implied valued, the value set by the user, default value when not defined
*
* @return the implied value
*/
public T get() {
return value != null ? value : defaultValue;
}
/**
* The value set by the user
*
* @return the value, null when not set
*/
public T getValue() {
return value;
}
/**
* The default value, as declared in the annotation
*
* @return the default value
*/
public T getDefaultValue() {
return defaultValue;
}
/**
* The annotation value, e.g. for printing messages {@link javax.annotation.processing.Messager#printMessage}
*
* @return the annotation value (null when not set)
*/
public AnnotationValue getAnnotationValue() {
return annotationValue;
}
/**
* @return true a value is set by user
*/
public boolean hasValue() {
return value != null;
}
/**
* An annotation set to be valid when set by user or a default value is present.
*
* @return true when valid
*/
public boolean isValid() {
return value != null || defaultValue != null;
}
private static class ValueAnnotationValueVisitor<V, R> extends SimpleAnnotationValueVisitor8<R, Class<V>> {
private final Function<V, R> valueMapper;
private ValueAnnotationValueVisitor(Function<V, R> valueMapper) {
this.valueMapper = valueMapper;
}
@Override
protected R defaultAction(Object o, Class<V> vClass) {
if ( o == null ) {
return null;
}
else if ( vClass.isInstance( o ) ) {
return valueMapper.apply( vClass.cast( o ) );
}
else {
return null;
}
}
}
private static class ValueAnnotationValueListVisitor<V, R>
extends SimpleAnnotationValueVisitor8<List<R>, Class<V>> {
private final ValueAnnotationValueVisitor<V, R> arrayVisitor;
private ValueAnnotationValueListVisitor(Function<V, R> valueMapper) {
arrayVisitor = new ValueAnnotationValueVisitor<>( valueMapper );
}
@Override
public List<R> visitArray(List<? extends AnnotationValue> vals, Class<V> vClass) {
List<R> values = new ArrayList<>( vals.size() );
for ( AnnotationValue val : vals ) {
R value = val.accept( arrayVisitor, vClass );
values.add( value );
}
return values;
}
}
}