Minor, drop useless class in cli-runner

Use a simple List<URL> instead of Classpath
This commit is contained in:
Alexander Udalov
2017-03-29 17:22:04 +03:00
parent 93d5f6e635
commit cbc893ed17
4 changed files with 24 additions and 48 deletions
@@ -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<URL>()
fun add(paths: String) {
for (path in paths.split(File.pathSeparator)) {
classpath.add(File(path).absoluteFile.toURI().toURL())
}
}
fun getURLs(): Array<URL> {
return classpath.toTypedArray()
}
}
@@ -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<String>) {
val classpath = Classpath()
val classpath = arrayListOf<URL>()
var runner: Runner? = null
var collectingArguments = false
val arguments = arrayListOf<String>()
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<URL>.addPaths(paths: String) {
for (path in paths.split(File.pathSeparator)) {
add(File(path).absoluteFile.toURI().toURL())
}
}
@JvmStatic
fun main(args: Array<String>) {
try {
@@ -16,6 +16,8 @@
package org.jetbrains.kotlin.runner
import java.net.URL
interface Runner {
fun run(classpath: Classpath, arguments: List<String>)
fun run(classpath: List<URL>, arguments: List<String>)
}
@@ -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<URL>): ClassLoader
override fun run(classpath: Classpath, arguments: List<String>) {
override fun run(classpath: List<URL>, arguments: List<String>) {
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<URL>): 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<URL>): 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<String>) {
override fun run(classpath: List<URL>, arguments: List<String>) {
// 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<String>) {
override fun run(classpath: List<URL>, arguments: List<String>) {
// TODO
throw RunnerException("running Kotlin scripts is not yet supported")
}
}
class ExpressionRunner(private val code: String) : Runner {
override fun run(classpath: Classpath, arguments: List<String>) {
override fun run(classpath: List<URL>, arguments: List<String>) {
// TODO
throw RunnerException("evaluating expressions is not yet supported")
}