Search in sources :

Example 1 with EclipseCompiler

use of org.eclipse.jdt.internal.compiler.tool.EclipseCompiler in project knime-core by knime.

the class JavaCodeCompiler method compile.

public void compile() throws CompilationFailedException {
    if (m_sources == null || m_sources.length == 0) {
        throw new CompilationFailedException("No sources set");
    }
    ArrayList<String> compileArgs = new ArrayList<String>();
    if (m_classpaths != null && m_classpaths.length > 0) {
        compileArgs.add("-classpath");
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < m_classpaths.length; i++) {
            if (i > 0) {
                b.append(File.pathSeparatorChar);
            }
            b.append(m_classpaths[i]);
            File file = m_classpaths[i];
            String filePath = file.getAbsolutePath();
            if (!file.exists()) {
                throw new CompilationFailedException("Can't read file \"" + filePath + "\"; invalid class path");
            }
        }
        compileArgs.add(b.toString());
    }
    final String javaVersion = getJavaVersion();
    compileArgs.add("-source");
    compileArgs.add(javaVersion);
    compileArgs.add("-target");
    compileArgs.add(javaVersion);
    compileArgs.add("-nowarn");
    if (m_additionalCompileArgs != null) {
        compileArgs.addAll(Arrays.asList(m_additionalCompileArgs));
    }
    final StringWriter logString = new StringWriter();
    // ServiceLoader<JavaCompiler> serviceLoader =
    // ServiceLoader.load(JavaCompiler.class);
    // the service loader sometimes didn't work in the RMI instance,
    // so we hard-code the compiler here.
    JavaCompiler compiler = new EclipseCompiler();
    // compiler = com.sun.tools.javac.api.JavacTool.create();
    if (m_sourceCodeDebugDir != null) {
        try {
            File tmpDir = m_sourceCodeDebugDir;
            tmpDir.mkdir();
            for (JavaFileObject source : m_sources) {
                CharSequence charContent = source.getCharContent(false);
                File out = new File(tmpDir, source.getName());
                out.getParentFile().mkdirs();
                StringReader reader = new StringReader(charContent.toString());
                FileWriter writer = new FileWriter(out);
                FileUtil.copy(reader, writer);
                writer.close();
                reader.close();
            }
        } catch (IOException e) {
            LOGGER.warn("Unable to write source code to \"" + m_sourceCodeDebugDir.getAbsolutePath() + "\": " + e.getMessage(), e);
        }
    }
    DiagnosticCollector<JavaFileObject> digsCollector = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager stdFileMgr = compiler.getStandardFileManager(digsCollector, null, null);
    m_fileMgr = new InMemoryJavaFileManager(stdFileMgr);
    CompilationTask compileTask = compiler.getTask(logString, m_fileMgr, digsCollector, compileArgs, null, Arrays.asList(m_sources));
    if (!compileTask.call()) {
        boolean hasDiagnostic = false;
        StringBuilder b = new StringBuilder("Unable to compile expression");
        for (Diagnostic<? extends JavaFileObject> d : digsCollector.getDiagnostics()) {
            switch(d.getKind()) {
                case ERROR:
                    String[] sourceLines = new String[0];
                    if (d.getSource() != null) {
                        JavaFileObject srcJavaFileObject = d.getSource();
                        String src;
                        if (srcJavaFileObject instanceof InMemorySourceJavaFileObject) {
                            src = ((InMemorySourceJavaFileObject) srcJavaFileObject).getSource();
                        } else {
                            try {
                                src = srcJavaFileObject.getCharContent(false).toString();
                            } catch (IOException ioe) {
                                src = null;
                            }
                        }
                        if (src != null) {
                            sourceLines = getSourceLines(src);
                        }
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("<<<< Expression Start >>>>");
                            LOGGER.debug("<<<< " + srcJavaFileObject.getName() + " >>>>");
                            for (int i = 0; i < sourceLines.length; i++) {
                                LOGGER.debug((i + 1) + ": " + sourceLines[i]);
                            }
                            LOGGER.debug("<<<< Expression End >>>>");
                        }
                    }
                    if (hasDiagnostic) {
                        // follow up error, insert empty line
                        b.append("\n");
                    }
                    hasDiagnostic = true;
                    int lineIndex = (int) (d.getLineNumber() - 1);
                    b.append("\nERROR at line ").append(lineIndex + 1);
                    b.append("\n").append(d.getMessage(Locale.US));
                    int sourceLineCount = sourceLines.length;
                    if (lineIndex - 1 >= 0 && lineIndex - 1 < sourceLineCount) {
                        // previous line
                        b.append("\n  Line : ").append(lineIndex);
                        b.append("  ").append(sourceLines[lineIndex - 1]);
                    }
                    if (lineIndex >= 0 && lineIndex < sourceLineCount) {
                        // error line
                        b.append("\n  Line : ").append(lineIndex + 1);
                        b.append("  ").append(sourceLines[lineIndex]);
                    }
                    break;
                default:
                    break;
            }
        }
        String errorOut = logString.toString();
        if (!hasDiagnostic) {
            b.append("\n").append(errorOut);
        } else {
            if (!errorOut.isEmpty()) {
                LOGGER.debug("Error output of compilation:\n" + errorOut);
                LOGGER.debug("Command line arguments were: " + compileArgs);
            }
        }
        throw new CompilationFailedException(b.toString());
    }
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File)

Example 2 with EclipseCompiler

use of org.eclipse.jdt.internal.compiler.tool.EclipseCompiler in project knime-core by knime.

the class JavaSnippetCompiler method getTask.

/**
 * Creates a compilation task.
 *
 * @param out a Writer for additional output from the compiler;
 * use System.err if null
 * @param digsCollector a diagnostic listener; if null use the compiler's
 * default method for reporting diagnostics
 * @return an object representing the compilation process
 * @throws IOException if temporary jar files cannot be created
 */
public CompilationTask getTask(final Writer out, final DiagnosticCollector<JavaFileObject> digsCollector) throws IOException {
    if (m_compiler == null) {
        m_compileArgs = new ArrayList<>();
        final File[] classpaths = m_snippet.getCompiletimeClassPath();
        m_compileArgs.add("-classpath");
        m_compileArgs.add(Arrays.stream(classpaths).map(f -> f.getAbsolutePath()).map(FilenameUtils::normalize).collect(Collectors.joining(File.pathSeparator)));
        m_compileArgs.add("-source");
        m_compileArgs.add("1.8");
        m_compileArgs.add("-target");
        m_compileArgs.add("1.8");
        m_compileArgs.add("-encoding");
        m_compileArgs.add("UTF-8");
        m_compiler = new EclipseCompiler();
    }
    final StandardJavaFileManager stdFileMgr = m_compiler.getStandardFileManager(digsCollector, null, Charset.forName("UTF-8"));
    final CompilationTask compileTask = m_compiler.getTask(out, stdFileMgr, digsCollector, m_compileArgs, null, m_snippet.getCompilationUnits());
    // Release all .jar files that may have been opened
    stdFileMgr.close();
    return compileTask;
}
Also used : Arrays(java.util.Arrays) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) URLClassLoader(java.net.URLClassLoader) Charset(java.nio.charset.Charset) CompilationTask(javax.tools.JavaCompiler.CompilationTask) Writer(java.io.Writer) DiagnosticCollector(javax.tools.DiagnosticCollector) FilenameUtils(org.apache.commons.io.FilenameUtils) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) FilenameUtils(org.apache.commons.io.FilenameUtils) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 3 with EclipseCompiler

use of org.eclipse.jdt.internal.compiler.tool.EclipseCompiler in project inmemantlr by julianthome.

the class StringCompiler method compile.

/**
 * do the compilation for the antlr artifacts
 * @param units string code generation pipeline
 * @param oprov compiler option provider
 * @throws CompilationErrorException if the compilation was not successful
 */
public void compile(Set<CunitProvider> units, CompilerOptionsProvider oprov) throws CompilationErrorException {
    JavaCompiler javac = new EclipseCompiler();
    StandardJavaFileManager sjfm = javac.getStandardFileManager(null, null, null);
    SpecialJavaFileManager fileManager = new SpecialJavaFileManager(sjfm, cl);
    List<MemorySource> cunit = new ArrayList<>();
    Set<MemorySource> mset = new HashSet<>();
    for (CunitProvider sc : units) {
        cunit.addAll(sc.getItems());
        for (MemorySource ms : sc.getItems()) {
            LOGGER.debug(ms.toString());
        }
    }
    mset.addAll(cunit);
    DiagnosticListener<? super JavaFileObject> dlistener = null;
    Iterable<String> classes = null;
    Writer out = new StringWriter();
    List<String> optionList = new ArrayList<>();
    optionList.addAll(oprov.getOptions());
    JavaCompiler.CompilationTask compile = javac.getTask(out, fileManager, dlistener, optionList, classes, cunit);
    boolean ret = compile.call();
    if (!ret) {
        throw new CompilationErrorException(out.toString());
    }
    // the corresponding byte code
    for (MemorySource ms : mset) {
        Set<MemoryByteCode> mb = fileManager.getByteCodeFromClass(ms.getClassName());
        if (mb.size() == 0)
            throw new IllegalArgumentException("MemoryByteCode must not be empty");
        // book keeping of source-bytecode tuples
        mt.addMemoryTuple(ms, mb);
    }
}
Also used : CompilationErrorException(org.snt.inmemantlr.exceptions.CompilationErrorException) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) StringWriter(java.io.StringWriter) MemoryByteCode(org.snt.inmemantlr.memobjects.MemoryByteCode) MemorySource(org.snt.inmemantlr.memobjects.MemorySource) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 4 with EclipseCompiler

use of org.eclipse.jdt.internal.compiler.tool.EclipseCompiler in project evosuite by EvoSuite.

the class PostProcessor method process.

/**
 * @param logs   logs of captured interaction
 * @param packages  package names associated with logs. Mapping logs.get(i) belongs to packages.get(i)
 * @throws IOException
 */
public static void process(final List<CaptureLog> logs, final List<String> packages, final List<Class<?>[]> observedClasses) throws IOException {
    if (logs == null) {
        throw new NullPointerException("list of CaptureLogs must not be null");
    }
    if (packages == null) {
        throw new NullPointerException("list of package names associated with logs must not be null");
    }
    if (observedClasses == null) {
        throw new NullPointerException("list of classes to be observed must not be null");
    }
    final int size = logs.size();
    if (packages.size() != size || observedClasses.size() != size) {
        throw new IllegalArgumentException("given lists must have same size");
    }
    // create post-processing sources in os specific temp folder
    final File tempDir = new File(System.getProperty("java.io.tmpdir"), "postprocessing_" + System.currentTimeMillis());
    tempDir.mkdir();
    CaptureLog log;
    String packageName;
    Class<?>[] classes;
    String targetFolder;
    File targetFile;
    final StringBuilder testClassNameBuilder = new StringBuilder();
    String testClassName;
    for (int i = 0; i < size; i++) {
        // =============== prepare carved test for post-processing ================================================
        packageName = packages.get(i);
        targetFolder = packageName.replace(".", File.separator);
        classes = observedClasses.get(i);
        if (classes.length == 0) {
            throw new IllegalArgumentException("there must be at least one class to be observed");
        }
        // for(int j = 0; j < classes.length; j++)
        // {
        // testClassNameBuilder.append(classes[j].getSimpleName());
        // if(testClassNameBuilder.length() >= 10)
        // {
        // break;
        // }
        // }
        testClassNameBuilder.append("CarvedTest");
        log = logs.get(i);
        long s = System.currentTimeMillis();
        logger.debug(">>>> (postprocess) start test create ");
        targetFile = new File(tempDir, targetFolder);
        targetFile.mkdirs();
        testClassName = getFreeClassName(targetFile, testClassNameBuilder.toString());
        targetFile = new File(targetFile, testClassName + ".java");
        // write out test files containing post-processing statements
        writeTest(log, packageName, testClassName, classes, targetFile, true);
        logger.debug(">>>> (postprocess) end test creation -> " + (System.currentTimeMillis() - s) / 1000);
        // =============== compile generated post-processing test ================================================
        final JavaCompiler compiler = new EclipseCompiler();
        // final JavaCompiler 			  compiler    = ToolProvider.getSystemJavaCompiler();
        final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File[] { targetFile }));
        // --- add modified bins (see Transformer and ClassPreparer.createPreparedBin()) to class path
        String classPath = Configuration.INSTANCE.getProperty(Configuration.MODIFIED_BIN_LOC);
        classPath += File.pathSeparator + System.getProperty("java.class.path");
        final Boolean wasCompilationSuccess = compiler.getTask(null, fileManager, null, Arrays.asList(new String[] { "-cp", classPath }), null, compilationUnits).call();
        if (!wasCompilationSuccess) {
            logger.error("Compilation was not not successful for " + targetFile);
            fileManager.close();
            continue;
        }
        fileManager.close();
        // =============== execute + observe post-processing test run ================================================
        final PostProcessorClassLoader cl = new PostProcessorClassLoader(tempDir);
        final Class<?> testClass = cl.findClass(packageName + '.' + testClassName);
        BlockJUnit4ClassRunner testRunner;
        try {
            testRunner = new BlockJUnit4ClassRunner(testClass);
            testRunner.run(new RunNotifier());
        } catch (InitializationError e) {
            logger.error("" + e, e);
        }
        // ============== generate final test file ================================================================
        final String targetDir = Configuration.INSTANCE.getProperty(Configuration.GEN_TESTS_LOC);
        targetFile = new File(new File(targetDir), targetFolder);
        targetFile.mkdirs();
        testClassName = getFreeClassName(targetFile, testClassNameBuilder.toString());
        targetFile = new File(targetFile, testClassName + ".java");
        writeTest(log, packageName, testClassName, classes, targetFile, false);
        // recycle StringBuilder for testClassName
        testClassNameBuilder.setLength(0);
    }
    // clean up post-processing stuff
    tempDir.delete();
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) InitializationError(org.junit.runners.model.InitializationError) CaptureLog(org.evosuite.testcarver.capture.CaptureLog) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) File(java.io.File)

Example 5 with EclipseCompiler

use of org.eclipse.jdt.internal.compiler.tool.EclipseCompiler in project querydsl by querydsl.

the class EclipseCompilationTest method test.

@Test
@Ignore
public void test() throws IOException {
    System.setProperty("jdt.compiler.useSingleThread", "true");
    // select classes
    List<String> classes = new ArrayList<String>();
    for (File file : new File(packagePath).listFiles()) {
        if (file.getName().endsWith(".java")) {
            classes.add(file.getPath());
        }
    }
    // prepare output
    File out = new File("target/out-eclipse");
    FileUtils.delete(out);
    if (!out.mkdirs()) {
        Assert.fail("Creation of " + out.getPath() + " failed");
    }
    String classPath = SimpleCompiler.getClassPath((URLClassLoader) getClass().getClassLoader());
    JavaCompiler compiler = new EclipseCompiler();
    List<String> options = new ArrayList<String>();
    options.add("-s");
    options.add("target/out-eclipse");
    options.add("-proc:only");
    options.add("-processor");
    options.add(QuerydslAnnotationProcessor.class.getName());
    options.add("-Aquerydsl.entityAccessors=true");
    options.add("-cp");
    options.add(classPath);
    options.add("-source");
    options.add("1.6");
    options.add("-verbose");
    options.addAll(classes);
    int compilationResult = compiler.run(null, System.out, System.err, options.toArray(new String[0]));
    if (compilationResult == 0) {
        System.out.println("Compilation is successful");
    } else {
        Assert.fail("Compilation Failed");
    }
    File resultFile = new File("target/out-eclipse/com/querydsl/eclipse/QSimpleEntity.java");
    assertTrue(resultFile.exists());
    String result = new String(Files.readAllBytes(resultFile.toPath()), StandardCharsets.UTF_8);
    assertTrue(result.contains("NumberPath<java.math.BigDecimal> bigDecimalProp"));
    assertTrue(result.contains("NumberPath<Integer> integerProp"));
    assertTrue(result.contains("NumberPath<Integer> intProp"));
    assertTrue(result.contains("StringPath stringProp"));
}
Also used : EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

EclipseCompiler (org.eclipse.jdt.internal.compiler.tool.EclipseCompiler)6 File (java.io.File)5 ArrayList (java.util.ArrayList)3 JavaCompiler (javax.tools.JavaCompiler)3 StandardJavaFileManager (javax.tools.StandardJavaFileManager)3 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 DiagnosticCollector (javax.tools.DiagnosticCollector)2 CompilationTask (javax.tools.JavaCompiler.CompilationTask)2 JavaFileObject (javax.tools.JavaFileObject)2 AutoAnnotationProcessor (com.google.auto.value.processor.AutoAnnotationProcessor)1 AutoBuilderProcessor (com.google.auto.value.processor.AutoBuilderProcessor)1 AutoOneOfProcessor (com.google.auto.value.processor.AutoOneOfProcessor)1 AutoValueProcessor (com.google.auto.value.processor.AutoValueProcessor)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 FileWriter (java.io.FileWriter)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1