更新:Oracle 已确认这是一个错误。
摘要:某些自定义BeanInfo s 和 PropertyDescriptor在 JDK 1.6 中工作的 s 在 JDK 1.7 中失败,有些仅在垃圾收集运行并清除某些 SoftReferences 后才会失败。
Edit: This will also break the
ExtendedBeanInfoin Spring 3.1 as noted at the bottom of the post.Edit: If you invoke sections 7.1 or 8.3 of the JavaBeans spec, explain exactly where those parts of the spec require anything. The language is not imperative or normative in those sections. The language in those sections is that of examples, which are at best ambiguous as a specification. Furthermore, the
BeanInfoAPI specifically allows one to change the default behavior, and it is clearly broken in the second example below.
java.beans.PropertyDescriptor 自定义 getter 和 setter 方法。 .使用它的最简单方法是指定 getter 和 setter 的名称。new PropertyDescriptor("foo", MyClass.class, "getFoo", "setFoo");
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import org.testng.annotations.*;
/**
* Shows what has worked up until JDK 1.7.
*/
public class PropertyDescriptorTest
{
private int i;
public int getI() { return i; }
// A setter that my people call "fluent".
public PropertyDescriptorTest setI(final int i) { this.i = i; return this; }
@Test
public void fluentBeans() throws IntrospectionException
{
// This throws an exception only in JDK 1.7.
final PropertyDescriptor pd = new PropertyDescriptor("i",
PropertyDescriptorTest.class, "getI", "setI");
assert pd.getReadMethod() != null;
assert pd.getWriteMethod() != null;
}
}
BeanInfo s,允许对 PropertyDescriptor 进行编程控制Java Beans 规范中的所有 setter 都使用 void 返回类型,但规范中没有任何内容表明这些示例是规范的,现在这个低级实用程序的行为在新的 Java 类中发生了变化,这恰好被破坏了我正在处理的一些代码。java.beans 中有很多变化JDK 1.6 和 1.7 之间的包,但导致此测试失败的包似乎在此差异中:@@ -240,11 +289,16 @@
}
if (writeMethodName == null) {
- writeMethodName = "set" + getBaseName();
+ writeMethodName = Introspector.SET_PREFIX + getBaseName();
}
- writeMethod = Introspector.findMethod(cls, writeMethodName, 1,
- (type == null) ? null : new Class[] { type });
+ Class[] args = (type == null) ? null : new Class[] { type };
+ writeMethod = Introspector.findMethod(cls, writeMethodName, 1, args);
+ if (writeMethod != null) {
+ if (!writeMethod.getReturnType().equals(void.class)) {
+ writeMethod = null;
+ }
+ }
try {
setWriteMethod(writeMethod);
} catch (IntrospectionException ex) {
PropertyDescriptor现在还检查返回类型以查看它是否为空,因此不再使用流畅的 setter。 PropertyDescriptor抛出一个 IntrospectionException在这种情况下:“找不到方法:setI”。PropertyDescriptor 中指定 getter 和 setter 方法的另一种方法用于定制 BeanInfo是使用实际Method对象:@Test
public void fluentBeansByMethod()
throws IntrospectionException, NoSuchMethodException
{
final Method readMethod = PropertyDescriptorTest.class.getMethod("getI");
final Method writeMethod = PropertyDescriptorTest.class.getMethod("setI",
Integer.TYPE);
final PropertyDescriptor pd = new PropertyDescriptor("i", readMethod,
writeMethod);
assert pd.getReadMethod() != null;
assert pd.getWriteMethod() != null;
}
PropertyDescriptor 时,唯一表明出现问题的迹象是. setter 为空,大多数实用程序代码都认为该属性是只读的。PropertyDescriptor.getWriteMethod() 内.它在 SoftReference 时执行持有实际的二传手 Method是空的。此代码由 PropertyDescriptor 调用第一个示例中的构造函数采用上述访问器方法名称,因为最初没有 Method保存在 SoftReference s 持有实际的 getter 和 setter。SoftReference 中。 PropertyDescriptor 中的对象通过构造函数,首先这些将包含对 readMethod 的引用。和 writeMethod setter和getter Method s 给构造函数。如果在某些时候这些软引用被清除,因为垃圾收集器被允许(并且会这样做),那么 getWriteMethod()代码会看到 SoftReference返回 null,它会尝试发现 setter。这一次,在 PropertyDescriptor 中使用相同的代码路径导致第一个示例在 JDK 1.7 中失败,它将设置写入 Method至 null因为返回类型不是 void . (返回类型不是 Java method signature 的一部分。)BeanInfo 时,行为会随着时间的推移而改变可能会非常困惑。试图复制导致垃圾收集器清除那些特定的条件 SoftReferences也很乏味(尽管一些仪器模拟可能会有所帮助。)ExtendedBeanInfo类具有与上述类似的测试。这是来自 ExtendedBeanInfoTest 的实际 Spring 3.1.1 单元测试这将在单元测试模式下通过,但被测试的代码将在后 GC 阴险模式下失败:@Test
public void nonStandardWriteMethodOnly() throws IntrospectionException {
@SuppressWarnings("unused") class C {
public C setFoo(String foo) { return this; }
}
BeanInfo bi = Introspector.getBeanInfo(C.class);
ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi);
assertThat(hasReadMethodForProperty(bi, "foo"), is(false));
assertThat(hasWriteMethodForProperty(bi, "foo"), is(false));
assertThat(hasReadMethodForProperty(ebi, "foo"), is(false));
assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true));
}
最佳答案
看起来规范没有改变(它需要 void setter)但实现已经更新为只允许 void setter。
规范:
http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html
更具体地说,请参阅第 7.1 节(访问器方法)和第 8.3 节(简单属性的设计模式)
请参阅此 stackoverflow 问题中的一些稍后答案:
Does Java bean's setter permit return this?
关于java - 为什么 PropertyDescriptor 行为从 Java 1.6 更改为 1.7?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10806895/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象