kotlin

How to obtain Properties or Function declared in kotlin extensions by java reflection

为君一笑 提交于 2021-01-27 07:02:13
问题 If I declare an extension function or some Extension Properties in Kotlin like this var View.newPosition: Int get():Int { return newPosition } set(value) { this.newPosition=value Log.e("test","newPosition"+newPosition) } fun View.setPosition(value:Int ){ Log.e("test", "Position" + value) } Then I want to obtain them by Java reflection like this Class stuClass = null; try { stuClass = Class.forName("android.view.View"); } catch (Exception e) { Log.e("test", e.toString()); } Field f = null; try

What is the purpose of inner class in kotlin?

Deadly 提交于 2021-01-27 07:02:03
问题 I am new to kotlin. i am going through Nested class and inner class concepts in kotlin. Below is a example of nested class . fun main(args: Array<String>) { val demo = Outer.Nested().foo() // calling nested class method print(demo) } class Outer { class Nested { fun foo() = "Hello world" } } which gives output as Hello world here is the example of inner class. fun main(args: Array<String>) { val demo = Outer().Nested().foo() // calling inner class method print(demo) } class Outer { private

Databinding not work together with viewbinding in Kotlin

若如初见. 提交于 2021-01-27 07:01:51
问题 Android Studio 3.6 build.gradle: buildscript { ext.kotlin_version = '1.3.50' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.6.0-beta01' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" in app/build.gradle: android { viewBinding.enabled = true dataBinding { enabled = true } in my activity: import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.viewpager2.widget

What is the purpose of inner class in kotlin?

南笙酒味 提交于 2021-01-27 07:01:10
问题 I am new to kotlin. i am going through Nested class and inner class concepts in kotlin. Below is a example of nested class . fun main(args: Array<String>) { val demo = Outer.Nested().foo() // calling nested class method print(demo) } class Outer { class Nested { fun foo() = "Hello world" } } which gives output as Hello world here is the example of inner class. fun main(args: Array<String>) { val demo = Outer().Nested().foo() // calling inner class method print(demo) } class Outer { private

How to list (java) annotations on Kotlin data class fields?

前提是你 提交于 2021-01-27 06:31:37
问题 I am using Firestore's Java-based annotation for marking fields and methods for mapping document fields to Java class elements: @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.FIELD}) public @interface PropertyName { String value(); } I am using it on a field in a Kotlin data class, which compiles fine: data class MyDataClass( @PropertyName("z") val x: Int ) In IntelliJ and Android Studio, I can see it show up in the decompiled class dump: public final data class

Android - javax.crypto.AEADBadTagException

半腔热情 提交于 2021-01-27 06:27:16
问题 I'm currently getting an AEADBadTagException when trying to decrypt a file I have encrypted. I have searched pretty much everywhere on stackoverflow and unable to find a solution, and hoping there is just a small mistake I have made or something to do with encoding etc., since GCM is unable to verify the tag that it is generating. I believe the problem is somewhere in the file I am trying to encrypt/decrypt. The same exact code works on an image, however, when I try to encrypt a PDF, it fails

Retain smartcast when creating custom extension

时光毁灭记忆、已成空白 提交于 2021-01-27 06:09:17
问题 I currently have to write val myList: List<Int>? = listOf() if(!myList.isNullOrEmpty()){ // myList manipulations } Which smartcasts myList to no non null. The below do not give any smartcast: if(!myList.orEmpty().isNotEmpty()){ // Compiler thinks myList can be null here // But this is not what I want either, I want the extension fun below } if(myList.isNotEmptyExtension()){ // Compiler thinks myList can be null here } private fun <T> Collection<T>?.isNotEmptyExtension() : Boolean { return

Retain smartcast when creating custom extension

社会主义新天地 提交于 2021-01-27 06:07:11
问题 I currently have to write val myList: List<Int>? = listOf() if(!myList.isNullOrEmpty()){ // myList manipulations } Which smartcasts myList to no non null. The below do not give any smartcast: if(!myList.orEmpty().isNotEmpty()){ // Compiler thinks myList can be null here // But this is not what I want either, I want the extension fun below } if(myList.isNotEmptyExtension()){ // Compiler thinks myList can be null here } private fun <T> Collection<T>?.isNotEmptyExtension() : Boolean { return

Retain smartcast when creating custom extension

戏子无情 提交于 2021-01-27 06:07:09
问题 I currently have to write val myList: List<Int>? = listOf() if(!myList.isNullOrEmpty()){ // myList manipulations } Which smartcasts myList to no non null. The below do not give any smartcast: if(!myList.orEmpty().isNotEmpty()){ // Compiler thinks myList can be null here // But this is not what I want either, I want the extension fun below } if(myList.isNotEmptyExtension()){ // Compiler thinks myList can be null here } private fun <T> Collection<T>?.isNotEmptyExtension() : Boolean { return

Retain smartcast when creating custom extension

时光毁灭记忆、已成空白 提交于 2021-01-27 06:07:00
问题 I currently have to write val myList: List<Int>? = listOf() if(!myList.isNullOrEmpty()){ // myList manipulations } Which smartcasts myList to no non null. The below do not give any smartcast: if(!myList.orEmpty().isNotEmpty()){ // Compiler thinks myList can be null here // But this is not what I want either, I want the extension fun below } if(myList.isNotEmptyExtension()){ // Compiler thinks myList can be null here } private fun <T> Collection<T>?.isNotEmptyExtension() : Boolean { return