package com.java110.service.smo.impl;


import javassist.CannotCompileException;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import junit.framework.TestCase;

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 {
        String javaCode = "public static void testJava2() {        System.out.println(\"123213\");\n}\n";
                String    javaCode2 ="public static void testJava1() {     testJava2();   System.out.println(\"223213\");\n}";
        ClassPool classPool = ClassPool.getDefault();
        CtClass ctClass = classPool.makeClass("com.java110.service.smo.WuxwTest");
        CtMethod helloM = CtNewMethod.make(javaCode, ctClass);
        ctClass.addMethod(helloM);

        CtMethod helloM1 = CtNewMethod.make(javaCode2, ctClass);
        ctClass.addMethod(helloM1);
        Class pc=ctClass.toClass();
        Method move= pc.getMethod("testJava1",new Class[]{});

        Constructor<?> con=pc.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");
    }
}
@@ -637,38 +637,131 @@
     
- System.out.println(%5C%22123213%5C%22
+DataQuery dataQuery = new DataQuery();%5Cn dataQuery.setServiceCode(%5C%22%E6%9C%8D%E5%8A%A1%E7%BC%96%E7%A0%81%5C%22);  System.out.println(dataQuery.getServiceCode()
 );%5Cn
@@ -846,32 +846,73 @@
    testJava2(); 
+ServiceSql serviceSql = new ServiceSql();
   System.out.pri
@@ -910,33 +910,33 @@
 m.out.println(%5C%22
-2
+6
 23213%5C%22);%5Cn%7D%22;%0A 
@@ -980,32 +980,179 @@
 l.getDefault();%0A
+        classPool.importPackage(%22com.java110.entity.service.DataQuery%22);%0A        classPool.importPackage(%22com.java110.entity.service.ServiceSql%22);%0A
         CtClass 
@@ -1605,16 +1605,19 @@
 e(con);%0A
+%0A%0A%0A
     %7D%0A%0A%0A

测试 javassist 成功