001/*
002 * Copyright 2002-2018 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
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027import org.springframework.util.StringUtils;
028
029/**
030 * ClassFileTransformer-based weaver, allowing for a list of transformers to be
031 * applied on a class byte array. Normally used inside class loaders.
032 *
033 * <p>Note: This class is deliberately implemented for minimal external dependencies,
034 * since it is included in weaver jars (to be deployed into application servers).
035 *
036 * @author Rod Johnson
037 * @author Costin Leau
038 * @author Juergen Hoeller
039 * @since 2.0
040 */
041public class WeavingTransformer {
042
043        @Nullable
044        private final ClassLoader classLoader;
045
046        private final List<ClassFileTransformer> transformers = new ArrayList<>();
047
048
049        /**
050         * Create a new WeavingTransformer for the given class loader.
051         * @param classLoader the ClassLoader to build a transformer for
052         */
053        public WeavingTransformer(@Nullable ClassLoader classLoader) {
054                this.classLoader = classLoader;
055        }
056
057
058        /**
059         * Add a class file transformer to be applied by this weaver.
060         * @param transformer the class file transformer to register
061         */
062        public void addTransformer(ClassFileTransformer transformer) {
063                Assert.notNull(transformer, "Transformer must not be null");
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 = StringUtils.replace(className, ".", "/");
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, @Nullable 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}