IntelliJ Java编辑器:无法显示接口参数名称

huangapple go评论52阅读模式
英文:

Intellij java editor: could not show interface parameter names

问题

Intellij Java编辑器:无法显示库jar中的接口参数名称。
例如,

public interface Foo {
    public Bar getBar(String name, int type);

    public default Bar getBar(String name) {
         return getBar(name, 1);
    }
}

在Java编辑器中:

Foo foo = getFoo();
foo.
    getBar(String s, int i)
    getBar(String name)

在键入foo后,会显示两个方法。第一个方法显示参数名称为"s"和"i",无法显示原始参数名称(name, type)。

第二个方法是"default"方法,其参数名称显示正确。

编辑器是否有办法显示接口方法的参数名称?

库jar源代码不可用。

更新:

  1. 将javadoc目录附加到库jar没有帮助。
  2. 使用-parameters选项编译,没有帮助。从javap中看,类文件包含所有接口方法参数。因此,Intellij应该能够获取它们。

No#1(附加javadoc)是首选解决方案。选项-parameters会使类文件大小变大,并可能会有性能开销。

英文:

Intellij java editor: could not show interface parameter names in a lib jar.
e.g.,

public interface Foo {
    public Bar getBar(String name, int type);

    public default Bar getBar(String name) {
         return getBar(name, 1);
    }

}

In java editor:

Foo foo = getFoo();
foo.
    getBar(String s, int i)
    getBar(String name)

When typing dot after foo, two methods show up. The first method shows parameter names as "s" and "i", could not show the original parameter names (name, type).

The second method is "default" method and its parameter names show up correctly.

Is there way for the editor to show the interface method parameter names ?

The lib jar source code is not available.

UPDATE:

  1. attaching javadoc directory to the lib jar did not help.
  2. compile with -parameters option, did not help. Seen from javap, class file contains all interface method parameters. So Intellij should be able to get them.

No#1(attaching javadoc) is the preferred solution. Option -parameters will make class file size bigger and may have performance overhead.

答案1

得分: 1

类文件不存储非默认方法的参数名称。所以如果你没有该代码的源代码,在反编译之后,IDEA对实际参数名称一无所知。

如果你使用 javap -verbose Foo.class 来检查它,你会看到字节码包含默认方法的参数名称,但不包含普通方法的参数名称:

常量池
   #1 = InterfaceMethodref #2.#3          // Foo.getBar:(Ljava/lang/String;I)LBar;
   #2 = Class              #4             // Foo
   #3 = NameAndType        #5:#6          // getBar:(Ljava/lang/String;I)LBar;
   #4 = Utf8               Foo
   #5 = Utf8               getBar
   #6 = Utf8               (Ljava/lang/String;I)LBar;
   #7 = Class              #8             // java/lang/Object
   #8 = Utf8               java/lang/Object
   #9 = Utf8               (Ljava/lang/String;)LBar;
  #10 = Utf8               Code
  #11 = Utf8               LineNumberTable
  #12 = Utf8               LocalVariableTable
  #13 = Utf8               this
  #14 = Utf8               LFoo;
  #15 = Utf8               name
  #16 = Utf8               Ljava/lang/String;
  #17 = Utf8               SourceFile
  #18 = Utf8               Foo.java
{
  public abstract Bar getBar(java.lang.String, int);
    descriptor: (Ljava/lang/String;I)LBar;
    flags: (0x0401) ACC_PUBLIC, ACC_ABSTRACT

  public default Bar getBar(java.lang.String);
    descriptor: (Ljava/lang/String;)LBar;
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=3, locals=2, args_size=2
         0: aload_0
         1: aload_1
         2: iconst_1
         3: invokeinterface #1,  3            // InterfaceMethod getBar:(Ljava/lang/String;I)LBar;
         8: areturn
      LineNumberTable:
        line 5: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  this   LFoo;
            0       9     1  name   Ljava/lang/String;
}
英文:

The class file doesn't store parameter names for non-default methods. So if you don't have the source code of that code, after the decompilation, the IDEA has no idea about the actual parameter names before.

If you use javap -verbose Foo.class to check it, you can see the bytecode contains the parameter name for the default method but not for the normal method:

Constant pool:
#1 = InterfaceMethodref #2.#3          // Foo.getBar:(Ljava/lang/String;I)LBar;
#2 = Class              #4             // Foo
#3 = NameAndType        #5:#6          // getBar:(Ljava/lang/String;I)LBar;
#4 = Utf8               Foo
#5 = Utf8               getBar
#6 = Utf8               (Ljava/lang/String;I)LBar;
#7 = Class              #8             // java/lang/Object
#8 = Utf8               java/lang/Object
#9 = Utf8               (Ljava/lang/String;)LBar;
#10 = Utf8               Code
#11 = Utf8               LineNumberTable
#12 = Utf8               LocalVariableTable
#13 = Utf8               this
#14 = Utf8               LFoo;
#15 = Utf8               name
#16 = Utf8               Ljava/lang/String;
#17 = Utf8               SourceFile
#18 = Utf8               Foo.java
{
public abstract Bar getBar(java.lang.String, int);
descriptor: (Ljava/lang/String;I)LBar;
flags: (0x0401) ACC_PUBLIC, ACC_ABSTRACT
public default Bar getBar(java.lang.String);
descriptor: (Ljava/lang/String;)LBar;
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=2, args_size=2
0: aload_0
1: aload_1
2: iconst_1
3: invokeinterface #1,  3            // InterfaceMethod getBar:(Ljava/lang/String;I)LBar;
8: areturn
LineNumberTable:
line 5: 0
LocalVariableTable:
Start  Length  Slot  Name   Signature
0       9     0  this   LFoo;
0       9     1  name   Ljava/lang/String;
}

huangapple
  • 本文由 发表于 2023年2月14日 08:12:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定