Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/annotation/Missing/MissingArrayElement/MissingClassArrayElementTest.java
41155 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 MissingClassArrayElementTest.java MissingClass.java MissingClass2.java
34
* @clean MissingClass MissingClass2
35
* @run main MissingClassArrayElementTest
36
*/
37
public class MissingClassArrayElementTest {
38
39
@Retention(RUNTIME)
40
@interface AnnotationAnnotation {
41
ClassArrayAnnotation[] value();
42
}
43
44
@Retention(RUNTIME)
45
@interface ClassArrayAnnotation {
46
Class<?>[] value();
47
}
48
49
@AnnotationAnnotation({
50
@ClassArrayAnnotation({MissingClass.class}),
51
@ClassArrayAnnotation({MissingClass.class, String.class}),
52
@ClassArrayAnnotation({String.class, MissingClass.class}),
53
@ClassArrayAnnotation({MissingClass.class, MissingClass2.class}),
54
@ClassArrayAnnotation({String.class})
55
})
56
static class Test {
57
void f(
58
@AnnotationAnnotation({
59
@ClassArrayAnnotation({MissingClass.class, MissingClass.class}),
60
@ClassArrayAnnotation({Float.class})
61
})
62
int x,
63
@AnnotationAnnotation({@ClassArrayAnnotation({Double.class})}) int y) {}
64
65
Test(
66
@AnnotationAnnotation({
67
@ClassArrayAnnotation({MissingClass.class, MissingClass.class}),
68
@ClassArrayAnnotation({Short.class}),
69
})
70
int x,
71
@AnnotationAnnotation({@ClassArrayAnnotation({Character.class})}) int y) {}
72
}
73
74
public static void main(String[] args) throws Exception {
75
classAnnotationTest();
76
methodParameterAnnotationsTest();
77
constructorParameterAnnotationsTest();
78
}
79
80
static void classAnnotationTest() throws Exception {
81
ClassArrayAnnotation[] outer = Test.class.getAnnotation(AnnotationAnnotation.class).value();
82
assertMissing(outer[0]);
83
assertMissing(outer[1]);
84
assertMissing(outer[2]);
85
assertMissing(outer[3]);
86
assertArrayEquals(outer[4].value(), new Class<?>[] {String.class});
87
}
88
89
static void methodParameterAnnotationsTest() throws Exception {
90
AnnotationAnnotation[] methodParameterAnnotations =
91
Arrays.stream(
92
Test.class
93
.getDeclaredMethod("f", int.class, int.class)
94
.getParameterAnnotations())
95
.map(x -> ((AnnotationAnnotation) x[0]))
96
.toArray(AnnotationAnnotation[]::new);
97
// The first parameter's annotation contains some well-formed values, and the second
98
// parameter's
99
// annotation is well-formed
100
assertArrayEquals(
101
methodParameterAnnotations[0].value()[1].value(), new Class<?>[] {Float.class});
102
assertArrayEquals(
103
methodParameterAnnotations[1].value()[0].value(), new Class<?>[] {Double.class});
104
// The first parameter's annotation contains a missing value
105
assertMissing(methodParameterAnnotations[0].value()[0]);
106
}
107
108
static void constructorParameterAnnotationsTest() throws Exception {
109
AnnotationAnnotation[] constructorParameterAnnotations =
110
Arrays.stream(
111
Test.class
112
.getDeclaredConstructor(int.class, int.class)
113
.getParameterAnnotations())
114
.map(x -> ((AnnotationAnnotation) x[0]))
115
.toArray(AnnotationAnnotation[]::new);
116
// The first parameter's annotation contains some well-formed values, and the second
117
// parameter's
118
// annotation is well-formed
119
assertArrayEquals(
120
constructorParameterAnnotations[0].value()[1].value(),
121
new Class<?>[] {Short.class});
122
assertArrayEquals(
123
constructorParameterAnnotations[1].value()[0].value(),
124
new Class<?>[] {Character.class});
125
// The first parameter's annotation contains a missing value
126
assertMissing(constructorParameterAnnotations[0].value()[0]);
127
}
128
129
static void assertArrayEquals(Object[] actual, Object[] expected) {
130
if (!Arrays.equals(actual, expected)) {
131
throw new AssertionError(
132
"expected: " + Arrays.toString(expected) + ", was: " + Arrays.toString(actual));
133
}
134
}
135
136
static void assertMissing(ClassArrayAnnotation missing) {
137
try {
138
missing.value();
139
throw new AssertionError("expected exception");
140
} catch (TypeNotPresentException expected) {
141
if (!expected.typeName().equals("MissingClass")) {
142
throw new AssertionError(
143
"expected TypeNotPresentException: MissingClass", expected);
144
}
145
}
146
}
147
}
148
149