refactor compiler launcher

* method should not modify input
* replace String with File
* add @NotNull and @Nullable annotations
This commit is contained in:
Stepan Koltsov
2012-06-10 03:44:29 +04:00
parent 4687635c8f
commit 35f1de468f
8 changed files with 36 additions and 26 deletions
@@ -97,8 +97,10 @@ public class BytecodeCompiler {
*/ */
public void sourcesToDir ( @NotNull String src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath ) { public void sourcesToDir ( @NotNull String src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath ) {
try { try {
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), null, output, false, true K2JVMCompileEnvironmentConfiguration configuration = env(stdlib, classpath);
/* Last arg is ignored anyway */); configuration.getEnvironment().addSources(src);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, null, new File(output), false, true);
if ( ! success ) { if ( ! success ) {
throw new CompileEnvironmentException( errorMessage( src, false )); throw new CompileEnvironmentException( errorMessage( src, false ));
} }
@@ -120,7 +122,10 @@ public class BytecodeCompiler {
*/ */
public void sourcesToJar ( @NotNull String src, @NotNull String jar, boolean includeRuntime, @Nullable String stdlib, @Nullable String[] classpath ) { public void sourcesToJar ( @NotNull String src, @NotNull String jar, boolean includeRuntime, @Nullable String stdlib, @Nullable String[] classpath ) {
try { try {
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), jar, null, false, includeRuntime); K2JVMCompileEnvironmentConfiguration configuration = env(stdlib, classpath);
configuration.getEnvironment().addSources(src);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, new File(jar), null, false, includeRuntime);
if ( ! success ) { if ( ! success ) {
throw new CompileEnvironmentException( errorMessage( src, false )); throw new CompileEnvironmentException( errorMessage( src, false ));
} }
@@ -145,7 +150,7 @@ public class BytecodeCompiler {
K2JVMCompileEnvironmentConfiguration env = env(stdlib, classpath); K2JVMCompileEnvironmentConfiguration env = env(stdlib, classpath);
List<Module> modules = CompileEnvironmentUtil.loadModuleScript(module, env.getMessageCollector()); List<Module> modules = CompileEnvironmentUtil.loadModuleScript(module, env.getMessageCollector());
File directory = new File(module).getParentFile(); File directory = new File(module).getParentFile();
boolean success = KotlinToJVMBytecodeCompiler.compileModules(env, modules, directory, jar, null, includeRuntime); boolean success = KotlinToJVMBytecodeCompiler.compileModules(env, modules, directory, new File(jar), null, includeRuntime);
if ( ! success ) { if ( ! success ) {
throw new CompileEnvironmentException( errorMessage( module, false )); throw new CompileEnvironmentException( errorMessage( module, false ));
} }
@@ -106,6 +106,9 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
try { try {
configureEnvironment(configuration, arguments); configureEnvironment(configuration, arguments);
File jar = arguments.jar != null ? new File(arguments.jar) : null;
File outputDir = arguments.outputDir != null ? new File(arguments.outputDir) : null;
boolean noErrors; boolean noErrors;
if (arguments.module != null) { if (arguments.module != null) {
boolean oldVerbose = messageCollector.isVerbose(); boolean oldVerbose = messageCollector.isVerbose();
@@ -115,28 +118,30 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
messageCollector.setVerbose(oldVerbose); messageCollector.setVerbose(oldVerbose);
File directory = new File(arguments.module).getParentFile(); File directory = new File(arguments.module).getParentFile();
noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules, noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
directory, arguments.jar, arguments.outputDir, directory, jar, outputDir,
arguments.includeRuntime); arguments.includeRuntime);
} }
else { else {
// TODO ideally we'd unify to just having a single field that supports multiple files/dirs // TODO ideally we'd unify to just having a single field that supports multiple files/dirs
if (arguments.getSourceDirs() != null) { if (arguments.getSourceDirs() != null) {
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSourceDirectories(configuration, noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSourceDirectories(configuration,
arguments.getSourceDirs(), arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime); arguments.getSourceDirs(), jar, outputDir, arguments.script, arguments.includeRuntime);
} }
else { else {
List<String> sources = Lists.newArrayList();
if (arguments.src != null) { if (arguments.src != null) {
sources.add(arguments.src); configuration.getEnvironment().addSources(arguments.src);
} }
if (arguments.script) { if (arguments.script) {
sources.add(arguments.freeArgs.get(0)); configuration.getEnvironment().addSources(arguments.freeArgs.get(0));
} }
else { else {
sources.addAll(arguments.freeArgs); for (String freeArg : arguments.freeArgs) {
configuration.getEnvironment().addSources(freeArg);
}
} }
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration,
sources, arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime); noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(
configuration, jar, outputDir, arguments.script, arguments.includeRuntime);
} }
} }
return noErrors ? OK : COMPILATION_ERROR; return noErrors ? OK : COMPILATION_ERROR;
@@ -276,7 +276,7 @@ public class CompileEnvironmentUtil {
} }
} }
public static void writeToOutputDirectory(ClassFileFactory factory, final String outputDir) { public static void writeToOutputDirectory(ClassFileFactory factory, @NotNull File outputDir) {
List<String> files = factory.files(); List<String> files = factory.files();
for (String file : files) { for (String file : files) {
File target = new File(outputDir, file); File target = new File(outputDir, file);
@@ -90,8 +90,8 @@ public class KotlinToJVMBytecodeCompiler {
K2JVMCompileEnvironmentConfiguration configuration, K2JVMCompileEnvironmentConfiguration configuration,
@NotNull List<Module> modules, @NotNull List<Module> modules,
@NotNull File directory, @NotNull File directory,
@Nullable String jarPath, @Nullable File jarPath,
@Nullable String outputDir, @Nullable File outputDir,
boolean jarRuntime) { boolean jarRuntime) {
for (Module moduleBuilder : modules) { for (Module moduleBuilder : modules) {
@@ -108,7 +108,7 @@ public class KotlinToJVMBytecodeCompiler {
CompileEnvironmentUtil.writeToOutputDirectory(moduleFactory, outputDir); CompileEnvironmentUtil.writeToOutputDirectory(moduleFactory, outputDir);
} }
else { else {
String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath(); File path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar");
FileOutputStream outputStream = null; FileOutputStream outputStream = null;
try { try {
outputStream = new FileOutputStream(path); outputStream = new FileOutputStream(path);
@@ -131,10 +131,10 @@ public class KotlinToJVMBytecodeCompiler {
return true; return true;
} }
private static boolean compileBunchOfSources( public static boolean compileBunchOfSources(
K2JVMCompileEnvironmentConfiguration configuration, K2JVMCompileEnvironmentConfiguration configuration,
String jar, @Nullable File jar,
String outputDir, @Nullable File outputDir,
boolean script, boolean script,
boolean includeRuntime boolean includeRuntime
) { ) {
@@ -202,7 +202,7 @@ public class KotlinToJVMBytecodeCompiler {
public static boolean compileBunchOfSources( public static boolean compileBunchOfSources(
K2JVMCompileEnvironmentConfiguration configuration, K2JVMCompileEnvironmentConfiguration configuration,
List<String> sourceFilesOrDirs, String jar, String outputDir, boolean script, boolean includeRuntime) { List<String> sourceFilesOrDirs, File jar, File outputDir, boolean script, boolean includeRuntime) {
for (String sourceFileOrDir : sourceFilesOrDirs) { for (String sourceFileOrDir : sourceFilesOrDirs) {
configuration.getEnvironment().addSources(sourceFileOrDir); configuration.getEnvironment().addSources(sourceFileOrDir);
} }
@@ -213,7 +213,7 @@ public class KotlinToJVMBytecodeCompiler {
public static boolean compileBunchOfSourceDirectories( public static boolean compileBunchOfSourceDirectories(
K2JVMCompileEnvironmentConfiguration configuration, K2JVMCompileEnvironmentConfiguration configuration,
List<String> sources, String jar, String outputDir, boolean script, boolean includeRuntime) { List<String> sources, File jar, File outputDir, boolean script, boolean includeRuntime) {
for (String source : sources) { for (String source : sources) {
configuration.getEnvironment().addSources(source); configuration.getEnvironment().addSources(source);
} }
@@ -78,7 +78,7 @@ public class CompileJavaAgainstKotlinTest extends TestCaseWithTmpdir {
ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR); ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR);
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir.getPath()); CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir);
Disposer.dispose(myTestRootDisposable); Disposer.dispose(myTestRootDisposable);
@@ -92,7 +92,7 @@ public class CompileKotlinAgainstKotlinTest extends TestCaseWithTmpdir {
ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR); ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR);
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, aDir.getPath()); CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, aDir);
Disposer.dispose(myTestRootDisposable); Disposer.dispose(myTestRootDisposable);
} }
@@ -110,7 +110,7 @@ public class CompileKotlinAgainstKotlinTest extends TestCaseWithTmpdir {
ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR); ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR);
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, bDir.getPath()); CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, bDir);
Disposer.dispose(myTestRootDisposable); Disposer.dispose(myTestRootDisposable);
} }
@@ -75,7 +75,7 @@ public class ReadKotlinBinaryClassTest extends TestCaseWithTmpdir {
ClassFileFactory classFileFactory = state.getFactory(); ClassFileFactory classFileFactory = state.getFactory();
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir.getPath()); CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir);
NamespaceDescriptor namespaceFromSource = state.getBindingContext().get(BindingContext.FILE_TO_NAMESPACE, psiFile); NamespaceDescriptor namespaceFromSource = state.getBindingContext().get(BindingContext.FILE_TO_NAMESPACE, psiFile);
@@ -83,7 +83,7 @@ public class WriteSignatureTest extends TestCaseWithTmpdir {
ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR); ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR);
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir.getPath()); CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir);
Disposer.dispose(myTestRootDisposable); Disposer.dispose(myTestRootDisposable);