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 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.Instrumentation;
  14. import java.lang.instrument.ClassFileTransformer;
  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 <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
  20. */
  21. public class Agent {
  22. /**
  23. * The instrumentation instance
  24. */
  25. private static Instrumentation s_instrumentation;
  26. /**
  27. * The ClassFileTransformer wrapping the weaver
  28. */
  29. private static ClassFileTransformer s_transformer = new ClassPreProcessorAgentAdapter();
  30. /**
  31. * JSR-163 preMain Agent entry method
  32. *
  33. * @param options
  34. * @param instrumentation
  35. */
  36. public static void premain(String options, Instrumentation instrumentation) {
  37. /* Handle duplicate agents */
  38. if (s_instrumentation != null) {
  39. return;
  40. }
  41. s_instrumentation = instrumentation;
  42. s_instrumentation.addTransformer(s_transformer);
  43. }
  44. /**
  45. * Returns the Instrumentation system level instance
  46. */
  47. public static Instrumentation getInstrumentation() {
  48. if (s_instrumentation == null) {
  49. throw new UnsupportedOperationException("Java 5 was not started with preMain -javaagent for AspectJ");
  50. }
  51. return s_instrumentation;
  52. }
  53. }