在尝试在运行时为某些属性获取 JPA 注释时,我遇到了这个问题。我无法解释为什么。
PS:在与 Spring 的调试会话之后,我找到了这个问题的解释:编译器在编译时引入的桥接方法。请看我自己对这个问题的回答..
这是一个复制问题的示例源代码(真实代码的简化版本)。
导入java.beans.BeanInfo;导入 java.beans.IntrospectionException;导入 java.beans.Introspector;导入 java.beans.MethodDescriptor;导入 java.io.Serializable;导入java.lang.reflect.Method;
公共类 MethodMasking {
public interface HasId<ID extends Serializable> {
void setId(ID id);
ID getId();
}
public interface Storeable extends HasId<Long> {}
class Item implements Storeable {Long id; String code;
Item(Long id, String code) { this.id = id; this.code = code; }
public Long getId() { return id; }
public void setId(Long id) {this.id = id;}
}
public static void main(String[] args) throws IntrospectionException {
final BeanInfo beanInfo = Introspector.getBeanInfo(Item.class);
java.lang.System.out.println("BeanInfo:methodDescriptors:");
final MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
for (MethodDescriptor methodDescriptor : methodDescriptors) {
java.lang.System.out.println("\t"+methodDescriptor.getMethod().getName());
}
java.lang.System.out.println("class:declaredMethods:");
final Method[] declaredMethods = Item.class.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
java.lang.System.out.println("\t"+declaredMethod.getName());
}
}
程序的输出:
BeanInfo:methodDescriptors:
hashCode
wait
getId
notifyAll
equals
wait
wait
toString
setId
notify
setId
getClass
class:declaredMethods:
getId
getId
setId
setId
现在我很困惑:
为什么在 beanInfo 中有两个方法描述符用于 setId 而只有一个用于 getId ?
为什么在声明的方法中有两个 getId 方法和两个 setId 方法?
在调试时,我在使用 getDeclaredMethods 时有这些方法签名:
[0] = {java.lang.reflect.Method@139}"public java.lang.Long MethodMasking$Item.getId()"
[1] = {java.lang.reflect.Method@446}"public java.io.Serializable MethodMasking$Item.getId()"
[2] = {java.lang.reflect.Method@447}"public void MethodMasking$Item.setId(java.lang.Long)"
[3] = {java.lang.reflect.Method@448}"public void MethodMasking$Item.setId(java.io.Serializable)"
编辑:经过一些测试,我发现问题的原因是 HasId 接口中泛型的使用......
以这种方式声明,问题就消失了:不再有重复的方法。
public interface HasId {
void setId(Long id);
Long getId();
}
public interface Storeable extends HasId {}