KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > instrument > ClassFileTransformer


1 /*
2  * @(#)ClassFileTransformer.java 1.5 04/05/05
3  *
4  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5  */

6
7 package java.lang.instrument;
8
9
10 import java.security.ProtectionDomain JavaDoc;
11
12 /*
13  * Copyright 2003 Wily Technology, Inc.
14  */

15
16 /**
17  * An agent provides an implementation of this interface in order
18  * to transform class files.
19  * The transformation occurs before the class is defined by the JVM.
20  * <P>
21  * Note the term <i>class file</i> is used as defined in the chapter
22  * <a HREF="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#80959">The class File Format</a>
23  * of <i>The Java Virtual Machine Specification</i>, to mean a sequence
24  * of bytes in class file format, whether or not they reside in a file.
25  *
26  * @see java.lang.instrument.Instrumentation
27  * @see java.lang.instrument.Instrumentation#addTransformer
28  * @see java.lang.instrument.Instrumentation#removeTransformer
29  * @since JDK1.5
30  */

31
32 public interface ClassFileTransformer {
33     /**
34      * The implementation of this method may transform the supplied class file and
35      * return a new replacement class file.
36      *
37      * <P>
38      * Once a transformer has been registered with
39      * {@link java.lang.instrument.Instrumentation#addTransformer Instrumentation.addTransformer},
40      * the transformer will be called for every new class definition and every class redefinition.
41      * The request for a new class definition is made with
42      * {@link java.lang.ClassLoader#defineClass ClassLoader.defineClass}.
43      * The request for a class redefinition is made with
44      * {@link java.lang.instrument.Instrumentation#redefineClasses Instrumentation.redefineClasses}
45      * or its native equivalents.
46      * The transformer is called during the processing of the request, before the class file bytes
47      * have been verified or applied.
48      *
49      * <P>
50      * If the implementing method determines that no transformations are needed,
51      * it should return <code>null</code>.
52      * Otherwise, it should create a new <code>byte[]</code> array,
53      * copy the input <code>classfileBuffer</code> into it,
54      * along with all desired transformations, and return the new array.
55      * The input <code>classfileBuffer</code> must not be modified.
56      *
57      * <P>
58      * In the redefine case, the transformer must support the redefinition semantics.
59      * If a class that the transformer changed during initial definition is later redefined, the
60      * transformer must insure that the second class output class file is a legal
61      * redefinition of the first output class file.
62      *
63      * <P>
64      * If the transformer believes the <code>classFileBuffer</code> does not
65      * represent a validly formatted class file, it should throw
66      * an <code>IllegalClassFormatException</code>. Subsequent transformers
67      * will still be called and the load or redefine will still
68      * be attempted. Throwing an <code>IllegalClassFormatException</code> thus
69      * has the same effect as returning null but facilitates the
70      * logging or debugging of format corruptions.
71      *
72      * @param loader the defining loader of the class to be transformed,
73      * may be <code>null</code> if the bootstrap loader
74      * @param className the name of the class in the internal form of fully
75      * qualified class and interface names as defined in
76      * <i>The Java Virtual Machine Specification</i>.
77      * For example, <code>"java/util/List"</code>.
78      * @param classBeingRedefined if this is a redefine, the class being redefined,
79      * otherwise <code>null</code>
80      * @param protectionDomain the protection domain of the class being defined or redefined
81      * @param classfileBuffer the input byte buffer in class file format - must not be modified
82      *
83      * @throws IllegalClassFormatException if the input does not represent a well-formed class file
84      * @return a well-formed class file buffer (the result of the transform),
85                 or <code>null</code> if no transform is performed.
86      * @see Instrumentation#redefineClasses
87      */

88     byte[]
89     transform( ClassLoader JavaDoc loader,
90                 String JavaDoc className,
91                 Class JavaDoc<?> classBeingRedefined,
92                 ProtectionDomain JavaDoc protectionDomain,
93                 byte[] classfileBuffer)
94         throws IllegalClassFormatException JavaDoc;
95 }
96
Popular Tags