Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89726
  • 博文数量: 100
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 658
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-10 08:29
文章分类

全部博文(100)

文章存档

2014年(100)

我的朋友

分类: Java

2014-07-07 13:04:18

package com.mclon.jmxsgl.service.impl;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
 * 动态编译代码类
 * @author zhangxu
public class DynamicEngine {
    public static DynamicEngine getInstance() {
        return new DynamicEngine();
    private URLClassLoader parentClassLoader;
    private DynamicEngine() {
        this.parentClassLoader = (URLClassLoader) this.getClass()
                .getClassLoader();
        this.buildClassPath();
    private void buildClassPath() {
        if (classpath == null) {
            StringBuilder sb = new StringBuilder();
            Map filePath = new HashMap();
            // 读取容器lib
            try {
                Enumeration urls = Thread.currentThread()
                        .getContextClassLoader().getResources(".");
                String tomPath = urls.nextElement().getPath();
                File file = new File(tomPath);
                if (file.isDirectory()) {
                    File[] fList = file.listFiles();
                    for (int j = 0; j < fList.length; j++) {
                       if (fList[j].isFile()
                                && (fList[j].getName().contains(".jar"))) {
                            fList[j].getPath();
                            filePath.put(fList[j].getName(),
                                    tomPath + fList[j].getName());
                } else if (file.getName().contains(".jar")) {
            } catch (Exception e) {
                System.out.println("Error: " + e);
            // 读取工程lib
            for (URL url : this.parentClassLoader.getURLs()) {
                String p = url.getFile();
                File file = new File(url.toString());
                String fileName = file.getName();
                filePath.put(fileName, p);
            // 组装classpath
                sb.append(filePath.get(key)).append(File.pathSeparator);
            System.out.println(sb.toString());
            this.classpath = sb.toString();
    public Object javaCodeToObject(String fullClassName, String javaCode)
            throws IllegalAccessException, InstantiationException {
        long start = System.currentTimeMillis();
        Object instance = null;
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        DiagnosticCollector diagnostics = new DiagnosticCollector();
        ClassFileManager fileManager = new ClassFileManager(
                compiler.getStandardFileManager(diagnostics, null, null));
        List jfiles = new ArrayList();
        jfiles.add(new CharSequenceJavaFileObject(fullClassName, javaCode));
        List options = new ArrayList();
        options.add("-encoding");
        options.add("UTF-8");
        options.add("-classpath");
        options.add(this.classpath);
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,
        boolean success = task.call();
        if (success) {
 JavaClassObject jco = fileManager.getJavaClassObject();
            DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(           this.parentClassLoader);
            Class clazz = dynamicClassLoader.loadClass(fullClassName, jco);
            instance = clazz.newInstance();
        } else {
            String error = "";
            for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
                error = error + compilePrint(diagnostic);
        long end = System.currentTimeMillis();
        System.out.println("javaCodeToObject use:" + (end - start) + "ms");
        return instance;

阅读(452) | 评论(0) | 转发(0) |
0

上一篇:国画菊花

下一篇:透明蓝色气泡PS笔刷

给主人留下些什么吧!~~