Skip to content

Commit b29e108

Browse files
author
Paul Sandoz
committedOct 5, 2020
8253944: Certain method references to VarHandle methods should fail
Reviewed-by: mcimadamore
1 parent 88d75c9 commit b29e108

File tree

5 files changed

+145
-33
lines changed

5 files changed

+145
-33
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java

+19-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -557,37 +557,24 @@ Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
557557
List<Type> argtypes) {
558558
final Type restype;
559559

560-
if (spMethod == null || types.isSameType(spMethod.getReturnType(), syms.objectType)) {
561-
// The return type of the polymorphic signature is polymorphic,
562-
// and is computed from the enclosing tree E, as follows:
563-
// if E is a cast, then use the target type of the cast expression
564-
// as a return type; if E is an expression statement, the return
565-
// type is 'void'; otherwise
566-
// the return type is simply 'Object'. A correctness check ensures
567-
// that env.next refers to the lexically enclosing environment in
568-
// which the polymorphic signature call environment is nested.
569-
570-
switch (env.next.tree.getTag()) {
571-
case TYPECAST:
572-
JCTypeCast castTree = (JCTypeCast)env.next.tree;
573-
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
574-
castTree.clazz.type :
575-
syms.objectType;
576-
break;
577-
case EXEC:
578-
JCTree.JCExpressionStatement execTree =
579-
(JCTree.JCExpressionStatement)env.next.tree;
580-
restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
581-
syms.voidType :
582-
syms.objectType;
583-
break;
584-
default:
585-
restype = syms.objectType;
586-
}
587-
} else {
588-
// The return type of the polymorphic signature is fixed
589-
// (not polymorphic)
590-
restype = spMethod.getReturnType();
560+
Type spType = spMethod == null ? syms.objectType : spMethod.getReturnType();
561+
562+
switch (env.next.tree.getTag()) {
563+
case TYPECAST:
564+
JCTypeCast castTree = (JCTypeCast)env.next.tree;
565+
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
566+
castTree.clazz.type :
567+
spType;
568+
break;
569+
case EXEC:
570+
JCTree.JCExpressionStatement execTree =
571+
(JCTree.JCExpressionStatement)env.next.tree;
572+
restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
573+
syms.voidType :
574+
spType;
575+
break;
576+
default:
577+
restype = spType;
591578
}
592579

593580
List<Type> paramtypes = argtypes.map(new ImplicitArgType(spMethod, resolveContext.step));

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -2742,11 +2742,23 @@ Symbol findPolymorphicSignatureInstance(final Symbol spMethod,
27422742
// Check that there is already a method symbol for the method
27432743
// type and owner
27442744
if (types.isSameType(mtype, sym.type) &&
2745-
spMethod.owner == sym.owner) {
2745+
spMethod.owner == sym.owner) {
27462746
return sym;
27472747
}
27482748
}
27492749

2750+
Type spReturnType = spMethod.asType().getReturnType();
2751+
if (types.isSameType(spReturnType, syms.objectType)) {
2752+
// Polymorphic return, pass through mtype
2753+
} else if (!types.isSameType(spReturnType, mtype.getReturnType())) {
2754+
// Retain the sig poly method's return type, which differs from that of mtype
2755+
// Will result in an incompatible return type error
2756+
mtype = new MethodType(mtype.getParameterTypes(),
2757+
spReturnType,
2758+
mtype.getThrownTypes(),
2759+
syms.methodClass);
2760+
}
2761+
27502762
// Create the desired method
27512763
// Retain static modifier is to support invocations to
27522764
// MethodHandle.linkTo* methods
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @summary test for VarHandle signature polymorphic methods
27+
* @run testng MethodReferenceTestVarHandle
28+
*/
29+
30+
import java.lang.invoke.*;
31+
import java.util.*;
32+
33+
import org.testng.annotations.Test;
34+
import static org.testng.Assert.assertEquals;
35+
36+
@Test
37+
public class MethodReferenceTestVarHandle {
38+
39+
interface Setter {
40+
void apply(int[] arr, int idx, int val);
41+
}
42+
43+
interface Getter {
44+
int apply(int[] arr, int idx);
45+
}
46+
47+
public void testSet() throws Throwable {
48+
VarHandle vh = MethodHandles.arrayElementVarHandle(int[].class);
49+
50+
Setter f = vh::set;
51+
52+
int[] data = {0};
53+
f.apply(data, 0, 42);
54+
assertEquals(42, data[0]);
55+
}
56+
57+
public void testGet() throws Throwable {
58+
VarHandle vh = MethodHandles.arrayElementVarHandle(int[].class);
59+
60+
Getter f = vh::get;
61+
62+
int[] data = {42};
63+
int v = f.apply(data, 0);
64+
assertEquals(42, v);
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @summary test for VarHandle signature polymorphic methods with wrong return type
27+
* @compile/fail/ref=MethodReferenceTestVarHandle_neg.out -XDrawDiagnostics MethodReferenceTestVarHandle_neg.java
28+
*/
29+
30+
import java.lang.invoke.*;
31+
import java.util.*;
32+
33+
public class MethodReferenceTestVarHandle_neg {
34+
35+
interface Setter {
36+
int apply(int[] arr, int idx, int val);
37+
}
38+
39+
public static void main(String[] args) {
40+
VarHandle vh = MethodHandles.arrayElementVarHandle(int[].class);
41+
42+
// Return type of Setter::apply does not match return type of VarHandle::set
43+
Setter f = vh::set;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MethodReferenceTestVarHandle_neg.java:43:18: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.mref: (compiler.misc.inconvertible.types: void, int))
2+
1 error

0 commit comments

Comments
 (0)
Please sign in to comment.