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: * <code>LoadTimeWeaver</code> that builds and exposes a
26: * {@link SimpleInstrumentableClassLoader}.
27: *
28: * <p>Mainly intended for testing environments, where it is sufficient to
29: * perform all class transformation on a newly created
30: * <code>ClassLoader</code> instance.
31: *
32: * @author Rod Johnson
33: * @author Juergen Hoeller
34: * @since 2.0
35: * @see #getInstrumentableClassLoader()
36: * @see SimpleInstrumentableClassLoader
37: * @see ReflectiveLoadTimeWeaver
38: */
39: public class SimpleLoadTimeWeaver implements LoadTimeWeaver {
40:
41: private final SimpleInstrumentableClassLoader classLoader;
42:
43: /**
44: * Create a new <code>SimpleLoadTimeWeaver</code> for the current context
45: * <code>ClassLoader</code>.
46: * @see SimpleInstrumentableClassLoader
47: */
48: public SimpleLoadTimeWeaver() {
49: this .classLoader = new SimpleInstrumentableClassLoader(
50: ClassUtils.getDefaultClassLoader());
51: }
52:
53: /**
54: * Create a new <code>SimpleLoadTimeWeaver</code> for the given
55: * <code>ClassLoader</code>.
56: * @param classLoader the <code>ClassLoader</code> to build a simple
57: * instrumentable <code>ClassLoader</code> on top of
58: */
59: public SimpleLoadTimeWeaver(
60: SimpleInstrumentableClassLoader classLoader) {
61: Assert.notNull(classLoader, "ClassLoader must not be null");
62: this .classLoader = classLoader;
63: }
64:
65: public void addTransformer(ClassFileTransformer transformer) {
66: this .classLoader.addTransformer(transformer);
67: }
68:
69: public ClassLoader getInstrumentableClassLoader() {
70: return this .classLoader;
71: }
72:
73: /**
74: * This implementation builds a {@link SimpleThrowawayClassLoader}.
75: */
76: public ClassLoader getThrowawayClassLoader() {
77: return new SimpleThrowawayClassLoader(
78: getInstrumentableClassLoader());
79: }
80:
81: }
|