001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.instrument.classloading;
018
019import java.lang.instrument.ClassFileTransformer;
020
021import org.springframework.util.Assert;
022import org.springframework.util.ClassUtils;
023
024/**
025 * {@code LoadTimeWeaver} that builds and exposes a
026 * {@link SimpleInstrumentableClassLoader}.
027 *
028 * <p>Mainly intended for testing environments, where it is sufficient to
029 * perform all class transformation on a newly created
030 * {@code ClassLoader} instance.
031 *
032 * @author Rod Johnson
033 * @author Juergen Hoeller
034 * @since 2.0
035 * @see #getInstrumentableClassLoader()
036 * @see SimpleInstrumentableClassLoader
037 * @see ReflectiveLoadTimeWeaver
038 */
039public class SimpleLoadTimeWeaver implements LoadTimeWeaver {
040
041        private final SimpleInstrumentableClassLoader classLoader;
042
043
044        /**
045         * Create a new {@code SimpleLoadTimeWeaver} for the current context
046         * {@code ClassLoader}.
047         * @see SimpleInstrumentableClassLoader
048         */
049        public SimpleLoadTimeWeaver() {
050                this.classLoader = new SimpleInstrumentableClassLoader(ClassUtils.getDefaultClassLoader());
051        }
052
053        /**
054         * Create a new {@code SimpleLoadTimeWeaver} for the given
055         * {@code ClassLoader}.
056         * @param classLoader the {@code ClassLoader} to build a simple
057         * instrumentable {@code ClassLoader} on top of
058         */
059        public SimpleLoadTimeWeaver(SimpleInstrumentableClassLoader classLoader) {
060                Assert.notNull(classLoader, "ClassLoader must not be null");
061                this.classLoader = classLoader;
062        }
063
064
065        @Override
066        public void addTransformer(ClassFileTransformer transformer) {
067                this.classLoader.addTransformer(transformer);
068        }
069
070        @Override
071        public ClassLoader getInstrumentableClassLoader() {
072                return this.classLoader;
073        }
074
075        /**
076         * This implementation builds a {@link SimpleThrowawayClassLoader}.
077         */
078        @Override
079        public ClassLoader getThrowawayClassLoader() {
080                return new SimpleThrowawayClassLoader(getInstrumentableClassLoader());
081        }
082
083}