Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/Class/getFields/Sanity.java
41153 views
1
/*
2
* Copyright 2014 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
/* @test
25
* @bug 8063147
26
* @summary Tests for Class.getFields().
27
* @run testng Sanity
28
*/
29
30
import java.lang.reflect.Field;
31
import java.util.ArrayList;
32
import java.util.Arrays;
33
import java.util.List;
34
import org.testng.annotations.Test;
35
import static org.testng.Assert.*;
36
37
public class Sanity {
38
public interface EmptyInterface {}
39
class EmptyClass {}
40
interface BI1 {
41
public int i = 1;
42
int j = 2;
43
}
44
interface BI2 {
45
int k = 1;
46
}
47
public interface DI extends BI1, BI2, EmptyInterface {
48
int m = 5;
49
}
50
interface DDI extends DI {
51
int n = 6;
52
}
53
54
public class D extends EmptyClass {
55
public int publicDField;
56
protected int protectedDField;
57
private int privateDField;
58
}
59
60
class DD extends D {
61
public int publicDDField;
62
protected int protectedDDField;
63
private int privateDDField;
64
}
65
66
public class Universe extends DD implements DDI {
67
public int publicUniverseField;
68
protected int protectedUniverseField;
69
private int privateUniverseField;
70
}
71
72
void assertContainsNoFields(Class<?> clazz) {
73
assertEquals(clazz.getFields().length, 0);
74
}
75
76
@Test
77
public void primitiveTypesHaveNoFields() throws Exception {
78
assertContainsNoFields(byte.class);
79
assertContainsNoFields(char.class);
80
assertContainsNoFields(short.class);
81
assertContainsNoFields(int.class);
82
assertContainsNoFields(long.class);
83
assertContainsNoFields(boolean.class);
84
assertContainsNoFields(void.class);
85
assertContainsNoFields(double.class);
86
assertContainsNoFields(float.class);
87
}
88
89
@Test
90
public void arrayTypesHaveNoFields() throws Exception {
91
assertContainsNoFields(byte[].class);
92
assertContainsNoFields(Object[].class);
93
assertContainsNoFields(java.util.Map[].class);
94
assertContainsNoFields(java.util.HashMap[].class);
95
}
96
97
@Test
98
public void emptyInterfacesHaveNoFields() throws Exception {
99
assertContainsNoFields(EmptyInterface.class);
100
}
101
102
@Test
103
public void emptyClassesHaveNoFields() throws Exception {
104
assertContainsNoFields(EmptyClass.class);
105
class EmptyLocalClass {}
106
assertContainsNoFields(EmptyLocalClass.class);
107
}
108
109
void assertContainsFields(Class<?> clazz, int count) {
110
assertEquals(clazz.getFields().length, count);
111
}
112
113
@Test
114
public void checkFieldCounts() throws Exception {
115
assertContainsFields(BI1.class, 2);
116
assertContainsFields(BI2.class, 1);
117
assertContainsFields(DI.class, 4);
118
assertContainsFields(DDI.class, 5);
119
assertContainsFields(D.class, 1);
120
assertContainsFields(DD.class, 2);
121
assertContainsFields(Universe.class, 8);
122
}
123
124
void assertContainsFields(Class<?> derived, Class<?> base) {
125
List<Field> derivedFields = Arrays.asList(derived.getFields());
126
List<Field> baseFields = Arrays.asList(base.getFields());
127
assertTrue(derivedFields.containsAll(baseFields));
128
}
129
130
List<Class<?>> directSupers(Class<?> clazz) {
131
List<Class<?>> directSupers = new ArrayList<>();
132
directSupers.addAll(Arrays.asList(clazz.getInterfaces()));
133
if (clazz.getSuperclass() != null) {
134
directSupers.add(clazz.getSuperclass());
135
}
136
return directSupers;
137
}
138
139
void assertContainsSuperFields(Class<?> clazz) {
140
for (Class<?> directSuper : directSupers(clazz)) {
141
assertContainsFields(clazz, directSuper);
142
}
143
}
144
145
List<Class<?>> testClasses() {
146
List<Class<?>> testClasses = new ArrayList<>();
147
testClasses.add(Sanity.class);
148
testClasses.addAll(Arrays.asList(Sanity.class.getDeclaredClasses()));
149
assertEquals(testClasses.size(), 10);
150
return testClasses;
151
}
152
153
@Test
154
public void fieldsAreInheritedFromSupers() throws Exception {
155
for (Class clazz : testClasses()) {
156
assertContainsSuperFields(clazz);
157
}
158
}
159
160
@Test
161
public void getFieldIsConsistentWithGetFields() throws Exception {
162
for (Class clazz : testClasses()) {
163
for (Field field : clazz.getFields()) {
164
assertEquals(field, clazz.getField(field.getName()));
165
}
166
}
167
}
168
}
169
170