简体   繁体   English

返回通用类类型的方法的Java VM类型签名是什么?

[英]What is the Java VM type signature for a method returning a generic class type?

The method is 方法是

public Set<BluetoothDevice> getBondedDevices ()

from Android.bluetooth.BluetoothAdapter. 来自Android.bluetooth.BluetoothAdapter。

I read the type signature guidance here , including how to specify an object with L class-name ; 我在这里阅读了类型签名指南,包括如何使用L class-name指定对象; but I don't know how to extend that to a generic class (or even if it is possible). 但是我不知道如何将其扩展到泛型类(或者即使有可能)。

Generics do not exist at runtime; 泛型在运行时不存在。 AFAIK, that syntax does not support type parameters. AFAIK,该语法不支持类型参数。

I'm not sure if that applies to type parameters for base classes (which do exist at runtime in Reflection). 我不确定这是否适用于基类的类型参数(在运行时确实存在于Reflection中)。

If you are not sure what is the signature of the method, javap comes to rescue. 如果不确定该方法的签名是什么,则可以使用javap进行救援。

import java.util.*;
public class Example {
  public Set<Float> getSet() {
    return null;
  }
}

you can easily check the signature following way 您可以按照以下方式轻松检查签名

> javac Example.java
> javap -s -cp . Example
Compiled from "Example.java"
public class Example {
  public Example();
    descriptor: ()V

  public java.util.Set<java.lang.Float> getSet();
    descriptor: ()Ljava/util/Set;
}

You are asking about Type Erasure in java. 您在问Java中的Type Erasure。 Your answer is Object. 您的答案是对象。 If you have: 如果你有:

List<E> myList;

The compiler will interpret E as Object. 编译器会将E解释为Object。 This case is known as Class Type Erasure. 这种情况称为“类类型擦除”。 There is also a case called Method Type Erasure. 还有一种情况称为方法类型擦除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM