实现通用的java接口增加了额外的方法 [英] Implementing generic java interface adds additional method

查看:205
本文介绍了实现通用的java接口增加了额外的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的java接口 Id< T> ,它有一个方法 T getId() MyClass 实现 Id< Long> 。当我使用java反射检查在 MyClass 上声明的方法时,我看到两个方法:一个使用返回类型 Long ,并且一个返回类型为 Object

这里是源代码:

  package mypackage; 

import java.lang.reflect.Method;
$ b $ public class MainClass {
public static void main(String [] args){
for(Method method:MyClass.class.getDeclaredMethods()){
System .out.println(方法);
}
//输出两行
// public java.lang.Long mypackage.MyClass.getId()< - ok
// public java.lang。 Object mypackage.MyClass.getId()< - not ok
}
}

interface Id< T> {
T getId();
}

class MyClass implements Id< Long> {
@Override
public Long getId(){
return new Long(0);
};


解决方案

第二种方法是 synthetic bridge 方法,你可以用 method.isSynthetic() method.isBridge )。你不能删除它,但是如果你不想在已声明的方法列表中看到它,只需检查所有这些方法是否有 isBridge()== false

合成桥接方法在编译期间自动添加到泛型类中。您可以阅读更多关于合成方法的此处

  for(方法method:MyClass.class.getDeclaredMethods()){
System.out.println(Method:+方法);
System.out.println(synthetic:+ method.isSynthetic());
System.out.println(bridge:+ method.isBridge());
}

// 1
//方法:public java.lang.Long Main $ MyClass.getId()
//合成:false
// bridge:false
// 2
//方法:public java.lang.Object Main $ MyClass.getId()
//合成:true
//桥:真


I have a generic java interface Id<T> with a single method T getId() and a class MyClass that implements Id<Long>. When I inspect the methods declared on MyClass using java reflection, I see two methods: one with return type Long, and one with return type Object. Where does the second method come from and how can I remove it?

Here is the source:

package mypackage;

import java.lang.reflect.Method;

public class MainClass {
    public static void main(String[] args) {
        for (Method method : MyClass.class.getDeclaredMethods()) {
            System.out.println(method);
        }
        // prints out two lines
        // public java.lang.Long mypackage.MyClass.getId()   <-- ok
        // public java.lang.Object mypackage.MyClass.getId() <-- not ok
    }
}

interface Id<T> {
    T getId();
}

class MyClass implements Id<Long> {
    @Override
    public Long getId() {
        return new Long(0);
    };
}

解决方案

The second method is a synthetic bridge method, you can check it with method.isSynthetic() or method.isBridge(). You can't remove it, but if you don't want to see it in the list of declared methods, just check all those methods to have isBridge() == false.

Synthetic bridge methods are added automatically to generic classes during compilation. You can read more about synthetic methods here.

for (Method method : MyClass.class.getDeclaredMethods()) {
    System.out.println("Method: " + method);
    System.out.println("synthetic: " + method.isSynthetic());
    System.out.println("bridge: " + method.isBridge());
}

// 1
//Method: public java.lang.Long Main$MyClass.getId()
//synthetic: false
//bridge: false
// 2
//Method: public java.lang.Object Main$MyClass.getId()
//synthetic: true
//bridge: true

这篇关于实现通用的java接口增加了额外的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆