Skip to content

Commit 58bdae3

Browse files
committed
annotation update
1 parent 4c258e8 commit 58bdae3

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

annotations/src/main/java/net/jbock/Parameter.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.lang.annotation.Retention;
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
7-
import java.util.function.Supplier;
87

98
/**
109
* <h3>Marker for parameter methods</h3>
@@ -56,8 +55,8 @@
5655
* <h3>Optional custom mapper</h3>
5756
*
5857
* <p>
59-
* The mapper is a {@link java.util.function.Supplier Supplier} that returns a
60-
* {@link java.util.function.Function Function&lt;String, X&gt;}.
58+
* The mapper is a either a {@link java.util.function.Function Function&lt;String, X&gt;}
59+
* or a {@link java.util.function.Supplier Supplier} that returns such a function.
6160
* The return value {@code X} is called the <em>mapper type</em>.
6261
* The parameter method must return {@code X}, or {@code Optional<X>} if the
6362
* parameter is {@link #optional()}, or {@code List<X>} if the parameter is
@@ -69,58 +68,57 @@
6968
* </p>
7069
*
7170
* <pre>{@code
72-
* class PositiveNumberMapper implements Supplier&lt;Function&lt;String, Integer&gt;&gt; {
73-
*
74-
* public Function&lt;String, Integer&gt; get() {
75-
* return s -> {
76-
* Integer r = Integer.valueOf(s);
77-
* if (r <= 0) {
78-
* throw new IllegalArgumentException("Positive number expected");
79-
* }
80-
* return r;
71+
* class PositiveNumberMapper implements Function<String, Integer> {
72+
*
73+
* public Integer apply(String s) {
74+
* Integer r = Integer.valueOf(s);
75+
* if (r <= 0) {
76+
* throw new IllegalArgumentException("Positive number expected");
8177
* }
78+
* return r;
8279
* }
8380
* }
8481
* }</pre>
8582
*
86-
* @return an optional mapper class
83+
* @return a mapper class
8784
*/
88-
Class<?> mappedBy() default Supplier.class;
85+
Class<?> mappedBy() default Object.class;
8986

9087
/**
9188
* <h3>Optional custom collector</h3>
9289
*
9390
* <p>
94-
* The supplier must return a {@link java.util.stream.Collector Collector&lt;M, ?, X&gt;}
95-
* where {@code X} is the parameter type, and {@code M} is the <em>mapper type</em>.
91+
* This is either a {@link java.util.stream.Collector Collector&lt;M, ?, X&gt;}
92+
* where {@code X} is the parameter type and {@code M} is the <em>mapper type</em>,
93+
* or a {@link java.util.function.Supplier Supplier} that returns such a collector.
9694
* </p>
9795
*
9896
* <p>
9997
* For example, the following collector creates a {@code Set}:
10098
* </p>
10199
*
102100
* <pre>{@code
103-
* class ToSetCollector&lt;E&gt; implements Supplier&lt;Collector&lt;E, ?, Set&lt;E&gt;&gt;&gt; {
101+
* class ToSetCollector<E>; implements Supplier<Collector<E, ?, Set<E>>> {
104102
*
105-
* public Collector&lt;E, ?, Set&lt;E&gt;&gt; get() {
103+
* public Collector<E, ?, Set<E>> get() {
106104
* return Collectors.toSet();
107105
* }
108106
* }
109107
* }</pre>
110108
*
111-
* @return an optional collector class
109+
* @return an collector class
112110
*/
113-
Class<?> collectedBy() default Supplier.class;
111+
Class<?> collectedBy() default Object.class;
114112

115113
/**
116-
* <p>Declares this parameter repeatable.</p>
114+
* <p>Declares this parameter as repeatable.</p>
117115
*
118116
* @return true if this parameter is repeatable
119117
*/
120118
boolean repeatable() default false;
121119

122120
/**
123-
* <p>Declares this parameter optional.</p>
121+
* <p>Declares this parameter as optional.</p>
124122
*
125123
* <p>
126124
* <em>Note:</em>
@@ -135,7 +133,7 @@
135133

136134
/**
137135
* <p>Declares a parameter that doesn't take an argument.
138-
* For example, the following shell command contains a flag:</p>
136+
* For example, the following shell command contains the flag {@code -l}:</p>
139137
*
140138
* <pre>{@code
141139
* ls -l
@@ -159,3 +157,4 @@
159157
*/
160158
String bundleKey() default "";
161159
}
160+

annotations/src/main/java/net/jbock/PositionalParameter.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,28 @@
5353
* Optional custom mapper.
5454
* See {@link Parameter#mappedBy()}.
5555
*
56-
* @return an optional mapper class
56+
* @return a mapper class
5757
*/
58-
Class<? extends Supplier> mappedBy() default Supplier.class;
58+
Class<?> mappedBy() default Object.class;
5959

6060
/**
6161
* Optional custom collector.
6262
* See {@link Parameter#collectedBy()}.
6363
*
64-
* @return an optional collector class
64+
* @return a collector class
6565
*/
66-
Class<? extends Supplier> collectedBy() default Supplier.class;
66+
Class<?> collectedBy() default Object.class;
6767

6868
/**
69-
* Declares this parameter repeatable.
69+
* Declares this parameter as repeatable.
7070
* See {@link Parameter#repeatable()}.
7171
*
7272
* @return true if this parameter is repeatable
7373
*/
7474
boolean repeatable() default false;
7575

7676
/**
77-
* Declares this parameter optional.
77+
* Declares this parameter as optional.
7878
* See {@link Parameter#optional()}.
7979
*
8080
* @return true if this parameter is optional
@@ -89,3 +89,4 @@
8989
*/
9090
String bundleKey() default "";
9191
}
92+

core/src/main/java/net/jbock/compiler/AnnotationUtil.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import javax.lang.model.element.AnnotationValueVisitor;
66
import javax.lang.model.element.ExecutableElement;
77
import javax.lang.model.element.TypeElement;
8+
import javax.lang.model.type.DeclaredType;
89
import javax.lang.model.type.TypeKind;
910
import javax.lang.model.type.TypeMirror;
11+
import javax.lang.model.type.TypeVisitor;
1012
import javax.lang.model.util.SimpleAnnotationValueVisitor8;
13+
import javax.lang.model.util.SimpleTypeVisitor8;
1114
import java.util.Map;
1215

1316
final class AnnotationUtil {
@@ -37,6 +40,20 @@ public TypeMirror visitType(TypeMirror mirror, AnnotationUtilContext utilContext
3740
}
3841
};
3942

43+
private static final TypeVisitor<Boolean, TypeTool> IS_JAVA_LANG_OBJECT = new SimpleTypeVisitor8<Boolean, TypeTool>() {
44+
@Override
45+
protected Boolean defaultAction(TypeMirror e, TypeTool tool) {
46+
return false;
47+
}
48+
49+
@Override
50+
public Boolean visitDeclared(DeclaredType type, TypeTool tool) {
51+
TypeElement element = tool.asTypeElement(type.asElement());
52+
return "java.lang.Object".equals(element.getQualifiedName().toString());
53+
}
54+
};
55+
56+
4057
private static TypeElement get(
4158
ExecutableElement sourceMethod,
4259
Class<?> annotationClass,
@@ -52,11 +69,16 @@ private static TypeElement get(
5269
return null;
5370
}
5471
TypeMirror typeMirror = annotationValue.accept(GET_TYPE, new AnnotationUtilContext(sourceMethod, attributeName));
72+
TypeTool tool = TypeTool.get();
73+
if (typeMirror.accept(IS_JAVA_LANG_OBJECT, tool)) {
74+
// if the default value is not overridden
75+
return null;
76+
}
5577
if (typeMirror.getKind() != TypeKind.DECLARED) {
5678
throw ValidationException.create(sourceMethod,
5779
String.format("Invalid value of '%s'.", attributeName));
5880
}
59-
return TypeTool.get().asTypeElement(typeMirror);
81+
return tool.asTypeElement(typeMirror);
6082
}
6183

6284

0 commit comments

Comments
 (0)