Kotlin语法糖去糖(Desugaring)过程深度解析(70)

Kotlin语法糖去糖(Desugaring)过程深度解析

一、语法糖概述与去糖原理

1.1 语法糖的定义与作用

Kotlin语法糖是指那些在语言层面提供的、能够简化代码编写的语法特性。这些特性并不增加语言的表达能力,但可以显著提高代码的可读性和开发效率。例如:

  1. 属性访问val name: String 简化了字段和访问器方法的定义
  2. Lambda表达式{ x, y -> x + y } 替代了繁琐的匿名内部类
  3. 数据类data class User(val name: String, val age: Int) 自动生成equals、hashCode等方法
  4. 扩展函数String.lastChar() 允许为现有类添加新方法
  5. 空安全操作符?.!! 简化了空值检查逻辑

1.2 去糖(Desugaring)的基本概念

去糖是指将语法糖转换为等价的底层代码的过程。在Kotlin中,这一过程发生在编译阶段,编译器将高级语法转换为JVM、JS或Native平台能够理解的底层表示。例如:

  1. Lambda表达式被转换为Function接口的实现类
  2. 数据类被转换为普通类,并添加自动生成的方法
  3. 扩展函数被转换为静态方法
  4. 协程被转换为状态机实现

1.3 去糖过程的重要性

理解去糖过程对Kotlin开发者有以下重要意义:

  1. 调试与性能优化:了解底层实现有助于定位问题和优化代码
  2. 与Java互操作性:理解去糖后的代码结构,更好地与Java代码交互
  3. 语言特性深入理解:掌握语法糖的本质,更深入地理解Kotlin语言
  4. 避免常见陷阱:了解语法糖的局限性,避免写出有潜在问题的代码

二、属性与访问器的去糖过程

2.1 简单属性的去糖

Kotlin的属性定义:

class Person {
    var name: String = "John"  // 可变属性
    val age: Int = 30         // 只读属性
}

去糖后的等价Java代码:

public final class Person {
    private String name = "John";  // 私有字段
    private final int age = 30;    // 私有final字段
    
    // 自动生成的name getter
    public final String getName() {
        return this.name;
    }
    
    // 自动生成的name setter
    public final void setName(String var1) {
        this.name = var1;
    }
    
    // 自动生成的age getter
    public final int getAge() {
        return this.age;
    }
}

2.2 自定义访问器的去糖

带有自定义访问器的属性:

class Rectangle(val width: Int, val height: Int) {
    val area: Int
        get() = width * height  // 自定义getter
}

去糖后的Java代码:

public final class Rectangle {
    private final int width;
    private final int height;
    
    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }
    
    // 自定义getter方法
    public final int getArea() {
        return this.width * this.height;
    }
    
    // width和height的getter方法
    public final int getWidth() {
        return this.width;
    }
    
    public final int getHeight() {
        return this.height;
    }
}

2.3 幕后字段(Backing Field)的去糖

使用field关键字的属性:

class User {
    var name: String = "John"
        set(value) {
            field = value.trim()  // 使用幕后字段
        }
}

去糖后的Java代码:

public final class User {
    private String name = "John";  // 幕后字段
    
    // 自定义setter方法
    public final void setName(String value) {
        this.name = value.trim();
    }
    
    // 自动生成的getter方法
    public final String getName() {
        return this.name;
    }
}

2.4 编译时常量(const val)的去糖

编译时常量:

class Config {
    companion object {
        const val MAX_SIZE = 100  // 编译时常量
    }
}

去糖后的Java代码:

public final class Config {
    public static final int MAX_SIZE = 100;  // 直接作为public static final字段
    
    public static final class Companion {}
}

三、Lambda表达式的去糖过程

3.1 简单Lambda表达式的去糖

简单的Lambda表达式:

val sum = { a: Int, b: Int -> a + b }

去糖后的Java代码:

import kotlin.jvm.functions.Function2;

public final class LambdaExample {
    // 实现Function2接口的匿名内部类
    private static final Function2<Integer, Integer, Integer> sum = 
        new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer invoke(Integer a, Integer b) {
                return a + b;
            }
        };
}

3.2 带接收者的Lambda(扩展Lambda)的去糖

带接收者的Lambda:

val greet: String.() -> String = { "Hello, $this!" }

去糖后的Java代码:

import kotlin.jvm.functions.Function1;

public final class LambdaWithReceiverExample {
    // 实现Function1接口的匿名内部类,第一个参数为接收者类型
    private static final Function1<String, String> greet = 
        new Function1<String, String>() {
            @Override
            public String invoke(String $receiver) {
                return "Hello, " + $receiver + "!";
            }
        };
}

3.3 Lambda捕获变量的去糖

捕获外部变量的Lambda:

fun counter(): () -> Int {
    var count = 0
    return { count++ }
}

去糖后的Java代码:

import kotlin.jvm.functions.Function0;

public final class ClosureExample {
    // 闭包类,保存捕获的变量
    private static final class Ref {
        public int count;
    }
    
    public static final Function0<Integer> counter() {
        final Ref ref = new Ref();  // 创建闭包实例
        ref.count = 0;
        
        // 返回实现Function0接口的匿名内部类
        return new Function0<Integer>() {
            @Override
            public Integer invoke() {
                return ref.count++;  // 访问闭包中的变量
            }
        };
    }
}

3.4 内联Lambda的去糖

内联函数中的Lambda:

inline fun runTwice(block: () -> Unit) {
    block()
    block()
}

// 调用
runTwice { println("Hello") }

去糖后的Java代码:

public final class InlineLambdaExample {
    // 内联函数被复制到调用处
    public static final void runTwice$lambda$1() {
        System.out.println("Hello");
    }
    
    public static final void main(String[] args) {
        // Lambda体被直接复制到调用处
        runTwice$lambda$1();
        runTwice$lambda$1();
    }
    
    // 原始内联函数可能仍然存在,但不会被直接调用
    public static final void runTwice(Function0<Unit> block) {
        block.invoke();
        block.invoke();
    }
}

四、数据类的去糖过程

4.1 基本数据类的去糖

简单的数据类:

data class User(val name: String, val age: Int)

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class User {
    private final String name;
    private final int age;
    
    // 构造函数
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // getter方法
    public final String getName() {
        return this.name;
    }
    
    public final int getAge() {
        return this.age;
    }
    
    // equals方法
    public boolean equals(Object var1) {
        if (this != var1) {
            if (var1 instanceof User) {
                User var2 = (User)var1;
                if (Intrinsics.areEqual(this.name, var2.name) && this.age == var2.age) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }
    
    // hashCode方法
    public int hashCode() {
        String var10000 = this.name;
        return (var10000 != null ? var10000.hashCode() : 0) * 31 + this.age;
    }
    
    // toString方法
    public String toString() {
        return "User(name=" + this.name + ", age=" + this.age + ")";
    }
    
    // copy方法
    public final User copy(String name, int age) {
        return new User(name, age);
    }
    
    // componentN方法
    public final String component1() {
        return this.name;
    }
    
    public final int component2() {
        return this.age;
    }
}

4.2 带默认参数的数据类的去糖

带默认参数的数据类:

data class Config(val host: String = "localhost", val port: Int = 8080)

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class Config {
    private final String host;
    private final int port;
    
    public static final Companion Companion = new Companion((DefaultConstructorMarker)null);
    
    // 主构造函数
    public Config(String host, int port) {
        this.host = host;
        this.port = port;
    }
    
    // 带默认参数的构造函数
    public Config() {
        this((String)null, 8080, 1, (DefaultConstructorMarker)null);
    }
    
    // 带默认参数的构造函数
    public Config(String host) {
        this(host, 8080, 2, (DefaultConstructorMarker)null);
    }
    
    // 私有构造函数,用于处理默认参数
    private Config(String host, int port, int mask, DefaultConstructorMarker $constructor_marker) {
        if ((mask & 1) != 0) {
            host = "localhost";
        }
        if ((mask & 2) != 0) {
            port = 8080;
        }
        this.host = host;
        this.port = port;
    }
    
    // 其他生成的方法(getter、equals、hashCode等)与基本数据类类似
}

4.3 数据类继承的去糖

继承自其他类的数据类:

open class Person(val name: String)
data class Employee(
    val id: Int,
    name: String
) : Person(name)

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public class Person {
    private final String name;
    
    public Person(String name) {
        this.name = name;
    }
    
    public final String getName() {
        return this.name;
    }
}

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class Employee extends Person {
    private final int id;
    
    // 构造函数
    public Employee(int id, String name) {
        super(name);
        this.id = id;
    }
    
    // getter方法
    public final int getId() {
        return this.id;
    }
    
    // equals方法(只比较id,因为name来自父类)
    public boolean equals(Object var1) {
        if (this != var1) {
            if (var1 instanceof Employee) {
                Employee var2 = (Employee)var1;
                if (this.getId() == var2.getId()) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }
    
    // 其他生成的方法(hashCode、toString、copy、componentN)
}

五、扩展函数与属性的去糖过程

5.1 扩展函数的去糖

字符串扩展函数:

fun String.lastChar(): Char = this[this.length - 1]

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class StringExtKt {
    // 静态方法,第一个参数为接收者类型
    public static final char lastChar(String $this$lastChar) {
        Intrinsics.checkNotNullParameter($this$lastChar, "$this$lastChar");
        return $this$lastChar.charAt($this$lastChar.length() - 1);
    }
}

5.2 扩展属性的去糖

字符串扩展属性:

val String.lastChar: Char
    get() = this[this.length - 1]

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class StringExtKt {
    // 静态getter方法,第一个参数为接收者类型
    public static final char getLastChar(String $this$lastChar) {
        Intrinsics.checkNotNullParameter($this$lastChar, "$this$lastChar");
        return $this$lastChar.charAt($this$lastChar.length() - 1);
    }
}

5.3 泛型扩展函数的去糖

泛型扩展函数:

fun <T> List<T>.secondOrNull(): T? = if (size >= 2) this[1] else null

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.Nullable;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ListExtKt {
    // 静态泛型方法,第一个参数为接收者类型
    @Nullable
    public static final <T> T secondOrNull(List<T> $this$secondOrNull) {
        Intrinsics.checkNotNullParameter($this$secondOrNull, "$this$secondOrNull");
        return $this$secondOrNull.size() >= 2 ? $this$secondOrNull.get(1) : null;
    }
}

5.4 伴生对象扩展的去糖

伴生对象扩展:

class MyClass {
    companion object {}
}

fun MyClass.Companion.create(): MyClass = MyClass()

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class MyClass {
    public static final Companion Companion = new Companion((DefaultConstructorMarker)null);
    
    @Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
    public static final class Companion {
        private Companion() {}
        
        public Companion(DefaultConstructorMarker $constructor_marker) {
            this();
        }
    }
}

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class MyClassKt {
    // 静态方法,第一个参数为伴生对象类型
    public static final MyClass create(MyClass.Companion $this$create) {
        Intrinsics.checkNotNullParameter($this$create, "$this$create");
        return new MyClass();
    }
}

六、空安全操作符的去糖过程

6.1 安全调用操作符(?.)的去糖

安全调用操作符:

val length: Int? = str?.length

去糖后的Java代码:

import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.Nullable;

public final class NullSafetyExample {
    @Nullable
    public static final Integer length(@Nullable String str) {
        // 空值检查
        return str != null ? str.length() : null;
    }
}

6.2 非空断言操作符(!!)的去糖

非空断言操作符:

val length: Int = str!!.length

去糖后的Java代码:

import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class NullSafetyExample {
    public static final int length(@Nullable String str) {
        // 空值检查,如果为空则抛出异常
        if (str == null) {
            Intrinsics.throwNpe();
        }
        return str.length();
    }
}

6.3 Elvis操作符(?:)的去糖

Elvis操作符:

val name: String = str ?: "default"

去糖后的Java代码:

import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class NullSafetyExample {
    @NotNull
    public static final String name(@Nullable String str) {
        // 空值检查,如果为空则返回默认值
        String var1;
        if (str != null) {
            var1 = str;
        } else {
            var1 = "default";
        }
        return var1;
    }
}

6.4 安全类型转换(as?)的去糖

安全类型转换:

val num: Int? = obj as? Int

去糖后的Java代码:

import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.Nullable;

public final class NullSafetyExample {
    @Nullable
    public static final Integer num(@Nullable Object obj) {
        // 类型检查,如果不是目标类型则返回null
        return obj instanceof Integer ? (Integer)obj : null;
    }
}

七、协程相关语法糖的去糖过程

7.1 suspend函数的去糖

suspend函数:

suspend fun fetchData(): String {
    delay(1000)
    return "Data"
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;
import kotlin.jvm.functions.Function2;
import kotlinx.coroutines.DelayKt;
import kotlinx.coroutines.ScopeCoroutine;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class CoroutineExampleKt {
    // 转换为带Continuation参数的函数
    @Nullable
    public static final Object fetchData(@NotNull Continuation<? super String> continuation) {
        // 创建状态机对象
        Object result = IntrinsicsKt.getCOROUTINE_SUSPENDED();
        switch (((SuspendLambda)continuation).label) {
            case 0:
                // 初始化状态机
                ((SuspendLambda)continuation).label = 1;
                // 调用delay函数
                if (DelayKt.delay(1000L, continuation) == result) {
                    return result;
                }
                break;
            case 1:
                // 恢复执行,检查异常
                Throwable exception = ((SuspendLambda)continuation).exception;
                if (exception != null) {
                    throw exception;
                }
                break;
            default:
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
        // 返回结果
        return "Data";
    }
}

7.2 协程构建器的去糖

launch协程构建器:

launch {
    delay(1000)
    println("Done")
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;
import kotlin.jvm.functions.Function2;
import kotlinx.coroutines.DelayKt;
import kotlinx.coroutines.Job;
import kotlinx.coroutines.ScopeCoroutine;
import kotlinx.coroutines.launchKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class CoroutineExampleKt {
    public static final void launchExample(@NotNull CoroutineScope scope) {
        // 创建并启动协程
        launchKt.launch(scope, (CoroutineContext)null, (Function2)null, new ExampleCoroutine(scope, (Continuation)null));
    }
    
    @Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 3, d1 = {"..."})
    static final class ExampleCoroutine extends SuspendLambda implements Function2<CoroutineScope, Continuation<? super Unit>, Object> {
        int label;
        private CoroutineScope p$;
        
        public ExampleCoroutine(CoroutineScope p$, @Nullable Continuation<? super ExampleCoroutine> continuation) {
            super(2, continuation);
            this.p$ = p$;
        }
        
        @NotNull
        public final Continuation<Unit> create(@NotNull Object value, @NotNull Continuation<?> continuation) {
            ExampleCoroutine var3 = new ExampleCoroutine((CoroutineScope)value, continuation);
            var3.p$ = (CoroutineScope)value;
            return var3;
        }
        
        public final Object invoke(CoroutineScope $this$launch, Continuation<? super Unit> continuation) {
            return ((ExampleCoroutine)create($this$launch, continuation)).invokeSuspend(Unit.INSTANCE);
        }
        
        @Nullable
        public final Object invokeSuspend(@NotNull Object $result) {
            Object result = IntrinsicsKt.getCOROUTINE_SUSPENDED();
            switch (this.label) {
                case 0:
                    ResultKt.throwOnFailure($result);
                    this.label = 1;
                    if (DelayKt.delay(1000L, this) == result) {
                        return result;
                    }
                    break;
                case 1:
                    ResultKt.throwOnFailure($result);
                    System.out.println("Done");
                    return Unit.INSTANCE;
                default:
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            return Unit.INSTANCE;
        }
    }
}

7.3 Flow的去糖

Flow构建器:

flow {
    for (i in 1..3) {
        delay(100)
        emit(i)
    }
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;
import kotlinx.coroutines.DelayKt;
import kotlinx.coroutines.flow.Flow;
import kotlinx.coroutines.flow.FlowCollector;
import kotlinx.coroutines.flow.FlowKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class FlowExampleKt {
    @NotNull
    public static final Flow<Integer> myFlow() {
        // 创建Flow对象
        return FlowKt.flow(new FlowExampleKt$myFlow$1());
    }
    
    @Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 3, d1 = {"..."})
    static final class FlowExampleKt$myFlow$1 extends SuspendLambda implements Function2<FlowCollector<? super Integer>, Continuation<? super Unit>, Object> {
        int label;
        private FlowCollector p$;
        private int i;
        
        public FlowExampleKt$myFlow$1(Continuation<? super FlowExampleKt$myFlow$1> continuation) {
            super(2, continuation);
        }
        
        @NotNull
        public final Continuation<Unit> create(@NotNull Object value, @NotNull Continuation<?> continuation) {
            FlowExampleKt$myFlow$1 var3 = new FlowExampleKt$myFlow$1(continuation);
            var3.p$ = (FlowCollector)value;
            return var3;
        }
        
        public final Object invoke(Object value, Continuation<?> continuation) {
            return ((FlowExampleKt$myFlow$1)create(value, continuation)).invokeSuspend(Unit.INSTANCE);
        }
        
        @Nullable
        public final Object invokeSuspend(@NotNull Object $result) {
            Object result = IntrinsicsKt.getCOROUTINE_SUSPENDED();
            FlowCollector<Integer> $this$flow = this.p$;
            int i;
            switch (this.label) {
                case 0:
                    ResultKt.throwOnFailure($result);
                    this.i = 1;
                    if (this.i > 3) {
                        return Unit.INSTANCE;
                    }
                    this.label = 1;
                    if (DelayKt.delay(100L, this) == result) {
                        return result;
                    }
                    break;
                case 1:
                    ResultKt.throwOnFailure($result);
                    i = this.i;
                    this.label = 2;
                    if ($this$flow.emit(i, this) == result) {
                        return result;
                    }
                    break;
                case 2:
                    ResultKt.throwOnFailure($result);
                    this.i++;
                    if (this.i <= 3) {
                        this.label = 3;
                        if (DelayKt.delay(100L, this) == result) {
                            return result;
                        }
                        break;
                    }
                    return Unit.INSTANCE;
                case 3:
                    ResultKt.throwOnFailure($result);
                    i = this.i;
                    this.label = 4;
                    if ($this$flow.emit(i, this) == result) {
                        return result;
                    }
                    break;
                case 4:
                    ResultKt.throwOnFailure($result);
                    this.i++;
                    if (this.i <= 3) {
                        this.label = 5;
                        if (DelayKt.delay(100L, this) == result) {
                            return result;
                        }
                        break;
                    }
                    return Unit.INSTANCE;
                case 5:
                    ResultKt.throwOnFailure($result);
                    i = this.i;
                    this.label = 6;
                    if ($this$flow.emit(i, this) == result) {
                        return result;
                    }
                    break;
                case 6:
                    ResultKt.throwOnFailure($result);
                    this.i++;
                    if (this.i <= 3) {
                        this.label = 7;
                        if (DelayKt.delay(100L, this) == result) {
                            return result;
                        }
                        break;
                    }
                    return Unit.INSTANCE;
                case 7:
                    ResultKt.throwOnFailure($result);
                    i = this.i;
                    this.label = 8;
                    if ($this$flow.emit(i, this) == result) {
                        return result;
                    }
                    break;
                case 8:
                    ResultKt.throwOnFailure($result);
                    this.i++;
                    if (this.i > 3) {
                        return Unit.INSTANCE;
                    }
                    this.label = 9;
                    if (DelayKt.delay(100L, this) == result) {
                        return result;
                    }
                    break;
                default:
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            return Unit.INSTANCE;
        }
    }
}

八、集合操作符的去糖过程

8.1 map操作符的去糖

map操作符:

val list = listOf(1, 2, 3).map { it * 2 }

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class CollectionExampleKt {
    @NotNull
    public static final List<Integer> mapExample() {
        // 创建原始列表
        List<Integer> list = CollectionsKt.listOf(1, 2, 3);
        
        // 创建结果列表
        Collection<Integer> destination = new ArrayList<>(list.size());
        
        // 遍历原始列表并应用转换函数
        for (Integer item : list) {
            destination.add(Integer.valueOf(item.intValue() * 2));
        }
        
        // 返回不可变列表
        return CollectionsKt.toList(destination);
    }
}

8.2 filter操作符的去糖

filter操作符:

val list = listOf(1, 2, 3).filter { it % 2 == 0 }

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class CollectionExampleKt {
    @NotNull
    public static final List<Integer> filterExample() {
        // 创建原始列表
        List<Integer> list = CollectionsKt.listOf(1, 2, 3);
        
        // 创建结果列表
        Collection<Integer> destination = new ArrayList<>();
        
        // 遍历原始列表并应用过滤条件
        for (Integer item : list) {
            if (item.intValue() % 2 == 0) {
                destination.add(item);
            }
        }
        
        // 返回不可变列表
        return CollectionsKt.toList(destination);
    }
}

8.3 flatMap操作符的去糖

flatMap操作符:

val list = listOf(listOf(1, 2), listOf(3, 4)).flatMap { it }

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class CollectionExampleKt {
    @NotNull
    public static final List<Integer> flatMapExample() {
        // 创建原始列表
        List<List<Integer>> list = CollectionsKt.listOf(
            CollectionsKt.listOf(1, 2),
            CollectionsKt.listOf(3, 4)
        );
        
        // 创建结果列表
        Collection<Integer> destination = new ArrayList<>();
        
        // 遍历原始列表并展开每个子列表
        for (List<Integer> subList : list) {
            destination.addAll(subList);
        }
        
        // 返回不可变列表
        return CollectionsKt.toList(d

8.4 reduce操作符的去糖

reduce操作符:

val sum = listOf(1, 2, 3).reduce { acc, i -> acc + i }

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class CollectionExampleKt {
    public static final int reduceExample() {
        // 创建原始列表
        List<Integer> list = CollectionsKt.listOf(1, 2, 3);
        
        // 检查列表是否为空
        if (list.isEmpty()) {
            throw new UnsupportedOperationException("Empty collection can't be reduced.");
        }
        
        // 初始化累加器为第一个元素
        int acc = list.get(0);
        
        // 从第二个元素开始遍历列表
        for (int i = 1; i < list.size(); i++) {
            acc = acc + list.get(i);
        }
        
        return acc;
    }
}

8.5 序列操作符的去糖

序列操作符:

val result = sequenceOf(1, 2, 3)
    .map { it * 2 }
    .filter { it % 3 == 0 }
    .toList()

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import kotlin.sequences.Sequence;
import kotlin.sequences.SequencesKt;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class SequenceExampleKt {
    @NotNull
    public static final List<Integer> sequenceExample() {
        // 创建原始序列
        Sequence<Integer> sequence = SequencesKt.sequenceOf(1, 2, 3);
        
        // 应用map操作(创建MapSequence)
        sequence = SequencesKt.map(sequence, new Function1<Integer, Integer>() {
            @Override
            public Integer invoke(Integer i) {
                return i * 2;
            }
        });
        
        // 应用filter操作(创建FilteringSequence)
        sequence = SequencesKt.filter(sequence, new Function1<Integer, Boolean>() {
            @Override
            public Boolean invoke(Integer i) {
                return i % 3 == 0;
            }
        });
        
        // 执行终止操作(转换为列表)
        Iterator<Integer> iterator = sequence.iterator();
        List<Integer> list = new ArrayList<>();
        
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        
        return list;
    }
}

九、委托属性的去糖过程

9.1 延迟初始化委托(lazy)的去糖

延迟初始化委托:

val lazyValue: String by lazy { "Hello" }

去糖后的Java代码:

import kotlin.Lazy;
import kotlin.LazyKt;
import kotlin.Metadata;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class DelegationExample {
    // 使用Lazy接口实现延迟初始化
    private final Lazy<String> lazyValue$delegate;
    
    public DelegationExample() {
        // 初始化Lazy实例,传入Supplier lambda
        this.lazyValue$delegate = LazyKt.lazy(new Function0<String>() {
            @NotNull
            @Override
            public String invoke() {
                return "Hello";
            }
        });
    }
    
    // 访问器方法,委托给Lazy实例
    @NotNull
    public final String getLazyValue() {
        return (String)this.lazyValue$delegate.getValue();
    }
}

9.2 可观察属性委托的去糖

可观察属性委托:

var name: String by Delegates.observable("John") { prop, old, new ->
    println("Property changed: $old -> $new")
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.Properties;
import kotlin.jvm.functions.Function3;
import kotlin.jvm.internal.Intrinsics;
import kotlin.properties.ObservableProperty;
import kotlin.properties.ReadWriteProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class DelegationExample {
    // 存储委托实例
    private final ReadWriteProperty<DelegationExample, String> name$delegate;
    
    public DelegationExample() {
        // 创建可观察属性委托实例
        this.name$delegate = Properties.observable("John", new Function3<kotlin.properties.Delegates.PropertyReference1<? extends String>, String, String, Unit>() {
            @Override
            public Unit invoke(kotlin.properties.Delegates.PropertyReference1<? extends String> prop, String old, String new) {
                System.out.print("Property changed: ");
                System.out.print(old);
                System.out.print(" -> ");
                System.out.println(new);
                return Unit.INSTANCE;
            }
        });
    }
    
    // 属性getter,委托给委托实例
    @NotNull
    public final String getName() {
        return this.name$delegate.getValue(this, $$delegatedProperties[0]);
    }
    
    // 属性setter,委托给委托实例
    public final void setName(@NotNull String value) {
        Intrinsics.checkNotNullParameter(value, "<set-?>");
        this.name$delegate.setValue(this, $$delegatedProperties[0], value);
    }
    
    // 用于引用属性的元数据
    private static final kotlin.reflect.KProperty[] $$delegatedProperties = {
        kotlin.reflect.jvm.internal.KProperty0Impl.makeReflect(DelegationExample.class, "name", "getName()Ljava/lang/String;", 0)
    };
}

9.3 Map委托的去糖

Map委托:

class User(val map: Map<String, Any?>) {
    val name: String by map
    val age: Int by map
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.Intrinsics;
import kotlin.properties.ReadOnlyProperty;
import kotlin.reflect.KProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class User {
    @NotNull
    private final Map<String, Object> map;
    
    // 存储委托实例
    private final ReadOnlyProperty<User, String> name$delegate;
    private final ReadOnlyProperty<User, Integer> age$delegate;
    
    public User(@NotNull Map<String, Object> map) {
        Intrinsics.checkNotNullParameter(map, "map");
        this.map = map;
        
        // 初始化委托实例
        this.name$delegate = new MapValueReadOnlyProperty(map, "name");
        this.age$delegate = new MapValueReadOnlyProperty(map, "age");
    }
    
    // name属性的getter,委托给委托实例
    @NotNull
    public final String getName() {
        return this.name$delegate.getValue(this, $$delegatedProperties[0]);
    }
    
    // age属性的getter,委托给委托实例
    public final int getAge() {
        return this.age$delegate.getValue(this, $$delegatedProperties[1]);
    }
    
    // 用于引用属性的元数据
    private static final kotlin.reflect.KProperty[] $$delegatedProperties = {
        kotlin.reflect.jvm.internal.KProperty0Impl.makeReflect(User.class, "name", "getName()Ljava/lang/String;", 0),
        kotlin.reflect.jvm.internal.KProperty0Impl.makeReflect(User.class, "age", "getAge()I", 0)
    };
    
    // 内部类:Map值委托实现
    private static final class MapValueReadOnlyProperty implements ReadOnlyProperty<User, Object> {
        @NotNull
        private final Map<String, Object> map;
        @NotNull
        private final String key;
        
        public MapValueReadOnlyProperty(@NotNull Map<String, Object> map, @NotNull String key) {
            Intrinsics.checkNotNullParameter(map, "map");
            Intrinsics.checkNotNullParameter(key, "key");
            this.map = map;
            this.key = key;
        }
        
        @Nullable
        @Override
        public Object getValue(@NotNull User receiver, @NotNull KProperty<?> property) {
            Intrinsics.checkNotNullParameter(receiver, "receiver");
            Intrinsics.checkNotNullParameter(property, "property");
            
            // 从Map中获取值
            Object value = this.map.get(this.key);
            if (value == null && !this.map.containsKey(this.key)) {
                throw new IllegalStateException("Property " + this.key + " not found in map.");
            }
            return value;
        }
    }
}

十、作用域函数的去糖过程

10.1 let函数的去糖

let函数:

val result = "Hello".let { it.length }

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ScopeFunctionExampleKt {
    public static final int letExample() {
        // 创建原始字符串
        String str = "Hello";
        
        // 调用let函数,传入lambda
        return ScopeFunctions.let(str, new Function1<String, Integer>() {
            @Override
            public Integer invoke(String it) {
                return it.length();
            }
        });
    }
}

// ScopeFunctions类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ScopeFunctions {
    public static final <T, R> R let(T receiver, Function1<? super T, ? extends R> block) {
        Intrinsics.checkNotNullParameter(block, "block");
        return block.invoke(receiver);
    }
}

10.2 run函数的去糖

run函数:

val result = "Hello".run { length }

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ScopeFunctionExampleKt {
    public static final int runExample() {
        // 创建原始字符串
        String str = "Hello";
        
        // 调用run函数,传入lambda
        return ScopeFunctions.run(str, new Function1<String, Integer>() {
            @Override
            public Integer invoke(String this$run) {
                return this$run.length();
            }
        });
    }
}

// ScopeFunctions类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ScopeFunctions {
    public static final <T, R> R run(T receiver, Function1<? super T, ? extends R> block) {
        Intrinsics.checkNotNullParameter(block, "block");
        return block.invoke(receiver);
    }
}

10.3 with函数的去糖

with函数:

val result = with("Hello") { length }

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ScopeFunctionExampleKt {
    public static final int withExample() {
        // 创建原始字符串
        String str = "Hello";
        
        // 调用with函数,传入lambda
        return ScopeFunctions.with(str, new Function1<String, Integer>() {
            @Override
            public Integer invoke(String this$with) {
                return this$with.length();
            }
        });
    }
}

// ScopeFunctions类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ScopeFunctions {
    public static final <T, R> R with(T receiver, Function1<? super T, ? extends R> block) {
        Intrinsics.checkNotNullParameter(block, "block");
        return block.invoke(receiver);
    }
}

10.4 apply函数的去糖

apply函数:

val result = StringBuilder().apply { append("Hello") }.toString()

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ScopeFunctionExampleKt {
    @NotNull
    public static final String applyExample() {
        // 创建StringBuilder实例
        StringBuilder builder = new StringBuilder();
        
        // 调用apply函数,传入lambda
        ScopeFunctions.apply(builder, new Function1<StringBuilder, Unit>() {
            @Override
            public Unit invoke(StringBuilder this$apply) {
                this$apply.append("Hello");
                return Unit.INSTANCE;
            }
        });
        
        // 返回结果
        return builder.toString();
    }
}

// ScopeFunctions类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class ScopeFunctions {
    public static final <T> T apply(T receiver, Function1<? super T, Unit> block) {
        Intrinsics.checkNotNullParameter(block, "block");
        block.invoke(receiver);
        return receiver;
    }
}

十一、解构声明的去糖过程

11.1 数据类解构声明的去糖

数据类解构声明:

data class Point(val x: Int, val y: Int)

val (x, y) = Point(10, 20)

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class DestructuringExampleKt {
    public static final void destructuringExample() {
        // 创建Point实例
        Point point = new Point(10, 20);
        
        // 调用componentN方法获取值
        int x = point.component1();
        int y = point.component2();
        
        // 使用解构后的值
        // ...
    }
}

// Point数据类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class Point {
    private final int x;
    private final int y;
    
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public final int component1() {
        return this.x;
    }
    
    public final int component2() {
        return this.y;
    }
    
    // 其他生成的方法(equals、hashCode、toString等)
}

11.2 自定义类解构声明的去糖

自定义类解构声明:

class User {
    val name: String = "John"
    val age: Int = 30
    
    operator fun component1(): String = name
    operator fun component2(): Int = age
}

val (name, age) = User()

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class DestructuringExampleKt {
    public static final void destructuringExample() {
        // 创建User实例
        User user = new User();
        
        // 调用componentN方法获取值
        String name = user.component1();
        int age = user.component2();
        
        // 使用解构后的值
        // ...
    }
}

// User类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class User {
    @NotNull
    private final String name = "John";
    private final int age = 30;
    
    @NotNull
    public final String component1() {
        return this.name;
    }
    
    public final int component2() {
        return this.age;
    }
}

11.3 解构声明在循环中的去糖

解构声明在循环中的应用:

val map = mapOf("a" to 1, "b" to 2)
for ((key, value) in map) {
    println("$key -> $value")
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Lambda;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.Map;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class DestructuringExampleKt {
    public static final void loopDestructuringExample() {
        // 创建Map实例
        Map<String, Integer> map = CollectionsKt.mapOf(
            new Pair("a", 1),
            new Pair("b", 2)
        );
        
        // 获取Map的迭代器
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        
        // 遍历Map
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            
            // 解构Map.Entry
            String key = entry.getKey();
            Integer value = entry.getValue();
            
            // 执行循环体
            System.out.print(key);
            System.out.print(" -> ");
            System.out.println(value);
        }
    }
}

// Pair类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class Pair<A, B> {
    private final A first;
    private final B second;
    
    public Pair(A first, B second) {
        this.first = first;
        this.second = second;
    }
    
    public final A getFirst() {
        return this.first;
    }
    
    public final B getSecond() {
        return this.second;
    }
    
    // component1和component2方法
    public final A component1() {
        return this.first;
    }
    
    public final B component2() {
        return this.second;
    }
}

十二、运算符重载的去糖过程

12.1 算术运算符重载的去糖

算术运算符重载:

data class Vector(val x: Int, val y: Int) {
    operator fun plus(other: Vector): Vector {
        return Vector(x + other.x, y + other.y)
    }
}

val result = Vector(1, 2) + Vector(3, 4)

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class OperatorOverloadExampleKt {
    @NotNull
    public static final Vector operatorOverloadExample() {
        // 创建Vector实例
        Vector a = new Vector(1, 2);
        Vector b = new Vector(3, 4);
        
        // 调用plus方法
        return a.plus(b);
    }
}

// Vector类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class Vector {
    private final int x;
    private final int y;
    
    public Vector(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    @NotNull
    public final Vector plus(@NotNull Vector other) {
        Intrinsics.checkNotNullParameter(other, "other");
        return new Vector(this.x + other.x, this.y + other.y);
    }
    
    // 其他生成的方法(componentN、equals、hashCode、toString等)
}

12.2 索引访问运算符重载的去糖

索引访问运算符重载:

class Matrix(private val data: Array<IntArray>) {
    operator fun get(row: Int, col: Int): Int {
        return data[row][col]
    }
    
    operator fun set(row: Int, col: Int, value: Int) {
        data[row][col] = value
    }
}

val matrix = Matrix(arrayOf(intArrayOf(1, 2), intArrayOf(3, 4)))
val value = matrix[0, 1]
matrix[1, 0] = 5

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class OperatorOverloadExampleKt {
    public static final void indexOperatorExample() {
        // 创建Matrix实例
        Matrix matrix = new Matrix(new int[][] {
            new int[] {1, 2},
            new int[] {3, 4}
        });
        
        // 调用get方法
        int value = matrix.get(0, 1);
        
        // 调用set方法
        matrix.set(1, 0, 5);
    }
}

// Matrix类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class Matrix {
    @NotNull
    private final int[][] data;
    
    public Matrix(@NotNull int[][] data) {
        Intrinsics.checkNotNullParameter(data, "data");
        this.data = data;
    }
    
    public final int get(int row, int col) {
        return this.data[row][col];
    }
    
    public final void set(int row, int col, int value) {
        this.data[row][col] = value;
    }
}

12.3 迭代器运算符重载的去糖

迭代器运算符重载:

class MyRange(val start: Int, val end: Int) {
    operator fun iterator(): Iterator<Int> {
        return object : Iterator<Int> {
            private var current = start
            
            override fun hasNext(): Boolean = current <= end
            
            override fun next(): Int {
                if (!hasNext()) throw NoSuchElementException()
                return current++
            }
        }
    }
}

for (i in MyRange(1, 3)) {
    println(i)
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.NoSuchElementException;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class OperatorOverloadExampleKt {
    public static final void iteratorExample() {
        // 创建MyRange实例
        MyRange range = new MyRange(1, 3);
        
        // 获取迭代器
        Iterator<Integer> iterator = range.iterator();
        
        // 遍历迭代器
        while (iterator.hasNext()) {
            int i = iterator.next();
            System.out.println(i);
        }
    }
}

// MyRange类的简化实现
@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public final class MyRange {
    private final int start;
    private final int end;
    
    public MyRange(int start, int end) {
        this.start = start;
        this.end = end;
    }
    
    @NotNull
    public final Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            private int current = MyRange.this.start;
            
            @Override
            public boolean hasNext() {
                return this.current <= MyRange.this.end;
            }
            
            @Override
            public Integer next() {
                if (!this.hasNext()) {
                    throw new NoSuchElementException();
                }
                int value = this.current;
                this.current++;
                return value;
            }
        };
    }
}

十三、密封类与when表达式的去糖过程

13.1 密封类的去糖

密封类:

sealed class Result {
    data class Success(val data: String) : Result()
    data class Error(val message: String) : Result()
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
public sealed abstract class Result {
    private Result() {}
    
    // 私有构造函数,用于子类调用
    private Result(DefaultConstructorMarker $constructor_marker) {
        this();
    }
    
    // Success子类
    @Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
    public static final class Success extends Result {
        @NotNull
        private final String data;
        
        public Success(@NotNull String data) {
            super((DefaultConstructorMarker)null);
            Intrinsics.checkNotNullParameter(data, "data");
            this.data = data;
        }
        
        @NotNull
        public final String getData() {
            return this.data;
        }
        
        // 生成的component1、equals、hashCode、toString方法
    }
    
    // Error子类
    @Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 1, d1 = {"..."})
    public static final class Error extends Result {
        @NotNull
        private final String message;
        
        public Error(@NotNull String message) {
            super((DefaultConstructorMarker)null);
            Intrinsics.checkNotNullParameter(message, "message");
            this.message = message;
        }
        
        @NotNull
        public final String getMessage() {
            return this.message;
        }
        
        // 生成的component1、equals、hashCode、toString方法
    }
}

13.2 带when表达式的密封类的去糖

带when表达式的密封类:

fun handleResult(result: Result): String = when (result) {
    is Result.Success -> "Success: ${result.data}"
    is Result.Error -> "Error: ${result.message}"
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class SealedClassExampleKt {
    @NotNull
    public static final String handleResult(@NotNull Result result) {
        Intrinsics.checkNotNullParameter(result, "result");
        
        // 类型检查和分支处理
        if (result instanceof Result.Success) {
            Result.Success success = (Result.Success)result;
            return "Success: " + success.getData();
        } else if (result instanceof Result.Error) {
            Result.Error error = (Result.Error)result;
            return "Error: " + error.getMessage();
        } else {
            // 理论上不会执行,因为密封类的所有子类都已覆盖
            throw new IllegalStateException("Unexpected value: " + result);
        }
    }
}

13.3 带else分支的when表达式的去糖

带else分支的when表达式:

fun getType(value: Any): String = when (value) {
    is String -> "String"
    is Int -> "Int"
    else -> "Other"
}

去糖后的Java代码:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

@Metadata(mv = {1, 4, 2}, bv = {1, 0, 3}, k = 2, d1 = {"..."})
public final class WhenExpressionExampleKt {
    @NotNull
    public static final String getType(@NotNull Object value) {
        Intrinsics.checkNotNullParameter(value, "value");
        
        // 类型检查和分支处理
        if (value instanceof String) {
            return "String";
        } else if (value instanceof Integer) {
            return "Int";
        } else {
            return "Other";
        }
    }
}

十四、Kotlin编译器插件与去糖过程

14.1 编译器插件的基本概念

Kotlin编译器插件是一种扩展Kotlin编译器功能的机制,它可以在编译过程的不同阶段插入自定义逻辑。常见的编译器插件包括:

  1. 语言特性插件:添加新的语言特性或语法糖
  2. 代码生成插件:在编译时生成额外的代码
  3. 代码转换插件:修改现有的AST或字节码
  4. 注解处理器插件:处理自定义注解并生成相应代码

14.2 自定义语法糖的实现

通过编译器插件实现自定义语法糖的基本步骤:

  1. 创建插件项目:使用Kotlin Compiler Plugin模板创建项目
  2. 实现插件组件:实现AbstractCliOptionContributorCompilerPluginRegistrar接口
  3. 定义语法转换规则:在编译过程的特定阶段修改AST
  4. 注册插件:在resources/META-INF/services目录下注册插件

14.3 插件开发示例:自动日志插件

实现一个自动为函数添加日志的插件:

// 自定义注解
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
annotation class Logged

// 插件实现(简化)
class LoggingPlugin : CompilerPluginRegistrar() {
    override val supportsK2: Boolean = false
    
    override fun ExtensionStorage.registerExtensions(
        project: org.jetbrains.kotlin.config.CompilerConfiguration
    ) {
        // 注册AST转换插件
        DeclarationTransformer.registerExtension(
            project,
            LoggingTransformer()
        )
    }
}

// AST转换器
class LoggingTransformer : DeclarationTransformer {
    override fun transformFlat(
        declaration: KSDeclaration,
        provider: KSDeclarationProvider
    ): List<KSDeclaration> {
        // 只处理带有@Logged注解的函数
        if (declaration is KSFunctionDeclaration && 
            declaration.annotations.any { it.shortName.asString() == "Logged" }) {
            
            // 获取函数体
            val body = declaration.body ?: return listOf(declaration)
            
            // 在函数体前添加日志代码
            val newBody = buildString {
                append("println(\"Entering function ${declaration.name.asString()}\")\n")
                append(body.asString())
                append("\nprintln(\"Exiting function ${declaration.name.asString()}\")")
            }
            
            // 创建修改后的函数声明
            return listOf(
                declaration.copy(
                    body = KSTextBody(newBody, declaration.body?.receiverType)
                )
            )
        }
        
        return listOf(declaration)
    }
}

14.4 插件对去糖过程的影响

编译器插件可以:

  1. 添加新的语法糖:通过转换AST实现新的语言特性
  2. 修改现有语法糖的去糖逻辑:改变现有语法糖的转换方式
  3. 优化去糖后的代码:对生成的代码进行进一步优化
  4. 集成第三方库:通过插件无缝集成
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android 小码蜂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值