You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SecurityActions.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.util.proxy;
  16. import java.lang.reflect.AccessibleObject;
  17. import java.lang.reflect.Constructor;
  18. import java.lang.reflect.Field;
  19. import java.lang.reflect.Method;
  20. import java.security.AccessController;
  21. import java.security.PrivilegedAction;
  22. import java.security.PrivilegedActionException;
  23. import java.security.PrivilegedExceptionAction;
  24. class SecurityActions {
  25. static Method[] getDeclaredMethods(final Class clazz) {
  26. if (System.getSecurityManager() == null)
  27. return clazz.getDeclaredMethods();
  28. else {
  29. return (Method[]) AccessController
  30. .doPrivileged(new PrivilegedAction() {
  31. public Object run() {
  32. return clazz.getDeclaredMethods();
  33. }
  34. });
  35. }
  36. }
  37. static Constructor[] getDeclaredConstructors(final Class clazz) {
  38. if (System.getSecurityManager() == null)
  39. return clazz.getDeclaredConstructors();
  40. else {
  41. return (Constructor[]) AccessController
  42. .doPrivileged(new PrivilegedAction() {
  43. public Object run() {
  44. return clazz.getDeclaredConstructors();
  45. }
  46. });
  47. }
  48. }
  49. static Method getDeclaredMethod(final Class clazz, final String name,
  50. final Class[] types) throws NoSuchMethodException {
  51. if (System.getSecurityManager() == null)
  52. return clazz.getDeclaredMethod(name, types);
  53. else {
  54. try {
  55. return (Method) AccessController
  56. .doPrivileged(new PrivilegedExceptionAction() {
  57. public Object run() throws Exception {
  58. return clazz.getDeclaredMethod(name, types);
  59. }
  60. });
  61. }
  62. catch (PrivilegedActionException e) {
  63. if (e.getCause() instanceof NoSuchMethodException)
  64. throw (NoSuchMethodException) e.getCause();
  65. throw new RuntimeException(e.getCause());
  66. }
  67. }
  68. }
  69. static Constructor getDeclaredConstructor(final Class clazz,
  70. final Class[] types)
  71. throws NoSuchMethodException
  72. {
  73. if (System.getSecurityManager() == null)
  74. return clazz.getDeclaredConstructor(types);
  75. else {
  76. try {
  77. return (Constructor) AccessController
  78. .doPrivileged(new PrivilegedExceptionAction() {
  79. public Object run() throws Exception {
  80. return clazz.getDeclaredConstructor(types);
  81. }
  82. });
  83. }
  84. catch (PrivilegedActionException e) {
  85. if (e.getCause() instanceof NoSuchMethodException)
  86. throw (NoSuchMethodException) e.getCause();
  87. throw new RuntimeException(e.getCause());
  88. }
  89. }
  90. }
  91. static void setAccessible(final AccessibleObject ao,
  92. final boolean accessible) {
  93. if (System.getSecurityManager() == null)
  94. ao.setAccessible(accessible);
  95. else {
  96. AccessController.doPrivileged(new PrivilegedAction() {
  97. public Object run() {
  98. ao.setAccessible(accessible);
  99. return null;
  100. }
  101. });
  102. }
  103. }
  104. static void set(final Field fld, final Object target, final Object value)
  105. throws IllegalAccessException
  106. {
  107. if (System.getSecurityManager() == null)
  108. fld.set(target, value);
  109. else {
  110. try {
  111. AccessController.doPrivileged(new PrivilegedExceptionAction() {
  112. public Object run() throws Exception {
  113. fld.set(target, value);
  114. return null;
  115. }
  116. });
  117. }
  118. catch (PrivilegedActionException e) {
  119. if (e.getCause() instanceof NoSuchMethodException)
  120. throw (IllegalAccessException) e.getCause();
  121. throw new RuntimeException(e.getCause());
  122. }
  123. }
  124. }
  125. }