001/*
002 * Copyright 2002-2007 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;
020import java.lang.instrument.IllegalClassFormatException;
021import java.security.ProtectionDomain;
022import java.util.ArrayList;
023import java.util.List;
024
025/**
026 * ClassFileTransformer-based weaver, allowing for a list of transformers to be
027 * applied on a class byte array. Normally used inside class loaders.
028 *
029 * <p>Note: This class is deliberately implemented for minimal external dependencies,
030 * since it is included in weaver jars (to be deployed into application servers).
031 *
032 * @author Rod Johnson
033 * @author Costin Leau
034 * @author Juergen Hoeller
035 * @since 2.0
036 */
037public class WeavingTransformer {
038
039        private final ClassLoader classLoader;
040
041        private final List<ClassFileTransformer> transformers = new ArrayList<ClassFileTransformer>();
042
043
044        /**
045         * Create a new WeavingTransformer for the given class loader.
046         * @param classLoader the ClassLoader to build a transformer for
047         */
048        public WeavingTransformer(ClassLoader classLoader) {
049                if (classLoader == null) {
050                        throw new IllegalArgumentException("ClassLoader must not be null");
051                }
052                this.classLoader = classLoader;
053        }
054
055
056        /**
057         * Add a class file transformer to be applied by this weaver.
058         * @param transformer the class file transformer to register
059         */
060        public void addTransformer(ClassFileTransformer transformer) {
061                if (transformer == null) {
062                        throw new IllegalArgumentException("Transformer must not be null");
063                }
064                this.transformers.add(transformer);
065        }
066
067
068        /**
069         * Apply transformation on a given class byte definition.
070         * The method will always return a non-null byte array (if no transformation has taken place
071         * the array content will be identical to the original one).
072         * @param className the full qualified name of the class in dot format (i.e. some.package.SomeClass)
073         * @param bytes class byte definition
074         * @return (possibly transformed) class byte definition
075         */
076        public byte[] transformIfNecessary(String className, byte[] bytes) {
077                String internalName = className.replace(".", "/");
078                return transformIfNecessary(className, internalName, bytes, null);
079        }
080
081        /**
082         * Apply transformation on a given class byte definition.
083         * The method will always return a non-null byte array (if no transformation has taken place
084         * the array content will be identical to the original one).
085         * @param className the full qualified name of the class in dot format (i.e. some.package.SomeClass)
086         * @param internalName class name internal name in / format (i.e. some/package/SomeClass)
087         * @param bytes class byte definition
088         * @param pd protection domain to be used (can be null)
089         * @return (possibly transformed) class byte definition
090         */
091        public byte[] transformIfNecessary(String className, String internalName, byte[] bytes, ProtectionDomain pd) {
092                byte[] result = bytes;
093                for (ClassFileTransformer cft : this.transformers) {
094                        try {
095                                byte[] transformed = cft.transform(this.classLoader, internalName, null, pd, result);
096                                if (transformed != null) {
097                                        result = transformed;
098                                }
099                        }
100                        catch (IllegalClassFormatException ex) {
101                                throw new IllegalStateException("Class file transformation failed", ex);
102                        }
103                }
104                return result;
105        }
106
107}