All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dvare.dynamic.compiler.DynamicCompiler Maven / Gradle / Ivy

/*The MIT License (MIT)

Copyright (c) 2016 Muhammad Hammad

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Sogiftware.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/


package org.dvare.dynamic.compiler;

import org.dvare.dynamic.exceptions.DynamicCompilerException;
import org.dvare.dynamic.loader.CustomClassLoader;
import org.dvare.dynamic.resources.CompiledCode;
import org.dvare.dynamic.resources.DynamicClassLoader;
import org.dvare.dynamic.resources.DynamicJavaFileManager;
import org.dvare.dynamic.resources.SourceCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.tools.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;

public class DynamicCompiler {
    private static Logger logger = LoggerFactory.getLogger(DynamicCompiler.class);
    private final javax.tools.JavaCompiler javac;
    private final StandardJavaFileManager standardFileManager;
    private List options = new ArrayList<>();
    private DynamicClassLoader dynamicClassLoader;
    private Map sourceCodes = new HashMap<>();
    private List jars = new ArrayList<>();
    private ClassLoader classLoader;
    private String classpath = "";
    private boolean separateContext = false;
    private boolean updateContextClassLoader = false;
    private boolean updateClassFile = false;


    public DynamicCompiler() {
        javac = ToolProvider.getSystemJavaCompiler();
        standardFileManager =
                javac.getStandardFileManager(null, null, null);

        classLoader = Thread.currentThread()
                .getContextClassLoader();
        init();
    }

    public DynamicCompiler(ClassLoader classLoader) {
        this.classLoader = classLoader;
        javac = ToolProvider.getSystemJavaCompiler();
        standardFileManager =
                javac.getStandardFileManager(null, null, null);

        init();
    }

    private void init() {

        options.add("-Xlint:unchecked");


        if (separateContext) {
            classLoader = new CustomClassLoader().getCustomURLClassLoader();
        }


        if (!(classLoader instanceof URLClassLoader)) {
            classLoader = this.getClass().getClassLoader();
        }


        if (classLoader instanceof URLClassLoader) {

            URLClassLoader urlClassLoader = (URLClassLoader) classLoader;

            buildClassPath(urlClassLoader);
            if (!classpath.trim().isEmpty()) {
                options.addAll(Arrays.asList("-classpath", classpath));
            }


        }
    }


    public void addSource(String className, String testSourceCode) {
        sourceCodes.put(className, new SourceCode(className, testSourceCode));
    }


    public void addSource(String className, File sourceFile) {
        Iterable compilationUnits = standardFileManager.getJavaFileObjectsFromFiles(Collections.singletonList(sourceFile));
        for (JavaFileObject javaFileObject : compilationUnits) {
            sourceCodes.put(className, javaFileObject);
        }
    }


    public void addJar(URL url) {
        jars.add(url);
    }


    public Map> build() throws DynamicCompilerException {


        dynamicClassLoader = new DynamicClassLoader(classLoader, updateClassFile);
        Collection compilationUnits = sourceCodes.values();
        List compiledCodes = new ArrayList<>();

        DynamicJavaFileManager fileManager = new DynamicJavaFileManager(standardFileManager, compiledCodes, dynamicClassLoader);


        DiagnosticCollector collector = new DiagnosticCollector<>();
        JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, collector, options, null, compilationUnits);


        if (updateContextClassLoader) {
            Thread.currentThread().setContextClassLoader(dynamicClassLoader);
        }

        try {

            if (!sourceCodes.keySet().isEmpty()) {
                boolean result = task.call();

                if (!result || collector.getDiagnostics().size() > 0) {
                    throw new DynamicCompilerException("Compilation Error", collector.getDiagnostics());
                }
            }

            Map> classes = new HashMap>();
            for (String className : sourceCodes.keySet()) {
                classes.put(className, dynamicClassLoader.getClass(className));
            }
            return classes;
        } catch (ClassFormatError | ClassNotFoundException e) {
            throw new DynamicCompilerException(e);
        }


    }


    private void buildClassPath(URLClassLoader urlClassLoader) {
        try {
            if (!jars.isEmpty()) {

                for (URL url : jars) {
                    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
                    method.setAccessible(true);
                    method.invoke(urlClassLoader, url);
                }
            }

            Method method2 = URLClassLoader.class.getDeclaredMethod("getURLs");
            method2.setAccessible(true);
            URL[] urls = (URL[]) method2.invoke(urlClassLoader);

            if (urls.length == 0) {

                urls = (URL[]) method2.invoke(getClass().getClassLoader());
            }
            StringBuilder classpathBuilder = new StringBuilder();
            for (URL url : urls) {
                File file = new File(url.getFile());

                classpathBuilder.append(file.getAbsolutePath()).append(File.pathSeparator);

                logger.debug(file.getAbsolutePath() + File.pathSeparator);
            }

            classpath = classpathBuilder.toString();


        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }


    /*Getter and Setters*/

    public void setSeparateContext(boolean separateContext) {
        this.separateContext = separateContext;
    }

    public ClassLoader getClassLoader() {
        return dynamicClassLoader;
    }


    public void setUpdateContextClassLoader(boolean updateContextClassLoader) {
        this.updateContextClassLoader = updateContextClassLoader;
    }

    public void setUpdateClassFile(boolean updateClassFile) {
        this.updateClassFile = updateClassFile;
    }

    public String getClasspath() {
        return classpath;
    }

    public void setClasspath(String classpath) {
        this.classpath = classpath;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy