bytebuddy代理静态类(内部类)拦截所有请求的一个问题NoSuchTypeException: Cannot resolve type description for

问题描述

Exception in thread "main" net.bytebuddy.pool.TypePool$Resolution$NoSuchTypeException: Cannot resolve type description for 

一、我出错的代码: 

 

    public static void init() {
        TypePool typePool = TypePool.Default.ofSystemLoader();
        new ByteBuddy()
                .redefine(typePool.describe("org.XXXX.HttpConnection.Response").resolve(),
                        ClassFileLocator.ForClassLoader.ofSystemLoader())
                .method(named("writePost"))
                .intercept(MethodDelegation.to(new GeneralInterceptor()))
                .make()
                .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded();

    }

二、这个问题关键在于

NoSuchTypeException 一般都是不存在这个class对象

所以我们应该去编译文件找找这个文件在不在

1、我就去找jar中文件是否存在,打开文件发现文件名字是这个,不对

bytebuddy代理静态类(内部类)拦截所有请求的一个问题NoSuchTypeException: Cannot resolve type description for_第1张图片

三、解决办法:

   重新修改了代码:

   关键修改的点:"org.XXXX.HttpConnection$Response

    public static void init() {
        TypePool typePool = TypePool.Default.ofSystemLoader();
        new ByteBuddy()
                .redefine(typePool.describe("org.XXXX.HttpConnection$Response").resolve(),
                        ClassFileLocator.ForClassLoader.ofSystemLoader())
                .method(named("writePost"))
                .intercept(MethodDelegation.to(new GeneralInterceptor()))
                .make()
                .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded();

    }

补充: 

 GeneralInterceptor 处理类:

import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;

import java.lang.reflect.Method;

public class GeneralInterceptor {

    @RuntimeType
    public Object intercept(@AllArguments Object[] allArguments,
                            @Origin Method method) {
        System.out.println(method.getName() + "执行了");
        return null;
    }
}

你可能感兴趣的:(java,前端,javascript)