wuxw
2019-09-04 9c914bf8219a9d059192b53e4ecd222a00a7fe4e
动态java 类开发中
4个文件已修改
3个文件已添加
161 ■■■■■ 已修改文件
java110-core/src/main/java/com/java110/core/javassist/IJavassist.java 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-core/src/main/java/com/java110/core/javassist/Java110CoreTemplateJavassist.java 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-service/pom.xml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java 61 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-service/src/test/java/com/java110/service/InterpreterTest.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-service/src/test/java/com/java110/service/smo/impl/QueryServiceSMOImplTest.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-core/src/main/java/com/java110/core/javassist/IJavassist.java
New file
@@ -0,0 +1,16 @@
package com.java110.core.javassist;
import com.java110.entity.service.DataQuery;
/**
 * @ClassName IJavassist
 * @Description TODO
 * @Author wuxw
 * @Date 2019/9/4 23:08
 * @Version 1.0
 * add by wuxw 2019/9/4
 **/
public interface IJavassist {
    public void execute(DataQuery dataQuery);
}
java110-core/src/main/java/com/java110/core/javassist/Java110CoreTemplateJavassist.java
New file
@@ -0,0 +1,12 @@
package com.java110.core.javassist;
/**
 * @ClassName CoreTemplateJavassist
 * @Description TODO
 * @Author wuxw
 * @Date 2019/9/4 23:11
 * @Version 1.0
 * add by wuxw 2019/9/4
 **/
public class Java110CoreTemplateJavassist {
}
java110-service/pom.xml
@@ -42,6 +42,10 @@
            <groupId>org.beanshell</groupId>
            <artifactId>bsh-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <!--<dependency>
java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
@@ -16,7 +16,13 @@
import com.java110.entity.service.ServiceSql;
import com.java110.service.dao.IQueryServiceDAO;
import com.java110.service.smo.IQueryServiceSMO;
import javassist.ClassPool;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.ognl.Ognl;
import org.apache.ibatis.ognl.OgnlException;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -186,11 +192,14 @@
                param = param.substring(0, param.length() - 1);
            }*/
            dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
                    "成功", JSONObject.parseObject(interpreter.eval("execute(" + dataQuery + ")").toString())));
        } catch (Exception e) {
            logger.error("数据交互异常:", e);
            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "数据交互异常,"+e.getMessage());
            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "数据交互异常," + e.getMessage());
        }
    }
@@ -252,7 +261,7 @@
            String currentSql = sqlObj.getString(dataQuery.getTemplateKey());
            //处理 if 判断
            currentSql = dealSqlIf(currentSql);
            currentSql = dealSqlIf(currentSql, params);
            String[] sqls = currentSql.split("#");
            String currentSqlNew = "";
@@ -325,22 +334,52 @@
    /**
     * 处理SQL语句
     * @param oldSql
     *              select * from s_a a
     *              where if(name != null && name != ''){
             *                  a.name = #name#
             *             }
     *
     * @param oldSql select * from s_a a
     *               where <if test="name != null && name != ''">
     *               a.name = #name#
     *               </if>
     * @return
     */
    private String dealSqlIf(String oldSql){
        String newSql = "";
    public String dealSqlIf(String oldSql, JSONObject requestParams) throws DocumentException, OgnlException {
        StringBuffer newSql = new StringBuffer();
        String tmpSql = "";
        Boolean conditionResult = false;
        // 未包含 条件语句
        if(!oldSql.replace(" ","").contains("if(")){
        if (!oldSql.contains("<if")) {
            return oldSql;
        }
        String[] oSqls = oldSql.split("</if>");
        for (String oSql : oSqls) {
            if (!oSql.startsWith("<if")) {
                newSql.append(oSql.substring(0, oSql.indexOf("<if")));
            }
        return newSql;
            tmpSql = oSql.substring(oSql.indexOf("<if")) + "</if>";
            Element root = DocumentHelper.parseText(tmpSql).getRootElement();
            String condition = root.attribute("test").getValue();
            Object condObj = Ognl.parseExpression(condition);
            Object value = Ognl.getValue(condObj, requestParams);
            if (value instanceof Boolean) {
                conditionResult = (Boolean) value;
            } else {
                throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "配置错误,if语句配置错误 " + condition);
            }
            if (conditionResult) {
                newSql.append(root.getText());
            }
        }
        return newSql.toString();
    }
java110-service/src/test/java/com/java110/service/InterpreterTest.java
@@ -1,9 +1,16 @@
package com.java110.service;
import bsh.Interpreter;
import com.alibaba.fastjson.JSONObject;
import com.java110.service.smo.impl.QueryServiceSMOImpl;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.ibatis.ognl.OgnlException;
import org.dom4j.DocumentException;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by wuxw on 2018/4/23.
@@ -45,4 +52,22 @@
        String param = "9,4";
        System.out.println(interpreter.eval("execute("+param+")").toString());
    }
    public void testDealSqlIf() throws OgnlException, DocumentException {
        String oldSql = "select * from s_a a\n" +
                "                  where <if test=\"name != null and name != ''\">\n" +
                "                          a.name = #name#\n" +
                "                      </if>" +
                "                       and a.sex = #name#" +
                "                       <if test=\"id != null and id!= ''\"> and a.id = #id# </if>";
        QueryServiceSMOImpl queryServiceSMO = new QueryServiceSMOImpl();
        JSONObject params = new JSONObject();
        params.put("id","123213");
        params.put("name","123213");
        System.out.println((queryServiceSMO.dealSqlIf(oldSql, params)));
    }
}
java110-service/src/test/java/com/java110/service/smo/impl/QueryServiceSMOImplTest.java
New file
@@ -0,0 +1,36 @@
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);
    }
}
pom.xml
@@ -398,6 +398,13 @@
                <version>2.0b4</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
            <dependency>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.25.0-GA</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>