google / ksp

Kotlin Symbol Processing API
https://github.com/google/ksp
Apache License 2.0
2.89k stars 274 forks source link

Parameter names are inconsistent between KSP and JavaAP for Android and java.lang classes #1459

Open kuanyingchou opened 1 year ago

kuanyingchou commented 1 year ago

When a Java class is compiled with "-parameters", both JavaAP and KSP can see the formal parameter names.

But for some framework classes, JavaAP sees the formal names while in KSP we see p0, p1, p2, etc.. For example, in an append function from java.lang.StringBuffer, the parameter name is "str" in JavaAP, but "p0" in KSP. Here's the function decompiled with javap. Note that there's no MethodParameters block:

public synchronized java.lang.StringBuffer append(java.lang.String);
    descriptor: (Ljava/lang/String;)Ljava/lang/StringBuffer;
    flags: (0x0021) ACC_PUBLIC, ACC_SYNCHRONIZED
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0
         1: aconst_null
         2: putfield      #34                 // Field toStringCache:Ljava/lang/String;
         5: aload_0
         6: aload_1
         7: invokespecial #73                 // Method java/lang/AbstractStringBuilder.append:(Ljava/lang/String;)Ljava/lang/AbstractStringBuilder;
        10: pop
        11: aload_0
        12: areturn
      LineNumberTable:
        line 312: 0
        line 313: 5
        line 314: 11
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      13     0  this   Ljava/lang/StringBuffer;
            0      13     1   str   Ljava/lang/String;
    RuntimeVisibleAnnotations:
      0: #392()
        jdk.internal.vm.annotation.IntrinsicCandidate
kuanyingchou commented 1 year ago

After more tests it seems that JavaAP can read the parameter names from LocalVariableTable (generated with -g or -g:vars flag in javac) when there is no MethodParameters.