src/share/classes/java/lang/reflect/AccessibleObject.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 6642881_8u40_jdk Sdiff src/share/classes/java/lang/reflect

src/share/classes/java/lang/reflect/AccessibleObject.java

Print this page
rev 9948 : 6642881: Improve performance of Class.getClassLoader()
Summary: Add classLoader to java/lang/Class instance for fast access
Reviewed-by: alanb, lfoltan, rriggs, vlivanov, twisti, mchung, jfranck, dholmes
   1 /*
   2  * Copyright (c) 1997, 2013, 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


 112      * <p>A {@code SecurityException} is raised if {@code flag} is
 113      * {@code true} but accessibility of this object may not be changed
 114      * (for example, if this element object is a {@link Constructor} object for
 115      * the class {@link java.lang.Class}).
 116      *
 117      * <p>A {@code SecurityException} is raised if this object is a {@link
 118      * java.lang.reflect.Constructor} object for the class
 119      * {@code java.lang.Class}, and {@code flag} is true.
 120      *
 121      * @param flag the new value for the {@code accessible} flag
 122      * @throws SecurityException if the request is denied.
 123      * @see SecurityManager#checkPermission
 124      * @see java.lang.RuntimePermission
 125      */
 126     public void setAccessible(boolean flag) throws SecurityException {
 127         SecurityManager sm = System.getSecurityManager();
 128         if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
 129         setAccessible0(this, flag);
 130     }
 131 
 132     /* Check that you aren't exposing java.lang.Class.<init>. */

 133     private static void setAccessible0(AccessibleObject obj, boolean flag)
 134         throws SecurityException
 135     {
 136         if (obj instanceof Constructor && flag == true) {
 137             Constructor<?> c = (Constructor<?>)obj;
 138             if (c.getDeclaringClass() == Class.class) {
 139                 throw new SecurityException("Can not make a java.lang.Class" +
 140                                             " constructor accessible");







 141             }
 142         }
 143         obj.override = flag;
 144     }
 145 
 146     /**
 147      * Get the value of the {@code accessible} flag for this object.
 148      *
 149      * @return the value of the object's {@code accessible} flag
 150      */
 151     public boolean isAccessible() {
 152         return override;
 153     }
 154 
 155     /**
 156      * Constructor: only used by the Java Virtual Machine.
 157      */
 158     protected AccessibleObject() {}
 159 
 160     // Indicates whether language-level access checks are overridden


   1 /*
   2  * Copyright (c) 1997, 2014, 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


 112      * <p>A {@code SecurityException} is raised if {@code flag} is
 113      * {@code true} but accessibility of this object may not be changed
 114      * (for example, if this element object is a {@link Constructor} object for
 115      * the class {@link java.lang.Class}).
 116      *
 117      * <p>A {@code SecurityException} is raised if this object is a {@link
 118      * java.lang.reflect.Constructor} object for the class
 119      * {@code java.lang.Class}, and {@code flag} is true.
 120      *
 121      * @param flag the new value for the {@code accessible} flag
 122      * @throws SecurityException if the request is denied.
 123      * @see SecurityManager#checkPermission
 124      * @see java.lang.RuntimePermission
 125      */
 126     public void setAccessible(boolean flag) throws SecurityException {
 127         SecurityManager sm = System.getSecurityManager();
 128         if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
 129         setAccessible0(this, flag);
 130     }
 131 
 132     /* Check that you aren't exposing java.lang.Class.<init> or sensitive
 133        fields in java.lang.Class. */
 134     private static void setAccessible0(AccessibleObject obj, boolean flag)
 135         throws SecurityException
 136     {
 137         if (obj instanceof Constructor && flag == true) {
 138             Constructor<?> c = (Constructor<?>)obj;
 139             if (c.getDeclaringClass() == Class.class) {
 140                 throw new SecurityException("Cannot make a java.lang.Class" +
 141                                             " constructor accessible");
 142             }
 143         } else if (obj instanceof Field && flag == true) {
 144             Field f = (Field)obj;
 145             if (f.getDeclaringClass() == Class.class &&
 146                 f.getName().equals("classLoader")) {
 147                 throw new SecurityException("Cannot make java.lang.Class.classLoader" +
 148                                             " accessible");
 149             }
 150         }
 151         obj.override = flag;
 152     }
 153 
 154     /**
 155      * Get the value of the {@code accessible} flag for this object.
 156      *
 157      * @return the value of the object's {@code accessible} flag
 158      */
 159     public boolean isAccessible() {
 160         return override;
 161     }
 162 
 163     /**
 164      * Constructor: only used by the Java Virtual Machine.
 165      */
 166     protected AccessibleObject() {}
 167 
 168     // Indicates whether language-level access checks are overridden


src/share/classes/java/lang/reflect/AccessibleObject.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File