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 5.0KB

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