Commit Graph

427 Commits

Author SHA1 Message Date
Alexander Udalov 117aae8a6b Use experimental javaType in full reflect where it's not supported yet
#KT-22936 Fixed
 #KT-34344 Fixed
2020-06-17 18:33:08 +02:00
Alexander Udalov 9e37b62f62 Support KType.javaType in stdlib
#KT-32241 Fixed
2020-06-17 18:33:08 +02:00
Abduqodiri Qurbonzoda e3fb74b656 Promote KClass.cast/safeCast, KAnnotatedElement.hasAnnotation() to stable 2020-06-03 23:50:48 +03:00
Alexander Udalov 1f1790d60e Do not rely on descriptors in KParameterImpl.equals/hashCode
For the same reason as in the previous commit: descriptors are cached
via weak references in moduleByClassLoader.kt and can be
garbage-collected at any point. So different instances of KParameterImpl
representing the same parameter may store different instances of
descriptors.
2020-05-28 14:17:37 +02:00
Alexander Udalov 55f384cb04 Do not rely on descriptors in KTypeParameterImpl.equals/hashCode
Descriptors are cached via weak references in moduleByClassLoader.kt and
can be garbage-collected at any point. So relying on identity of
descriptors in KTypeParameterImpl is dangerous because the same type
parameter can be represented by different descriptors. For example, the
test equalsOnFunctionParameters.kt was flaky before this change because
of this issue, and that could be reproduced by running it a few hundred
times in the same process.

Instead, use the type parameter's container (which is either KClass or
KCallable) and name, in equals/hashCode. KClass and KCallable already
have equals/hashCode independent of descriptors, so this works in case
the descriptor is invalidated.
2020-05-28 14:17:37 +02:00
Alexander Udalov d25f9dee62 Minor, remove temporary workarounds for JPS build 2020-05-28 14:17:36 +02:00
Alexander Udalov 82551e91a4 Add KClass.isFun modifier to reflection
#KT-38881 Fixed
2020-05-14 14:05:51 +02:00
Alexander Udalov 0ce16b9d8c Support non-reified type parameters in typeOf in JVM and JVM_IR
#KT-30279 Fixed
2020-05-13 10:04:28 +02:00
Steven Schäfer 905a8ca0d0 Reflect: Handle methods with inline class default parameters 2020-04-28 19:49:35 +02:00
Alexander Udalov 683489687e Fix NPE in KCallable.isAccessible for interfaces loaded by boot class loader
Class.getClassLoader returns null for classes loaded by the system class
loader (the one used to start the application). In this case we need to
use ClassLoader.getSystemClassLoader. We already have an extension
`safeClassLoader` specifically for this purpose in reflection.jvm, but
forgot to use it in d59f2bcc80.

 #KT-37707 Fixed
2020-04-09 12:35:28 +02:00
Mathias Quintero 34a64d9171 Making var arg kParameters default to empty array no argument given
fixes KT-29969
2020-04-01 16:00:16 +02:00
Georgy Bronnikov b0d96eb140 Revert "Workaround for KT-37302"
This reverts commit ae208f58a4.
Issue fixed, workaround no longer needed.
2020-03-27 14:19:45 +03:00
Georgy Bronnikov ae208f58a4 Workaround for KT-37302 2020-03-10 10:58:36 +03:00
Abduqodiri Qurbonzoda fe50bb4b93 KProperty and ReadOnlyProperty type parameter names #KT-16529 2020-02-26 17:34:03 +03:00
Will Boyd e27b2990e3 Update message on KotlinReflectionInternalError to reflect that typealiases are also unsupported 2020-01-31 15:58:30 +01:00
Zac Sweers 482874fdc1 Embed proguard/R8 rules in kotlin-reflect artifact jar
The Android build pipeline can extract embedded proguard configurations
from dependencies and merge them automatically. This adds a conservative
proguard configuration to the kotlin-reflect JVM artifact in support of
that. This focuses mostly on just retaining what's necessary for
kotlin-reflect's own functionality to operate, but could be expanded if
community feedback discovers other good candidate rules.

With this in place - most Android projects using R8 or Proguard should
Just Work™️ with kotlin-reflect.
2020-01-17 20:03:52 +01:00
Ilmir Usmanov ca527444cb Return Unit manually in callSuspend and callSuspendBy if callable
returns Unit. The reason is the same as in the previous commit.
If the callable is tail-call and its callee returns something other that
Unit and suspends, on resume the result of the call will not be Unit.
 #KT-34703
2019-11-01 17:48:04 +03:00
Alexander Udalov e8e04ca98e Do not use new stdlib API in reflection implementation
Otherwise this brings incompatibility between kotlin-reflect 1.3.70+ and
kotlin-stdlib 1.3.60-.

This commit reverts the relevant parts of c164745301, 5c89f2fa54 and
896512f7cd.
2019-10-31 17:39:34 +01:00
Alexander Udalov 896512f7cd Support KClass.isInstance/cast/safeCast in stdlib-only reflection implementation
#KT-14720 Fixed
2019-10-29 15:52:00 +01:00
Alexander Udalov 5c89f2fa54 Support KClass.qualifiedName in stdlib-only reflection implementation
#KT-34586 Fixed
2019-10-29 15:51:25 +01:00
Alexander Udalov c164745301 Support KClass.simpleName in stdlib-only reflection implementation
#KT-33646 Fixed
2019-10-29 15:51:21 +01:00
Alexander Udalov c24f2d06d4 Mark value fields in ReflectProperties as volatile
These fields were originally non-volatile because of an incorrect
assumption I had at the time that a value was safely published if the
underlying object's class has at least one final field. This is true for
almost all values used in lazy/lazySoft: DeserializedFunctionDescriptor,
DeserializedPropertyDescriptor, KTypeImpl, java.lang.reflect.Field, etc.
But of course, this only means that the object was _safely initialized_
and not safely published via the non-volatile reference, where other
threads can still observe null, even after that constructed object was
leaked to the outer world by some other means and led to observable
changes in behavior.

This can fix some concurrency issues in kotlin-reflect. I wasn't able to
reproduce the problem in stress tests though.
2019-10-24 16:57:53 +02:00
Steven Schäfer a8730eae62 Minor: Fix error message in reflection.jvm 2019-10-18 17:20:22 +02:00
Alexander Udalov aa77f6c245 Improve KParameter.toString for instance and extension receiver parameter
#KT-17860 Fixed
2019-09-18 14:05:43 +02:00
Ilya Chernikov 689a5cbdf5 Add descriptors.runtime to the compiler...
move it into appropriate package and ensure that it gets relocated
properly in kotlin-reflect.jar
This change is needed to use the functionality that provides descriptors
from classloaders for scripts compilation.
2019-09-17 12:43:15 +02:00
Alexander Udalov d59f2bcc80 Fix KotlinReflectionInternalError on invoking callBy with defaults in supertypes
There are two parts in this change:

1) Previously, we looked up $default methods with the incorrect
   signature in supertypes. For example in defaultInSuperClass.kt, we'd
   try to find a method foo$default with the signature `(B, String,
   String, int, Object)` in the class A. Now we're modifying the array
   of parameter types on each step if we're looking for a static
   $default method, by assigning its first element to be the containing
   class. This fixes cases when defaults come from a superclass.
2) For interfaces, $default methods are actually located in the
   corresponding DefaultImpls class. Now we look up that class and
   search for the $default method there. Note that this is needed
   because of KT-33430. This fixes cases when defaults come from a
   superinterface.

 #KT-13936 Fixed
2019-08-22 18:04:00 +02:00
Alexander Udalov a6be6f4986 Use Class.forName instead of ClassLoader.loadClass in reflection
This fixes an issue in constructing annotation instances with array
class elements. For some reason, behavior of `ClassLoader.loadClass`
differs from `Class.forName` in handling arrays, namely:

* `loadClass("[Ltest.Foo;")` returns null
* `Class.forName("[Ltest.Foo;")` returns class for array of test.Foo

Overall, there doesn't seem to be any way to load an array class with
`CLassLoader.loadClass`.

We pass initialize=false to forName because this is the behavior of
ClassLoader.loadClass: it doesn't perform class initialization (e.g.
<clinit> is not executed).

 #KT-31318 Fixed
2019-07-09 18:14:45 +02:00
Alexander Udalov 4c9e9b1f3a Fix KotlinReflectionInternalError on encountering 'clone' in a class
`RuntimeTypeMapper.mapSignature` threw exception because the descriptor
for `clone` was created manually in CloneableClassScope and therefore it
didn't have a JVM signature as in deserialized descriptors, and wasn't
recognized as a Java method either.

 #KT-22923 Fixed
2019-05-31 12:29:20 +02:00
Alexander Udalov f9d61f2dc7 Make inline+reified functions synthetic instead of private in bytecode
#KT-18563 Fixed
2019-05-28 16:17:49 +02:00
Alexander Udalov 4e15b95d17 Minor, inline JvmAbi.DEFAULT_MODULE_NAME 2019-05-24 14:42:25 +02:00
nikita.movshin 65244b4bea Update copyright.
Change the copyright from "JetBrains s.r.o." to
"JetBrains s.r.o. and Kotlin Project contributors"
Update only 2 lines copyright.
2019-04-23 20:09:22 +03:00
Alexander Udalov cd6c88fa2c Do not use .kotlin_module files in reflection
Previously, we used a pretty roundabout way to load a MemberScope from a
single file facade represented by KPackageImpl, which involved going
through ModuleDescriptor, PackageFragmentProvider, PackagePartProvider
etc. The only advantage of this approach was that it sort of works
similarly as in the compiler, however mutable state in
RuntimePackagePartProvider and the fact that .kotlin_module files were
required for this to work diminished this advantage.

In this change, we load MemberScope from a KPackageImpl pretty much
directly, by using the existing method
`DeserializedDescriptorResolver.createKotlinPackagePartScope` and
caching the result in the new component PackagePartScopeCache.

 #KT-30344 Fixed
2019-03-29 13:44:23 +01:00
Alexander Udalov d8b364ea40 Mark KAnnotatedElement.hasAnnotation as experimental
#KT-29041 Fixed
2019-03-08 14:57:01 +01:00
Kerooker a342f844b8 Implements KAnnotatedElement.hasAnnotation()
This commit fixes KT-29041, by adding a convenience method to verify if a certain `KAnnotatedElement` has an annotation or not
2019-03-08 14:53:51 +01:00
Alexander Udalov d1e33534db Implement typeOf intrinsic on JVM
#KT-29915 Fixed
2019-03-05 18:16:31 +01:00
Alexander Udalov 3f1533c35b Restore support for local class literals in annotation arguments
This was broken in c1ab08c8ce where we started to represent KClassValue
as a ClassId of the referenced class + number of times it's been wrapped
into kotlin.Array. Local classes do not have a sane ClassId, so in this
change we restore the old behavior by representing KClassValue with a
sealed class value instead

 #KT-29891 Fixed
2019-02-25 19:22:09 +01:00
Alexander Udalov 67bc8d62fc Do not store JVM reflection objects by soft reference
Otherwise they might be garbage-collected before being made accessible
with `isAccessible = true` and the reflective call.

Also, compute BoxUnboxData in InlineClassAwareCaller right away, to
prevent storing a hard reference on the descriptor (all descriptors and
related data are stored by soft references in kotlin-reflect).

 #KT-27585 Fixed
2018-11-20 17:54:01 +01:00
Dmitry Petrov 3efb07bf7b Return kotlin ctor mapped to a java ctor even if java ctor is synthetic
This allows working with constructors with inline class parameters using
Kotlin reflection, as described in
https://youtrack.jetbrains.com/issue/KT-27429#focus=streamItem-27-3161148-0-0

 #KT-27913
 #KT-27429
2018-11-20 09:53:18 +03:00
Dmitry Petrov 99d8f2eb0c Support 'call' for primary value of an inline class
Getter of a primary value of an inline class belongs to the box class.
Its arguments should not be unboxed when the method is called.
However, its result might require boxing if it's an inline class value.

When we have an internal primary value, there's no getter method.
In fact, we can use box/unbox methods for inline class directly
(don't forget to box the result, it may be an inline class type value).

 #KT-26748
2018-11-14 09:57:51 +03:00
Dmitry Petrov 6d0a403ead KT-26765: Support 'call' for constructors with inline class parameters 2018-11-09 10:21:46 +03:00
Alexander Udalov c5275f178a Do not use getMethod/getConstructor in reflection
Use only getDeclaredMethod/getDeclaredConstructor instead. The reason is
that getMethod/getConstructor only finds public-API (public or protected
on JVM) declarations, and to determine if a declaration is public-API in
the class file we used isPublicInBytecode, which was trying to load
annotations on the declaration to see if it was InlineOnly, and that
required lots of time-consuming actions and worsened the stack trace (as
can be seen e.g. in KT-27878). In fact, the implementation of
Class.getMethod is not supposed to do anything complicated except
loading annotations from each superclass and superinterface of the given
class. Doing it in our codebase simplifies implementation and probably
improves performance
2018-10-31 17:21:06 +01:00
Alexander Udalov c1ab08c8ce Refactor KClassValue to store ClassLiteralValue internally
Only invariant array projections and non-null element types will be
supported soon (see KT-26568), so it makes no sense to store the
complete type in KClassValue. What we need is only the ClassId of the
class, and the number of times it's wrapped into kotlin/Array, which is
exactly what ClassLiteralValue represents.

This change helps in decoupling annotation values from
descriptors/types. The only constant value that depends on descriptors
is now AnnotationValue.

 #KT-26582 Fixed
2018-10-25 16:27:23 +02:00
Alexander Udalov f868964e25 Fix most unchecked/deprecation javac warnings in compiler modules 2018-10-17 18:39:49 +02:00
Dmitry Petrov 5032c106af Support 'call' for references to inline class constructors
#KT-26748
2018-10-17 16:38:22 +03:00
Dmitry Petrov 94e1701089 Support 'call' for references to inline class members
This includes overriding and non-overriding functions and properties.

 #KT-26748
2018-10-17 16:38:22 +03:00
Dmitry Petrov 5003388d38 Unbox bound receiver if inline class type expected 2018-10-17 16:38:22 +03:00
Alexander Udalov 9d27771f86 Fix instance parameter type of inherited declaration in reflection
Both for callables obtained via reflection API (KClass.members etc) and
for callables obtained via ::-references, the instance parameter is now
the class which was used to construct the type at the left-hand side of
the reference, NOT the class where the callable is originally declared
as is known at compile-time. The reason is to reduce the difference in
behavior of KCallable.call vs FunctionN.invoke: the latter always
required the subclass instance for a fake override, and it's reasonable
that the former would require it as well.

Note that in Java reflection, behavior could differ in a similar case.
For a simple fake override, Class.getMethod would return the method
declared in the base class and that method will accept instances of the
base class in invoke. However, it's difficult to rely on this behavior
because if there's a bridge for a fake override in the derived class
(e.g. when overridden members have different signatures), the returned
Method object is accepting the derived class as the receiver. This just
confirms the fact that Java reflection operates on a different level of
abstraction, namely JVM methods in .class files, which is not applicable
to our use cases directly. Another reason not to replicate Java
reflection's behavior is the uncertainty as to which member is returned
in case there are several in the hierarchy for a given fake override:
see the "otherwise one of the methods is chosen arbitrarily" note in
javadoc on Class.getMethod.

 #KT-24170 Fixed
2018-09-21 17:32:43 +03:00
Alexander Udalov 3a5de13dd4 Support inline classes in function signatures in call/callBy
#KT-25664 Fixed
 #KT-26748 Open
 #KT-26765 Open
2018-09-13 16:04:20 +03:00
Alexander Udalov 3e79bd2b0e Make CallerImpl with subclasses a sealed hierarchy 2018-09-13 15:54:20 +03:00
Alexander Udalov 8c8f0639f8 Remove unneeded CallerImpl implementations
Also fix a bug where nullability of the assigned value was not checked
in ClassCompanionFieldSetter because it wasn't a subclass of FieldSetter
where this check occurred
2018-09-13 15:54:20 +03:00