2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / lang / reflect / AccessibleObject.java
blob4fb89bb44b5fa4b89421cfdd498018ebbd39546b
1 /* java.lang.reflect.AccessibleObject
2 Copyright (C) 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.lang.reflect;
41 /**
42 * This class is the superclass of various reflection classes, and
43 * allows sufficiently trusted code to bypass normal restrictions to
44 * do necessary things like invoke private methods outside of the
45 * class during Serialization. If you don't have a good reason
46 * to mess with this, don't try. Fortunately, there are adequate
47 * security checks before you can set a reflection object as accessible.
49 * @author Tom Tromey <tromey@cygnus.com>
50 * @author Eric Blake <ebb9@email.byu.edu>
51 * @see Field
52 * @see Constructor
53 * @see Method
54 * @see ReflectPermission
55 * @since 1.2
56 * @status updated to 1.4
58 public class AccessibleObject
60 /**
61 * True if this object is marked accessible, which means the reflected
62 * object bypasses normal security checks.
64 // default visibility for use by inherited classes
65 boolean flag = false;
67 /**
68 * Only the three reflection classes that extend this can create an
69 * accessible object. This is not serializable for security reasons.
71 protected AccessibleObject()
75 /**
76 * Return the accessibility status of this object.
78 * @return true if this object bypasses security checks
80 public boolean isAccessible()
82 return flag;
85 /**
86 * Convenience method to set the flag on a number of objects with a single
87 * security check. If a security manager exists, it is checked for
88 * <code>ReflectPermission("suppressAccessChecks")</code>.<p>
90 * It is forbidden to set the accessibility flag to true on any constructor
91 * for java.lang.Class. This will result in a SecurityException. If the
92 * SecurityException is thrown for any of the passed AccessibleObjects,
93 * the accessibility flag will be set on AccessibleObjects in the array prior
94 * to the one which resulted in the exception.
96 * @param array the array of accessible objects
97 * @param flag the desired state of accessibility, true to bypass security
98 * @throws NullPointerException if array is null
99 * @throws SecurityException if the request is denied
100 * @see SecurityManager#checkPermission(java.security.Permission)
101 * @see RuntimePermission
103 public static void setAccessible(AccessibleObject[] array, boolean flag)
105 checkPermission();
106 for (int i = 0; i < array.length; i++)
107 array[i].secureSetAccessible(flag);
111 * Sets the accessibility flag for this reflection object. If a security
112 * manager exists, it is checked for
113 * <code>ReflectPermission("suppressAccessChecks")</code>.<p>
115 * It is forbidden to set the accessibility flag to true on any constructor for
116 * java.lang.Class. This will result in a SecurityException.
118 * @param flag the desired state of accessibility, true to bypass security
119 * @throws NullPointerException if array is null
120 * @throws SecurityException if the request is denied
121 * @see SecurityManager#checkPermission(java.security.Permission)
122 * @see RuntimePermission
124 public void setAccessible(boolean flag)
126 checkPermission();
127 secureSetAccessible(flag);
131 * Performs the specified security check, for
132 * <code>ReflectPermission("suppressAccessChecks")</code>.
134 * @throws SecurityException if permission is denied
136 private static final void checkPermission()
138 SecurityManager sm = System.getSecurityManager();
139 if (sm != null)
140 sm.checkPermission(new ReflectPermission("suppressAccessChecks"));
144 * Performs the actual accessibility change, this must always be invoked
145 * after calling checkPermission.
147 * @param flag the desired status
148 * @throws SecurityException if flag is true and this is a constructor
149 * for <code>java.lang.Class</code>.
151 private final void secureSetAccessible(boolean flag)
153 if (flag &&
154 (this instanceof Constructor
155 && ((Constructor) this).getDeclaringClass() == Class.class))
156 throw new SecurityException("Cannot make object accessible: " + this);
157 this.flag = flag;