Kotlin可空类型
可空类型的实现原理
在字节码层面上可空类型没有做文章,String?和String其实是同一个类型
可空类型不同于java的普通类型有两点:
一是有编译器的限定,可空类型不能直接访问成员变量和成员方法(可空类型的访问要了用?.安全访问,要么使用!!强制访问)
二是可空类型的?.和!!以及?:会生成额外的字节码
可空类型的定义123val nullable1: String? = ""val nullable2: String? = nullval notNull: String = ""
对比字节码
nullable112LDC "" #将常量池的内容压入操作数栈ASTORE 0 #将操作数栈的栈顶元素弹入局部变量表index为0的位置
nullable212ACONST_NULL #将null压入操作数栈ASTORE 1 #将操作数栈的栈顶元素弹入局部变量表index为0的位置
notNull12LDC "" #将常量池内元素""压入操作数栈ASTOR ...
Kotlin尾递归
尾递归优化
尾递归就是虚伪的递归,他是一种类似于循环的执行流程
递归字节码123456var a = 0fun a() { println(a++) a()}
字节码如下
12345678910111213141516171819202122public final static a()V L0 LINENUMBER 15 L0 GETSTATIC TailrecTestKt.a : I DUP ISTORE 0 ICONST_1 IADD PUTSTATIC TailrecTestKt.a : I ILOAD 0 ISTORE 0 L1 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ILOAD 0 INVOKEVIRTUAL java/io/PrintStream.println (I)V L2 L3 LINENUMBER 16 L3 INVOKESTATIC TailrecTestKt ...
ViewModel
viewmodel
ViewModel作为Android的元老级别的内容,你不知道就out了
ViewModel的实现主要就是能在activity配置发生变化的时候还能保存值。
保存策略突破点
viewModel的获取
1val vm = ViewModelProvider(this).get<LiveDataEventVM>()
总是有这样一段代码
12345678public constructor( owner: ViewModelStoreOwner) : this(owner.viewModelStore, defaultFactory(owner), defaultCreationExtras(owner))public constructor(owner: ViewModelStoreOwner, factory: Factory) : this( owner.viewModelStore, factory, defaultCreationExtras(owner)
获取viewmodel的Store owner
123456 ...
Lifecycle
LifecycleLifecycle不是什么奇怪的的东西,就是一个Lifecycle-aware components
lifecycle中有三个东西,viewmodel,livedata,lifecycle,本文着重分析lifecycle。
即
1androidx.lifecycle:lifecycle-runtime
具体分析
项目依赖结构
由于库的代码不多,就先直接从库的源码开始。
少的可怜的runtime
一共就4个类
LifecycleRegistry
An implementation of Lifecycle that can handle multiple observers.It is used by Fragments and Support Library Activities. You can also directly use it if you have a custom LifecycleOwner.
Lifecycle的实现类,用于处理监听者。
它主要是用于Fragment和一些Activity,我们也可以直接使用它,如果我们有自 ...
LiveData
LiveDataLiveData是什么?live-data即有生命的数据。就实现上来说就是lifecycle+data。给data包了一层lifecycle这样减少了在不可见生命周期使用数据的不安全行为。
内容lifecycle-livedata也就3个类
ComputableLiveData
A LiveData class that can be invalidated & computed when there are active observers.It can be invalidated via invalidate(), which will result in a call to compute() if there are active observers (or when they start observing)This is an internal class for now, might be public if we see the necessity.
一个仅有到生命周期处于可见状态的时候才会回调invalidate。
这是一个内部的cla ...
Retrofit分析
Retrofit基本使用12345678910111213141516171819interface Api { @GET("friend/json") fun test():Call<Bean>}object ApiService { private val retrofit: Retrofit = Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl("https://www.wanandroid.com") .client(OkHttpClient()) .validateEagerly(false) .build() val api = retrofit.create<Api&g ...
Kotlin Collection
Collection
Time: 2022-1-31
首先得知道Collection是kotlin-stdlib里面的东西。
TypeKotlin的集合框架有如下的类型
List
List is an ordered collection with access to elements by indices – integer numbers that reflect their position. Elements can occur more than once in a list. An example of a list is a telephone number: it’s a group of digits, their order is important, and they can repeat.
List是一个有序的几口,通过一个下标来索引与那苏,(一个integer来反映元素的位置)
元素在List内可以重复出现
简单的例子如电话号码,他是数字的一个List,可以重复。
Set
Set is a collection of unique elemen ...
Kotlin协程
关于本篇文章是记录的在协程源码分析的内容合集
Suspend function 1 ——Source
Time :2022-1-14——利用Kotlin ByteCode插件对挂起函数进行分析。(这个插件不是很好用。)
Time:2022-1-24——使用Jadx对编译后打包的jar包进行分析
概述
挂起函数初探
挂起函数是什么?
挂起函数能干什么?
那些是挂起函数?
挂起函数怎么实现的?
挂起函数是什么挂起函数本质还是函数,只不过这个函数在经过Kotlin编译器以后会进行一些特殊的处理。以达到用同步的代码写出异步的操作逻辑。
挂起函数能干什么就简单而言,挂起函数具有以下两种功能。
——挂起,恢复。
由于这两种功能使得挂起函数具备了——使用同步的写法,达到了异步的逻辑。
哪些是挂起函数我觉得标题改为 怎么定义,使用挂起函数。会更加贴合实际。hh
关于什么是挂起函数很简单。
这是挂起函数的定义,和普通函数的定义其实是一致的。
123suspend fun suspendFunc1(){}
除此之外还有一种奇怪的定义方式。(其实也不是很奇怪啦,上面是挂起函数的‘常 ...
Kotlin委托
委托Kotlin对于委托是开箱支持的,委托是一种消除样板代码的方式
The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
接口委托一个类可以实现一个接口,然后把这个接口的公开方法委托给另外一个类。
words is cheap ,give me the code
12345678910111213141516171819interface Base { fun print()}class BaseDelegate: Base { override fun print() { println("我是Base的Delegate我现在非常的后悔,为啥我要给BaseImp背锅") }}class BaseImp(delegate ...
Kotlin契约
ContractContract的中文叫做协议,协约,合同。
这个合同呢是和编译器签订的。
kotlin的编译器比较聪明但是不是太聪明。
所以有的时候会犯傻hh。
引入Contract比如这同样。
ensureNotNullAndEmpty已经确定了str不为空,也不为””
但是使用的时候编译器还是认为这个东西可能是空的。
如果这个时候我们想告诉编译器这个东西不是空的,那该怎么弄。
这就得使用contract了
很简单就修改一下代码
123456789@OptIn(ExperimentalContracts::class)fun String?.ensureNotNullAndEmpty(): Boolean { contract { returns(true) implies (this@ensureNotNullAndEmpty != null) } return this != null && !isEmpty()}
这样就不报错了
Contract使用Contract内部的内容比较少
...