ReflectUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* ReflectUtil.java - JSR 166 reflection hooks
  2. Copyright (C) 2006 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package sun.reflect.misc;
  32. import java.lang.reflect.Modifier;
  33. public class ReflectUtil
  34. {
  35. // We use this inaccessible inner class as an argument type
  36. // in verifyMemberAccess. All current users of this method
  37. // in the JSR 166 RI pass 'null' for this argument, and
  38. // consequently we don't know what it means. Using a funny
  39. // type like this for the argument means that if the RI changes,
  40. // we will see a compilation error.
  41. private static class MustBeNull
  42. {
  43. }
  44. /**
  45. * Check if the current thread is allowed to access the package of
  46. * the declaringClass.
  47. *
  48. * @param declaringClass class name to check access to
  49. * @throws SecurityException if permission is denied
  50. * @throws NullPointerException if declaringClass is null
  51. */
  52. public static void checkPackageAccess(Class declaringClass)
  53. {
  54. SecurityManager sm;
  55. if ((sm = System.getSecurityManager()) != null)
  56. {
  57. while (declaringClass.isArray())
  58. declaringClass = declaringClass.getComponentType();
  59. String name = declaringClass.getName();
  60. int i = name.lastIndexOf('.');
  61. if (i != -1) // if declaringClass is a member of a package
  62. {
  63. name = name.substring(0, i);
  64. sm.checkPackageAccess(name);
  65. }
  66. }
  67. }
  68. /**
  69. * Perform access checks on a member of a class. This API is
  70. * derived from the public domain code in the JSR 166 reference
  71. * implementation.
  72. * @param caller the class requesting access to the member
  73. * @param declarer the declaring class of the member
  74. * @param ignored unknown parameter; always null
  75. * @param modifiers the modifiers on the member
  76. * @return true if access is granted, false otherwise
  77. */
  78. public static void ensureMemberAccess(Class caller,
  79. Class declarer,
  80. MustBeNull ignored,
  81. int modifiers)
  82. {
  83. // Same class, always ok.
  84. if (caller == declarer)
  85. return;
  86. // Public access is ok.
  87. if ((modifiers & Modifier.PUBLIC) != 0)
  88. return;
  89. // Protected access and request comes from
  90. // a subclass of the declarer -- ok.
  91. if ((modifiers & Modifier.PROTECTED) != 0
  92. && declarer.isAssignableFrom(caller))
  93. return;
  94. // Package-private access, or protected access,
  95. // and the packages are the same --ok.
  96. if ((modifiers & Modifier.PRIVATE) == 0
  97. && caller.getPackage() == declarer.getPackage())
  98. return;
  99. // Otherwise, no.
  100. throw new IllegalAccessError();
  101. }
  102. }