-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathAnnotatable.java
More file actions
192 lines (147 loc) · 6.24 KB
/
Annotatable.java
File metadata and controls
192 lines (147 loc) · 6.24 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
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
class Annotatable {
@interface CustomAnnotation {}
@Inherited
@interface CustomInheritedAnnotation {
String value();
}
@CustomAnnotation
@CustomInheritedAnnotation("base")
class WithDeclared {
// Annotations on methods are not inherited
@CustomInheritedAnnotation("base-method")
void methodWithDeclared() {}
}
class Subclass extends WithDeclared {
@Override
void methodWithDeclared() {}
}
// Inheritance from super-superclass
class SubSubclass extends Subclass {}
// Prevents inheriting annotation of same type
@CustomInheritedAnnotation("sub")
class SubclassDeclaringSameAnnotation extends WithDeclared {}
// Annotations on interfaces are not inherited
@CustomInheritedAnnotation("base")
interface InterfaceWithDeclared {}
interface ExtendingInterface extends InterfaceWithDeclared {}
class ImplementingInterface implements InterfaceWithDeclared {}
@interface ContainerAnnotation {
RepeatableAnnotation[] value();
}
@Repeatable(ContainerAnnotation.class)
@interface RepeatableAnnotation {
String value();
}
@Inherited
@interface InheritedContainerAnnotation {
InheritedRepeatableAnnotation[] value();
}
@Inherited
@Repeatable(InheritedContainerAnnotation.class)
@interface InheritedRepeatableAnnotation {
String value();
}
@Inherited
@interface InheritedContainerAnnotation2 {
NonInheritedRepeatableAnnotation[] value();
}
// Container is marked as @Inherited, but this annotation type is not
// This is allowed, but means that associated annotations will not be inherited,
// see java.lang.reflect.AnnotatedElement documentation
@Repeatable(InheritedContainerAnnotation2.class)
@interface NonInheritedRepeatableAnnotation {
String value();
}
@RepeatableAnnotation("base")
@InheritedRepeatableAnnotation("base")
@NonInheritedRepeatableAnnotation("base")
class WithAssociatedSingle {}
@RepeatableAnnotation("sub-1")
@RepeatableAnnotation("sub-2")
@InheritedRepeatableAnnotation("sub-1")
@InheritedRepeatableAnnotation("sub-2")
@NonInheritedRepeatableAnnotation("sub-1")
@NonInheritedRepeatableAnnotation("sub-2")
class SubclassWithMultiple extends WithAssociatedSingle {}
// Empty container annotations have no effect; annotations are inherited from superclass
@InheritedContainerAnnotation({})
@InheritedContainerAnnotation2({})
class SubclassOfSingleWithEmptyContainer extends WithAssociatedSingle {}
// Annotations on interfaces are not inherited
@InheritedRepeatableAnnotation("base-1")
@InheritedRepeatableAnnotation("base-2")
interface InterfaceWithAssociated {}
interface ExtendingInterfaceWithAssociated extends InterfaceWithAssociated {}
class ImplementingInterfaceWithAssociated implements InterfaceWithAssociated {}
@RepeatableAnnotation("base-1")
@RepeatableAnnotation("base-2")
@InheritedRepeatableAnnotation("base-1")
@InheritedRepeatableAnnotation("base-2")
// These annotations are not inherited, but their (implicit) container annotation
// is inherited
@NonInheritedRepeatableAnnotation("base-1")
@NonInheritedRepeatableAnnotation("base-2")
class WithAssociatedMultiple {
// Annotations on methods are not inherited
@InheritedRepeatableAnnotation("base-method")
void methodWithAssociatedSingle() {}
// Annotations on methods are not inherited
@InheritedRepeatableAnnotation("base-method-1")
@InheritedRepeatableAnnotation("base-method-2")
void methodWithAssociated() {}
}
class WithAssociatedSubclass extends WithAssociatedMultiple {
@Override
void methodWithAssociatedSingle() {}
@Override
void methodWithAssociated() {}
}
// Inheritance from super-superclass
class WithAssociatedSubSubclass extends WithAssociatedSubclass {}
@RepeatableAnnotation("sub-1")
@InheritedRepeatableAnnotation("sub-1")
@NonInheritedRepeatableAnnotation("sub-1")
class SubclassWithSingle extends WithAssociatedMultiple {}
// Empty container annotations have no effect; associated annotations are inherited from superclass
@InheritedContainerAnnotation({})
@InheritedContainerAnnotation2({})
class SubclassOfMultipleWithEmptyContainer extends WithAssociatedMultiple {}
// This annotation exists on its own without a container
@InheritedRepeatableAnnotation("single")
// TODO: Has currently spurious results for ArrayInit due to https://github.com/github/codeql/issues/8647
@InheritedContainerAnnotation({
@InheritedRepeatableAnnotation("container-1"),
@InheritedRepeatableAnnotation("container-2")
})
class ExplicitContainerAndSingleContained {}
class ExplicitContainerSubclass extends ExplicitContainerAndSingleContained {}
@Inherited
@interface NestedAnnotationContainer1 {
NestedAnnotation1[] value();
}
@Inherited
@Repeatable(NestedAnnotationContainer1.class)
@interface NestedAnnotation1 {
NestedAnnotation2[] value();
}
@Inherited
@Repeatable(NestedAnnotation1.class)
@interface NestedAnnotation2 {
String value();
}
// This annotation exists on its own without a container
@NestedAnnotation2("1")
// But these are nested inside an implicit @NestedAnnotationContainer1
// Nested repeated annotations (@NestedAnnotation2) are not considered associated
@NestedAnnotation1({@NestedAnnotation2("1-1"), @NestedAnnotation2("1-2")})
@NestedAnnotation1({@NestedAnnotation2("2-1"), @NestedAnnotation2("2-2")})
class WithNestedAssociated {}
class WithNestedAssociatedSubclass extends WithNestedAssociated {}
// Nested repeated annotations (@NestedAnnotation2) are not considered associated
@NestedAnnotation1({@NestedAnnotation2("1-1"), @NestedAnnotation2("1-2")})
@NestedAnnotation1({@NestedAnnotation2("2-1"), @NestedAnnotation2("2-2")})
class WithNestedAssociatedExplicitContainers {}
class WithNestedAssociatedExplicitContainersSubclass extends WithNestedAssociatedExplicitContainers {}
}