Package com.ddubyat.develop.jhawtcode.dynamic

Examples of com.ddubyat.develop.jhawtcode.dynamic.DynamicClassLoaderJavaFileManager


        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();

        log.debug("In Memory File Manager Setup");
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
        JavaFileManager javaFileManager = new DynamicClassLoaderJavaFileManager(loader, fileManager);

        String fullClassPath = System.getProperty("java.class.path");
        File[] allClassPath = ClassLoaderUtil.getDefaultClasspath(loader);
        for(File f : allClassPath) {
            fullClassPath += ":" + f.getAbsolutePath();
        }

        log.trace("Full classpath of the container is: {}", fullClassPath);

        List<String> optionList = new ArrayList<>();
        optionList.addAll(Arrays.asList("-classpath", fullClassPath));

        log.debug("Java Source File Created in Memory");
        List<JavaFileObject> sourceJavaFiles = new ArrayList<>();
        sourceJavaFiles.add(new InMemoryJavaFileObject(fileName, source));

        log.debug("Compilation Beginning");
        JavaCompiler.CompilationTask ct = compiler.getTask(null, javaFileManager, diagnostics, optionList, null, sourceJavaFiles);

        try {
            ct.call();
        } catch (Throwable t) {
            log.debug("Could not compile", t);
            return null;
        }

        javaFileManager.close();
        fileManager.close();
        log.debug("Compilation Completed");

        String compileErrors = "Compiler Exception" + System.lineSeparator();
        Boolean hasErrors = Boolean.FALSE;

        if(diagnostics.getDiagnostics().size() > 0) {
            log.debug("Diagnostics Generated during compilation");
            for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
                log.debug("Diagnostic on line: {} column: {} with message {}", diagnostic.getLineNumber(), diagnostic.getColumnNumber(), diagnostic.getMessage(Locale.ENGLISH));
                if(diagnostic.getKind().equals(Diagnostic.Kind.ERROR)) {
                    compileErrors += "  Error on line: " + diagnostic.getLineNumber() + " column: " + diagnostic.getColumnNumber() +
                            System.lineSeparator() + "   " + diagnostic.getMessage(Locale.ENGLISH);
                    System.out.println(compileErrors);
                    hasErrors = Boolean.TRUE;
                } else {
                    System.out.println("Compiler Diagnostic Message: " + diagnostic.getLineNumber() + " " + diagnostic.getMessage(Locale.ENGLISH));
                }
            }
            if(hasErrors.booleanValue()) {
                log.debug("Compilation Failed due to Errors");
                throw new Exception(compileErrors);
            }
        }

        return javaFileManager.getClassLoader(null).loadClass(classPath.replaceAll("/",".") + fileName);
    }
View Full Code Here


        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();

        log.debug("In Memory File Manager Setup");
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
        JavaFileManager javaFileManager = new DynamicClassLoaderJavaFileManager(loader, fileManager);

        String fullClassPath = System.getProperty("java.class.path");
        File[] allClassPath = ClassLoaderUtil.getDefaultClasspath(loader);
        for(File f : allClassPath) {
            fullClassPath += ":" + f.getAbsolutePath();
        }

        log.trace("Full classpath of the container is: {}", fullClassPath);

        List<String> optionList = new ArrayList<>();
        optionList.addAll(Arrays.asList("-classpath", fullClassPath));

        log.debug("Java Source File Created in Memory");
        List<JavaFileObject> sourceJavaFiles = new ArrayList<>();
        sourceJavaFiles.add(new InMemoryJavaFileObject(fileName, source));

        log.debug("Compilation Beginning");
        JavaCompiler.CompilationTask ct = compiler.getTask(null, javaFileManager, diagnostics, optionList, null, sourceJavaFiles);

        try {
            ct.call();
        } catch (Throwable t) {
            log.debug("Could not compile", t);
            return null;
        }

        javaFileManager.close();
        fileManager.close();
        log.debug("Compilation Completed");

        String compileErrors = "Compiler Exception" + System.lineSeparator();
        Boolean hasErrors = Boolean.FALSE;

        if(diagnostics.getDiagnostics().size() > 0) {
            log.debug("Diagnostics Generated during compilation");
            for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
                log.debug("Diagnostic on line: {} column: {} with message {}", diagnostic.getLineNumber(), diagnostic.getColumnNumber(), diagnostic.getMessage(Locale.ENGLISH));
                if(diagnostic.getKind().equals(Diagnostic.Kind.ERROR)) {
                    compileErrors += "  Error on line: " + diagnostic.getLineNumber() + " column: " + diagnostic.getColumnNumber() +
                            System.lineSeparator() + "   " + diagnostic.getMessage(Locale.ENGLISH);
                    System.out.println(compileErrors);
                    hasErrors = Boolean.TRUE;
                } else {
                    System.out.println("Compiler Diagnostic Message: " + diagnostic.getLineNumber() + " " + diagnostic.getMessage(Locale.ENGLISH));
                }
            }
            if(hasErrors.booleanValue()) {
                log.debug("Compilation Failed due to Errors");
                throw new Exception(compileErrors);
            }
        }

        return javaFileManager.getClassLoader(null).loadClass(classPath.replaceAll("/",".") + fileName);
    }
View Full Code Here

TOP

Related Classes of com.ddubyat.develop.jhawtcode.dynamic.DynamicClassLoaderJavaFileManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.