Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问我在使用processcontroller.returnimmediately(object)的时候 无法构造mock对象 要怎么处理呢   #336

Closed
Maginx opened this issue Aug 1, 2021 · 5 comments

Comments

@Maginx
Copy link

Maginx commented Aug 1, 2021

No description provided.

@Maginx Maginx changed the title process processcontroller.returnimmediately object Aug 1, 2021
@Maginx Maginx changed the title processcontroller.returnimmediately object 请问我在使用processcontroller.returnimmediately(object)的时候 无法构造mock对象 要怎么处理呢   Aug 1, 2021
@Maginx
Copy link
Author

Maginx commented Aug 1, 2021

提示castclassexcpetion 异常,应该是因为不同的classloader问题。请问有什么办法可以获取到jvm进程中的classloader进行对象实例化嘛

@ijustyce
Copy link

@Maginx 我遇到了同样的问题,你解决了没呢?方便讨论下嘛?我的场景是,使用了mybatis,然后将基本的增删改查丢到了泛型的父类里面,子类继承父类并指定泛型的类型,在这儿 return 时就会报错,告诉我 classcastexception cannot cast 子类 to 父类 …

@tmaerd
Copy link

tmaerd commented Aug 16, 2022

@Maginx 我遇到了同样的问题,你解决了没呢?方便讨论下嘛?我的场景是,使用了mybatis,然后将基本的增删改查丢到了泛型的父类里面,子类继承父类并指定泛型的类型,在这儿 return 时就会报错,告诉我 classcastexception cannot cast 子类 to 父类 …

需要将在module中定义的类,使用宿主classloader进行加载,如javaasssit实现:
protected void before(Advice advice) throws Throwable {
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(A.class));
CtClass ctClass= pool.get("com.A");
ctClass.toClass(advice.getLoader(), null);
Class<?> loadClass = ctClass.toClass(advice.getLoader(), null);
// 通过ProcessController来改变原有方法的执行流程
ProcessController.returnImmediately(loadClass.newInstance());
}

@oldmanpushcart
Copy link
Collaborator

@MaginxBeforeEvent.javaClassLoader可以拿到对应的ClassLoader,当然像 @tmaerd 一样从Advice中拿会更方便。

@reducm
Copy link

reducm commented Oct 19, 2022

对于泛型的获取,录制与Mock的过程都有各种不同情况,简单的原则是,原来代码的泛型类型定义在哪里(类 方法返回 方法参数)就从哪里获取

例如GuavaCache,get的真实返回值的,要回塑到CacheBuilder的动态load方法的签名,通过反射就能获取

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants