मेरे एप्लिकेशन में मैं एक समस्या में भाग लेता हूं कि जब किसी क्लास में गेटर केवल इंटरफ़ेस (जावा 8 फीचर) में डिफॉल्ट होता है, तो जावा बीन्स प्रॉपर्टी नहीं होती है परिणाम। अर्थात। सामान्य विधि मंगलाचरण के लिए यह सिर्फ एक मानक पद्धति के रूप में, लेकिन "गुण" यह अचानक अलग ढंग से व्यवहार करती है के माध्यम से उपयोग करने के लिए काम करता है ...जावा 8 इंटरफ़ेस डिफ़ॉल्ट विधि प्रॉपर्टी घोषित नहीं करती है
यहाँ एक परीक्षण मामला है:
import java.beans.Introspector;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.beanutils.PropertyUtils;
public class test
{
public static void main (String[] arguments) throws Exception
{
// Normal language-level invocation, works fine.
System.out.println (new Bean1().getFoo());
System.out.println (new Bean2().getFoo());
// Printing Java Beans properties; Bean2 doesn't have 'foo' property...
System.out.println (Arrays.stream (Introspector.getBeanInfo (Bean1.class).getPropertyDescriptors())
.map ((property) -> property.getName())
.collect (Collectors.joining (", ")));
System.out.println (Arrays.stream (Introspector.getBeanInfo (Bean2.class).getPropertyDescriptors())
.map ((property) -> property.getName())
.collect (Collectors.joining (", ")));
// First call behaves as expected, second dies with exception.
System.out.println (PropertyUtils.getProperty (new Bean1(), "foo"));
System.out.println (PropertyUtils.getProperty (new Bean2(), "foo"));
}
public interface Foo
{
default String getFoo()
{
return "default foo";
}
}
public static class Bean1 implements Foo
{
@Override
public String getFoo()
{
return "special foo";
}
}
public static class Bean2 implements Foo
{ }
}
परिणाम:
special foo
default foo
class, foo
class
special foo
Exception in thread "main" java.lang.NoSuchMethodException: Unknown property 'foo' on class 'class test$Bean2'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1257)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:808)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:884)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:464)
at test.main(test.java:21)
प्रश्न: क्या मैं कुछ गलत करता हूं या जावा में यह एक बग है? क्या किसी भी समय बाद में "संपत्ति" के रूप में उन्हें एक्सेस करने की आवश्यकता हो सकती है, तो डिफॉल्ट विधियों (गेटर्स/सेटर्स के लिए) का उपयोग करने के अलावा कोई कामकाज नहीं है?
मैं हमेशा "सम्मेलन द्वारा गुणों" से नफरत करता हूं जो टूटने लगते हैं क्योंकि आप गलत तरीके से छींकते हैं।
ऐसा लगता है कि यह [जेडीके -8071693] (https://bugs.openjdk.java.net/browse/JDK-8071693) द्वारा कवर किया गया है, अभी तक किसी भी जेडीके रिलीज में तय नहीं है। –
हां, वास्तव में। आशा है कि वे इसे ठीक करें। – doublep
मुझे इस बग द्वारा भी काट दिया गया है। ओपनजेडीके बग जावा 9 के लिए निर्धारित है जिसका मतलब है कि इसे कम से कम सितंबर 2016 तक तय करना होगा। इस बीच मैं उस वर्ग में एक प्रतिनिधि विधि तैयार करूंगा जिसकी संपत्ति की आवश्यकता है। –