Do not generate properties from script constructor

From now all script parameter properties are generated by template class
This commit is contained in:
Mikhail Zarechenskiy
2016-09-12 02:40:33 +03:00
parent 0ba2d374bc
commit b18cbc37c6
6 changed files with 55 additions and 266 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -17,8 +17,6 @@
package org.jetbrains.kotlin.scripts
import com.intellij.openapi.util.Disposer
import junit.framework.TestCase
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.*
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
@@ -27,9 +25,7 @@ import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
import org.jetbrains.kotlin.codegen.CompilationException
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.addKotlinSourceRoot
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.script.KotlinScriptDefinition
import org.jetbrains.kotlin.script.ScriptParameter
import org.jetbrains.kotlin.script.StandardScriptDefinition
import org.jetbrains.kotlin.test.ConfigurationKind
import org.jetbrains.kotlin.test.KotlinTestUtils
@@ -39,17 +35,9 @@ import org.jetbrains.kotlin.utils.PathUtil
import org.junit.Assert
import org.junit.Test
import java.io.File
import java.lang.reflect.InvocationTargetException
import java.net.URLClassLoader
class ScriptTest : KtUsefulTestCase() {
@Test
fun testScriptWithParam() {
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()))
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}
@Test
fun testStandardScriptWithParams() {
val aClass = compileScript("fib_std.kts", StandardScriptDefinition)
@@ -74,108 +62,13 @@ class ScriptTest : KtUsefulTestCase() {
Assert.assertNotNull(aClass)
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
Assert.assertNotNull(anObj)
val savedClassLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()))
val savedClassLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), aClass.classLoader)
val aClassSaved = savedClassLoader.loadClass(aClass.name)
Assert.assertNotNull(aClassSaved)
val anObjSaved = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClassSaved!!, emptyList())
Assert.assertNotNull(anObjSaved)
}
@Test
fun testScriptWithParamConversion() {
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()))
Assert.assertNotNull(aClass)
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4"))
Assert.assertNotNull(anObj)
}
@Test
fun testScriptWithPackage() {
val aClass = compileScript("fib.pkg.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()))
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}
@Test
fun testScriptWithScriptDefinition() {
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()))
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}
@Test
fun testScriptWithClassParameter() {
val aClass = compileScript("fib_cp.kts", ReflectedParamClassTestScriptDefinition(".kts", "param", TestParamClass::class), runIsolated = false)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(TestParamClass::class.java).newInstance(TestParamClass(4))
}
@Test
fun testScriptWithBaseClass() {
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassTestScriptDefinition(".kts", numIntParam(), TestDSLClass::class), runIsolated = false)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}
@Test
fun testScriptWithBaseClassWithParam() {
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassWithParamsTestScriptDefinition(".kts", numIntParam() + numIntParam("passthrough"), TestDSLClassWithParam::class, numIntParam("passthrough")), runIsolated = false)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE, Integer.TYPE).newInstance(4, 1)
}
@Test
fun testScriptWithInterface() {
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassTestScriptDefinition(".kts", numIntParam(), TestDSLInterface::class), runIsolated = false)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}
@Test
fun testScriptWithClasspath() {
val aClass1 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = emptyList()), runIsolated = true, suppressOutput = true)
Assert.assertNull(aClass1)
val cp = classpathFromClassloader(ScriptTest::class.java.classLoader).filter { it.name.contains("kotlin-runtime") || it.name.contains("junit") }
Assert.assertFalse(cp.isEmpty())
val aClass2 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = cp), runIsolated = true)
Assert.assertNotNull(aClass2)
}
@Test
fun testScriptWithExtraImports() {
val aClass1 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = emptyList()), runIsolated = true, suppressOutput = true)
Assert.assertNull(aClass1)
val cp = classpathFromClassloader(ScriptTest::class.java.classLoader).filter { it.name.contains("kotlin-runtime") || it.name.contains("junit") }
Assert.assertFalse(cp.isEmpty())
val aClass2 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = cp, extraDependencies = SimpleScriptExtraDependencies(cp)), runIsolated = true)
Assert.assertNotNull(aClass2)
}
@Test
fun testSmokeScriptException() {
val aClass = compileSmokeTestScript(
"scriptException/script.kts",
StandardWithClasspathScriptDefinition(
".kts",
listOf("dependencies/bootstrap-compiler/Kotlin/kotlinc/lib/kotlin-runtime.jar",
"dependencies/bootstrap-compiler/Kotlin/kotlinc/lib/kotlin-reflect.jar")
.map { File(it) }))
Assert.assertNotNull(aClass)
var exceptionThrown = false
try {
KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
}
catch (e: InvocationTargetException) {
Assert.assertTrue(e.cause is IllegalStateException)
exceptionThrown = true
}
Assert.assertTrue(exceptionThrown)
}
private fun compileScript(
scriptPath: String,
scriptDefinition: KotlinScriptDefinition,
@@ -184,14 +77,6 @@ class ScriptTest : KtUsefulTestCase() {
saveClassesDir: File? = null): Class<*>? =
compileScriptImpl("compiler/testData/script/" + scriptPath, scriptDefinition, runIsolated, suppressOutput, saveClassesDir)
private fun compileSmokeTestScript(
scriptPath: String,
scriptDefinition: KotlinScriptDefinition,
runIsolated: Boolean = true,
suppressOutput: Boolean = false,
saveClassesDir: File? = null): Class<*>? =
compileScriptImpl("compiler/testData/integration/smoke/" + scriptPath, scriptDefinition, runIsolated, suppressOutput, saveClassesDir)
private fun compileScriptImpl(
scriptPath: String,
scriptDefinition: KotlinScriptDefinition,
@@ -235,10 +120,6 @@ class ScriptTest : KtUsefulTestCase() {
Disposer.dispose(rootDisposable)
}
}
private fun numIntParam(name: String = "num"): List<ScriptParameter> {
return listOf(ScriptParameter(Name.identifier(name), DefaultBuiltIns.Instance.intType))
}
}
class TestParamClass(val memberNum: Int)
@@ -34,6 +34,7 @@ import org.junit.Assert
import org.junit.Test
import java.io.File
import java.lang.Exception
import java.lang.reflect.InvocationTargetException
import java.net.URLClassLoader
import java.util.concurrent.Future
import kotlin.reflect.KClass
@@ -134,6 +135,43 @@ class ScriptTest2 {
aClass!!.getConstructor(Array<String>::class.java).newInstance(arrayOf("4", "other"))
}
@Test
fun testScriptWithPackage() {
val aClass = compileScript("fib.pkg.kts", ScriptWithIntParam::class)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}
@Test
fun testScriptWithScriptDefinition() {
val aClass = compileScript("fib.kts", ScriptWithIntParam::class)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}
@Test
fun testScriptWithParamConversion() {
val aClass = compileScript("fib.kts", ScriptWithIntParam::class)
Assert.assertNotNull(aClass)
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4"))
Assert.assertNotNull(anObj)
}
@Test
fun testSmokeScriptException() {
val aClass = compileScript("smoke_exception.kts", ScriptWithArrayParam::class)
Assert.assertNotNull(aClass)
var exceptionThrown = false
try {
KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
}
catch (e: InvocationTargetException) {
Assert.assertTrue(e.cause is IllegalStateException)
exceptionThrown = true
}
Assert.assertTrue(exceptionThrown)
}
private fun compileScript(
scriptPath: String,
scriptBase: KClass<out Any>,