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

com.github.bloodshura.x.utilities.compiler.ExternalCompiler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2013-2018, João Vitor Verona Biazibetti - All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see .
 *
 * https://www.github.com/BloodShura
 */

package com.github.bloodshura.x.utilities.compiler;

import com.github.bloodshura.x.charset.TextBuilder;
import com.github.bloodshura.x.collection.list.XList;
import com.github.bloodshura.x.collection.list.impl.XArrayList;
import com.github.bloodshura.x.collection.view.XBasicView;
import com.github.bloodshura.x.collection.view.XView;
import com.github.bloodshura.x.io.exception.FileException;
import com.github.bloodshura.x.io.file.Directory;
import com.github.bloodshura.x.io.file.File;
import com.github.bloodshura.x.resource.Resource;
import com.github.bloodshura.x.sys.XSystem;
import com.github.bloodshura.x.utilities.compiler.exception.CompileException;

import javax.annotation.Nonnull;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.IOException;
import java.util.Locale;

public class ExternalCompiler extends XArrayList {
	public void add(@Nonnull CharSequence className, @Nonnull Resource resource) throws FileException, IOException {
		add(XSystem.getTempFolder(), className, resource);
	}

	public void add(@Nonnull Directory output, @Nonnull CharSequence className, @Nonnull Resource resource) throws FileException, IOException {
		File file = new File(output, className + ".java");

		file.write(resource);
		add(file);
	}

	@Nonnull
	public XView calculateOutputFiles() throws FileException {
		XList list = new XArrayList<>();

		forEach(source -> list.add(new File(source.getParent(), source.getName() + ".class")));

		return new XBasicView<>(list);
	}

	public void compile() throws CompileException {
		try {
			JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

			if (compiler == null) {
				throw new CompileException("Java Development Kit not available");
			}

			DiagnosticCollector diagnostics = new DiagnosticCollector<>();

			try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
				java.io.File[] handles = new java.io.File[size()];

				for (int i = 0; i < handles.length; i++) {
					handles[i] = get(i).handle();
				}

				Iterable compilationUnits = fileManager.getJavaFileObjects(handles);
				CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);
				boolean result = task.call();

				if (!result) {
					for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
						if (diagnostic.getKind() == Kind.ERROR) {
							TextBuilder builder = new TextBuilder().setSeparator(' ');

							builder.append("An error occurred while compiling code");
							builder.append("at line #" + diagnostic.getLineNumber() + " and column #" + diagnostic.getColumnNumber() + ":");
							builder.append(diagnostic.getMessage(Locale.getDefault()));

							throw new CompileException(builder);
						}
					}
				}
			}
		}
		catch (IOException exception) {
			throw new CompileException(exception);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy