java源码动态生成编译,以及方法调用

 public class DynamicCalculator
{
    private Class calculatorC = null;
    private Method calculatorM = null;
    private long currentLong = 0;
    /**
     *
     */
    public DynamicCalculator()
    {

    }
    /**
     *
     */
    private static Logger log = LoggerFactory.getLogger(LoggerModel.
            MODEL_PU_LIST);
    /**
     *
     * @param expr String
     * @throws Exception Exception
     */
    public void parseExpression(String expr)
            throws Exception
    {
        String str = expr;
        str = str.toLowerCase();
        String webpackageName = WebInstance.class.getPackage().getName();
        StringBuffer sb = new StringBuffer();
        sb.append(" package ").append(webpackageName).append(";");
        sb.append("   class LeeCalculator").append(currentLong).append(" {");
        sb.append("       public static double GetExprValue(double responsetime,");
        sb.append("double sessioncount,double requestcount,double cpuvalue,double loadavgvalue) {");
        sb.append("          return ").append(str).append(";");
        sb.append("       }");
        sb.append("   }");
        String path = getClassPath();

        File m_File = new File(path + "LeeCalculator" + currentLong + ".java");

        PrintWriter pw = new PrintWriter(new FileWriter(m_File));
        pw.println(sb.toString());
        pw.close();
        Main javac = new Main();

        String javaFilePath = path + "LeeCalculator" + currentLong + ".java";
        String[] cpargs = new String[]
                          {
                          "-d", path, javaFilePath};
        int status = javac.compile(cpargs);
        if (status != 0)
        {
            System.out.println("DynamicCalculator compile failed");
            return;
        }
        String cClassName = webpackageName + ".LeeCalculator" + currentLong;
        calculatorC = null;
        calculatorM = null;
        calculatorC = Class.forName(cClassName);
        calculatorM = calculatorC.getDeclaredMethod("GetExprValue", new Class[]
                {double.class, double.class, double.class, double.class, double.class});
        File oldFile = new File(path + "LeeCalculator" + currentLong + ".java");
        if (oldFile.exists())
        {
            oldFile.delete();
        }

        File oldClassFile = new File(path + "LeeCalculator" + currentLong
                                     +
                                     ".java");
        if (oldClassFile.exists())
        {
            oldClassFile.delete();
        }

        currentLong++;
        System.out.println("DynamicCalculator : create successfully!");
//            calculatorM=calculatorC.getMethod("GetExprValue",new Class[]{ String[].class });


    }
    /**
     *
     */
    public void getExpressionValue()
    {
        double value = 0;
        try
        {
            WebInstance ins = WebInstance.getInstance();
            double responsetime = ins.getResponseTime();
            double sessioncount = ins.getUserCount();
            double requestcount = ins.getRequestCount();
            double cpuvalue = ins.getCpuValue();
            double loadavgvalue = ins.getLoadAvgValue();

            log.debug("responsetime===" + responsetime);
            log.debug("sessioncount===" + sessioncount);
            log.debug("requestcount===" + requestcount);
            log.debug("cpuvalue===" + cpuvalue);
            log.debug("loadavgvalue===" + loadavgvalue);

            Object result = calculatorM.invoke(null,
                                               new Object[]
                                               {new Double(responsetime),
                                               new Double(sessioncount),
                                               new Double(requestcount),
                                               new Double(cpuvalue),
                                               new Double(loadavgvalue)});
            value = Double.parseDouble(result.toString());
        } catch (Exception ex)
        {
            value = WebInstance.getInstance().getDefaultExpressionValue();
        }
        WebInstance.getInstance().setCurrentValue(value);
    }
    /**
     *
     * @return String
     */
    private String getClassPath()
    {
        URL url = DynamicCalculator.class.getResource("/");
        String path = url.getPath();
        log.debug("path===" + path);
        return path;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值