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.

HotSwapTestHelper.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /**
  27. * Shortcut methods for testing. Methods are named this way to make them more visible in the test code.
  28. * @author Ivan Dubrov
  29. */
  30. public class HotSwapTestHelper {
  31. /**
  32. * Returns the current version of the inner classes of an outer class.
  33. * <p/>
  34. * Caller class is used as an outer class.
  35. *
  36. * @return the version of the inner classes of the outer class
  37. */
  38. public static int __version__() {
  39. return HotSwapTool.getCurrentVersion(determineOuter(0));
  40. }
  41. /**
  42. * Redefines all inner classes of a outer class to a specified version. Inner classes who do not have a particular
  43. * representation for a version remain unchanged.
  44. * <p/>
  45. * Caller class is used as an outer class.
  46. *
  47. * @param versionNumber the target version number
  48. */
  49. public static void __toVersion__(int versionNumber) {
  50. HotSwapTool.toVersion(determineOuter(0), versionNumber);
  51. }
  52. /**
  53. * Helper method to determine caller outer class.
  54. * <p/>
  55. * Takes caller class and finds its top enclosing class (which is supposed to be test class).
  56. *
  57. * @param level on which level this call is being made. 0 - call is made immediately in the method of HotSwapTool.
  58. * @return outer class reference
  59. */
  60. private static Class<?> determineOuter(int level) {
  61. StackTraceElement[] stack = Thread.currentThread().getStackTrace();
  62. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  63. // one for Thread#getStackTrace
  64. // one for #determineOuter
  65. // one for the caller
  66. String callerName = stack[level + 3].getClassName();
  67. try {
  68. Class<?> clazz = cl.loadClass(callerName);
  69. while (clazz.getEnclosingClass() != null) {
  70. clazz = clazz.getEnclosingClass();
  71. }
  72. return clazz;
  73. } catch (ClassNotFoundException e) {
  74. throw new IllegalArgumentException("Cannot find caller class: " + callerName, e);
  75. }
  76. }
  77. }