01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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: * Create a new SimpleLoadTimeWeaver for the current context class loader.
42: */
43: public SimpleLoadTimeWeaver() {
44: this .classLoader = new SimpleInstrumentableClassLoader(
45: 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(
54: SimpleInstrumentableClassLoader classLoader) {
55: Assert.notNull(classLoader, "ClassLoader must not be null");
56: this .classLoader = classLoader;
57: }
58:
59: public void addTransformer(ClassFileTransformer transformer) {
60: this .classLoader.addTransformer(transformer);
61: }
62:
63: public ClassLoader getInstrumentableClassLoader() {
64: return this .classLoader;
65: }
66:
67: /**
68: * This implementation builds a {@link SimpleThrowawayClassLoader}.
69: */
70: public ClassLoader getThrowawayClassLoader() {
71: return new SimpleThrowawayClassLoader(
72: getInstrumentableClassLoader());
73: }
74:
75: }
|