There are two ways to obtain an instance of the
Instrumentation
interface:
When a JVM is launched in a way that indicates an agent
class. In that case an Instrumentation
instance
is passed to the premain
method of the agent class.
When a JVM provides a mechanism to start agents sometime
after the JVM is launched. In that case an Instrumentation
instance is passed to the agentmain
method of the
agent code.
These mechanisms are described in the package specification.
Once an agent acquires an Instrumentation
instance,
the agent may call methods on the instance at any time.
API Note
This interface is not intended to be implemented outside of the java.instrument module.
Modifier and Type | Method and Description |
---|---|
public void | addTransformer(ClassFileTransformer
the transformer to register transformer, boolean can this transformer's transformations be retransformed canRetransform)Registers the supplied transformer. |
public void | addTransformer(ClassFileTransformer
the transformer to register transformer)Registers the supplied transformer. |
public void | appendToBootstrapClassLoaderSearch(JarFile
The JAR file to be searched when the bootstrap class loader
unsuccessfully searches for a class. jarfile)Specifies a JAR file with instrumentation classes to be defined by the bootstrap class loader. |
public void | appendToSystemClassLoaderSearch(JarFile
The JAR file to be searched when the system class loader
unsuccessfully searches for a class. jarfile)Specifies a JAR file with instrumentation classes to be defined by the system class loader. |
public Class | Returns: an array containing all the classes loaded by the JVM, zero-length if there are noneReturns an array of all classes currently loaded by the JVM. |
public Class | Returns: an array containing all classes whichloader can find by name;
zero-length if there are nonethe loader whose initiated class list will be returned loader)Returns an array of all classes which |
public long | Returns: an implementation-specific approximation of the amount of storage consumed by the specified objectthe object to size objectToSize)Returns an implementation-specific approximation of the amount of storage consumed by the specified object. |
public boolean | Returns: whether or not the argument class is modifiablethe class to check for being modifiable theClass)Tests whether a class is modifiable by retransformation or redefinition. |
public boolean | Returns: true if the module is modifiable, otherwise false the module to test if it can be modified module)Tests whether a module can be modified with |
public boolean | Returns: true if the current JVM configuration supports setting a native method prefix, false if not.Returns whether the current JVM configuration supports setting a native method prefix. |
public boolean | Returns: true if the current JVM configuration supports redefinition of classes, false if not.Returns whether or not the current JVM configuration supports redefinition of classes. |
public boolean | Returns: true if the current JVM configuration supports retransformation of classes, false if not.Returns whether or not the current JVM configuration supports retransformation of classes. |
public void | redefineClasses(ClassDefinition...
array of classes to redefine with corresponding definitions;
a zero-length array is allowed, in this case, this method does nothing definitions)Redefine the supplied set of classes using the supplied class files. |
public void | redefineModule(Module
the module to redefine module, Set<Module> the possibly-empty set of additional modules to read extraReads, Map<String, Set<Module>> the possibly-empty map of additional packages to export extraExports, Map<String, Set<Module>> the possibly-empty map of additional packages to open extraOpens, Set<Class<?>> the possibly-empty set of additional services to use extraUses, Map<Class<?>, List<Class<?>>> the possibly-empty map of additional services to provide extraProvides)Redefine a module to expand the set of modules that it reads, the set of packages that it exports or opens, or the services that it uses or provides. |
public boolean | Returns: true if the transformer was found and removed, false if the transformer was not foundthe transformer to unregister transformer)Unregisters the supplied transformer. |
public void | retransformClasses(Class<?>...
array of classes to retransform;
a zero-length array is allowed, in this case, this method does nothing classes)Retransform the supplied set of classes. |
public void | setNativeMethodPrefix(ClassFileTransformer
The ClassFileTransformer which wraps using this prefix. transformer, String The prefix to apply to wrapped native methods when
retrying a failed native method resolution. If prefix
is either prefix)null or the empty string, then
failed native method resolutions are not retried for
this transformer.This method modifies the failure handling of native method resolution by allowing retry with a prefix applied to the name. |
addTransformer | back to summary |
---|---|
public void addTransformer(ClassFileTransformer transformer, boolean canRetransform) Registers the supplied transformer. All future class definitions
will be seen by the transformer, except definitions of classes upon which any
registered transformer is dependent.
The transformer is called when classes are loaded, when they are
redefined. and if This method is intended for use in instrumentation, as described in the class specification.
|
addTransformer | back to summary |
---|---|
public void addTransformer(ClassFileTransformer transformer) Registers the supplied transformer.
Same as
|
appendToBootstrapClassLoaderSearch | back to summary |
---|---|
public void appendToBootstrapClassLoaderSearch(JarFile jarfile) Specifies a JAR file with instrumentation classes to be defined by the bootstrap class loader. When the virtual machine's built-in class loader, known as the "bootstrap
class loader", unsuccessfully searches for a class, the entries in the This method may be used multiple times to add multiple JAR files to be searched in the order that this method was invoked. The agent should take care to ensure that the JAR does not contain any
classes or resources other than those to be defined by the bootstrap
class loader for the purpose of instrumentation.
Failure to observe this warning could result in unexpected
behavior that is difficult to diagnose. For example, suppose there is a
loader L, and L's parent for delegation is the bootstrap class loader.
Furthermore, a method in class C, a class defined by L, makes reference to
a non-public accessor class C$1. If the JAR file contains a class C$1 then
the delegation to the bootstrap class loader will cause C$1 to be defined
by the bootstrap class loader. In this example an The Java Virtual Machine Specification specifies that a subsequent attempt to resolve a symbolic reference that the Java virtual machine has previously unsuccessfully attempted to resolve always fails with the same error that was thrown as a result of the initial resolution attempt. Consequently, if the JAR file contains an entry that corresponds to a class for which the Java virtual machine has unsuccessfully attempted to resolve a reference, then subsequent attempts to resolve that reference will fail with the same error as the initial attempt.
|
appendToSystemClassLoaderSearch | back to summary |
---|---|
public void appendToSystemClassLoaderSearch(JarFile jarfile) Specifies a JAR file with instrumentation classes to be defined by the
system class loader.
When the system class loader for delegation (see
This method may be used multiple times to add multiple JAR files to be searched in the order that this method was invoked. The agent should take care to ensure that the JAR does not contain any
classes or resources other than those to be defined by the system class
loader for the purpose of instrumentation.
Failure to observe this warning could result in unexpected
behavior that is difficult to diagnose (see
The system class loader supports adding a JAR file to be searched if
it implements a method named The Java Virtual Machine Specification specifies that a subsequent attempt to resolve a symbolic reference that the Java virtual machine has previously unsuccessfully attempted to resolve always fails with the same error that was thrown as a result of the initial resolution attempt. Consequently, if the JAR file contains an entry that corresponds to a class for which the Java virtual machine has unsuccessfully attempted to resolve a reference, then subsequent attempts to resolve that reference will fail with the same error as the initial attempt. This method does not change the value of
|
getAllLoadedClasses | back to summary |
---|---|
public Class Returns an array of all classes currently loaded by the JVM. The returned array includes all classes and interfaces, including hidden classes or interfaces, and array classes of all types.
|
getInitiatedClasses | back to summary |
---|---|
public Class Returns an array of all classes which The returned array does not include hidden classes or interfaces or array classes whose element type is a hidden class or interface. as they cannot be discovered by any class loader.
|
getObjectSize | back to summary |
---|---|
public long getObjectSize(Object objectToSize) Returns an implementation-specific approximation of the amount of storage consumed by the specified object. The result may include some or all of the object's overhead, and thus is useful for comparison within an implementation but not between implementations. The estimate may change during a single invocation of the JVM.
|
isModifiableClass | back to summary |
---|---|
public boolean isModifiableClass(Class<?> theClass) Tests whether a class is modifiable by
retransformation
or redefinition.
If a class is modifiable then this method returns
For a class to be retransformed,
Primitive classes (for example,
|
isModifiableModule | back to summary |
---|---|
public boolean isModifiableModule(Module module) Tests whether a module can be modified with
|
isNativeMethodPrefixSupported | back to summary |
---|---|
public boolean isNativeMethodPrefixSupported() Returns whether the current JVM configuration supports
setting a native method prefix.
The ability to set a native method prefix is an optional
capability of a JVM.
Setting a native method prefix will only be supported if the
|
isRedefineClassesSupported | back to summary |
---|---|
public boolean isRedefineClassesSupported() Returns whether or not the current JVM configuration supports redefinition
of classes.
The ability to redefine an already loaded class is an optional capability
of a JVM.
Redefinition will only be supported if the
|
isRetransformClassesSupported | back to summary |
---|---|
public boolean isRetransformClassesSupported() Returns whether or not the current JVM configuration supports retransformation
of classes.
The ability to retransform an already loaded class is an optional capability
of a JVM.
Retransformation will only be supported if the
|
redefineClasses | back to summary |
---|---|
public void redefineClasses(ClassDefinition... definitions) throws ClassNotFoundException, UnmodifiableClassException Redefine the supplied set of classes using the supplied class files.
This method is used to replace the definition of a class without reference
to the existing class file bytes, as one might do when recompiling from source
for fix-and-continue debugging.
Where the existing class file bytes are to be transformed (for
example in bytecode instrumentation)
This method operates on a set in order to allow interdependent changes to more than one class at the same time (a redefinition of class A can require a redefinition of class B). If a redefined method has active stack frames, those active frames continue to run the bytecodes of the original method. The redefined method will be used on new invokes. This method does not cause any initialization except that which would occur under the customary JVM semantics. In other words, redefining a class does not cause its initializers to be run. The values of static variables will remain as they were prior to the call. Instances of the redefined class are not affected. The supported class file changes are described in JVM TI RedefineClasses. The class file bytes are not checked, verified and installed until after the transformations have been applied, if the resultant bytes are in error this method will throw an exception. If this method throws an exception, no classes have been redefined. This method is intended for use in instrumentation, as described in the class specification.
|
redefineModule | back to summary |
---|---|
public void redefineModule(Module module, Set<Module> extraReads, Map<String, Set<Module>> extraExports, Map<String, Set<Module>> extraOpens, Set<Class<?>> extraUses, Map<Class<?>, List<Class<?>>> extraProvides) Redefine a module to expand the set of modules that it reads, the set of packages that it exports or opens, or the services that it uses or provides. This method facilitates the instrumentation of code in named modules where that instrumentation requires changes to the set of modules that are read, the packages that are exported or open, or the services that are used or provided. This method cannot reduce the set of modules that a module reads, nor reduce the set of packages that it exports or opens, nor reduce the set of services that it uses or provides. This method is a no-op when invoked to redefine an unnamed module. When expanding the services that a module uses or provides then the onus is on the agent to ensure that the service type will be accessible at each instrumentation site where the service type is used. This method does not check if the service type is a member of the module or in a package exported to the module by another module that it reads. The The This method is safe for concurrent use and so allows multiple agents to instrument and update the same module at around the same time.
|
removeTransformer | back to summary |
---|---|
public boolean removeTransformer(ClassFileTransformer transformer) Unregisters the supplied transformer. Future class definitions will not be shown to the transformer. Removes the most-recently-added matching instance of the transformer. Due to the multi-threaded nature of class loading, it is possible for a transformer to receive calls after it has been removed. Transformers should be written defensively to expect this situation.
|
retransformClasses | back to summary |
---|---|
public void retransformClasses(Class<?>... classes) throws UnmodifiableClassException Retransform the supplied set of classes.
This function facilitates the instrumentation
of already loaded classes.
When classes are initially loaded or when they are
redefined,
the initial class file bytes can be transformed with the
The order of transformation is described in
The initial class file bytes represent the bytes passed to
This method operates on a set in order to allow interdependent changes to more than one class at the same time (a retransformation of class A can require a retransformation of class B). If a retransformed method has active stack frames, those active frames continue to run the bytecodes of the original method. The retransformed method will be used on new invokes. This method does not cause any initialization except that which would occur under the customary JVM semantics. In other words, redefining a class does not cause its initializers to be run. The values of static variables will remain as they were prior to the call. Instances of the retransformed class are not affected. The supported class file changes are described in JVM TI RetransformClasses. The class file bytes are not checked, verified and installed until after the transformations have been applied, if the resultant bytes are in error this method will throw an exception. If this method throws an exception, no classes have been retransformed. This method is intended for use in instrumentation, as described in the class specification.
|
setNativeMethodPrefix | back to summary |
---|---|
public void setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) This method modifies the failure handling of
native method resolution by allowing retry
with a prefix applied to the name.
When used with the
Since native methods cannot be directly instrumented (they have no bytecodes), they must be wrapped with a non-native method which can be instrumented. For example, if we had: native boolean foo(int x); We could transform the class file (with the ClassFileTransformer during the initial definition of the class) so that this becomes: boolean foo(int x) { ... record entry to foo ... return wrapped_foo(x); } native boolean wrapped_foo(int x);
Where
The wrapper will allow data to be collected on the native
method call, but now the problem becomes linking up the
wrapped method with the native implementation.
That is, the method Java_somePackage_someClass_foo(JNIEnv* env, jint x)
This function allows the prefix to be specified and the
proper resolution to occur.
Specifically, when the standard resolution fails, the
resolution is retried taking the prefix into consideration.
There are two ways that resolution occurs, explicit
resolution with the JNI function
When this fails, the resolution will be retried with the specified prefix prepended to the method name, yielding the correct resolution:
For automatic resolution, the JVM will attempt:
When this fails, the resolution will be retried with the specified prefix deleted from the implementation name, yielding the correct resolution:
Note that since the prefix is only used when standard resolution fails, native methods can be wrapped selectively.
Since each
|