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