Deprecate KPackage, to be removed later

This commit is contained in:
Alexander Udalov
2015-10-08 16:45:28 +03:00
parent 790c59d9e5
commit 0218e0351b
15 changed files with 31 additions and 195 deletions
@@ -35,7 +35,6 @@ import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
import org.jetbrains.kotlin.incremental.components.NoLookupLocation; import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
import org.jetbrains.kotlin.load.java.JvmAbi; import org.jetbrains.kotlin.load.java.JvmAbi;
import org.jetbrains.kotlin.load.java.JvmAnnotationNames; import org.jetbrains.kotlin.load.java.JvmAnnotationNames;
import org.jetbrains.kotlin.load.kotlin.PackageClassUtils;
import org.jetbrains.kotlin.psi.JetElement; import org.jetbrains.kotlin.psi.JetElement;
import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils;
@@ -357,14 +356,10 @@ public class ClosureCodegen extends MemberCodegen<JetElement> {
value.put(K_CLASS_TYPE, iv); value.put(K_CLASS_TYPE, iv);
} }
else if (container instanceof PackageFragmentDescriptor) { else if (container instanceof PackageFragmentDescriptor) {
String packageClassInternalName = PackageClassUtils.getPackageClassInternalName( iv.aconst(state.getTypeMapper().mapOwner(descriptor));
((PackageFragmentDescriptor) container).getFqName()
);
iv.aconst(Type.getObjectType(packageClassInternalName));
iv.aconst(state.getModuleName()); iv.aconst(state.getModuleName());
// TODO: create KPackage with a useful class, not the old package facade iv.invokestatic(REFLECTION, "getOrCreateKotlinPackage",
iv.invokestatic(REFLECTION, "createKotlinPackage", Type.getMethodDescriptor(K_DECLARATION_CONTAINER_TYPE, getType(Class.class), getType(String.class)), false);
Type.getMethodDescriptor(K_PACKAGE_TYPE, getType(Class.class), getType(String.class)), false);
} }
else if (container instanceof ScriptDescriptor) { else if (container instanceof ScriptDescriptor) {
// TODO: correct container for scripts (KScript?) // TODO: correct container for scripts (KScript?)
@@ -43,7 +43,6 @@ public class AsmTypes {
public static final Type K_CLASS_TYPE = reflect("KClass"); public static final Type K_CLASS_TYPE = reflect("KClass");
public static final Type K_CLASS_ARRAY_TYPE = Type.getObjectType("[" + K_CLASS_TYPE.getDescriptor()); public static final Type K_CLASS_ARRAY_TYPE = Type.getObjectType("[" + K_CLASS_TYPE.getDescriptor());
public static final Type K_PACKAGE_TYPE = reflect("KPackage");
public static final Type K_DECLARATION_CONTAINER_TYPE = reflect("KDeclarationContainer"); public static final Type K_DECLARATION_CONTAINER_TYPE = reflect("KDeclarationContainer");
public static final Type K_FUNCTION = reflect("KFunction"); public static final Type K_FUNCTION = reflect("KFunction");
@@ -15,8 +15,8 @@ fun box(): String {
val getter = p.javaGetter!! val getter = p.javaGetter!!
val setter = p.javaSetter!! val setter = p.javaSetter!!
assertEquals(getter, Class.forName("_DefaultPackage").getMethod("getExt", javaClass<K>())) assertEquals(getter, Class.forName("ExtensionPropertyKt").getMethod("getExt", javaClass<K>()))
assertEquals(setter, Class.forName("_DefaultPackage").getMethod("setExt", javaClass<K>(), javaClass<Double>())) assertEquals(setter, Class.forName("ExtensionPropertyKt").getMethod("setExt", javaClass<K>(), javaClass<Double>()))
val k = K(42L) val k = K(42L)
assert(getter.invoke(null, k) == 42.0, "Fail k getter") assert(getter.invoke(null, k) == 42.0, "Fail k getter")
@@ -1,13 +0,0 @@
package test
import kotlin.reflect.jvm.*
import kotlin.test.*
fun box(): String {
val facadeJClass = Class.forName("test.TestPackage") as Class<Any>
assertEquals(facadeJClass, facadeJClass.kotlinPackage!!.javaFacade)
assertEquals(facadeJClass, facadeJClass.kotlin.java)
return "OK"
}
@@ -14,8 +14,8 @@ fun box(): String {
val getter = p.javaGetter!! val getter = p.javaGetter!!
val setter = p.javaSetter!! val setter = p.javaSetter!!
assertEquals(getter, Class.forName("_DefaultPackage").getMethod("getTopLevel")) assertEquals(getter, Class.forName("TopLevelPropertyKt").getMethod("getTopLevel"))
assertEquals(setter, Class.forName("_DefaultPackage").getMethod("setTopLevel", javaClass<String>())) assertEquals(setter, Class.forName("TopLevelPropertyKt").getMethod("setTopLevel", javaClass<String>()))
assert(getter.invoke(null) == "123", "Fail k getter") assert(getter.invoke(null) == "123", "Fail k getter")
setter.invoke(null, "456") setter.invoke(null, "456")
@@ -1,8 +0,0 @@
import kotlin.test.*
import kotlin.reflect.jvm.kotlinPackage
fun box(): String {
val p = Class.forName("_DefaultPackage").kotlinPackage
if ("$p" != "package <default>") return "Fail: $p"
return "OK"
}
@@ -1,10 +0,0 @@
package test.foo.bar
import kotlin.test.*
import kotlin.reflect.jvm.kotlinPackage
fun box(): String {
val p = Class.forName("test.foo.bar.BarPackage").kotlinPackage
if ("$p" != "package test.foo.bar") return "Fail: $p"
return "OK"
}
@@ -1,23 +0,0 @@
import kotlin.reflect.*
import kotlin.reflect.jvm.*
import kotlin.test.assertEquals
fun foo() {}
fun Int.bar() {}
val baz = 42
val Int.quux: Int get() = this
fun box(): String {
fun check(actual: Collection<KCallable<*>>, expected: Set<String>) {
assertEquals(expected, actual.map { it.name }.toSet())
}
val kp = Class.forName("_DefaultPackage").kotlinPackage ?: return "Fail: package class not found"
check(kp.members, setOf("bar", "baz", "foo", "box", "quux"))
check(kp.functions, setOf("bar", "foo", "box"))
check(kp.properties, setOf("baz"))
check(kp.extensionProperties, setOf("quux"))
return "OK"
}
@@ -3678,12 +3678,6 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
doTestWithStdlib(fileName); doTestWithStdlib(fileName);
} }
@TestMetadata("package.kt")
public void testPackage() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/mapping/package.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("propertyAccessors.kt") @TestMetadata("propertyAccessors.kt")
public void testPropertyAccessors() throws Exception { public void testPropertyAccessors() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/mapping/propertyAccessors.kt"); String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/mapping/propertyAccessors.kt");
@@ -3801,12 +3795,6 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
doTestWithStdlib(fileName); doTestWithStdlib(fileName);
} }
@TestMetadata("defaultPackageToString.kt")
public void testDefaultPackageToString() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/defaultPackageToString.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("extensionPropertyReceiverToString.kt") @TestMetadata("extensionPropertyReceiverToString.kt")
public void testExtensionPropertyReceiverToString() throws Exception { public void testExtensionPropertyReceiverToString() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/extensionPropertyReceiverToString.kt"); String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/extensionPropertyReceiverToString.kt");
@@ -3831,12 +3819,6 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
doTestWithStdlib(fileName); doTestWithStdlib(fileName);
} }
@TestMetadata("packageToString.kt")
public void testPackageToString() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/packageToString.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("parametersEqualsHashCode.kt") @TestMetadata("parametersEqualsHashCode.kt")
public void testParametersEqualsHashCode() throws Exception { public void testParametersEqualsHashCode() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/parametersEqualsHashCode.kt"); String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/parametersEqualsHashCode.kt");
@@ -3919,21 +3901,6 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
} }
} }
@TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/packages")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Packages extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInPackages() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithStdlib/reflection/packages"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("simpleGetMembers.kt")
public void testSimpleGetMembers() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/packages/simpleGetMembers.kt");
doTestWithStdlib(fileName);
}
}
@TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/parameters") @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/parameters")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)
@@ -19,4 +19,6 @@ package kotlin.reflect
/** /**
* Represents a package and provides introspection capabilities. * Represents a package and provides introspection capabilities.
*/ */
@Deprecated("This class will be deleted soon because reflection on packages is going to be redesigned " +
"after the introduction of new kinds of runtime representations of packages on JVM.")
public interface KPackage : KDeclarationContainer public interface KPackage : KDeclarationContainer
@@ -1,37 +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 kotlin.reflect
import kotlin.reflect.jvm.internal.KPackageImpl
/**
* Returns all non-extension properties declared in this package.
*/
public val KPackage.properties: Collection<KProperty0<*>>
get() = (this as KPackageImpl)
.getMembers(scope, declaredOnly = false, nonExtensions = true, extensions = false)
.filterIsInstance<KProperty0<*>>()
.toList()
/**
* Returns all extension properties declared in this package.
*/
public val KPackage.extensionProperties: Collection<KProperty1<*, *>>
get() = (this as KPackageImpl)
.getMembers(scope, declaredOnly = false, nonExtensions = false, extensions = true)
.filterIsInstance<KProperty1<*, *>>()
.toList()
@@ -18,7 +18,6 @@ package kotlin.reflect.jvm.internal;
import kotlin.jvm.internal.*; import kotlin.jvm.internal.*;
import kotlin.reflect.*; import kotlin.reflect.*;
import org.jetbrains.kotlin.load.java.JvmAbi;
/** /**
* @suppress * @suppress
@@ -31,12 +30,7 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
} }
@Override @Override
public KPackage createKotlinPackage(Class javaClass) { public KDeclarationContainer getOrCreateKotlinPackage(Class javaClass, String moduleName) {
return createKotlinPackage(javaClass, JvmAbi.DEFAULT_MODULE_NAME);
}
@Override
public KPackage createKotlinPackage(Class javaClass, String moduleName) {
return new KPackageImpl(javaClass, moduleName); return new KPackageImpl(javaClass, moduleName);
} }
@@ -92,4 +86,9 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
public KClass foreignKotlinClass(Class javaClass) { public KClass foreignKotlinClass(Class javaClass) {
return getOrCreateKotlinClass(javaClass); return getOrCreateKotlinClass(javaClass);
} }
@Override
public KPackage createKotlinPackage(Class javaClass, String moduleName) {
return new KPackageImpl(javaClass, moduleName);
}
} }
@@ -19,24 +19,15 @@ package kotlin.reflect.jvm
import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.load.java.JvmAbi
import java.lang.reflect.* import java.lang.reflect.*
import java.util.* import java.util.*
import kotlin.jvm.internal.KotlinFileFacade
import kotlin.jvm.internal.Reflection
import kotlin.reflect.* import kotlin.reflect.*
import kotlin.reflect.jvm.internal.KCallableImpl import kotlin.reflect.jvm.internal.KCallableImpl
import kotlin.reflect.jvm.internal.KPackageImpl
import kotlin.reflect.jvm.internal.KPropertyImpl import kotlin.reflect.jvm.internal.KPropertyImpl
import kotlin.reflect.jvm.internal.KTypeImpl import kotlin.reflect.jvm.internal.KTypeImpl
// Kotlin reflection -> Java reflection // Kotlin reflection -> Java reflection
/**
* Returns a Java [Class] instance that represents a Kotlin package.
* The methods and fields of this class are generated from top level functions and properties in the Kotlin package.
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/java-interop.html#package-level-functions)
* for more information.
*/
public val KPackage.javaFacade: Class<*>
get() = (this as KPackageImpl).jClass
/** /**
* Returns a Java [Field] instance corresponding to the backing field of the given property, * Returns a Java [Field] instance corresponding to the backing field of the given property,
* or `null` if the property has no backing field. * or `null` if the property has no backing field.
@@ -87,31 +78,6 @@ public val KType.javaType: Type
// Java reflection -> Kotlin reflection // Java reflection -> Kotlin reflection
/**
* Returns a [KPackage] instance corresponding to the Java [Class] instance.
* The given class is generated from top level functions and properties in the Kotlin package.
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/java-interop.html#package-level-functions)
* for more information.
*/
@Deprecated("After dropping old package facades it would be impossible to retrieve package by java class")
public val Class<*>.kotlinPackage: KPackage?
get() = if (getSimpleName().endsWith("Package") &&
getAnnotation(javaClass<kotlin.jvm.internal.KotlinPackage>()) != null) {
try {
val field = this.getField(JvmAbi.MODULE_NAME_FIELD)
if (field != null) {
KPackageImpl(this, field.get(null) as String)
}
else {
null
}
}
catch(e: NoSuchFieldException) {
null
}
} else null
/** /**
* Returns a [KProperty] instance corresponding to the given Java [Field] instance, * Returns a [KProperty] instance corresponding to the given Java [Field] instance,
* or `null` if this field cannot be represented by a Kotlin property * or `null` if this field cannot be represented by a Kotlin property
@@ -137,10 +103,7 @@ public val Method.kotlinFunction: KFunction<*>?
if (isSynthetic) return null if (isSynthetic) return null
if (Modifier.isStatic(modifiers)) { if (Modifier.isStatic(modifiers)) {
val kotlinPackage = declaringClass.kotlinPackage // TODO: support file facades & multifile classes
if (kotlinPackage != null) {
return kotlinPackage.functions.firstOrNull { it.javaMethod == this }
}
// For static bridge method generated for a jvmStatic function in the companion object, also try to find the latter // For static bridge method generated for a jvmStatic function in the companion object, also try to find the latter
val companion = declaringClass.kotlin.companionObject val companion = declaringClass.kotlin.companionObject
@@ -45,12 +45,8 @@ public class Reflection {
return factory.createKotlinClass(javaClass); return factory.createKotlinClass(javaClass);
} }
public static KPackage createKotlinPackage(Class javaClass) { public static KDeclarationContainer getOrCreateKotlinPackage(Class javaClass, String moduleName) {
return factory.createKotlinPackage(javaClass); return factory.getOrCreateKotlinPackage(javaClass, moduleName);
}
public static KPackage createKotlinPackage(Class javaClass, String moduleName) {
return factory.createKotlinPackage(javaClass, moduleName);
} }
public static KClass getOrCreateKotlinClass(Class javaClass) { public static KClass getOrCreateKotlinClass(Class javaClass) {
@@ -101,6 +97,11 @@ public class Reflection {
// Deprecated // Deprecated
@Deprecated
public static KPackage createKotlinPackage(Class javaClass, String moduleName) {
return (KPackage) factory.getOrCreateKotlinPackage(javaClass, moduleName);
}
@Deprecated @Deprecated
public static KClass foreignKotlinClass(Class javaClass) { public static KClass foreignKotlinClass(Class javaClass) {
return getOrCreateKotlinClass(javaClass); return getOrCreateKotlinClass(javaClass);
@@ -23,11 +23,7 @@ public class ReflectionFactory {
return new ClassReference(javaClass); return new ClassReference(javaClass);
} }
public KPackage createKotlinPackage(Class javaClass) { public KDeclarationContainer getOrCreateKotlinPackage(Class javaClass, String moduleName) {
return null;
}
public KPackage createKotlinPackage(Class javaClass, String moduleName) {
return null; return null;
} }
@@ -73,4 +69,9 @@ public class ReflectionFactory {
public KClass foreignKotlinClass(Class javaClass) { public KClass foreignKotlinClass(Class javaClass) {
return new ClassReference(javaClass); return new ClassReference(javaClass);
} }
@Deprecated
public KPackage createKotlinPackage(Class javaClass, String moduleName) {
return null;
}
} }