< prev index next >

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

Print this page


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


 653         }
 654         if (targetClass != null // instance member or constructor
 655             && Modifier.isProtected(modifiers)
 656             && targetClass != memberClass) {
 657             if (isAccessChecked(caller, targetClass)) {
 658                 return true;         // ACCESS IS OK
 659             }
 660         } else if (isAccessChecked(caller)) {
 661             // Non-protected case (or targetClass == memberClass or static member).
 662             return true;             // ACCESS IS OK
 663         }
 664 
 665         // If no return, fall through to the slow path.
 666         return slowVerifyAccess(caller, memberClass, targetClass, modifiers);
 667     }
 668 
 669     // Keep all this slow stuff out of line:
 670     private boolean slowVerifyAccess(Class<?> caller, Class<?> memberClass,
 671                                      Class<?> targetClass, int modifiers)
 672     {








 673         if (!Reflection.verifyMemberAccess(caller, memberClass, targetClass, modifiers)) {
 674             // access denied
 675             return false;
 676         }
 677 
 678         // access okay
 679         logIfExportedForIllegalAccess(caller, memberClass);
 680 
 681         // Success: Update the cache.
 682         Object cache = (targetClass != null
 683                         && Modifier.isProtected(modifiers)
 684                         && targetClass != memberClass)
 685                         ? Cache.protectedMemberCallerCache(caller, targetClass)
 686                         : new WeakReference<>(caller);
 687         accessCheckCache = cache;         // write volatile
 688         return true;
 689     }
 690 
 691     // true to print a stack trace when access fails
 692     private static volatile boolean printStackWhenAccessFails;


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


 653         }
 654         if (targetClass != null // instance member or constructor
 655             && Modifier.isProtected(modifiers)
 656             && targetClass != memberClass) {
 657             if (isAccessChecked(caller, targetClass)) {
 658                 return true;         // ACCESS IS OK
 659             }
 660         } else if (isAccessChecked(caller)) {
 661             // Non-protected case (or targetClass == memberClass or static member).
 662             return true;             // ACCESS IS OK
 663         }
 664 
 665         // If no return, fall through to the slow path.
 666         return slowVerifyAccess(caller, memberClass, targetClass, modifiers);
 667     }
 668 
 669     // Keep all this slow stuff out of line:
 670     private boolean slowVerifyAccess(Class<?> caller, Class<?> memberClass,
 671                                      Class<?> targetClass, int modifiers)
 672     {
 673 
 674         if (caller == null) {
 675             // No caller frame when a native thread attaches to the VM
 676             // can access public member of a type in unconditional exported API packages
 677             Module module = memberClass.getModule();
 678             return Modifier.isPublic(modifiers) && module.isExported(memberClass.getPackageName());
 679         }
 680 
 681         if (!Reflection.verifyMemberAccess(caller, memberClass, targetClass, modifiers)) {
 682             // access denied
 683             return false;
 684         }
 685 
 686         // access okay
 687         logIfExportedForIllegalAccess(caller, memberClass);
 688 
 689         // Success: Update the cache.
 690         Object cache = (targetClass != null
 691                         && Modifier.isProtected(modifiers)
 692                         && targetClass != memberClass)
 693                         ? Cache.protectedMemberCallerCache(caller, targetClass)
 694                         : new WeakReference<>(caller);
 695         accessCheckCache = cache;         // write volatile
 696         return true;
 697     }
 698 
 699     // true to print a stack trace when access fails
 700     private static volatile boolean printStackWhenAccessFails;


< prev index next >