Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HotSwapTestHelper.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2010, 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.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. package com.github.dcevm.test.util;
  25. import com.github.dcevm.HotSwapTool;
  26. import sun.reflect.CallerSensitive;
  27. /**
  28. * Shortcut methods for testing. Methods are named this way to make them more visible in the test code.
  29. * @author Ivan Dubrov
  30. */
  31. public class HotSwapTestHelper {
  32. /**
  33. * Returns the current version of the inner classes of an outer class.
  34. * <p/>
  35. * Caller class is used as an outer class.
  36. *
  37. * @return the version of the inner classes of the outer class
  38. */
  39. public static int __version__() {
  40. return HotSwapTool.getCurrentVersion(determineOuter(0));
  41. }
  42. /**
  43. * Redefines all inner classes of a outer class to a specified version. Inner classes who do not have a particular
  44. * representation for a version remain unchanged.
  45. * <p/>
  46. * Caller class is used as an outer class.
  47. *
  48. * @param versionNumber the target version number
  49. */
  50. public static void __toVersion__(int versionNumber) {
  51. HotSwapTool.toVersion(determineOuter(0), versionNumber);
  52. }
  53. /**
  54. * Helper method to determine caller outer class.
  55. * <p/>
  56. * Takes caller class and finds its top enclosing class (which is supposed to be test class).
  57. *
  58. * @param level on which level this call is being made. 0 - call is made immediately in the method of HotSwapTool.
  59. * @return outer class reference
  60. */
  61. private static Class<?> determineOuter(int level) {
  62. StackTraceElement[] stack = Thread.currentThread().getStackTrace();
  63. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  64. // one for Thread#getStackTrace
  65. // one for #determineOuter
  66. // one for the caller
  67. String callerName = stack[level + 3].getClassName();
  68. try {
  69. Class<?> clazz = cl.loadClass(callerName);
  70. while (clazz.getEnclosingClass() != null) {
  71. clazz = clazz.getEnclosingClass();
  72. }
  73. return clazz;
  74. } catch (ClassNotFoundException e) {
  75. throw new IllegalArgumentException("Cannot find caller class: " + callerName, e);
  76. }
  77. }
  78. }