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

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

Go to download

Java In memory Compiler thats enables to compile java file and code string in memory

There is a newer version: 3.2
Show newest version
/*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 static final List options = new ArrayList<>(Arrays.asList("-Xlint:unchecked"));
    private static javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    private StandardJavaFileManager standardFileManager =
            javac.getStandardFileManager(null, null, null);
    private DynamicClassLoader dynamicClassLoader;
    private Map sourceCodes = new HashMap<>();
    private List jars = new ArrayList<>();
    private String classpath = "";
    private boolean separateContext = false;
    private boolean updateContextClassLoader = false;

    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(Arrays.asList(sourceFile));
        for (JavaFileObject javaFileObject : compilationUnits) {
            sourceCodes.put(className, javaFileObject);
        }
    }


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


    public Map> build() throws DynamicCompilerException {

        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        if (separateContext) {
            classLoader = new CustomClassLoader().getCustomURLClassLoader();
        }


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


        if (classLoader instanceof URLClassLoader) {

            URLClassLoader urlClassLoader = (URLClassLoader) classLoader;

            try {
                if (!jars.isEmpty()) {

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

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

                if (urls.length == 0) {

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

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

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


            } catch (Exception e) {
                e.printStackTrace();
            }


        }

        dynamicClassLoader = new DynamicClassLoader(classLoader);
        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(collector.getDiagnostics());
                }
            }

            Map> classes = new HashMap>();
            for (String className : sourceCodes.keySet()) {
                classes.put(className, dynamicClassLoader.loadClass(className));
            }
            return classes;
        } catch (ClassFormatError e) {
            throw new DynamicCompilerException(e);
        } catch (ClassNotFoundException e) {
            throw new DynamicCompilerException(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 String getClasspath() {
        return classpath;
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy