From 1d1194721bbf7749899315b2316a31ad143f083a Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Mon, 21 Apr 2014 20:59:59 +0400 Subject: [PATCH] 8040656: Classes with overriden methods with covariant returns return random read methods Reviewed-by: alexsch, serb --- .../classes/java/beans/MethodDescriptor.java | 13 +- .../java/beans/Introspector/Test8040656.java | 311 ++++++++++++++++++ 2 files changed, 322 insertions(+), 2 deletions(-) create mode 100644 jdk/test/java/beans/Introspector/Test8040656.java diff --git a/jdk/src/share/classes/java/beans/MethodDescriptor.java b/jdk/src/share/classes/java/beans/MethodDescriptor.java index da2f754c97e..1e169046746 100644 --- a/jdk/src/share/classes/java/beans/MethodDescriptor.java +++ b/jdk/src/share/classes/java/beans/MethodDescriptor.java @@ -162,6 +162,16 @@ public class MethodDescriptor extends FeatureDescriptor { : null; } + private static Method resolve(Method oldMethod, Method newMethod) { + if (oldMethod == null) { + return newMethod; + } + if (newMethod == null) { + return oldMethod; + } + return !oldMethod.isSynthetic() && newMethod.isSynthetic() ? oldMethod : newMethod; + } + /* * Package-private constructor * Merge two method descriptors. Where they conflict, give the @@ -173,8 +183,7 @@ public class MethodDescriptor extends FeatureDescriptor { MethodDescriptor(MethodDescriptor x, MethodDescriptor y) { super(x, y); - Method method = y.methodRef.get(); - this.methodRef.set(null != method ? method : x.methodRef.get()); + this.methodRef.set(resolve(x.methodRef.get(), y.methodRef.get())); params = x.params; if (y.params != null) { params = y.params; diff --git a/jdk/test/java/beans/Introspector/Test8040656.java b/jdk/test/java/beans/Introspector/Test8040656.java new file mode 100644 index 00000000000..73e80ac377f --- /dev/null +++ b/jdk/test/java/beans/Introspector/Test8040656.java @@ -0,0 +1,311 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.beans.Introspector; +import java.beans.MethodDescriptor; + +/* + * @test + * @bug 8040656 + * @summary Tests that a normal method is preferred to a synthetic one + * @author Sergey Malenkov + */ + +public class Test8040656 { + public static void main(String[] args) throws Exception { + test(String.class, C.class); + test(String.class, C1.class); + test(String.class, C2.class); + test(String.class, C3.class); + test(String.class, C4.class); + test(String.class, C5.class); + test(String.class, C6.class); + test(String.class, C7.class); + test(String.class, C8.class); + test(String.class, C9.class); + } + + private static void test(Class type, Class bean) throws Exception { + for (MethodDescriptor md : Introspector.getBeanInfo(bean).getMethodDescriptors()) { + if (md.getName().equals("getFoo")) { + if (type != md.getMethod().getReturnType()) { + throw new Error("unexpected type"); + } + } + } + } + + public interface A { + public Object getFoo(); + } + + public class C implements A { + @Override + public String getFoo() { + return null; + } + } + + public class C1 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + } + + public class C2 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + + public String getFoo2() { + return null; + } + } + + public class C3 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + + public String getFoo2() { + return null; + } + + public String getFoo3() { + return null; + } + } + + public class C4 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + + public String getFoo2() { + return null; + } + + public String getFoo3() { + return null; + } + + public String getFoo4() { + return null; + } + } + + public class C5 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + + public String getFoo2() { + return null; + } + + public String getFoo3() { + return null; + } + + public String getFoo4() { + return null; + } + + public String getFoo5() { + return null; + } + } + + public class C6 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + + public String getFoo2() { + return null; + } + + public String getFoo3() { + return null; + } + + public String getFoo4() { + return null; + } + + public String getFoo5() { + return null; + } + + public String getFoo6() { + return null; + } + } + + public class C7 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + + public String getFoo2() { + return null; + } + + public String getFoo3() { + return null; + } + + public String getFoo4() { + return null; + } + + public String getFoo5() { + return null; + } + + public String getFoo6() { + return null; + } + + public String getFoo7() { + return null; + } + } + + public class C8 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + + public String getFoo2() { + return null; + } + + public String getFoo3() { + return null; + } + + public String getFoo4() { + return null; + } + + public String getFoo5() { + return null; + } + + public String getFoo6() { + return null; + } + + public String getFoo7() { + return null; + } + + public String getFoo8() { + return null; + } + } + + public class C9 implements A { + @Override + public String getFoo() { + return null; + } + + public String getFoo1() { + return null; + } + + public String getFoo2() { + return null; + } + + public String getFoo3() { + return null; + } + + public String getFoo4() { + return null; + } + + public String getFoo5() { + return null; + } + + public String getFoo6() { + return null; + } + + public String getFoo7() { + return null; + } + + public String getFoo8() { + return null; + } + + public String getFoo9() { + return null; + } + } +}