aa51513
2021-05-15 d06fbb7626ca5c9e4e7a91ac06a6f2ae186c0f27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.java110.service.smo.impl;
 
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.NotFoundException;
import javassist.util.HotSwapper;
import junit.framework.TestCase;
 
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
public class QueryServiceSMOImplTest extends TestCase {
 
 
    public void testJava() throws CannotCompileException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NotFoundException, IOException, ClassNotFoundException {
        String javaCode = "public static void testJava2() {       DataQuery dataQuery = new DataQuery();\n dataQuery.setServiceCode(\"服务编码\");  System.out.println(dataQuery.getServiceCode());\n}\n";
                String    javaCode2 ="public static void testJava1() {     testJava2(); ServiceSql serviceSql = new ServiceSql();  System.out.println(\"623213\");\n}";
                String    javaCode3 ="public static void testJava3() {     ServiceSql serviceSql = new ServiceSql();  System.out.println(\"723213\");\n}";
        ClassPool classPool = ClassPool.getDefault();
        classPool.importPackage("com.java110.entity.service.DataQuery");
        classPool.importPackage("com.java110.entity.service.ServiceSql");
        CtClass ctClass = classPool.makeClass("com.java110.service.smo.WuxwTest3");
        CtMethod helloM = CtNewMethod.make(javaCode, ctClass);
        ctClass.addMethod(helloM);
 
        CtMethod helloM1 = CtNewMethod.make(javaCode2, ctClass);
        ctClass.addMethod(helloM1);
        Class<?> pc=ctClass.toClass();
        //ctClass.writeFile("./1111");
        Method move= pc.getMethod("testJava1",new Class[]{});
 
        Constructor<?> con=pc.getConstructor(new Class[]{});
 
        move.invoke(con);
 
        CtClass ctClass2 = classPool.get("com.java110.service.smo.WuxwTest3");
        CtMethod helloM3 = CtNewMethod.make(javaCode3, ctClass2);
        ctClass2.addMethod(helloM3);
        //ctClass.detach();
 
       // CtClass ctClass3 = classPool.get("com.java110.service.smo.WuxwTest1");
 
        /*HotSwapper swap = new HotSwapper(8000);
        swap.reload("com.java110.service.smo.WuxwTest3",ctClass2.toBytecode());
        Class ctClass1 = Class.forName("com.java110.service.smo.WuxwTest3");
 
 
         move= ctClass1.getMethod("testJava3",new Class[]{});
 
         con=ctClass1.getConstructor(new Class[]{});
 
        move.invoke(con);*/
 
 
 
 
 
 
 
    }
 
 
 
    public void testExistsJavaClass() throws Exception{
        ClassPool classPool = ClassPool.getDefault();
        CtClass ctClass = classPool.get("com.java110.core.javassist.Java110CoreTemplateJavassist");
 
 
 
        String javaCode = "public static void testJava2() {        System.out.println(\"123213\");\n}\n";
        String    javaCode2 ="public static void testJava1() {     testJava2();   System.out.println(\"223213\");\n}";
        CtMethod helloM = CtNewMethod.make(javaCode, ctClass);
        ctClass.addMethod(helloM);
 
        CtMethod helloM1 = CtNewMethod.make(javaCode2, ctClass);
        ctClass.addMethod(helloM1);
        //ctClass
        ctClass.writeFile("E:\\project\\HC\\MicroCommunity\\11");
    }
}