com.hpl.mds.annotations.processors.JCompiler.java Source code

Java tutorial

Introduction

Here is the source code for com.hpl.mds.annotations.processors.JCompiler.java

Source

/*
 *
 *  Managed Data Structures
 *  Copyright  2016 Hewlett Packard Enterprise Development Company LP.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  As an exception, the copyright holders of this Library grant you permission
 *  to (i) compile an Application with the Library, and (ii) distribute the 
 *  Application containing code generated by the Library and added to the 
 *  Application during this compilation process under terms of your choice, 
 *  provided you also meet the terms and conditions of the Application license.
 *
 */

package com.hpl.mds.annotations.processors;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

import org.apache.commons.io.FileUtils;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;

public class JCompiler {

    private static final String RESOURCES_DIR = "test/resources/";
    private static final String OUTPUT_DIR = "test/generated/";
    private static final String ANNOTATIONS_LIB = "jars/mds-annotations-processor.jar";
    private static final String API_LIB = "jars/mds-java-api.jar";
    private static final String RECORD_CLASSPATH = API_LIB + ":" + OUTPUT_DIR + ":" + RESOURCES_DIR;
    private static final String SCHEMA_CLASSPATH = ANNOTATIONS_LIB + ":" + API_LIB + ":" + OUTPUT_DIR + ":"
            + RESOURCES_DIR;
    private static final JavaCompiler COMPILER = ToolProvider.getSystemJavaCompiler();
    private static URLClassLoader CLASS_LOADER;
    private static JCompiler SINGLETON = new JCompiler();

    public static JCompiler getInstance() {
        return SINGLETON;
    }

    // disabling public constructor
    private JCompiler() {
        try {
            Path outputPath = Paths.get(OUTPUT_DIR);
            File outputDirFile = outputPath.toFile();
            if (outputDirFile.exists()) {
                FileUtils.cleanDirectory(outputDirFile);
            }
            URL url = outputDirFile.toURI().toURL();
            CLASS_LOADER = new URLClassLoader(new URL[] { url });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Compiles the list of source files and returns the class associated with
     * the first argument
     * 
     * @param srcFiles
     * @return
     * @throws ClassNotFoundException
     */
    public void compileLoadRecord(String recordSrcFile) throws ClassNotFoundException {
        assertEquals("error while compiling generated record", 0,
                compile(null, RECORD_CLASSPATH, OUTPUT_DIR, recordSrcFile));
        String className = recordSrcFile.substring(0, recordSrcFile.length() - 5).replace('/', '.');
        CLASS_LOADER.loadClass(className);
    }

    public int compileSchema(String... schemaSrcFiles) {
        return compileSchema(null, schemaSrcFiles);
    }

    public int compileSchema(OutputStream err, String... schemaSrcFiles) {
        return compile(err, SCHEMA_CLASSPATH, RESOURCES_DIR, schemaSrcFiles);
    }

    private int compile(OutputStream err, String classpath, String srcDir, String... srcFiles) {
        String[] args = new String[srcFiles.length + 5];
        args[0] = "-implicit:class";
        args[1] = "-cp";
        args[2] = classpath;
        args[3] = "-s";
        args[4] = OUTPUT_DIR;
        for (int i = 0; i < srcFiles.length; i++) {
            args[i + 5] = srcDir + srcFiles[i];
        }
        return COMPILER.run(null, null, err, args);
    }

    public void parse(String javaSrcFile, ParserConfig config) throws ParseException, IOException {
        Visitor visitor = new Visitor(config);
        visitor.visit(JavaParser.parse(new File(OUTPUT_DIR + javaSrcFile)), null);
        visitor.verify();
    }
}