Java 如何将方法.getGenericReturnType()转换为jvm类型签名

Java 如何将方法.getGenericReturnType()转换为jvm类型签名,java,java-bytecode-asm,Java,Java Bytecode Asm,如何将java.lang.reflect.Type的实例转换为jvm通用类型签名 Type type = method.getGenericReturnType(); String signature = toTypeSig(type); 例如,这种类型: Map<String, Map<?, ? super Integer>> Map我猜您希望从类文件(或者您是指从源文件)获取jvm签名 从一个类中,下面可能是一个解决方案 public class GetSigna

如何将
java.lang.reflect.Type
的实例转换为jvm通用类型签名

Type type = method.getGenericReturnType();
String signature = toTypeSig(type);
例如,这种类型:

Map<String, Map<?, ? super Integer>>

Map我猜您希望从类文件(或者您是指从源文件)获取jvm签名

从一个类中,下面可能是一个解决方案

public class GetSignature {

    // the method for which you want to retrieve the signature
    Map<String, Map<?, ? super Integer>> someMethod() {
        return null;
    }

    public static void main(String[] args) throws Exception {
        // make the private method in class Method accessible
        Method methodGenericSignature = Method.class.getDeclaredMethod(
                "getGenericSignature",
                (Class<?>[]) null
        );
        methodGenericSignature.setAccessible(true);

        // get the signature from the method
        Method method = GetSignature.class.getDeclaredMethod("someMethod",
                (Class<?>[]) null
        );
        String returnValue = (String) methodGenericSignature.invoke(method,
                (Object[]) null
        );
        System.out.println("signature: " + returnValue);
    }
}
样本输出

method: someMethod  descriptor: ()Ljava/util/Map;  signature: ()Ljava/util/Map<Ljava/lang/String;Ljava/util/Map<*-Ljava/lang/Integer;>;>;
method:someMethod描述符:()Ljava/util/Map;签名:()Ljava/util/Map;

我猜您希望从类文件(或者您是指从源文件)获取jvm签名

从一个类中,下面可能是一个解决方案

public class GetSignature {

    // the method for which you want to retrieve the signature
    Map<String, Map<?, ? super Integer>> someMethod() {
        return null;
    }

    public static void main(String[] args) throws Exception {
        // make the private method in class Method accessible
        Method methodGenericSignature = Method.class.getDeclaredMethod(
                "getGenericSignature",
                (Class<?>[]) null
        );
        methodGenericSignature.setAccessible(true);

        // get the signature from the method
        Method method = GetSignature.class.getDeclaredMethod("someMethod",
                (Class<?>[]) null
        );
        String returnValue = (String) methodGenericSignature.invoke(method,
                (Object[]) null
        );
        System.out.println("signature: " + returnValue);
    }
}
样本输出

method: someMethod  descriptor: ()Ljava/util/Map;  signature: ()Ljava/util/Map<Ljava/lang/String;Ljava/util/Map<*-Ljava/lang/Integer;>;>;
method:someMethod描述符:()Ljava/util/Map;签名:()Ljava/util/Map;

我猜您希望从类文件(或者您是指从源文件)获取jvm签名

从一个类中,下面可能是一个解决方案

public class GetSignature {

    // the method for which you want to retrieve the signature
    Map<String, Map<?, ? super Integer>> someMethod() {
        return null;
    }

    public static void main(String[] args) throws Exception {
        // make the private method in class Method accessible
        Method methodGenericSignature = Method.class.getDeclaredMethod(
                "getGenericSignature",
                (Class<?>[]) null
        );
        methodGenericSignature.setAccessible(true);

        // get the signature from the method
        Method method = GetSignature.class.getDeclaredMethod("someMethod",
                (Class<?>[]) null
        );
        String returnValue = (String) methodGenericSignature.invoke(method,
                (Object[]) null
        );
        System.out.println("signature: " + returnValue);
    }
}
样本输出

method: someMethod  descriptor: ()Ljava/util/Map;  signature: ()Ljava/util/Map<Ljava/lang/String;Ljava/util/Map<*-Ljava/lang/Integer;>;>;
method:someMethod描述符:()Ljava/util/Map;签名:()Ljava/util/Map;

我猜您希望从类文件(或者您是指从源文件)获取jvm签名

从一个类中,下面可能是一个解决方案

public class GetSignature {

    // the method for which you want to retrieve the signature
    Map<String, Map<?, ? super Integer>> someMethod() {
        return null;
    }

    public static void main(String[] args) throws Exception {
        // make the private method in class Method accessible
        Method methodGenericSignature = Method.class.getDeclaredMethod(
                "getGenericSignature",
                (Class<?>[]) null
        );
        methodGenericSignature.setAccessible(true);

        // get the signature from the method
        Method method = GetSignature.class.getDeclaredMethod("someMethod",
                (Class<?>[]) null
        );
        String returnValue = (String) methodGenericSignature.invoke(method,
                (Object[]) null
        );
        System.out.println("signature: " + returnValue);
    }
}
样本输出

method: someMethod  descriptor: ()Ljava/util/Map;  signature: ()Ljava/util/Map<Ljava/lang/String;Ljava/util/Map<*-Ljava/lang/Integer;>;>;
method:someMethod描述符:()Ljava/util/Map;签名:()Ljava/util/Map;

我的最终解决方案。我发帖供参考。因为当
method.setAccessible(true)
不可用时,它可能很有用

这或多或少就是我最终使用的。 因为我需要将泛型返回类型转换为类签名。我没有使用类型变量

它适用于没有类型参数的方法。 对于具有泛型类型参数的方法,它尝试替换参数

正在处理此方法的通用返回类型:
ListsomeMethod()
结果显示为
“Ljava/util/List;”

静态字符串到通用签名(最终类型)
{
StringBuilder sb=新的StringBuilder();
通用签名(sb,类型);
使某人返回字符串();
}
静态void toGenericSignature(StringBuilder sb,最终类型)
{
if(类型instanceof GenericArrayType)
{
某人加上(“[”);
toGenericSignature(sb,((GenericArrayType)类型).getGenericComponentType());
}
else if(参数化类型的instanceof)
{
ParameteredType pt=(ParameteredType)类型;
某人附加('L');
sb.append(((类)pt.getRawType()).getName().replace('.','/');

sb.append(“我的最终解决方案。我发布此帖子以供参考。因为当
method.setAccessible(true)
不可用时,它可能会有用

这或多或少就是我最终使用的。 因为我需要将泛型返回类型转换为类签名,并且我没有使用类型变量

它适用于没有类型参数的方法。 对于具有泛型类型参数的方法,它尝试替换参数

正在处理此方法的通用返回类型:
ListsomeMethod()
结果显示为
“Ljava/util/List;”

静态字符串到通用签名(最终类型)
{
StringBuilder sb=新的StringBuilder();
通用签名(sb,类型);
使某人返回字符串();
}
静态void toGenericSignature(StringBuilder sb,最终类型)
{
if(类型instanceof GenericArrayType)
{
某人加上(“[”);
toGenericSignature(sb,((GenericArrayType)类型).getGenericComponentType());
}
else if(参数化类型的instanceof)
{
ParameteredType pt=(ParameteredType)类型;
某人附加('L');
sb.append(((类)pt.getRawType()).getName().replace('.','/');

sb.append(“我的最终解决方案。我发布此帖子以供参考。因为当
method.setAccessible(true)
不可用时,它可能会有用

这或多或少就是我最终使用的。 因为我需要将泛型返回类型转换为类签名,并且我没有使用类型变量

它适用于没有类型参数的方法。 对于具有泛型类型参数的方法,它尝试替换参数

正在处理此方法的通用返回类型:
ListsomeMethod()
结果显示为
“Ljava/util/List;”

静态字符串到通用签名(最终类型)
{
StringBuilder sb=新的StringBuilder();
通用签名(sb,类型);
使某人返回字符串();
}
静态void toGenericSignature(StringBuilder sb,最终类型)
{
if(类型instanceof GenericArrayType)
{
某人加上(“[”);
toGenericSignature(sb,((GenericArrayType)类型).getGenericComponentType());
}
else if(参数化类型的instanceof)
{
ParameteredType pt=(ParameteredType)类型;
某人附加('L');
sb.append(((类)pt.getRawType()).getName().replace('.','/');

sb.append(“我的最终解决方案。我发布此帖子以供参考。因为当
method.setAccessible(true)
不可用时,它可能会有用

这或多或少就是我最终使用的。 因为我需要将泛型返回类型转换为类签名,并且我没有使用类型变量

它适用于没有类型参数的方法。 对于具有泛型类型参数的方法,它尝试替换参数

正在处理此方法的通用返回类型:
ListsomeMethod()
结果显示为
“Ljava/util/List;”

静态字符串到通用签名(最终类型)
{
StringBuilder sb=新的StringBuilder();
通用签名(sb,类型);
使某人返回字符串();
}
静态void toGenericSignature(StringBuilder sb,最终类型)
{
if(类型instanceof GenericArrayType)
{
某人加上(“[”);
toGenericSignature(sb,((GenericArrayType)类型).getGenericComponentType());
}
else if(参数化类型的instanceof)
{
ParameteredType pt=(参数