8040656: Classes with overriden methods with covariant returns return random read methods

Reviewed-by: alexsch, serb
This commit is contained in:
Sergey Malenkov 2014-04-21 20:59:59 +04:00
parent c6886100be
commit 1d1194721b
2 changed files with 322 additions and 2 deletions

View File

@ -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;

View File

@ -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;
}
}
}