1. /*
  2. * @(#)Instrumentation.java 1.7 04/06/08
  3. *
  4. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  5. */
  6. package java.lang.instrument;
  7. import java.io.File;
  8. import java.io.IOException;
  9. /*
  10. * Copyright 2003 Wily Technology, Inc.
  11. */
  12. /**
  13. * This class provides services needed to instrument Java
  14. * programming language code.
  15. * Instrumentation is the addition of byte-codes to methods for the
  16. * purpose of gathering data to be utilized by tools.
  17. * Since the changes are purely additive, these tools do not modify
  18. * application state or behavior.
  19. * Examples of such benign tools include monitoring agents, profilers,
  20. * coverage analyzers, and event loggers.
  21. *
  22. * <P>
  23. * The only way to access an instance of the <code>Instrumentation</code>
  24. * interface is for the JVM to be launched in a way that indicates
  25. * the agent class - see
  26. * {@linkplain java.lang.instrument the package specification}.
  27. * The <code>Instrumentation</code> instance is passed
  28. * to the <code>premain</code> method of the agent class.
  29. * Once an agent acquires the <code>Instrumentation</code> instance,
  30. * the agent may call methods on the instance at any time.
  31. *
  32. * @since JDK1.5
  33. */
  34. public interface Instrumentation {
  35. /**
  36. * Registers the supplied transformer. All future class definitions
  37. * will be seen by the transformer, except definitions of classes upon which any
  38. * registered transformer is dependent. If multiple transformers are
  39. * registered, they will be called in the order added. If a transformer throws
  40. * during execution, the JVM will still call the other registered transformers in order.
  41. * The same transformer may be added more than once.
  42. * All transformers registered with <code>addTransformer</code>
  43. * will always see the class files before any external JVMTI ClassFileLoadHook event listener does.
  44. * <P>
  45. * This method is intended for use in instrumentation, as described in the
  46. * {@linkplain Instrumentation class specification}.
  47. *
  48. * @param transformer the transformer to register
  49. * @throws java.lang.NullPointerException if passed a <code>null</code> transformer
  50. */
  51. void
  52. addTransformer(ClassFileTransformer transformer);
  53. /**
  54. * Unregisters the supplied transformer. Future class definitions will
  55. * not be shown to the transformer. Removes the most-recently-added matching
  56. * instance of the transformer. Due to the multi-threaded nature of
  57. * class loading, it is possible for a transformer to receive calls
  58. * after it has been removed. Transformers should be written defensively
  59. * to expect this situation.
  60. *
  61. * @param transformer the transformer to unregister
  62. * @return true if the transformer was found and removed, false if the
  63. * transformer was not found
  64. * @throws java.lang.NullPointerException if passed a <code>null</code> transformer
  65. */
  66. boolean
  67. removeTransformer(ClassFileTransformer transformer);
  68. /**
  69. * Returns whether or not the current JVM configuration supports redefinition of classes.
  70. * The ability to redefine an already loaded class is an optional capability
  71. * of a JVM.
  72. * During a single instantiation of a single JVM, multiple calls to this
  73. * method will always return the same answer.
  74. * @return true if the current JVM configuration supports redefinition of classes,
  75. * false if not.
  76. * @see #redefineClasses
  77. */
  78. boolean
  79. isRedefineClassesSupported();
  80. /**
  81. * Redefine the supplied set of classes using the supplied class files. Operates on
  82. * a set in order to allow interlocked changes to more than one class at the same time
  83. * (a redefinition of class A can require a redefinition of class B).
  84. *
  85. * <P>
  86. * If a redefined method has active stack frames, those active frames continue to
  87. * run the bytecodes of the original method.
  88. * The redefined method will be used on new invokes.
  89. *
  90. * <P>
  91. * This method does not cause any initialization except that which would occur
  92. * under the customary JVM semantics. In other words, redefining a class
  93. * does not cause its initializers to be run. The values of static variables
  94. * will remain as they were prior to the call.
  95. *
  96. * <P>
  97. * Instances of the redefined class are not affected.
  98. *
  99. * <P>
  100. * Registered transformers will be called before the redefine operation is applied.
  101. *
  102. * <P>
  103. * The redefinition may change method bodies, the constant pool and attributes.
  104. * The redefinition must not add, remove or rename fields or methods, change the
  105. * signatures of methods, or change inheritance. These restrictions maybe be
  106. * lifted in future versions.
  107. *
  108. * <P>
  109. * A zero-length <code>definitions</code> array is allowed, in this case, this
  110. * method does nothing.
  111. *
  112. * <P>
  113. * If this method throws an exception, no classes have been redefined.
  114. * <P>
  115. * This method is intended for use in instrumentation, as described in the
  116. * {@linkplain Instrumentation class specification}.
  117. *
  118. * @param definitions array of classes to redefine with corresponding definitions
  119. * @throws java.lang.ClassNotFoundException if a specified class cannot be found
  120. * @throws java.lang.instrument.UnmodifiableClassException if a specified class cannot be modified
  121. * @throws java.lang.UnsupportedOperationException if the current configuration of the JVM does not allow
  122. * redefinition ({@link #isRedefineClassesSupported} is false) or the redefinition made unsupported changes
  123. * @throws java.lang.ClassFormatError if the data did not contain a valid class
  124. * @throws java.lang.NoClassDefFoundError if the name in the class file is not equal to the name of the class
  125. * @throws java.lang.UnsupportedClassVersionError if the class file version numbers are not supported
  126. * @throws java.lang.ClassCircularityError if the new classes contain a circularity
  127. * @throws java.lang.LinkageError if a linkage error occurs
  128. * @throws java.lang.NullPointerException if the supplied definitions array or any of its components is <code>null</code>.
  129. *
  130. * @see #isRedefineClassesSupported
  131. * @see #addTransformer
  132. * @see java.lang.instrument.ClassFileTransformer
  133. */
  134. void
  135. redefineClasses(ClassDefinition[] definitions)
  136. throws ClassNotFoundException, UnmodifiableClassException;
  137. /**
  138. * Returns an array of all classes currently loaded by the JVM.
  139. *
  140. * @return an array containing all the classes loaded by the JVM, zero-length if there are none
  141. */
  142. Class[]
  143. getAllLoadedClasses();
  144. /**
  145. * Returns an array of all classes for which <code>loader</code> is an initiating loader.
  146. * If the supplied loader is <code>null</code>, classes initiated by the bootstrap class
  147. * loader are returned.
  148. *
  149. * @param loader the loader whose initiated class list will be returned
  150. * @return an array containing all the classes for which loader is an initiating loader,
  151. * zero-length if there are none
  152. */
  153. Class[]
  154. getInitiatedClasses(ClassLoader loader);
  155. /**
  156. * Returns an implementation-specific approximation of the amount of storage consumed by
  157. * the specified object. The result may include some or all of the object's overhead,
  158. * and thus is useful for comparison within an implementation but not between implementations.
  159. *
  160. * The estimate may change during a single invocation of the JVM.
  161. *
  162. * @param objectToSize the object to size
  163. * @return an implementation-specific approximation of the amount of storage consumed by the specified object
  164. * @throws java.lang.NullPointerException if the supplied Object is <code>null</code>.
  165. */
  166. long
  167. getObjectSize(Object objectToSize);
  168. }