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.core.OverridingClassLoader;
22:
23: /**
24: * Simplistic implementation of an instrumentable <code>ClassLoader</code>.
25: *
26: * <p>Usable in tests and standalone environments.
27: *
28: * @author Rod Johnson
29: * @author Costin Leau
30: * @since 2.0
31: */
32: public class SimpleInstrumentableClassLoader extends
33: OverridingClassLoader {
34:
35: private final WeavingTransformer weavingTransformer;
36:
37: /**
38: * Create a new <code>SimpleLoadTimeWeaver</code> for the given
39: * <code>ClassLoader</code>.
40: * @param parent the <code>ClassLoader</code> to build a simple
41: * instrumentable <code>ClassLoader</code> for
42: */
43: public SimpleInstrumentableClassLoader(ClassLoader parent) {
44: super (parent);
45: this .weavingTransformer = new WeavingTransformer(parent);
46: }
47:
48: /**
49: * Add a <code>ClassFileTransformer</code> to be applied by this
50: * <code>ClassLoader</code>.
51: * @param transformer the <code>ClassFileTransformer</code> to register
52: */
53: public void addTransformer(ClassFileTransformer transformer) {
54: this .weavingTransformer.addTransformer(transformer);
55: }
56:
57: @Override
58: protected byte[] transformIfNecessary(String name, byte[] bytes) {
59: return this.weavingTransformer
60: .transformIfNecessary(name, bytes);
61: }
62:
63: }
|