parent
							
								
									8294db3e2a
								
							
						
					
					
						commit
						e2b65fe549
					
				
				 6 changed files with 200 additions and 0 deletions
			
			
		| @ -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/ | ||||
| @ -0,0 +1,45 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <groupId>top.meethigher</groupId> | ||||
|     <artifactId>mock-instance</artifactId> | ||||
|     <version>1.0</version> | ||||
| 
 | ||||
|     <properties> | ||||
|         <maven.compiler.source>8</maven.compiler.source> | ||||
|         <maven.compiler.target>8</maven.compiler.target> | ||||
|     </properties> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>top.meethigher</groupId> | ||||
|             <artifactId>cache-store</artifactId> | ||||
|             <version>1.1</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>top.meethigher</groupId> | ||||
|             <artifactId>object-converter</artifactId> | ||||
|             <version>1.1</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <!-- https://mvnrepository.com/artifact/cglib/cglib --> | ||||
|         <dependency> | ||||
|             <groupId>cglib</groupId> | ||||
|             <artifactId>cglib</artifactId> | ||||
|             <version>3.3.0</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy --> | ||||
|         <dependency> | ||||
|             <groupId>net.bytebuddy</groupId> | ||||
|             <artifactId>byte-buddy</artifactId> | ||||
|             <version>1.12.18</version> | ||||
|         </dependency> | ||||
| 
 | ||||
| 
 | ||||
|     </dependencies> | ||||
| 
 | ||||
| </project> | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
| @ -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获取内容如下
 | ||||
| //         * <p>
 | ||||
| //         * ObjectTest objectTest=new ObjectTest();
 | ||||
| //         * objectTest.setMoney("money-test");
 | ||||
| //         * objectTest.setName("name-test");
 | ||||
| //         * </p>
 | ||||
| //         */
 | ||||
| ////        System.out.println(objectTest.toString());
 | ||||
| //
 | ||||
| //        /**
 | ||||
| //         * 获取内容如下
 | ||||
| //         * <p>
 | ||||
| //         * name-test
 | ||||
| //         * </p>
 | ||||
| //         */
 | ||||
| //        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()); | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| @ -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 { | ||||
| } | ||||
| @ -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> 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; | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue