From cbc893ed17acae96f0ac06113287b5568a5b6a5d Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 29 Mar 2017 17:22:04 +0300 Subject: [PATCH] Minor, drop useless class in cli-runner Use a simple List instead of Classpath --- .../org/jetbrains/kotlin/runner/Classpath.kt | 34 ------------------- .../src/org/jetbrains/kotlin/runner/Main.kt | 17 +++++++--- .../src/org/jetbrains/kotlin/runner/Runner.kt | 4 ++- .../org/jetbrains/kotlin/runner/runners.kt | 17 +++++----- 4 files changed, 24 insertions(+), 48 deletions(-) delete mode 100644 compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Classpath.kt diff --git a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Classpath.kt b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Classpath.kt deleted file mode 100644 index 6bb10080703..00000000000 --- a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Classpath.kt +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.runner - -import java.io.File -import java.net.URL - -class Classpath { - private val classpath = arrayListOf() - - fun add(paths: String) { - for (path in paths.split(File.pathSeparator)) { - classpath.add(File(path).absoluteFile.toURI().toURL()) - } - } - - fun getURLs(): Array { - return classpath.toTypedArray() - } -} diff --git a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt index bea46a4c810..e56f63e0461 100644 --- a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt +++ b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.runner import java.io.File import java.io.FileNotFoundException +import java.net.URL import java.util.* object Main { @@ -33,13 +34,13 @@ object Main { } private fun run(args: Array) { - val classpath = Classpath() + val classpath = arrayListOf() var runner: Runner? = null var collectingArguments = false val arguments = arrayListOf() var noReflect = false - classpath.add(".") + classpath.addPaths(".") var i = 0 while (i < args.size) { @@ -64,7 +65,7 @@ object Main { printVersionAndExit() } else if ("-classpath" == arg || "-cp" == arg) { - classpath.add(next()) + classpath.addPaths(next()) } else if ("-expression" == arg || "-e" == arg) { runner = ExpressionRunner(next()) @@ -91,10 +92,10 @@ object Main { i++ } - classpath.add(KOTLIN_HOME.toString() + "/lib/kotlin-runtime.jar") + classpath.addPaths(KOTLIN_HOME.toString() + "/lib/kotlin-runtime.jar") if (!noReflect) { - classpath.add(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar") + classpath.addPaths(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar") } if (runner == null) { @@ -104,6 +105,12 @@ object Main { runner.run(classpath, arguments) } + private fun MutableList.addPaths(paths: String) { + for (path in paths.split(File.pathSeparator)) { + add(File(path).absoluteFile.toURI().toURL()) + } + } + @JvmStatic fun main(args: Array) { try { diff --git a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Runner.kt b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Runner.kt index 1302eb94b89..fbb611e50c7 100644 --- a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Runner.kt +++ b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Runner.kt @@ -16,6 +16,8 @@ package org.jetbrains.kotlin.runner +import java.net.URL + interface Runner { - fun run(classpath: Classpath, arguments: List) + fun run(classpath: List, arguments: List) } diff --git a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt index c565aa2ca52..417a94bd21b 100644 --- a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt +++ b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt @@ -20,6 +20,7 @@ import java.io.File import java.io.IOException import java.lang.reflect.InvocationTargetException import java.lang.reflect.Modifier +import java.net.URL import java.net.URLClassLoader import java.util.jar.Attributes import java.util.jar.JarFile @@ -29,9 +30,9 @@ class RunnerException(message: String) : RuntimeException(message) abstract class AbstractRunner : Runner { protected abstract val className: String - protected abstract fun createClassLoader(classpath: Classpath): ClassLoader + protected abstract fun createClassLoader(classpath: List): ClassLoader - override fun run(classpath: Classpath, arguments: List) { + override fun run(classpath: List, arguments: List) { val classLoader = createClassLoader(classpath) val mainClass = try { @@ -69,8 +70,8 @@ abstract class AbstractRunner : Runner { } class MainClassRunner(override val className: String) : AbstractRunner() { - override fun createClassLoader(classpath: Classpath): ClassLoader = - URLClassLoader(classpath.getURLs(), null) + override fun createClassLoader(classpath: List): ClassLoader = + URLClassLoader(classpath.toTypedArray(), null) } class JarRunner(private val path: String) : AbstractRunner() { @@ -89,7 +90,7 @@ class JarRunner(private val path: String) : AbstractRunner() { } ?: throw RunnerException("no Main-Class entry found in manifest in $path") - override fun createClassLoader(classpath: Classpath): ClassLoader { + override fun createClassLoader(classpath: List): ClassLoader { // 'kotlin *.jar' ignores the passed classpath as 'java -jar' does // TODO: warn on non-empty classpath? @@ -98,21 +99,21 @@ class JarRunner(private val path: String) : AbstractRunner() { } class ReplRunner : Runner { - override fun run(classpath: Classpath, arguments: List) { + override fun run(classpath: List, arguments: List) { // TODO: run REPL instead throw RunnerException("please specify at least one name or file to run") } } class ScriptRunner(private val path: String) : Runner { - override fun run(classpath: Classpath, arguments: List) { + override fun run(classpath: List, arguments: List) { // TODO throw RunnerException("running Kotlin scripts is not yet supported") } } class ExpressionRunner(private val code: String) : Runner { - override fun run(classpath: Classpath, arguments: List) { + override fun run(classpath: List, arguments: List) { // TODO throw RunnerException("evaluating expressions is not yet supported") }