<commit_before>package com.ai.cloud.skywalking.plugin.interceptor;

import com.ai.cloud.skywalking.plugin.PluginCfg;
import com.ai.cloud.skywalking.util.StringUtil;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.pool.TypePool.Resolution;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.not;

public class EnhanceClazz4Interceptor {
    private static Logger logger = LogManager
            .getLogger(EnhanceClazz4Interceptor.class);

    private TypePool typePool;

    public static final String contextAttrName = "_$EnhancedClassInstanceContext";

    public EnhanceClazz4Interceptor() {
        typePool = TypePool.Default.ofClassPath();
    }

    public void enhance() {
        List<String> interceptorClassList = PluginCfg.CFG
                .getInterceptorClassList();

        for (String interceptorClassName : interceptorClassList) {
            try {
                enhance0(interceptorClassName);
            } catch (Throwable t) {
                logger.error("enhance class [{}] for intercept failure.",
                        interceptorClassName, t);
            }
        }
    }

    private void enhance0(String interceptorDefineClassName)
            throws InstantiationException, IllegalAccessException,
            ClassNotFoundException, EnhanceException {
        logger.debug("prepare to enhance class by {}.",
                interceptorDefineClassName);
        InterceptorDefine define = (InterceptorDefine) Class.forName(
                interceptorDefineClassName).newInstance();

        String enhanceOriginClassName = define.getBeInterceptedClassName();
        if (StringUtil.isEmpty(enhanceOriginClassName)) {
            logger.warn("classname of being intercepted is not defined by {}.",
                    interceptorDefineClassName);
            return;
        }

        logger.debug("prepare to enhance class {} by {}.",
                enhanceOriginClassName, interceptorDefineClassName);

        Resolution resolution = typePool.describe(enhanceOriginClassName);
        if (!resolution.isResolved()) {
            logger.warn("class {} can't be resolved, enhance by {} failue.",
                    enhanceOriginClassName, interceptorDefineClassName);
            return;
        }

        /**
         * find origin class source code for interceptor
         */
        DynamicType.Builder<?> newClassBuilder = new ByteBuddy()
                .rebase(resolution.resolve(),
                        ClassFileLocator.ForClassLoader.ofClassPath());

        /**
         * alter class source code.<br/>
         *
         * new class need:<br/>
         * 1.add field '_$EnhancedClassInstanceContext' of type
         * EnhancedClassInstanceContext <br/>
         *
         * 2.intercept constructor by default, and intercept method which it's
         * required by interceptorDefineClass. <br/>
         */
        IAroundInterceptor interceptor = define.instance();
        if (interceptor == null) {
            throw new EnhanceException("no IAroundInterceptor instance. ");
        }

        newClassBuilder = newClassBuilder
                .defineField(contextAttrName,
                        EnhancedClassInstanceContext.class)
                .constructor(any())
                .intercept(
                        SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(
                                new ClassConstructorInterceptor(interceptor))
                                .appendParameterBinder(
                                        FieldProxy.Binder.install(
                                                FieldGetter.class,
                                                FieldSetter.class))));

        MethodMatcher[] methodMatchers = define.getBeInterceptedMethodsMatchers();
        ClassMethodInterceptor classMethodInterceptor = new ClassMethodInterceptor(
                interceptor);

        StringBuilder enhanceRules = new StringBuilder("\nprepare to enhance class [" + enhanceOriginClassName + "] as following rules:\n");
        int ruleIdx = 1;
        for (MethodMatcher methodMatcher : methodMatchers) {
            enhanceRules.append("\t" + ruleIdx++ + ". " + methodMatcher + "\n");
        }
        logger.debug(enhanceRules);
        ElementMatcher.Junction matcher = null;
        for (MethodMatcher methodMatcher : methodMatchers) {
            logger.debug("enhance class {} by rule: {}",
                    enhanceOriginClassName, methodMatcher);
            if (matcher == null) {
                matcher = methodMatcher.buildMatcher();
                continue;
            }

            matcher = matcher.or(methodMatcher.buildMatcher());

        }

        // TODO: not support static method now. will enhance it.
        matcher = matcher.and(not(ElementMatchers.isStatic()));
        newClassBuilder = newClassBuilder.method(matcher).intercept(
                MethodDelegation.to(classMethodInterceptor));

        /**
         * naming class as origin class name, make and load class to
         * classloader.
         */
        newClassBuilder
                .name(enhanceOriginClassName)
                .make()
                .load(ClassLoader.getSystemClassLoader(),
                        ClassLoadingStrategy.Default.INJECTION).getLoaded();

        logger.debug("enhance class {} by {} completely.",
                enhanceOriginClassName, interceptorDefineClassName);
    }
}
<commit_msg>修复一个编译警告<commit_after>package com.ai.cloud.skywalking.plugin.interceptor;

import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.not;

import java.util.List;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.pool.TypePool.Resolution;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.ai.cloud.skywalking.plugin.PluginCfg;
import com.ai.cloud.skywalking.util.StringUtil;

public class EnhanceClazz4Interceptor {
    private static Logger logger = LogManager
            .getLogger(EnhanceClazz4Interceptor.class);

    private TypePool typePool;

    public static final String contextAttrName = "_$EnhancedClassInstanceContext";

    public EnhanceClazz4Interceptor() {
        typePool = TypePool.Default.ofClassPath();
    }

    public void enhance() {
        List<String> interceptorClassList = PluginCfg.CFG
                .getInterceptorClassList();

        for (String interceptorClassName : interceptorClassList) {
            try {
                enhance0(interceptorClassName);
            } catch (Throwable t) {
                logger.error("enhance class [{}] for intercept failure.",
                        interceptorClassName, t);
            }
        }
    }

    private void enhance0(String interceptorDefineClassName)
            throws InstantiationException, IllegalAccessException,
            ClassNotFoundException, EnhanceException {
        logger.debug("prepare to enhance class by {}.",
                interceptorDefineClassName);
        InterceptorDefine define = (InterceptorDefine) Class.forName(
                interceptorDefineClassName).newInstance();

        String enhanceOriginClassName = define.getBeInterceptedClassName();
        if (StringUtil.isEmpty(enhanceOriginClassName)) {
            logger.warn("classname of being intercepted is not defined by {}.",
                    interceptorDefineClassName);
            return;
        }

        logger.debug("prepare to enhance class {} by {}.",
                enhanceOriginClassName, interceptorDefineClassName);

        Resolution resolution = typePool.describe(enhanceOriginClassName);
        if (!resolution.isResolved()) {
            logger.warn("class {} can't be resolved, enhance by {} failue.",
                    enhanceOriginClassName, interceptorDefineClassName);
            return;
        }

        /**
         * find origin class source code for interceptor
         */
        DynamicType.Builder<?> newClassBuilder = new ByteBuddy()
                .rebase(resolution.resolve(),
                        ClassFileLocator.ForClassLoader.ofClassPath());

        /**
         * alter class source code.<br/>
         *
         * new class need:<br/>
         * 1.add field '_$EnhancedClassInstanceContext' of type
         * EnhancedClassInstanceContext <br/>
         *
         * 2.intercept constructor by default, and intercept method which it's
         * required by interceptorDefineClass. <br/>
         */
        IAroundInterceptor interceptor = define.instance();
        if (interceptor == null) {
            throw new EnhanceException("no IAroundInterceptor instance. ");
        }

        newClassBuilder = newClassBuilder
                .defineField(contextAttrName,
                        EnhancedClassInstanceContext.class)
                .constructor(any())
                .intercept(
                        SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(
                                new ClassConstructorInterceptor(interceptor))
                                .appendParameterBinder(
                                        FieldProxy.Binder.install(
                                                FieldGetter.class,
                                                FieldSetter.class))));

        MethodMatcher[] methodMatchers = define.getBeInterceptedMethodsMatchers();
        ClassMethodInterceptor classMethodInterceptor = new ClassMethodInterceptor(
                interceptor);

        StringBuilder enhanceRules = new StringBuilder("\nprepare to enhance class [" + enhanceOriginClassName + "] as following rules:\n");
        int ruleIdx = 1;
        for (MethodMatcher methodMatcher : methodMatchers) {
            enhanceRules.append("\t" + ruleIdx++ + ". " + methodMatcher + "\n");
        }
        logger.debug(enhanceRules);
        ElementMatcher.Junction<MethodDescription> matcher = null;
        for (MethodMatcher methodMatcher : methodMatchers) {
            logger.debug("enhance class {} by rule: {}",
                    enhanceOriginClassName, methodMatcher);
            if (matcher == null) {
                matcher = methodMatcher.buildMatcher();
                continue;
            }

            matcher = matcher.or(methodMatcher.buildMatcher());

        }

        /**
         * exclude static methods.
         */
        matcher = matcher.and(not(ElementMatchers.isStatic()));
        newClassBuilder = newClassBuilder.method(matcher).intercept(
                MethodDelegation.to(classMethodInterceptor));

        /**
         * naming class as origin class name, make and load class to
         * classloader.
         */
        newClassBuilder
                .name(enhanceOriginClassName)
                .make()
                .load(ClassLoader.getSystemClassLoader(),
                        ClassLoadingStrategy.Default.INJECTION).getLoaded();

        logger.debug("enhance class {} by {} completely.",
                enhanceOriginClassName, interceptorDefineClassName);
    }
}

{'fraction_non_alphanumeric': 0.08652342142092698, 'fraction_numerical': 0.0016464970774676875, 'mean_word_length': 2.2222811671087532, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '836168', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2877, 'n_words': 586}