Code cleanup in reflection.jvm, generator scripts
This commit is contained in:
@@ -24,7 +24,7 @@ package kotlin.reflect
|
||||
*
|
||||
* @see [kotlin.reflect.jvm.isAccessible]
|
||||
*/
|
||||
public class IllegalCallableAccessException(cause: IllegalAccessException) : Exception(cause.getMessage()) {
|
||||
public class IllegalCallableAccessException(cause: IllegalAccessException) : Exception(cause.message) {
|
||||
init {
|
||||
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
|
||||
(this as java.lang.Throwable).initCause(cause)
|
||||
|
||||
@@ -27,5 +27,5 @@ import kotlin.reflect.jvm.internal.KClassImpl
|
||||
*/
|
||||
public val KClass<*>.jvmName: String
|
||||
get() {
|
||||
return (this as KClassImpl).jClass.getName()
|
||||
return (this as KClassImpl).jClass.name
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ internal abstract class FunctionCaller<out M : Member>(
|
||||
abstract fun call(args: Array<*>): Any?
|
||||
|
||||
protected open fun checkArguments(args: Array<*>) {
|
||||
if (parameterTypes.size() != args.size()) {
|
||||
throw IllegalArgumentException("Callable expects ${parameterTypes.size()} arguments, but ${args.size()} were provided.")
|
||||
if (parameterTypes.size != args.size) {
|
||||
throw IllegalArgumentException("Callable expects ${parameterTypes.size} arguments, but ${args.size} were provided.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ internal abstract class FunctionCaller<out M : Member>(
|
||||
class InstanceMethod(method: ReflectMethod) : Method(method) {
|
||||
override fun call(args: Array<*>): Any? {
|
||||
checkArguments(args)
|
||||
return callMethod(args[0], args.asList().subList(1, args.size()).toTypedArray())
|
||||
return callMethod(args[0], args.asList().subList(1, args.size).toTypedArray())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ internal abstract class FunctionCaller<out M : Member>(
|
||||
override fun call(args: Array<*>): Any? {
|
||||
checkArguments(args)
|
||||
checkObjectInstance(args.firstOrNull())
|
||||
return callMethod(null, args.asList().subList(1, args.size()).toTypedArray())
|
||||
return callMethod(null, args.asList().subList(1, args.size).toTypedArray())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ internal interface KCallableImpl<out R> : KCallable<R>, KAnnotatedElementImpl {
|
||||
// See ArgumentGenerator#generate
|
||||
override fun callBy(args: Map<KParameter, Any?>): R {
|
||||
val parameters = parameters
|
||||
val arguments = ArrayList<Any?>(parameters.size())
|
||||
val arguments = ArrayList<Any?>(parameters.size)
|
||||
var mask = 0
|
||||
val masks = ArrayList<Int>(1)
|
||||
var index = 0
|
||||
|
||||
@@ -42,7 +42,7 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
||||
val classId = classId
|
||||
|
||||
val descriptor =
|
||||
if (classId.isLocal()) moduleData.localClassResolver.resolveLocalClass(classId)
|
||||
if (classId.isLocal) moduleData.localClassResolver.resolveLocalClass(classId)
|
||||
else moduleData.module.findClassAcrossModuleDependencies(classId)
|
||||
|
||||
descriptor ?: throw KotlinReflectionInternalError("Class not resolved: $jClass")
|
||||
@@ -67,8 +67,8 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
||||
override val constructorDescriptors: Collection<ConstructorDescriptor>
|
||||
get() {
|
||||
val descriptor = descriptor
|
||||
if (descriptor.getKind() == ClassKind.CLASS || descriptor.getKind() == ClassKind.ENUM_CLASS) {
|
||||
return descriptor.getConstructors()
|
||||
if (descriptor.kind == ClassKind.CLASS || descriptor.kind == ClassKind.ENUM_CLASS) {
|
||||
return descriptor.constructors
|
||||
}
|
||||
return emptyList()
|
||||
}
|
||||
@@ -76,39 +76,39 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun getProperties(name: Name): Collection<PropertyDescriptor> =
|
||||
(memberScope.getContributedVariables(name, NoLookupLocation.FROM_REFLECTION) +
|
||||
staticScope.getContributedVariables(name, NoLookupLocation.FROM_REFLECTION)) as Collection<PropertyDescriptor>
|
||||
staticScope.getContributedVariables(name, NoLookupLocation.FROM_REFLECTION))
|
||||
|
||||
override fun getFunctions(name: Name): Collection<FunctionDescriptor> =
|
||||
memberScope.getContributedFunctions(name, NoLookupLocation.FROM_REFLECTION) +
|
||||
staticScope.getContributedFunctions(name, NoLookupLocation.FROM_REFLECTION)
|
||||
|
||||
override val simpleName: String? get() {
|
||||
if (jClass.isAnonymousClass()) return null
|
||||
if (jClass.isAnonymousClass) return null
|
||||
|
||||
val classId = classId
|
||||
return when {
|
||||
classId.isLocal() -> calculateLocalClassName(jClass)
|
||||
else -> classId.getShortClassName().asString()
|
||||
classId.isLocal -> calculateLocalClassName(jClass)
|
||||
else -> classId.shortClassName.asString()
|
||||
}
|
||||
}
|
||||
|
||||
private fun calculateLocalClassName(jClass: Class<*>): String {
|
||||
val name = jClass.getSimpleName()
|
||||
jClass.getEnclosingMethod()?.let { method ->
|
||||
return name.substringAfter(method.getName() + "$")
|
||||
val name = jClass.simpleName
|
||||
jClass.enclosingMethod?.let { method ->
|
||||
return name.substringAfter(method.name + "$")
|
||||
}
|
||||
jClass.getEnclosingConstructor()?.let { constructor ->
|
||||
return name.substringAfter(constructor.getName() + "$")
|
||||
jClass.enclosingConstructor?.let { constructor ->
|
||||
return name.substringAfter(constructor.name + "$")
|
||||
}
|
||||
return name.substringAfter('$')
|
||||
}
|
||||
|
||||
override val qualifiedName: String? get() {
|
||||
if (jClass.isAnonymousClass()) return null
|
||||
if (jClass.isAnonymousClass) return null
|
||||
|
||||
val classId = classId
|
||||
return when {
|
||||
classId.isLocal() -> null
|
||||
classId.isLocal -> null
|
||||
else -> classId.asSingleFqName().asString()
|
||||
}
|
||||
}
|
||||
@@ -165,9 +165,9 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
||||
|
||||
override fun toString(): String {
|
||||
return "class " + classId.let { classId ->
|
||||
val packageFqName = classId.getPackageFqName()
|
||||
val packagePrefix = if (packageFqName.isRoot()) "" else packageFqName.asString() + "."
|
||||
val classSuffix = classId.getRelativeClassName().asString().replace('.', '$')
|
||||
val packageFqName = classId.packageFqName
|
||||
val packagePrefix = if (packageFqName.isRoot) "" else packageFqName.asString() + "."
|
||||
val classSuffix = classId.relativeClassName.asString().replace('.', '$')
|
||||
packagePrefix + classSuffix
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,9 +56,9 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
fun getMembers(scope: MemberScope, declaredOnly: Boolean, nonExtensions: Boolean, extensions: Boolean): Sequence<KCallable<*>> {
|
||||
val visitor = object : DeclarationDescriptorVisitorEmptyBodies<KCallable<*>?, Unit>() {
|
||||
private fun skipCallable(descriptor: CallableMemberDescriptor): Boolean {
|
||||
if (declaredOnly && !descriptor.getKind().isReal()) return true
|
||||
if (declaredOnly && !descriptor.kind.isReal) return true
|
||||
|
||||
val isExtension = descriptor.getExtensionReceiverParameter() != null
|
||||
val isExtension = descriptor.extensionReceiverParameter != null
|
||||
if (isExtension && !extensions) return true
|
||||
if (!isExtension && !nonExtensions) return true
|
||||
|
||||
@@ -80,7 +80,7 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
|
||||
return scope.getContributedDescriptors().asSequence()
|
||||
.filter { descriptor ->
|
||||
descriptor !is MemberDescriptor || descriptor.getVisibility() != Visibilities.INVISIBLE_FAKE
|
||||
descriptor !is MemberDescriptor || descriptor.visibility != Visibilities.INVISIBLE_FAKE
|
||||
}
|
||||
.map { descriptor ->
|
||||
descriptor.accept(visitor, Unit)
|
||||
@@ -115,11 +115,11 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
RuntimeTypeMapper.mapPropertySignature(descriptor).asString() == signature
|
||||
}
|
||||
|
||||
if (properties.size() != 1) {
|
||||
if (properties.size != 1) {
|
||||
val debugText = "'$name' (JVM signature: $signature)"
|
||||
throw KotlinReflectionInternalError(
|
||||
if (properties.isEmpty()) "Property $debugText not resolved in $this"
|
||||
else "${properties.size()} properties $debugText resolved in $this: $properties"
|
||||
else "${properties.size} properties $debugText resolved in $this: $properties"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -132,11 +132,11 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
RuntimeTypeMapper.mapSignature(descriptor).asString() == signature
|
||||
}
|
||||
|
||||
if (functions.size() != 1) {
|
||||
if (functions.size != 1) {
|
||||
val debugText = "'$name' (JVM signature: $signature)"
|
||||
throw KotlinReflectionInternalError(
|
||||
if (functions.isEmpty()) "Function $debugText not resolved in $this"
|
||||
else "${functions.size()} functions $debugText resolved in $this: $functions"
|
||||
else "${functions.size} functions $debugText resolved in $this: $functions"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
private fun addParametersAndMasks(result: MutableList<Class<*>>, desc: String) {
|
||||
val valueParameters = loadParameterTypes(desc)
|
||||
result.addAll(valueParameters)
|
||||
repeat((valueParameters.size() + Integer.SIZE - 1) / Integer.SIZE) {
|
||||
repeat((valueParameters.size + Integer.SIZE - 1) / Integer.SIZE) {
|
||||
result.add(Integer.TYPE)
|
||||
}
|
||||
}
|
||||
@@ -251,7 +251,7 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
): Field? {
|
||||
val owner = implClassForCallable(nameResolver, proto) ?:
|
||||
if (proto.hasExtension(JvmProtoBuf.propertySignature) &&
|
||||
proto.getExtension(JvmProtoBuf.propertySignature).let { it.hasField() && it.field.getIsStaticInOuter() }) {
|
||||
proto.getExtension(JvmProtoBuf.propertySignature).let { it.hasField() && it.field.isStaticInOuter }) {
|
||||
jClass.enclosingClass ?: throw KotlinReflectionInternalError("Inconsistent metadata for field $name in $jClass")
|
||||
} else jClass
|
||||
|
||||
|
||||
@@ -27,11 +27,11 @@ import kotlin.reflect.KotlinReflectionInternalError
|
||||
internal class KFunctionFromReferenceImpl(
|
||||
val reference: FunctionReference
|
||||
): KFunctionImpl(
|
||||
reference.getOwner() as? KDeclarationContainerImpl ?: EmptyContainerForLocal,
|
||||
reference.owner as? KDeclarationContainerImpl ?: EmptyContainerForLocal,
|
||||
reference.name,
|
||||
reference.getSignature()
|
||||
reference.signature
|
||||
) {
|
||||
override fun getArity() = reference.getArity()
|
||||
override fun getArity() = reference.arity
|
||||
|
||||
override val name = reference.name
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ internal open class KFunctionImpl protected constructor(
|
||||
}
|
||||
|
||||
override fun getArity(): Int {
|
||||
return descriptor.valueParameters.size() +
|
||||
return descriptor.valueParameters.size +
|
||||
(if (descriptor.dispatchReceiverParameter != null) 1 else 0) +
|
||||
(if (descriptor.extensionReceiverParameter != null) 1 else 0)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@file:Suppress("DEPRECATION")
|
||||
package kotlin.reflect.jvm.internal
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
@@ -45,7 +46,7 @@ internal class KPackageImpl(override val jClass: Class<*>, val moduleName: Strin
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun getProperties(name: Name): Collection<PropertyDescriptor> =
|
||||
scope.getContributedVariables(name, NoLookupLocation.FROM_REFLECTION) as Collection<PropertyDescriptor>
|
||||
scope.getContributedVariables(name, NoLookupLocation.FROM_REFLECTION)
|
||||
|
||||
override fun getFunctions(name: Name): Collection<FunctionDescriptor> =
|
||||
scope.getContributedFunctions(name, NoLookupLocation.FROM_REFLECTION)
|
||||
@@ -57,8 +58,8 @@ internal class KPackageImpl(override val jClass: Class<*>, val moduleName: Strin
|
||||
jClass.hashCode()
|
||||
|
||||
override fun toString(): String {
|
||||
val name = jClass.getName()
|
||||
return "package " + if (jClass.isAnnotationPresent(javaClass<KotlinPackage>())) {
|
||||
val name = jClass.name
|
||||
return "package " + if (jClass.isAnnotationPresent(KotlinPackage::class.java)) {
|
||||
if (name == "_DefaultPackage") "<default>" else name.substringBeforeLast(".")
|
||||
}
|
||||
else name
|
||||
|
||||
@@ -27,8 +27,8 @@ import java.lang.ref.WeakReference;
|
||||
public static abstract class Val<T> {
|
||||
private static final Object NULL_VALUE = new Object() {};
|
||||
|
||||
@SuppressWarnings("UnusedParameters")
|
||||
public T get(Object instance, Object metadata) {
|
||||
@SuppressWarnings({"UnusedParameters", "unused"})
|
||||
public final T getValue(Object instance, Object metadata) {
|
||||
return invoke();
|
||||
}
|
||||
|
||||
|
||||
@@ -58,17 +58,17 @@ internal object ReflectionObjectRenderer {
|
||||
|
||||
// TODO: include visibility
|
||||
fun renderProperty(descriptor: PropertyDescriptor): String {
|
||||
return StringBuilder {
|
||||
return buildString {
|
||||
append(if (descriptor.isVar) "var " else "val ")
|
||||
appendReceiversAndName(descriptor)
|
||||
|
||||
append(": ")
|
||||
append(renderType(descriptor.type))
|
||||
}.toString()
|
||||
}
|
||||
}
|
||||
|
||||
fun renderFunction(descriptor: FunctionDescriptor): String {
|
||||
return StringBuilder {
|
||||
return buildString {
|
||||
append("fun ")
|
||||
appendReceiversAndName(descriptor)
|
||||
|
||||
@@ -78,11 +78,11 @@ internal object ReflectionObjectRenderer {
|
||||
|
||||
append(": ")
|
||||
append(renderType(descriptor.returnType!!))
|
||||
}.toString()
|
||||
}
|
||||
}
|
||||
|
||||
fun renderParameter(parameter: KParameterImpl): String {
|
||||
return StringBuilder {
|
||||
return buildString {
|
||||
when (parameter.kind) {
|
||||
KParameter.Kind.EXTENSION_RECEIVER -> append("extension receiver")
|
||||
KParameter.Kind.INSTANCE -> append("instance")
|
||||
@@ -91,7 +91,7 @@ internal object ReflectionObjectRenderer {
|
||||
|
||||
append(" of ")
|
||||
append(renderCallable(parameter.callable.descriptor))
|
||||
}.toString()
|
||||
}
|
||||
}
|
||||
|
||||
fun renderType(type: KotlinType): String {
|
||||
|
||||
@@ -209,7 +209,7 @@ internal object RuntimeTypeMapper {
|
||||
val parameters = function.valueParameters
|
||||
|
||||
when (function.name.asString()) {
|
||||
"equals" -> if (parameters.size() == 1 && KotlinBuiltIns.isNullableAny(parameters.single().type)) {
|
||||
"equals" -> if (parameters.size == 1 && KotlinBuiltIns.isNullableAny(parameters.single().type)) {
|
||||
return JvmFunctionSignature.BuiltInFunction.Predefined("equals(Ljava/lang/Object;)Z",
|
||||
Any::class.java.getDeclaredMethod("equals", Any::class.java))
|
||||
}
|
||||
@@ -240,7 +240,7 @@ internal object RuntimeTypeMapper {
|
||||
}
|
||||
|
||||
val classId = klass.classId
|
||||
if (!classId.isLocal()) {
|
||||
if (!classId.isLocal) {
|
||||
JavaToKotlinClassMap.INSTANCE.mapJavaToKotlin(classId.asSingleFqName())?.let { return it.classId }
|
||||
}
|
||||
|
||||
@@ -248,5 +248,5 @@ internal object RuntimeTypeMapper {
|
||||
}
|
||||
|
||||
private val Class<*>.primitiveType: PrimitiveType?
|
||||
get() = if (isPrimitive()) JvmPrimitiveType.get(getSimpleName()).getPrimitiveType() else null
|
||||
get() = if (isPrimitive) JvmPrimitiveType.get(simpleName).primitiveType else null
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package kotlin.reflect.jvm.internal
|
||||
|
||||
import java.lang.ref.WeakReference
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.jvm.internal.pcollections.HashPMap
|
||||
|
||||
// TODO: collect nulls periodically
|
||||
@@ -29,7 +28,7 @@ private var K_CLASS_CACHE = HashPMap.empty<String, Any>()
|
||||
|
||||
// This function is invoked on each reflection access to Java classes, properties, etc. Performance is critical here.
|
||||
internal fun <T : Any> getOrCreateKotlinClass(jClass: Class<T>): KClassImpl<T> {
|
||||
val name = jClass.getName()
|
||||
val name = jClass.name
|
||||
val cached = K_CLASS_CACHE[name]
|
||||
if (cached is WeakReference<*>) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@@ -40,7 +39,8 @@ internal fun <T : Any> getOrCreateKotlinClass(jClass: Class<T>): KClassImpl<T> {
|
||||
}
|
||||
else if (cached != null) {
|
||||
// If the cached value is not a weak reference, it's an array of weak references
|
||||
cached as Array<WeakReference<KClassImpl<T>>>
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(cached as Array<WeakReference<KClassImpl<T>>>)
|
||||
for (ref in cached) {
|
||||
val kClass = ref.get()
|
||||
if (kClass?.jClass == jClass) {
|
||||
@@ -50,7 +50,7 @@ internal fun <T : Any> getOrCreateKotlinClass(jClass: Class<T>): KClassImpl<T> {
|
||||
|
||||
// This is the most unlikely case: we found a cached array of references of length at least 2 (can't be 1 because
|
||||
// the single element would be cached instead), and none of those classes is the one we're looking for
|
||||
val size = cached.size()
|
||||
val size = cached.size
|
||||
// Don't use Array constructor because it creates a lambda
|
||||
val newArray = arrayOfNulls<WeakReference<KClassImpl<*>>>(size + 1)
|
||||
// Don't use Arrays.copyOf because it works reflectively
|
||||
|
||||
@@ -21,7 +21,7 @@ fun main(args: Array<String>) {
|
||||
val INCLUDE_END = ".java</include>"
|
||||
val POM_PATH = "META-INF/maven/com.google.protobuf/protobuf-java/pom.xml"
|
||||
|
||||
if (args.size() != 2) {
|
||||
if (args.size != 2) {
|
||||
error("Usage: kotlinc -script build-protobuf-lite.kts <path-to-protobuf-jar> <output-path>")
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ fun loadVersion(library: File): IntArray {
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (args.size() != 2) {
|
||||
if (args.size != 2) {
|
||||
error("Usage: kotlinc -script check-library-abi-version.kts <jar-1> <jar-2>")
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.io.File
|
||||
fun main(args: Array<String>) {
|
||||
val filePathDefault = "updated-version.txt"
|
||||
|
||||
if (args.isEmpty() || args.size() > 2) {
|
||||
if (args.isEmpty() || args.size > 2) {
|
||||
error("Usage: kotlinc -script increment-version.kts " +
|
||||
"<version> " +
|
||||
"<file-path='$filePathDefault'>")
|
||||
@@ -31,7 +31,7 @@ fun main(args: Array<String>) {
|
||||
var versionStr = args[0]
|
||||
|
||||
val incrementPartStr = versionStr.takeLastWhile { it.isDigit() }
|
||||
val versionPrefix = versionStr.take(versionStr.length() - incrementPartStr.length())
|
||||
val versionPrefix = versionStr.take(versionStr.length - incrementPartStr.length)
|
||||
val incrementPart = incrementPartStr.toInt()
|
||||
|
||||
var filePath = args.getOrNull(1) ?: filePathDefault
|
||||
@@ -43,4 +43,4 @@ fun main(args: Array<String>) {
|
||||
File("$filePath").writeText("$result")
|
||||
}
|
||||
|
||||
main(args)
|
||||
main(args)
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.util.zip.ZipOutputStream
|
||||
*/
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (args.size() != 4) {
|
||||
if (args.size != 4) {
|
||||
error("Usage: kotlinc -script strip-kotlin-annotations.kts <annotation-internal-name-regex> <class-internal-name-regex> <path-to-in-jar> <path-to-out-jar>")
|
||||
}
|
||||
|
||||
@@ -70,8 +70,8 @@ fun main(args: Array<String>) {
|
||||
val inBytes = inJar.getInputStream(entry).readBytes()
|
||||
val outBytes = transform(entry.getName(), inBytes)
|
||||
|
||||
if (inBytes.size() < outBytes.size()) {
|
||||
error("Size increased for ${entry.getName()}: was ${inBytes.size()} bytes, became ${outBytes.size()} bytes")
|
||||
if (inBytes.size < outBytes.size) {
|
||||
error("Size increased for ${entry.getName()}: was ${inBytes.size} bytes, became ${outBytes.size} bytes")
|
||||
}
|
||||
|
||||
entry.setCompressedSize(-1L)
|
||||
@@ -90,4 +90,4 @@ fun main(args: Array<String>) {
|
||||
println("Output file size: ${outFile.length()} bytes")
|
||||
}
|
||||
|
||||
main(args)
|
||||
main(args)
|
||||
|
||||
Reference in New Issue
Block a user