|
1 /* |
|
2 * Copyright (c) 2016, Oracle and/or its affiliates. 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 package b; |
|
24 |
|
25 import java.lang.reflect.AccessibleObject; |
|
26 import java.lang.reflect.Constructor; |
|
27 import java.lang.reflect.Field; |
|
28 import java.lang.reflect.InvocationTargetException; |
|
29 import java.lang.reflect.Method; |
|
30 |
|
31 /** |
|
32 * A package-private class in b. |
|
33 */ |
|
34 class Package { |
|
35 |
|
36 // fields |
|
37 private static int privateStatic; |
|
38 private int privateInstance; |
|
39 static int packageStatic; |
|
40 int packageInstance; |
|
41 protected static int protectedStatic; |
|
42 protected int protectedInstance; |
|
43 public static int publicStatic; |
|
44 public int publicInstance; |
|
45 |
|
46 // methods |
|
47 private static int privateStatic() { return 42; } |
|
48 private int privateInstance() { return 42; } |
|
49 static int packageStatic() { return 42; } |
|
50 int packageInstance() { return 42; } |
|
51 protected static int protectedStatic() { return 42; } |
|
52 protected int protectedInstance() { return 42; } |
|
53 public static int publicStatic() { return 42; } |
|
54 public int publicInstance() { return 42; } |
|
55 |
|
56 // constructors |
|
57 private Package(Void _1, Void _2, Void _3) {} |
|
58 Package(Void _1, Void _2) {} |
|
59 protected Package(Void _1) {} |
|
60 public Package() {} |
|
61 |
|
62 |
|
63 // testing method |
|
64 public static void checkAccess(AccessibleObject accessibleObject, Object obj) |
|
65 throws IllegalAccessException, |
|
66 InvocationTargetException, |
|
67 InstantiationException |
|
68 { |
|
69 if (accessibleObject instanceof Field) { |
|
70 Field field = (Field) accessibleObject; |
|
71 field.set(obj, 42); |
|
72 field.get(obj); |
|
73 } else if (accessibleObject instanceof Method) { |
|
74 Method method = (Method) accessibleObject; |
|
75 method.invoke(obj); |
|
76 } else if (accessibleObject instanceof Constructor) { |
|
77 Constructor<?> constructor = (Constructor<?>) accessibleObject; |
|
78 Object[] params = new Object[constructor.getParameterCount()]; |
|
79 constructor.newInstance(params); |
|
80 } |
|
81 } |
|
82 } |