Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/annotation/Missing/MissingArrayElement/MissingEnumArrayElementTest.java
41154 views
1
/*
2
* Copyright (c) 2018, Google Inc. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import static java.lang.annotation.RetentionPolicy.RUNTIME;
25
26
import java.lang.annotation.Retention;
27
import java.util.Arrays;
28
29
/*
30
* @test
31
* @bug 7183985
32
* @summary getAnnotation() throws an ArrayStoreException when the annotation class not present
33
* @compile MissingEnumArrayElementTest.java EnumToCompileAgainst.java
34
* @clean Enum
35
* @compile EnumToRunAgainst.java
36
* @run main MissingEnumArrayElementTest
37
*/
38
public class MissingEnumArrayElementTest {
39
40
@Retention(RUNTIME)
41
@interface AnnotationAnnotation {
42
EnumArrayAnnotation[] value();
43
}
44
45
@Retention(RUNTIME)
46
@interface EnumArrayAnnotation {
47
Enum[] value();
48
}
49
50
@AnnotationAnnotation({
51
@EnumArrayAnnotation({Enum.TWO}),
52
@EnumArrayAnnotation({Enum.ONE, Enum.TWO}),
53
@EnumArrayAnnotation({Enum.TWO, Enum.ONE}),
54
@EnumArrayAnnotation({Enum.TWO, Enum.THREE}),
55
@EnumArrayAnnotation({Enum.ONE}),
56
})
57
static class Test {
58
void f(
59
@AnnotationAnnotation({
60
@EnumArrayAnnotation({Enum.TWO, Enum.ONE}),
61
@EnumArrayAnnotation({Enum.ONE})
62
})
63
int x,
64
@AnnotationAnnotation({@EnumArrayAnnotation({Enum.ONE})}) int y) {}
65
66
Test(
67
@AnnotationAnnotation({
68
@EnumArrayAnnotation({Enum.TWO, Enum.ONE}),
69
@EnumArrayAnnotation({Enum.ONE})
70
})
71
int x,
72
@AnnotationAnnotation({@EnumArrayAnnotation({Enum.ONE})}) int y) {}
73
}
74
75
public static void main(String[] args) throws Exception {
76
classAnnotationTest();
77
methodParameterAnnotationsTest();
78
constructorParameterAnnotationsTest();
79
}
80
81
static void classAnnotationTest() throws Exception {
82
EnumArrayAnnotation[] outer = Test.class.getAnnotation(AnnotationAnnotation.class).value();
83
assertMissing(outer[0]);
84
assertMissing(outer[1]);
85
assertMissing(outer[2]);
86
assertMissing(outer[3]);
87
assertArrayEquals(outer[4].value(), new Enum[] {Enum.ONE});
88
}
89
90
static void methodParameterAnnotationsTest() throws Exception {
91
AnnotationAnnotation[] methodParameterAnnotations =
92
Arrays.stream(
93
Test.class
94
.getDeclaredMethod("f", int.class, int.class)
95
.getParameterAnnotations())
96
.map(x -> ((AnnotationAnnotation) x[0]))
97
.toArray(AnnotationAnnotation[]::new);
98
// The first parameter's annotation contains some well-formed values, and the second
99
// parameter's
100
// annotation is well-formed
101
assertArrayEquals(methodParameterAnnotations[0].value()[1].value(), new Enum[] {Enum.ONE});
102
assertArrayEquals(methodParameterAnnotations[1].value()[0].value(), new Enum[] {Enum.ONE});
103
// The first parameter's annotation contains a missing value
104
assertMissing(methodParameterAnnotations[0].value()[0]);
105
}
106
107
static void constructorParameterAnnotationsTest() throws Exception {
108
AnnotationAnnotation[] constructorParameterAnnotations =
109
Arrays.stream(
110
Test.class
111
.getDeclaredConstructor(int.class, int.class)
112
.getParameterAnnotations())
113
.map(x -> ((AnnotationAnnotation) x[0]))
114
.toArray(AnnotationAnnotation[]::new);
115
// The first parameter's annotation contains some well-formed values, and the second
116
// parameter's
117
// annotation is well-formed
118
assertArrayEquals(constructorParameterAnnotations[0].value()[1].value(), new Enum[] {Enum.ONE});
119
assertArrayEquals(constructorParameterAnnotations[1].value()[0].value(), new Enum[] {Enum.ONE});
120
// The first parameter's annotation contains a missing value
121
assertMissing(constructorParameterAnnotations[0].value()[0]);
122
}
123
124
static void assertArrayEquals(Object[] actual, Object[] expected) {
125
if (!Arrays.equals(actual, expected)) {
126
throw new AssertionError(
127
"expected: " + Arrays.toString(expected) + ", was: " + Arrays.toString(actual));
128
}
129
}
130
131
static void assertMissing(EnumArrayAnnotation annotation) throws Exception {
132
try {
133
annotation.value();
134
throw new AssertionError("expected exception");
135
} catch (EnumConstantNotPresentException expected) {
136
if (!expected.getMessage().equals("Enum.TWO")) {
137
throw new AssertionError(
138
"expected EnumConstantNotPresentException for Enum.TWO", expected);
139
}
140
}
141
}
142
}
143
144