/test/functional/cmdline_options_testresources/src_80/com/ibm/j9/getcallerclass/GetCallerClassTests.java

https://github.com/eclipse/openj9 · Java · 254 lines · 160 code · 23 blank · 71 comment · 0 complexity · a3ceccb86fbd647c4657d30fbf1ac555 MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2014, 2018 IBM Corp. and others
  3. *
  4. * This program and the accompanying materials are made available under
  5. * the terms of the Eclipse Public License 2.0 which accompanies this
  6. * distribution and is available at https://www.eclipse.org/legal/epl-2.0/
  7. * or the Apache License, Version 2.0 which accompanies this distribution and
  8. * is available at https://www.apache.org/licenses/LICENSE-2.0.
  9. *
  10. * This Source Code may also be made available under the following
  11. * Secondary Licenses when the conditions for such availability set
  12. * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
  13. * General Public License, version 2 with the GNU Classpath
  14. * Exception [1] and GNU General Public License, version 2 with the
  15. * OpenJDK Assembly Exception [2].
  16. *
  17. * [1] https://www.gnu.org/software/classpath/license.html
  18. * [2] http://openjdk.java.net/legal/assembly-exception.html
  19. *
  20. * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
  21. *******************************************************************************/
  22. package com.ibm.j9.getcallerclass;
  23. /**
  24. * The <code>getCallerClassTests</code> class contains all test cases used by
  25. * the cmdLineTester_getCallerClassTests to verify the behavior when calling
  26. * getCallerClass()/ensureCalledFromBootstrapClass() from the
  27. * application/bootstrap/extension classloader
  28. */
  29. public class GetCallerClassTests {
  30. /**
  31. * Call getCallerClass() with @CallerSensitive annotation from the
  32. * application classloader
  33. */
  34. @sun.reflect.CallerSensitive
  35. public static boolean test_getCallerClass_fromAppWithAnnotation() {
  36. final String TESTCASE_NAME = "test_getCallerClass_fromAppWithAnnotation";
  37. try {
  38. sun.reflect.Reflection.getCallerClass();
  39. System.out.println(TESTCASE_NAME + ": FAILED 1");
  40. return false;
  41. } catch (InternalError e) {
  42. System.out.println(TESTCASE_NAME + ": PASSED");
  43. return true;
  44. } catch (Exception e) {
  45. System.out.println(TESTCASE_NAME + ": FAILED 2");
  46. e.printStackTrace();
  47. return false;
  48. }
  49. }
  50. /**
  51. * Call getCallerClass() without @CallerSensitive annotation from the
  52. * application classloader
  53. */
  54. public static boolean test_getCallerClass_fromAppWithoutAnnotation() {
  55. final String TESTCASE_NAME = "test_getCallerClass_fromAppWithoutAnnotation";
  56. try {
  57. sun.reflect.Reflection.getCallerClass();
  58. System.out.println(TESTCASE_NAME + ": FAILED 1");
  59. return false;
  60. } catch (InternalError e) {
  61. System.out.println(TESTCASE_NAME + ": PASSED");
  62. return true;
  63. } catch (Exception e) {
  64. System.out.println(TESTCASE_NAME + ": FAILED 2");
  65. e.printStackTrace();
  66. return false;
  67. }
  68. }
  69. /**
  70. * Call getCallerClass() with @CallerSensitive annotation from the
  71. * bootstrap/extension classloader
  72. */
  73. @sun.reflect.CallerSensitive
  74. public static boolean test_getCallerClass_fromBootExtWithAnnotation() {
  75. final String TESTCASE_NAME = "test_getCallerClass_fromBootExtWithAnnotation";
  76. try {
  77. sun.reflect.Reflection.getCallerClass();
  78. System.out.println(TESTCASE_NAME + ": PASSED");
  79. return true;
  80. } catch (InternalError e) {
  81. System.out.println(TESTCASE_NAME + ": FAILED 1");
  82. e.printStackTrace();
  83. return false;
  84. } catch (Exception e) {
  85. System.out.println(TESTCASE_NAME + ": FAILED 2");
  86. e.printStackTrace();
  87. return false;
  88. }
  89. }
  90. /**
  91. * Method used when calling getCallerClass() via MethodHandle
  92. */
  93. @sun.reflect.CallerSensitive
  94. public static Class<?> test_getCallerClass_MethodHandle() {
  95. Class<?> cls = null;
  96. try {
  97. cls = sun.reflect.Reflection.getCallerClass();
  98. } catch (InternalError e) {
  99. return null;
  100. } catch (Exception e) {
  101. return null;
  102. }
  103. return cls;
  104. }
  105. /**
  106. * Empty method used when calling getCallerClass() via MethodHandle using an
  107. * ArgumentHelper
  108. */
  109. public static Class<?> test_getCallerClass_ArgumentHelper(Class<?> cls) {
  110. return cls;
  111. }
  112. /**
  113. * Call getCallerClass() without @CallerSensitive annotation from the
  114. * bootstrap/extension classloader
  115. */
  116. public static boolean test_getCallerClass_fromBootExtWithoutAnnotation() {
  117. final String TESTCASE_NAME = "test_getCallerClass_fromBootExtWithoutAnnotation";
  118. try {
  119. sun.reflect.Reflection.getCallerClass();
  120. System.out.println(TESTCASE_NAME + ": FAILED 1");
  121. return false;
  122. } catch (InternalError e) {
  123. System.out.println(TESTCASE_NAME + ": PASSED");
  124. return true;
  125. } catch (Exception e) {
  126. System.out.println(TESTCASE_NAME + ": FAILED 2");
  127. e.printStackTrace();
  128. return false;
  129. }
  130. }
  131. /**
  132. * Call ensureCalledFromBootstrapClass() with @CallerSensitive annotation
  133. * from the application classloader
  134. */
  135. @sun.reflect.CallerSensitive
  136. public static boolean test_ensureCalledFromBootstrapClass_fromAppWithAnnotation() {
  137. final String TESTCASE_NAME = "test_ensureCalledFromBootstrapClass_fromAppWithAnnotation";
  138. try {
  139. com.ibm.oti.vm.VM.ensureCalledFromBootstrapClass();
  140. System.out.println(TESTCASE_NAME + ": FAILED 1");
  141. return false;
  142. } catch (InternalError e) {
  143. System.out.println(TESTCASE_NAME + ": PASSED");
  144. return true;
  145. } catch (Exception e) {
  146. System.out.println(TESTCASE_NAME + ": FAILED 2");
  147. e.printStackTrace();
  148. return false;
  149. }
  150. }
  151. /**
  152. * Call ensureCalledFromBootstrapClass() without @CallerSensitive annotation
  153. * from the application classloader
  154. */
  155. public static boolean test_ensureCalledFromBootstrapClass_fromAppWithoutAnnotation() {
  156. final String TESTCASE_NAME = "test_ensureCalledFromBootstrapClass_fromAppWithoutAnnotation";
  157. try {
  158. com.ibm.oti.vm.VM.ensureCalledFromBootstrapClass();
  159. System.out.println(TESTCASE_NAME + ": FAILED 1");
  160. return false;
  161. } catch (InternalError e) {
  162. System.out.println(TESTCASE_NAME + ": PASSED");
  163. return true;
  164. } catch (Exception e) {
  165. System.out.println(TESTCASE_NAME + ": FAILED 2");
  166. e.printStackTrace();
  167. return false;
  168. }
  169. }
  170. /**
  171. * Call ensureCalledFromBootstrapClass() with @CallerSensitive annotation
  172. * from the bootstrap classloader
  173. */
  174. @sun.reflect.CallerSensitive
  175. public static boolean test_ensureCalledFromBootstrapClass_fromBootWithAnnotation() {
  176. final String TESTCASE_NAME = "test_ensureCalledFromBootstrapClass_fromBootWithAnnotation";
  177. try {
  178. com.ibm.oti.vm.VM.ensureCalledFromBootstrapClass();
  179. System.out.println(TESTCASE_NAME + ": PASSED");
  180. return true;
  181. } catch (InternalError e) {
  182. System.out.println(TESTCASE_NAME + ": FAILED 1");
  183. e.printStackTrace();
  184. return false;
  185. } catch (Exception e) {
  186. System.out.println(TESTCASE_NAME + ": FAILED 2");
  187. e.printStackTrace();
  188. return false;
  189. }
  190. }
  191. /**
  192. * Call ensureCalledFromBootstrapClass() with @CallerSensitive annotation
  193. * from the extension classloader
  194. */
  195. @sun.reflect.CallerSensitive
  196. public static boolean test_ensureCalledFromBootstrapClass_fromExtWithAnnotation() {
  197. final String TESTCASE_NAME = "test_ensureCalledFromBootstrapClass_fromExtWithAnnotation";
  198. try {
  199. com.ibm.oti.vm.VM.ensureCalledFromBootstrapClass();
  200. System.out.println(TESTCASE_NAME + ": FAILED");
  201. return false;
  202. } catch (SecurityException e) {
  203. /* SecurityException is expected to be thrown out if not called from bootstrap classloader */
  204. System.out.println(TESTCASE_NAME + ": PASSED");
  205. return true;
  206. }
  207. }
  208. /**
  209. * Call ensureCalledFromBootstrapClass() without @CallerSensitive annotation
  210. * from the bootstrap/extension classloader
  211. */
  212. public static boolean test_ensureCalledFromBootstrapClass_fromBootExtWithoutAnnotation() {
  213. final String TESTCASE_NAME = "test_ensureCalledFromBootstrapClass_fromBootExtWithoutAnnotation";
  214. try {
  215. com.ibm.oti.vm.VM.ensureCalledFromBootstrapClass();
  216. System.out.println(TESTCASE_NAME + ": FAILED 1");
  217. return false;
  218. } catch (InternalError e) {
  219. System.out.println(TESTCASE_NAME + ": PASSED");
  220. return true;
  221. } catch (SecurityException e) {
  222. System.out.println(TESTCASE_NAME + ": FAILED 2");
  223. e.printStackTrace();
  224. return false;
  225. } catch (Exception e) {
  226. System.out.println(TESTCASE_NAME + ": FAILED 3");
  227. e.printStackTrace();
  228. return false;
  229. }
  230. }
  231. }