- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当试图在运行时为某些属性获取 JPA 注释时,我遇到了这个问题。我无法解释为什么。
PS:在使用Spring进行调试后,我找到了这个问题的解释:编译器在编译时引入的桥接方法。请看我自己对这个问题的回答..
这是一个复制问题的示例源代码(真实代码的简化版本)。
导入java.beans.BeanInfo;导入 java.beans.IntrospectionException;导入 java.beans.Introspector;导入 java.beans.MethodDescriptor;导入 java.io.Serializable;导入 java.lang.reflect.Method;
公共(public)类方法掩码{
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 中有 2 个方法描述符用于 setId 而只有一个用于 getId ?
为什么在声明的方法中有 2 个 getId 方法和 2 个 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 接口(interface)中使用泛型......
这样声明,问题就消失了:不再有重复的方法。
public interface HasId {
void setId(Long id);
Long getId();
}
public interface Storeable extends HasId {}
最佳答案
这是因为编译器在使用泛型时引入了桥接方法:
some explanation here
关于java - BeanInfo :methodDescriptors and class:declaredMethods : multiple methods with same name and method masking 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6954586/
有没有办法使用 IntelliJ IDEA 使用一些图形界面和 BeanInfo 类实现(如 NetBeans 中的 https://docs.oracle.com/javase/tutorial/j
我正在尝试使用 ASM Framework 动态创建一个子类。我能够创建该类并实例化它。但是当我尝试这样做时 org.apache.commons.beanutils.BeanUtils.copyPr
我正在使用 Grails 2.2.2 和 GGTS 3.3M1。当我运行我的应用程序(grails run-app)并更改 groovy 文件中的某些内容时,Grails 会重新编译该文件。在此过程中
JEP 256: BeanInfo Annotations提供JavaBean和 BeanProperty注释。虽然没有太多文档,但我一直希望这将允许我们使用注释将类上的字段指定为 JavaBean
我想为我们的自定义操作添加描述,但 install4J 似乎找不到我的 BeanInfo。我尝试了 install4J 提供的 customCode-sample,但它也不起作用。我的代码分布在一个
我注意到 Spring Boot 将 spring.beaninfo.ignore 设置为 true(如果未设置)。 Spring 对此属性做了什么? 最佳答案 “spring.beaninfo.ig
当试图在运行时为某些属性获取 JPA 注释时,我遇到了这个问题。我无法解释为什么。 PS:在使用Spring进行调试后,我找到了这个问题的解释:编译器在编译时引入的桥接方法。请看我自己对这个问题的回答
我有使用 java.beans.Introspector、java.beans.BeanInfo 的第 3 方 jar(我无法更改)和 java.beans.PropertyDescriptor. 如
我是一名优秀的程序员,十分优秀!