< prev index next >

src/java.base/share/classes/jdk/internal/reflect/Reflection.java

Print this page


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


 126             return false;
 127         }
 128 
 129         boolean gotIsSameClassPackage = false;
 130         boolean isSameClassPackage = false;
 131 
 132         if (!Modifier.isPublic(getClassAccessFlags(memberClass))) {
 133             isSameClassPackage = isSameClassPackage(currentClass, memberClass);
 134             gotIsSameClassPackage = true;
 135             if (!isSameClassPackage) {
 136                 return false;
 137             }
 138         }
 139 
 140         // At this point we know that currentClass can access memberClass.
 141 
 142         if (Modifier.isPublic(modifiers)) {
 143             return true;
 144         }
 145 











 146         boolean successSoFar = false;
 147 
 148         if (Modifier.isProtected(modifiers)) {
 149             // See if currentClass is a subclass of memberClass
 150             if (isSubclassOf(currentClass, memberClass)) {
 151                 successSoFar = true;
 152             }
 153         }
 154 
 155         if (!successSoFar && !Modifier.isPrivate(modifiers)) {
 156             if (!gotIsSameClassPackage) {
 157                 isSameClassPackage = isSameClassPackage(currentClass,
 158                                                         memberClass);
 159                 gotIsSameClassPackage = true;
 160             }
 161 
 162             if (isSameClassPackage) {
 163                 successSoFar = true;
 164             }
 165         }


 334             memberSuffix = " (in " + m2 + ")";
 335 
 336         String memberPackageName = memberClass.getPackageName();
 337 
 338         String msg = currentClass + currentSuffix + " cannot access ";
 339         if (m2.isExported(memberPackageName, m1)) {
 340 
 341             // module access okay so include the modifiers in the message
 342             msg += "a member of " + memberClass + memberSuffix +
 343                     " with modifiers \"" + Modifier.toString(modifiers) + "\"";
 344 
 345         } else {
 346             // module access failed
 347             msg += memberClass + memberSuffix+ " because "
 348                    + m2 + " does not export " + memberPackageName;
 349             if (m2.isNamed()) msg += " to " + m1;
 350         }
 351 
 352         return new IllegalAccessException(msg);
 353     }








 354 }
   1 /*
   2  * Copyright (c) 2001, 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


 126             return false;
 127         }
 128 
 129         boolean gotIsSameClassPackage = false;
 130         boolean isSameClassPackage = false;
 131 
 132         if (!Modifier.isPublic(getClassAccessFlags(memberClass))) {
 133             isSameClassPackage = isSameClassPackage(currentClass, memberClass);
 134             gotIsSameClassPackage = true;
 135             if (!isSameClassPackage) {
 136                 return false;
 137             }
 138         }
 139 
 140         // At this point we know that currentClass can access memberClass.
 141 
 142         if (Modifier.isPublic(modifiers)) {
 143             return true;
 144         }
 145 
 146         // Check for nestmate access if member is private
 147         if (Modifier.isPrivate(modifiers)) {
 148           // assert: isSubclassof(targetClass, memberClass)
 149           // Note: targetClass may be outside the nest, but that is okay
 150           //       as long as memberClass is in the nest.
 151           boolean nestmates = areNestMates(currentClass, memberClass);
 152           if (nestmates) {
 153             return true;
 154           }
 155         }
 156 
 157         boolean successSoFar = false;
 158 
 159         if (Modifier.isProtected(modifiers)) {
 160             // See if currentClass is a subclass of memberClass
 161             if (isSubclassOf(currentClass, memberClass)) {
 162                 successSoFar = true;
 163             }
 164         }
 165 
 166         if (!successSoFar && !Modifier.isPrivate(modifiers)) {
 167             if (!gotIsSameClassPackage) {
 168                 isSameClassPackage = isSameClassPackage(currentClass,
 169                                                         memberClass);
 170                 gotIsSameClassPackage = true;
 171             }
 172 
 173             if (isSameClassPackage) {
 174                 successSoFar = true;
 175             }
 176         }


 345             memberSuffix = " (in " + m2 + ")";
 346 
 347         String memberPackageName = memberClass.getPackageName();
 348 
 349         String msg = currentClass + currentSuffix + " cannot access ";
 350         if (m2.isExported(memberPackageName, m1)) {
 351 
 352             // module access okay so include the modifiers in the message
 353             msg += "a member of " + memberClass + memberSuffix +
 354                     " with modifiers \"" + Modifier.toString(modifiers) + "\"";
 355 
 356         } else {
 357             // module access failed
 358             msg += memberClass + memberSuffix+ " because "
 359                    + m2 + " does not export " + memberPackageName;
 360             if (m2.isNamed()) msg += " to " + m1;
 361         }
 362 
 363         return new IllegalAccessException(msg);
 364     }
 365 
 366     /**
 367      * Returns true if {@code currentClass} and {@code memberClass}
 368      * are nestmates - that is, if they have the same nesthost as
 369      * determined by the VM.
 370      */
 371     public static native boolean areNestMates(Class<?> currentClass,
 372                                               Class<?> memberClass);
 373 }
< prev index next >