|
1 | 1 | package org.mapstruct.tools.gem; |
2 | 2 |
|
| 3 | +import java.util.List; |
| 4 | +import java.util.function.Function; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.stream.Stream; |
| 7 | +import javax.lang.model.element.AnnotationMirror; |
3 | 8 | import javax.lang.model.element.AnnotationValue; |
| 9 | +import javax.lang.model.element.VariableElement; |
4 | 10 |
|
5 | 11 | /** |
6 | 12 | * Class representing a annotation value |
7 | 13 | * |
8 | 14 | * @param <T> the type represented by this annotation value |
9 | 15 | */ |
10 | | -public interface GemValue<T> { |
| 16 | +public class GemValue<T> { |
| 17 | + |
| 18 | + |
| 19 | + public static <V> GemValue<V> create(AnnotationValue annotationValue, |
| 20 | + AnnotationValue annotationDefaultValue, Class<V> valueClass) { |
| 21 | + V value = annotationValue == null ? null : valueClass.cast( annotationValue.getValue() ); |
| 22 | + V defaultValue = annotationDefaultValue == null ? null : valueClass.cast( annotationDefaultValue.getValue() ); |
| 23 | + return new GemValue<>(value, defaultValue, annotationValue); |
| 24 | + } |
| 25 | + |
| 26 | + public static <V> GemValue<List<V>> createArray(AnnotationValue annotationValue, |
| 27 | + AnnotationValue annotationDefaultValue, Class<V> valueClass) { |
| 28 | + List<V> value = extractListValues( annotationValue, valueClass, Function.identity() ); |
| 29 | + List<V> defaultValue = extractListValues( annotationDefaultValue, valueClass, Function.identity() ); |
| 30 | + |
| 31 | + return new GemValue<>(value, defaultValue, annotationValue); |
| 32 | + } |
| 33 | + |
| 34 | + public static GemValue<String> createEnum(AnnotationValue annotationValue, |
| 35 | + AnnotationValue annotationDefaultValue) { |
| 36 | + String value = annotationValue == null ? null : ( (VariableElement) annotationValue.getValue() ).getSimpleName().toString(); |
| 37 | + String defaultValue = annotationDefaultValue == null ? null : ( (VariableElement) annotationDefaultValue.getValue() ).getSimpleName().toString(); |
| 38 | + return new GemValue<>(value, defaultValue, annotationValue); |
| 39 | + } |
| 40 | + |
| 41 | + public static GemValue<List<String>> createEnumArray(AnnotationValue annotationValue, |
| 42 | + AnnotationValue annotationDefaultValue) { |
| 43 | + List<String> value = extractListValues( annotationValue, VariableElement.class, variableElement -> variableElement.getSimpleName().toString() ); |
| 44 | + List<String> defaultValue = extractListValues( annotationDefaultValue, VariableElement.class, variableElement -> variableElement.getSimpleName().toString() ); |
| 45 | + |
| 46 | + return new GemValue<>(value, defaultValue, annotationValue); |
| 47 | + } |
| 48 | + |
| 49 | + public static <V> GemValue<V> create(AnnotationValue annotationValue, AnnotationValue annotationDefaultValue, |
| 50 | + Function<AnnotationMirror, V> creator) { |
| 51 | + V value = annotationValue == null ? null : creator.apply( (AnnotationMirror) annotationValue.getValue() ); |
| 52 | + V defaultValue = annotationDefaultValue == null ? null : creator.apply( (AnnotationMirror) annotationDefaultValue.getValue() ); |
| 53 | + return new GemValue<>( value, defaultValue, annotationValue ); |
| 54 | + } |
| 55 | + |
| 56 | + public static <V> GemValue<List<V>> createArray(AnnotationValue annotationValue, AnnotationValue annotationDefaultValue, |
| 57 | + Function<AnnotationMirror, V> creator) { |
| 58 | + List<V> value = extractListValues( annotationValue, AnnotationMirror.class, creator ); |
| 59 | + List<V> defaultValue = extractListValues( annotationDefaultValue, AnnotationMirror.class, creator ); |
| 60 | + return new GemValue<>( value, defaultValue, annotationValue ); |
| 61 | + } |
| 62 | + |
| 63 | + private static <V, R> List<R> extractListValues(AnnotationValue annotationValue, Class<V> valueClass, |
| 64 | + Function<V, R> mapper) { |
| 65 | + List<R> value; |
| 66 | + if ( annotationValue != null ) { |
| 67 | + Object definedValue = annotationValue.getValue(); |
| 68 | + if ( definedValue instanceof List ) { |
| 69 | + value = toStream( (List) definedValue, valueClass ).map( mapper ).collect( Collectors.toList() ); |
| 70 | + } |
| 71 | + else { |
| 72 | + value = null; |
| 73 | + } |
| 74 | + } |
| 75 | + else { |
| 76 | + value = null; |
| 77 | + } |
| 78 | + return value; |
| 79 | + } |
| 80 | + |
| 81 | + private static <T> Stream<T> toStream(List<?> annotationValues, Class<T> clz ) { |
| 82 | + return annotationValues.stream() |
| 83 | + .filter( AnnotationValue.class::isInstance ) |
| 84 | + .map( AnnotationValue.class::cast ) |
| 85 | + .map( AnnotationValue::getValue ) |
| 86 | + .filter( clz::isInstance ) |
| 87 | + .map( clz::cast ); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + private final T value; |
| 92 | + private final T defaultValue; |
| 93 | + private final AnnotationValue annotationValue; |
| 94 | + |
| 95 | + private GemValue(T value, T defaultValue, AnnotationValue annotationValue) { |
| 96 | + this.value = value; |
| 97 | + this.defaultValue = defaultValue; |
| 98 | + this.annotationValue = annotationValue; |
| 99 | + } |
11 | 100 |
|
12 | 101 | /** |
13 | 102 | * The implied valued, the value set by the user, default value when not defined |
14 | 103 | * |
15 | 104 | * @return the implied value |
16 | 105 | */ |
17 | | - T get(); |
| 106 | + public T get() { |
| 107 | + return value != null ? value : defaultValue; |
| 108 | + } |
18 | 109 |
|
19 | 110 | /** |
20 | 111 | * The value set by the user |
21 | 112 | * |
22 | 113 | * @return the value, null when not set |
23 | 114 | */ |
24 | | - T getValue(); |
25 | | - |
| 115 | + public T getValue() { |
| 116 | + return value; |
| 117 | + } |
26 | 118 | /** |
27 | 119 | * The default value, as declared in the annotation |
28 | 120 | * |
29 | 121 | * @return the default value |
30 | 122 | */ |
31 | | - T getDefaultValue(); |
32 | | - |
| 123 | + public T getDefaultValue() { |
| 124 | + return defaultValue; |
| 125 | + } |
33 | 126 | /** |
34 | 127 | * The annotation value, e.g. for printing messages {@link javax.annotation.processing.Messager#printMessage} |
35 | 128 | * |
36 | 129 | * @return the annotation value (null when not set) |
37 | 130 | */ |
38 | | - AnnotationValue getAnnotationValue(); |
39 | | - |
| 131 | + public AnnotationValue getAnnotationValue() { |
| 132 | + return annotationValue; |
| 133 | + } |
40 | 134 | /** |
41 | | - * |
42 | 135 | * @return true a value is set by user |
43 | 136 | */ |
44 | | - boolean hasValue(); |
| 137 | + public boolean hasValue() { |
| 138 | + return value != null; |
| 139 | + } |
45 | 140 |
|
46 | 141 | /** |
47 | 142 | * An annotation set to be valid when set by user or a default value is present. |
48 | 143 | * |
49 | 144 | * @return true when valid |
50 | 145 | */ |
51 | | - boolean isValid(); |
| 146 | + public boolean isValid() { |
| 147 | + return value != null || defaultValue != null; |
| 148 | + } |
| 149 | + |
52 | 150 | } |
0 commit comments