KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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
21 import org.springframework.util.Assert;
22 import org.springframework.util.ClassUtils;
23
24 /**
25  * LoadTimeWeaver that builds and exposes a {@link SimpleInstrumentableClassLoader}.
26  * Mainly intended for testing environments, where it is sufficient to perform
27  * all class transformation on a newly created ClassLoader instance.
28  *
29  * @author Rod Johnson
30  * @author Juergen Hoeller
31  * @since 2.0
32  * @see #getInstrumentableClassLoader()
33  * @see SimpleInstrumentableClassLoader
34  * @see ReflectiveLoadTimeWeaver
35  */

36 public class SimpleLoadTimeWeaver implements LoadTimeWeaver {
37
38     private final SimpleInstrumentableClassLoader classLoader;
39
40
41     /**
42      * Create a new SimpleLoadTimeWeaver for the current context class loader.
43      */

44     public SimpleLoadTimeWeaver() {
45         this.classLoader = new SimpleInstrumentableClassLoader(ClassUtils.getDefaultClassLoader());
46     }
47
48     /**
49      * Create a new SimpleLoadTimeWeaver for the given class loader.
50      * @param classLoader the ClassLoader to build an simple instrumentable
51      * ClassLoader on top of
52      */

53     public SimpleLoadTimeWeaver(SimpleInstrumentableClassLoader classLoader) {
54         Assert.notNull(classLoader, "ClassLoader must not be null");
55         this.classLoader = classLoader;
56     }
57
58
59     public void addTransformer(ClassFileTransformer JavaDoc transformer) {
60         this.classLoader.addTransformer(transformer);
61     }
62
63     public ClassLoader JavaDoc getInstrumentableClassLoader() {
64         return this.classLoader;
65     }
66
67     /**
68      * This implementation builds a {@link SimpleThrowawayClassLoader}.
69      */

70     public ClassLoader JavaDoc getThrowawayClassLoader() {
71         return new SimpleThrowawayClassLoader(getInstrumentableClassLoader());
72     }
73
74 }
75
Popular Tags