diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5eac309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c80cf2f --- /dev/null +++ b/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + top.meethigher + mock-instance + 1.0 + + + 8 + 8 + + + + + top.meethigher + cache-store + 1.1 + + + top.meethigher + object-converter + 1.1 + + + + + cglib + cglib + 3.3.0 + + + + + net.bytebuddy + byte-buddy + 1.12.18 + + + + + + diff --git a/src/main/java/top/meethigher/mock/instance/ObjectTest.java b/src/main/java/top/meethigher/mock/instance/ObjectTest.java new file mode 100644 index 0000000..d3e60b5 --- /dev/null +++ b/src/main/java/top/meethigher/mock/instance/ObjectTest.java @@ -0,0 +1,24 @@ +package top.meethigher.mock.instance; + +public class ObjectTest { + + private String name; + + private String money; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMoney() { + return money; + } + + public void setMoney(String money) { + this.money = money; + } +} \ No newline at end of file diff --git a/src/main/java/top/meethigher/mock/instance/Test.java b/src/main/java/top/meethigher/mock/instance/Test.java new file mode 100644 index 0000000..1e1df29 --- /dev/null +++ b/src/main/java/top/meethigher/mock/instance/Test.java @@ -0,0 +1,56 @@ +package top.meethigher.mock.instance; + + + +import net.bytebuddy.ByteBuddy; +import net.bytebuddy.dynamic.DynamicType; +import net.bytebuddy.implementation.FixedValue; +import net.bytebuddy.matcher.ElementMatchers; +import top.meethigher.mock.instance.proxy.MockInstanceHandler; + +public class Test { + + + + public static void main(String[] args) throws InstantiationException, IllegalAccessException { + +// //第一种方式,有注解:需要参考一下Lombok的做法 +// //@MockInstance +// //ObjectTest objectTest=new ObjectTest(); +// +// //第二种方式,无注解 +// ObjectTest objectTest = new MockInstanceHandler().instance(ObjectTest.class); +// +// /** +// * toString获取内容如下 +// *

+// * ObjectTest objectTest=new ObjectTest(); +// * objectTest.setMoney("money-test"); +// * objectTest.setName("name-test"); +// *

+// */ +//// System.out.println(objectTest.toString()); +// +// /** +// * 获取内容如下 +// *

+// * name-test +// *

+// */ +// System.out.println(objectTest.getName()); + + Class dynamicType = new ByteBuddy() + .subclass(Object.class) + .method(ElementMatchers.named("toString")) + .intercept(FixedValue.value("Hello World")) + .make() + .load(ObjectTest.class.getClassLoader()) + .getLoaded(); + + ObjectTest instance = (ObjectTest)dynamicType.newInstance(); + String toString = instance.toString(); + System.out.println(toString); + System.out.println(instance.getClass().getCanonicalName()); + + } +} diff --git a/src/main/java/top/meethigher/mock/instance/annotation/MockInstance.java b/src/main/java/top/meethigher/mock/instance/annotation/MockInstance.java new file mode 100644 index 0000000..fe15ee0 --- /dev/null +++ b/src/main/java/top/meethigher/mock/instance/annotation/MockInstance.java @@ -0,0 +1,15 @@ +package top.meethigher.mock.instance.annotation; + +import java.lang.annotation.*; + +/** + * 针对测试时使用的Mock注解 + * + * @author chenchuancheng + * @since 2022/11/8 16:12 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.LOCAL_VARIABLE) +public @interface MockInstance { +} diff --git a/src/main/java/top/meethigher/mock/instance/proxy/MockInstanceHandler.java b/src/main/java/top/meethigher/mock/instance/proxy/MockInstanceHandler.java new file mode 100644 index 0000000..1c94028 --- /dev/null +++ b/src/main/java/top/meethigher/mock/instance/proxy/MockInstanceHandler.java @@ -0,0 +1,27 @@ +package top.meethigher.mock.instance.proxy; + + +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +public class MockInstanceHandler { + + public T instance(Class clazz) { + if (clazz == null) { + throw new IllegalArgumentException("参数 'clazz' 不可为空"); + } + T t = (T) Enhancer.create(clazz, (MethodInterceptor) (o, method, objects, methodProxy) -> { + System.out.println(method.getName()); + return method.invoke(o, objects); + }); + return t; + } + + +}