1. /*
  2. * @(#)ClassFileTransformer.java 1.5 04/05/05
  3. *
  4. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  5. */
  6. package java.lang.instrument;
  7. import java.security.ProtectionDomain;
  8. /*
  9. * Copyright 2003 Wily Technology, Inc.
  10. */
  11. /**
  12. * An agent provides an implementation of this interface in order
  13. * to transform class files.
  14. * The transformation occurs before the class is defined by the JVM.
  15. * <P>
  16. * Note the term <i>class file</i> is used as defined in the chapter
  17. * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#80959">The class File Format</a>
  18. * of <i>The Java Virtual Machine Specification</i>, to mean a sequence
  19. * of bytes in class file format, whether or not they reside in a file.
  20. *
  21. * @see java.lang.instrument.Instrumentation
  22. * @see java.lang.instrument.Instrumentation#addTransformer
  23. * @see java.lang.instrument.Instrumentation#removeTransformer
  24. * @since JDK1.5
  25. */
  26. public interface ClassFileTransformer {
  27. /**
  28. * The implementation of this method may transform the supplied class file and
  29. * return a new replacement class file.
  30. *
  31. * <P>
  32. * Once a transformer has been registered with
  33. * {@link java.lang.instrument.Instrumentation#addTransformer Instrumentation.addTransformer},
  34. * the transformer will be called for every new class definition and every class redefinition.
  35. * The request for a new class definition is made with
  36. * {@link java.lang.ClassLoader#defineClass ClassLoader.defineClass}.
  37. * The request for a class redefinition is made with
  38. * {@link java.lang.instrument.Instrumentation#redefineClasses Instrumentation.redefineClasses}
  39. * or its native equivalents.
  40. * The transformer is called during the processing of the request, before the class file bytes
  41. * have been verified or applied.
  42. *
  43. * <P>
  44. * If the implementing method determines that no transformations are needed,
  45. * it should return <code>null</code>.
  46. * Otherwise, it should create a new <code>byte[]</code> array,
  47. * copy the input <code>classfileBuffer</code> into it,
  48. * along with all desired transformations, and return the new array.
  49. * The input <code>classfileBuffer</code> must not be modified.
  50. *
  51. * <P>
  52. * In the redefine case, the transformer must support the redefinition semantics.
  53. * If a class that the transformer changed during initial definition is later redefined, the
  54. * transformer must insure that the second class output class file is a legal
  55. * redefinition of the first output class file.
  56. *
  57. * <P>
  58. * If the transformer believes the <code>classFileBuffer</code> does not
  59. * represent a validly formatted class file, it should throw
  60. * an <code>IllegalClassFormatException</code>. Subsequent transformers
  61. * will still be called and the load or redefine will still
  62. * be attempted. Throwing an <code>IllegalClassFormatException</code> thus
  63. * has the same effect as returning null but facilitates the
  64. * logging or debugging of format corruptions.
  65. *
  66. * @param loader the defining loader of the class to be transformed,
  67. * may be <code>null</code> if the bootstrap loader
  68. * @param className the name of the class in the internal form of fully
  69. * qualified class and interface names as defined in
  70. * <i>The Java Virtual Machine Specification</i>.
  71. * For example, <code>"java/util/List"</code>.
  72. * @param classBeingRedefined if this is a redefine, the class being redefined,
  73. * otherwise <code>null</code>
  74. * @param protectionDomain the protection domain of the class being defined or redefined
  75. * @param classfileBuffer the input byte buffer in class file format - must not be modified
  76. *
  77. * @throws IllegalClassFormatException if the input does not represent a well-formed class file
  78. * @return a well-formed class file buffer (the result of the transform),
  79. or <code>null</code> if no transform is performed.
  80. * @see Instrumentation#redefineClasses
  81. */
  82. byte[]
  83. transform( ClassLoader loader,
  84. String className,
  85. Class<?> classBeingRedefined,
  86. ProtectionDomain protectionDomain,
  87. byte[] classfileBuffer)
  88. throws IllegalClassFormatException;
  89. }