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.

Agent.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Alexandre Vasseur initial implementation
  11. *******************************************************************************/
  12. package org.aspectj.weaver.loadtime;
  13. import java.lang.instrument.ClassFileTransformer;
  14. import java.lang.instrument.Instrumentation;
  15. /**
  16. * Java 1.5 preMain agent to hook in the class pre processor
  17. * Can be used with -javaagent:aspectjweaver.jar
  18. *
  19. * @author Alexandre Vasseur
  20. * @author Alexander Kriegisch
  21. */
  22. public class Agent {
  23. /**
  24. * The instrumentation instance
  25. */
  26. private static Instrumentation s_instrumentation;
  27. /**
  28. * The ClassFileTransformer wrapping the weaver
  29. */
  30. private static ClassFileTransformer s_transformer = new ClassPreProcessorAgentAdapter();
  31. /**
  32. * JSR-163 preMain Agent entry method
  33. *
  34. * @param options
  35. * @param instrumentation
  36. */
  37. public static void premain(String options, Instrumentation instrumentation) {
  38. /* Handle duplicate agents */
  39. if (s_instrumentation != null) {
  40. return;
  41. }
  42. s_instrumentation = instrumentation;
  43. s_instrumentation.addTransformer(s_transformer);
  44. }
  45. public static void agentmain(String options, Instrumentation instrumentation) {
  46. premain(options, instrumentation);
  47. }
  48. /**
  49. * Returns the Instrumentation system level instance
  50. */
  51. public static Instrumentation getInstrumentation() {
  52. if (s_instrumentation == null) {
  53. throw new UnsupportedOperationException(
  54. "AspectJ weaving agent was neither started via '-javaagent' (preMain) " +
  55. "nor attached via 'VirtualMachine.loadAgent' (agentMain)");
  56. }
  57. return s_instrumentation;
  58. }
  59. }