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.

JRockitAgent.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*******************************************************************************
  2. * Copyright (c) 2006 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Matthew Webster - initial implementation
  10. *******************************************************************************/
  11. package org.aspectj.weaver.loadtime;
  12. import java.util.Stack;
  13. import com.bea.jvm.ClassLibrary;
  14. import com.bea.jvm.JVMFactory;
  15. /**
  16. * BEA JRocket JMAPI agent.
  17. *
  18. * Use "-Xmanagement:class=org.aspectj.weaver.loadtime.JRockitAgent"
  19. */
  20. public class JRockitAgent implements com.bea.jvm.ClassPreProcessor {
  21. private ClassPreProcessor preProcessor;
  22. /*
  23. * This is used to implement the recursion protection offered by JVMTI but not by JRockit JMAPI. I we are called to preProcess a
  24. * class while already preProcessing another we will return immediately
  25. */
  26. private static ThreadLocalStack stack = new ThreadLocalStack();
  27. public JRockitAgent() {
  28. this.preProcessor = new Aj();
  29. ClassLibrary cl = JVMFactory.getJVM().getClassLibrary();
  30. cl.setClassPreProcessor(this);
  31. }
  32. public byte[] preProcess(ClassLoader loader, String className, byte[] bytes) {
  33. byte[] newBytes = bytes;
  34. if (stack.empty()) {
  35. stack.push(className);
  36. newBytes = preProcessor.preProcess(className, bytes, loader, null);
  37. stack.pop();
  38. }
  39. return newBytes;
  40. }
  41. private static class ThreadLocalStack extends ThreadLocal {
  42. public boolean empty() {
  43. Stack stack = (Stack) get();
  44. return stack.empty();
  45. }
  46. public Object peek() {
  47. Object obj = null;
  48. Stack stack = (Stack) get();
  49. if (!stack.empty())
  50. obj = stack.peek();
  51. return obj;
  52. }
  53. public void push(Object obj) {
  54. Stack stack = (Stack) get();
  55. if (!stack.empty() && obj == stack.peek())
  56. throw new RuntimeException(obj.toString());
  57. stack.push(obj);
  58. }
  59. public Object pop() {
  60. Stack stack = (Stack) get();
  61. return stack.pop();
  62. }
  63. protected Object initialValue() {
  64. return new Stack();
  65. }
  66. }
  67. }