<commit_before>/*******************************************************************************
 * Copyright (c) 2015 pf-miles.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     pf-miles - initial API and implementation
 ******************************************************************************/
package com.github.pfmiles.kanjava.compile;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.Processor;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardLocation;

import com.github.pfmiles.kanjava.JavaSourceFile;
import com.github.pfmiles.kanjava.KanJavaException;

/**
 * 将传入的内存java文件结合内存classpath，编译为内存class文件
 * 
 * @author pf-miles 2014-4-3 下午4:29:31
 */
public class DynaCompileUtil {

    public static final String PATH_SEP = "/";

    /**
     * 传入源码以及classpath中的jar包,编译出class文件(内存文件)
     * 
     * @param srcs
     *            源码文件
     * @param cpJarFilePathStr
     *            classpath字符串，可为null
     * @param procExe
     *            是否仅仅执行annotation processor或仅仅执行编译： 0: 既检查又编译, 1: 仅执行检查, 2:
     *            仅执行编译
     * @return 编译结果
     */
    public static CompilationResult compile(Set<JavaSourceFile> srcs, String cpJarFilePathStr, Iterable<? extends Processor> processors, int procExe) {
        // JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        JavaCompiler compiler = null;
        try {
            compiler = Class.forName("com.sun.tools.javac.api.JavacTool").asSubclass(JavaCompiler.class).newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        DynaFileManager fileManager = new DynaFileManager(compiler.getStandardFileManager(null, null, null));
        try {
            List<String> options = new ArrayList<String>();
            if (cpJarFilePathStr != null) {
                options.add("-classpath");
                options.add(cpJarFilePathStr);
            }
            switch (procExe) {
            case 1:
                options.add("-proc:only");
                break;
            case 2:
                options.add("-proc:none");
            default:
                break;
            }
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
            StringWriter out = new StringWriter();
            CompilationTask task = compiler.getTask(out, fileManager, diagnostics, options, null, srcs);
            if (processors != null)
                task.setProcessors(processors);
            if (!task.call()) {
                for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
                    try {
                        out.append("Error on line " + diagnostic.getLineNumber() + " in " + diagnostic).append('\n')
                                .append("Code: \n" + diagnostic.getSource().getCharContent(true)).append("\n");
                    } catch (IOException e) {
                        // ignore...
                        out.append("Error on line " + diagnostic.getLineNumber() + " in " + diagnostic).append('\n');
                    }
                }
                return new CompilationResult(out.toString());
            } else {
                try {
                    return new CompilationResult(fileManager.list(StandardLocation.CLASS_OUTPUT, null, new HashSet<Kind>(Arrays.asList(Kind.CLASS)),<commit_msg>work test pass<commit_after>/*******************************************************************************
 * Copyright (c) 2015 pf-miles.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     pf-miles - initial API and implementation
 ******************************************************************************/
package com.github.pfmiles.kanjava.compile;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.Processor;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardLocation;

import com.github.pfmiles.kanjava.JavaSourceFile;
import com.github.pfmiles.kanjava.KanJavaException;

/**
 * 将传入的内存java文件结合内存classpath，编译为内存class文件
 * 
 * @author pf-miles 2014-4-3 下午4:29:31
 */
public class DynaCompileUtil {

    public static final String PATH_SEP = "/";

    /**
     * 传入源码以及classpath中的jar包,编译出class文件(内存文件)
     * 
     * @param srcs
     *            源码文件
     * @param cpJarFilePathStr
     *            classpath字符串，可为null
     * @param procExe
     *            是否仅仅执行annotation processor或仅仅执行编译： 0: 既检查又编译, 1: 仅执行检查, 2:
     *            仅执行编译
     * @return 编译结果
     */
    public static CompilationResult compile(Set<JavaSourceFile> srcs, String cpJarFilePathStr, Iterable<? extends Processor> processors, int procExe) {
        // JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        JavaCompiler compiler = null;
        try {
            compiler = Class.forName("com.sun.tools.javac.api.JavacTool").asSubclass(JavaCompiler.class).newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        DynaFileManager fileManager = new DynaFileManager(compiler.getStandardFileManager(null, null, null));
        try {
            List<String> options = new ArrayList<String>();
            if (cpJarFilePathStr != null) {
                options.add("-classpath");
                options.add(cpJarFilePathStr);
            }
            switch (procExe) {
            case 1:
                options.add("-proc:only");
                break;
            case 2:
                options.add("-proc:none");
            default:
                break;
            }
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
            StringWriter out = new StringWriter();
            CompilationTask task = compiler.getTask(out, fileManager, diagnostics, options, null, srcs);
            if (processors != null)
                task.setProcessors(processors);
            if (!task.call()) {
                for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
                    try {
                        // errors only
                        if (javax.tools.Diagnostic.Kind.ERROR == diagnostic.getKind()) {
                            out.append("Error on line " + diagnostic.getLineNumber() + " in " + diagnostic).append('\n');
                            if (diagnostic.getSource() != null)
                                out.append("Code: \n" + diagnostic.getSource().getCharContent(true)).append("\n");
                        }
                    } catch (IOException e) {
                        // ignore...
                        out.append("Error on line " + diagnostic.getLineNumber() + " in " + diagnostic).append('\n');
                    }
                }
                return new CompilationResult(out.toString());
            } else {
                try {
                    return new CompilationResult(fileManager.list(StandardLocation.CLASS_OUTPUT, null, new HashSet<Kind>(Arrays.asList(Kind.CLASS)),
{'fraction_non_alphanumeric': 0.13789411064842355, 'fraction_numerical': 0.005710886377156455, 'mean_word_length': 2.3786173633440515, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5833187', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2322, 'n_words': 475}