History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/reflect/misc/ReflectUtil.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/reflect/misc/ReflectUtil.java+147−0
@@ -0,0 +1,147 @@
1/*
2 * Copyright (c) 2005, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26
27package sun.reflect.misc;
28
29import java.lang.reflect.Modifier;
30import sun.reflect.Reflection;
31
32public final class ReflectUtil {
33
34 private ReflectUtil() {
35 }
36
37 public static Class forName(String name)
38 throws ClassNotFoundException {
39 checkPackageAccess(name);
40 return Class.forName(name);
41 }
42
43 public static Object newInstance(Class cls)
44 throws InstantiationException, IllegalAccessException {
45 checkPackageAccess(cls);
46 return cls.newInstance();
47 }
48
49 /*
50 * Reflection.ensureMemberAccess is overly-restrictive
51 * due to a bug. We awkwardly work around it for now.
52 */
53 public static void ensureMemberAccess(Class currentClass,
54 Class memberClass,
55 Object target,
56 int modifiers)
57 throws IllegalAccessException
58 {
59 if (target == null && Modifier.isProtected(modifiers)) {
60 int mods = modifiers;
61 mods = mods & (~Modifier.PROTECTED);
62 mods = mods | Modifier.PUBLIC;
63
64 /*
65 * See if we fail because of class modifiers
66 */
67 Reflection.ensureMemberAccess(currentClass,
68 memberClass,
69 target,
70 mods);
71 try {
72 /*
73 * We're still here so class access was ok.
74 * Now try with default field access.
75 */
76 mods = mods & (~Modifier.PUBLIC);
77 Reflection.ensureMemberAccess(currentClass,
78 memberClass,
79 target,
80 mods);
81 /*
82 * We're still here so access is ok without
83 * checking for protected.
84 */
85 return;
86 } catch (IllegalAccessException e) {
87 /*
88 * Access failed but we're 'protected' so
89 * if the test below succeeds then we're ok.
90 */
91 if (isSubclassOf(currentClass, memberClass)) {
92 return;
93 } else {
94 throw e;
95 }
96 }
97 } else {
98 Reflection.ensureMemberAccess(currentClass,
99 memberClass,
100 target,
101 modifiers);
102 }
103 }
104
105 private static boolean isSubclassOf(Class queryClass,
106 Class ofClass)
107 {
108 while (queryClass != null) {
109 if (queryClass == ofClass) {
110 return true;
111 }
112 queryClass = queryClass.getSuperclass();
113 }
114 return false;
115 }
116
117
118 public static void checkPackageAccess(Class clazz) {
119 checkPackageAccess(clazz.getName());
120 }
121
122 public static void checkPackageAccess(String name) {
123 SecurityManager s = System.getSecurityManager();
124 if (s != null) {
125 String cname = name.replace('/', '.');
126 if (cname.startsWith("[")) {
127 int b = cname.lastIndexOf('[') + 2;
128 if (b > 1 && b < cname.length()) {
129 cname = cname.substring(b);
130 }
131 }
132 int i = cname.lastIndexOf('.');
133 if (i != -1) {
134 s.checkPackageAccess(cname.substring(0, i));
135 }
136 }
137 }
138
139 public static boolean isPackageAccessible(Class clazz) {
140 try {
141 checkPackageAccess(clazz);
142 } catch (SecurityException e) {
143 return false;
144 }
145 return true;
146 }
147}