File: ReflectAccess.java

package info (click to toggle)
mauve 20161030-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 44,628 kB
  • ctags: 35,425
  • sloc: java: 336,555; sh: 2,834; xml: 208; makefile: 72
file content (255 lines) | stat: -rw-r--r-- 6,414 bytes parent folder | download | duplicates (5)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Tags: JDK1.1
// Uses: sub/OtherPkg sub/Super Other

// Test reflection member accessibility checks.

package gnu.testlet.java.lang.reflect;

import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;

import java.lang.reflect.*;
import gnu.testlet.java.lang.reflect.sub.*;

public class ReflectAccess extends Super implements Testlet
{
  TestHarness harness;
  
  public void test(TestHarness harness)
  {
    this.harness = harness;
    try 
    {
      doTest();
    }
    catch (Exception x)
    {
      harness.debug(x);
      harness.fail(x.toString());
    }
  }
  
  void doTest() throws Exception
  {
    Method methodA = ReflectAccess.class.getDeclaredMethod("a", null);
    Method methodB = ReflectAccess.class.getDeclaredMethod("b", null);
    Method methodC = ReflectAccess.class.getDeclaredMethod("c", null);

    Field fieldD = ReflectAccess.class.getDeclaredField("d");
    Field fieldE = ReflectAccess.class.getDeclaredField("e");
    Field fieldF = ReflectAccess.class.getDeclaredField("f");
    
    Method methodG = OtherPkg.class.getDeclaredMethod("g", null);
    Method methodH = OtherPkg.class.getDeclaredMethod("h", null);
    Method methodI = OtherPkg.class.getDeclaredMethod("i", null);

    Field fieldJ = OtherPkg.class.getDeclaredField("j");
    Field fieldK = OtherPkg.class.getDeclaredField("k");
    Field fieldL = OtherPkg.class.getDeclaredField("l");
    
    Method methodM = Other.class.getDeclaredMethod("m", null);
    Method methodN = Other.class.getDeclaredMethod("n", null);
    Method methodO = Other.class.getDeclaredMethod("o", null);

    Field fieldP = Other.class.getDeclaredField("p");
    Field fieldQ = Other.class.getDeclaredField("q");
    Field fieldR = Other.class.getDeclaredField("r");

    try
    {
      Method methodT = ReflectAccess.class.getDeclaredMethod("t", null);
      harness.fail(methodT + " is not declared in class ReflectAccess");
    }
    catch (NoSuchMethodException x)
    {
      // ok
      harness.check(true, "method 't' is declared in class ReflectAccess");
    }
    
    Method methodS = Super.class.getDeclaredMethod("s", null);
    Method methodT = Super.class.getDeclaredMethod("t", null);
    Method methodU = Super.class.getDeclaredMethod("u", null);
    Method methodV = Super.class.getDeclaredMethod("v", null);

    Field fieldW = Super.class.getDeclaredField("w");
    Field fieldX = Super.class.getDeclaredField("x");
    Field fieldY = Super.class.getDeclaredField("y");
    Field fieldZ = Super.class.getDeclaredField("z");

    Object obj = new ReflectAccess();
    
    methodA.invoke(obj, null);
    methodB.invoke(null, null);
    methodC.invoke(obj, null);
    
    harness.check (fieldD.getChar(obj) == 'd', "field d is accessible");
    harness.check (fieldE.getChar(obj) == 'e', "field e is accessible");
    harness.check (fieldF.getChar(obj) == 'f', "field f is accessible");

    obj = new OtherPkg();
    
    try
    {
      methodG.invoke(obj, null);
      harness.fail(methodG + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, methodG + " is inaccessible");
    }
    try
    {
      methodH.invoke(obj, null);
      harness.fail(methodH + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, methodH + " is inaccessible");
    }
    try
    {
      methodI.invoke(obj, null);
      harness.fail(methodI + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, methodI + " is inaccessible");
    }
    try
    {
      fieldJ.getChar(obj);
      harness.fail(fieldJ + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, fieldJ + " is inaccessible");
    }
    try
    {
      fieldK.getChar(obj);
      harness.fail(fieldK + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, fieldK + " is inaccessible");
    }
    try
    {
      fieldL.getChar(obj);
      harness.fail(fieldL + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, fieldL + " is inaccessible");
    }
    
    obj = new Other();

    methodM.invoke(null, null);
    methodN.invoke(obj, null);
    
    try
    {
      methodO.invoke(obj, null);
      harness.fail(methodO + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, methodO + " is inaccessible");
    }
    
    methodO.setAccessible(true);
    methodO.invoke(obj, null);

    harness.check (fieldP.getChar(obj) == 'p');
    harness.check (fieldQ.getChar(obj) == 'q');
    
    try
    {
      fieldR.getChar(obj);
      harness.fail(fieldR + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, fieldR + " is inaccessible");
    }
    
    fieldR.setAccessible(true);
    harness.check(fieldR.getChar(obj) == 'r', fieldR + " is accessible");
    obj = new ReflectAccess();
    
    try 
    {
      methodS.invoke(obj, null);
      harness.fail(methodS + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, methodS + " is inaccessible");
    }
    
    methodT.invoke(obj, null);
    methodU.invoke(obj, null);
    
    try 
    { 
      methodV.invoke(obj, null);
      harness.fail(methodV + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, methodV + " is inaccessible");
    }

    harness.check (fieldW.getChar(obj) == 'w');
    harness.check (fieldX.getChar(obj) == 'x');

    try 
    { 
      fieldY.getChar(obj);
      harness.fail(fieldY + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, fieldY + " is inaccessible");
    }
    
    try
    { 
      fieldZ.getChar(obj); 
      harness.fail(fieldZ + " should not be accessible");
    }
    catch (IllegalAccessException x)
    {
      // ok
      harness.check(true, fieldZ + " is inaccessible");
    }
  }
  
  private void a() 
  { 
  }

  private static void b()
  {
  }
  
  protected void c()
  {
  }
  
  private char d = 'd';
  private static char e = 'e';
  protected char f = 'f';
}