/libraryInterface/OpenJDK/GPL/src/org/jikesrvm/classlibrary/openjdk/replacements/sun_reflect_Reflection.java

https://github.com/joekoolade/JEI · Java · 104 lines · 48 code · 11 blank · 45 comment · 6 complexity · a202be10b2a09dcc70838d14271dcdca MD5 · raw file

  1. /*
  2. * Copyright (c) 2001, 2006, 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. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package org.jikesrvm.classlibrary.openjdk.replacements;
  26. import org.jikesrvm.VM;
  27. import org.jikesrvm.classloader.RVMClass;
  28. import org.jikesrvm.classloader.RVMType;
  29. import org.jikesrvm.runtime.StackBrowser;
  30. import org.jikesrvm.scheduler.RVMThread;
  31. import org.vmmagic.pragma.ReplaceClass;
  32. import org.vmmagic.pragma.ReplaceMember;
  33. @ReplaceClass(className = "sun.reflect.Reflection")
  34. public class sun_reflect_Reflection {
  35. private static final boolean DEBUG_GET_CALLER_CLASS = true;
  36. @ReplaceMember
  37. public static Class<?> getCallerClass(int i) {
  38. // TODO OPENJDK/ICEDTEA this implementation is rather messy. If we have to adjust this again,
  39. // we ought to write a better one, with a test case for all the cases.
  40. StackBrowser b = new StackBrowser();
  41. VM.disableGC();
  42. b.init();
  43. // int up = i;
  44. // VM.sysWriteln("up ", up);
  45. // while (up-- > 0)
  46. // {
  47. // b.up();
  48. // }
  49. b.up(); // skip sun.reflect.Reflection.getCallerClass (this call)
  50. b.up();
  51. /*
  52. * Skip Method.invoke and Constructor.newInstance, (if the caller was called by
  53. * reflection)
  54. */
  55. if (b.currentMethodIs_Java_Lang_Reflect_Method_InvokeMethod() || b.currentMethodIs_Java_Lang_Reflect_Constructor_NewInstance())
  56. {
  57. b.up();
  58. }
  59. /*
  60. * Work around OpenJDK's work around for Reflection.getCallerClass(..) in
  61. * java.lang.reflect.Method.invoke(..). The OpenJDK implementation of
  62. * getCallerClass assumes a fixed stack depth of 2. The Jikes RVM implementation
  63. * is different so we have to work around OpenJDK's work around
  64. */
  65. if (b.currentMethodIs_Java_Lang_Reflect_Method_GetCallerClass())
  66. {
  67. b.up();
  68. }
  69. /* Skip JNI if necessary */
  70. while (b.currentMethodIsPartOfJikesRVMJNIImplementation())
  71. {
  72. b.up();
  73. }
  74. /* Don't skip if we're already in the application */
  75. if (b.currentMethodIsInClassLibrary())
  76. {
  77. b.up(); // skip method that contains the call
  78. }
  79. RVMType ret = b.getCurrentClass();
  80. VM.enableGC();
  81. Class<?> clazz = ret.getClassForType();
  82. if (DEBUG_GET_CALLER_CLASS) {
  83. VM.sysWriteln("Returning caller class " + clazz + " for stack:");
  84. // RVMThread.dumpStack();
  85. }
  86. return clazz;
  87. }
  88. @ReplaceMember
  89. private static int getClassAccessFlags(Class c) {
  90. RVMClass clazz = JikesRVMSupport.getTypeForClass(c).asClass();
  91. return clazz.getOriginalModifiers();
  92. }
  93. }