KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > instrument > classloading > WeavingTransformer


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.instrument.classloading;
18
19 import java.lang.instrument.ClassFileTransformer JavaDoc;
20 import java.lang.instrument.IllegalClassFormatException JavaDoc;
21 import java.security.ProtectionDomain JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * ClassFileTransformer-based weaver, allowing for a list of transformers to be
27  * applied on a class byte array. Normally used inside class loaders.
28  *
29  * <p>Note: This class is deliberately implemented for minimal external dependencies,
30  * since it is included in weaver jars (to be deployed into application servers).
31  *
32  * @author Rod Johnson
33  * @author Costin Leau
34  * @author Juergen Hoeller
35  * @since 2.0
36  */

37 public class WeavingTransformer {
38
39     private final ClassLoader JavaDoc classLoader;
40
41     private final List JavaDoc<ClassFileTransformer JavaDoc> transformers = new ArrayList JavaDoc<ClassFileTransformer JavaDoc>();
42
43
44     /**
45      * Create a new WeavingTransformer for the current context class loader.
46      */

47     public WeavingTransformer() {
48         this(null);
49     }
50
51     /**
52      * Create a new WeavingTransformer for the given class loader.
53      * @param classLoader the ClassLoader to build a transformer for
54      */

55     public WeavingTransformer(ClassLoader JavaDoc classLoader) {
56         this.classLoader = (classLoader != null ? classLoader :getDefaultClassLoader());
57     }
58
59
60     /**
61      * Add a class file transformer to be applied by this weaver.
62      * @param transformer the class file transformer to register
63      */

64     public void addTransformer(ClassFileTransformer JavaDoc transformer) {
65         if (transformer == null) {
66             throw new IllegalArgumentException JavaDoc("Transformer must not be null");
67         }
68         this.transformers.add(transformer);
69     }
70
71
72     /**
73      * Apply transformation on a given class byte definition.
74      * The method will always return a non-null byte array (if no transformation has taken place
75      * the array content will be identical to the original one).
76      *
77      * @param className the full qualified name of the class in dot format (i.e. some.package.SomeClass)
78      * @param bytes class byte definition
79      * @return (possibly transformed) class byte definition
80      */

81     public byte[] transformIfNecessary(String JavaDoc className, byte[] bytes) {
82         String JavaDoc internalName = className.replace(".", "/");
83         return transformIfNecessary(className, internalName, bytes, null);
84     }
85
86     /**
87      * Apply transformation on a given class byte definition.
88      * The method will always return a non-null byte array (if no transformation has taken place
89      * the array content will be identical to the original one).
90      *
91      * @param className the full qualified name of the class in dot format (i.e. some.package.SomeClass)
92      * @param internalName class name internal name in / format (i.e. some/package/SomeClass)
93      * @param bytes class byte definition
94      * @param pd protection domain to be used (can be null)
95      *
96      * @return (possibly transformed) class byte definition
97      */

98     public byte[] transformIfNecessary(String JavaDoc className, String JavaDoc internalName, byte[] bytes, ProtectionDomain JavaDoc pd) {
99         byte[] result = bytes;
100         for (ClassFileTransformer JavaDoc cft : this.transformers) {
101             try {
102                 byte[] transformed = cft.transform(this.classLoader, internalName, null, pd, result);
103                 if (transformed != null) {
104                     result = transformed;
105                 }
106             }
107             catch (IllegalClassFormatException JavaDoc ex) {
108                 throw new IllegalStateException JavaDoc("Class file transformation failed", ex);
109             }
110         }
111         return result;
112     }
113
114
115     /**
116      * See ClassUtils. We don't depend on that to avoid pulling in more of Spring.
117      * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
118      */

119     protected ClassLoader JavaDoc getDefaultClassLoader() {
120         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
121         if (cl == null) {
122             // No thread context class loader -> use class loader of this class.
123
cl = getClass().getClassLoader();
124         }
125         return cl;
126     }
127
128 }
129
Popular Tags