-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotation.java
More file actions
92 lines (73 loc) · 2.79 KB
/
Copy pathAnnotation.java
File metadata and controls
92 lines (73 loc) · 2.79 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
package jtree.nodes;
import static jtree.util.Utils.*;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import jtree.util.Either;
import jtree.util.Utils;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.NonNull;
@EqualsAndHashCode
@Getter @Setter
public class Annotation extends Node implements AnnotationValue {
protected GenericType type;
protected Optional<Either<? extends List<AnnotationArgument>, ? extends AnnotationValue>> arguments;
public Annotation(GenericType type) {
this(type, Optional.empty());
}
public Annotation(GenericType type, @NonNull List<AnnotationArgument> arguments) {
this(type, Either.first(arguments));
}
public Annotation(GenericType type, @NonNull AnnotationValue value) {
this(type, Either.second(value));
}
public Annotation(GenericType type, Either<? extends List<AnnotationArgument>, ? extends AnnotationValue> arguments) {
this(type, Optional.of(arguments));
}
public Annotation(GenericType type, Optional<Either<? extends List<AnnotationArgument>, ? extends AnnotationValue>> arguments) {
setType(type);
setArguments(arguments);
}
@Override
public Annotation clone() {
return new Annotation(getType().clone(), clone(getArguments()));
}
@Override
public String toCode() {
return "@" + getType().toCode()
+ getArguments().map(either -> either.unravel(nodes -> "(" + joinNodes(", ", nodes) + ")",
value -> "(" + value.toCode() + ")")
).orElse("");
}
public void setArguments(@NonNull Optional<Either<? extends List<AnnotationArgument>, ? extends AnnotationValue>> arguments) {
this.arguments = arguments.map(either -> either.map(Utils::newList, Objects::requireNonNull));
}
public final void setArguments(Either<? extends List<AnnotationArgument>, ? extends AnnotationValue> arguments) {
setArguments(Optional.of(arguments));
}
public final void setArguments(@NonNull List<AnnotationArgument> arguments) {
setArguments(Either.first(arguments));
}
public final void setArguments(@NonNull AnnotationValue value) {
setArguments(Either.second(value));
}
public final void setArguments(AnnotationArgument... arguments) {
setArguments(List.of(arguments));
}
public final void setArguments() {
setArguments(Optional.empty());
}
@Override
public <N extends INode> void accept(TreeVisitor visitor, Node parent, Consumer<N> replacer) {
if(visitor.visitAnnotation(this, parent, cast(replacer))) {
getType().accept(visitor, this, this::setType);
getArguments().ifPresent(either -> either.accept(
list -> visitList(visitor, list),
value -> value.accept(visitor, this, (AnnotationValue replacement) -> setArguments(replacement))
));
}
}
}