mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-17 09:34:38 +02:00
8003280: Add lambda tests
Turn on lambda expression, method reference and default method support Reviewed-by: jjg
This commit is contained in:
parent
c39f1d99b4
commit
a494f0ab86
451 changed files with 15433 additions and 488 deletions
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test bridge methods in certain SAM conversion
|
||||
* @compile BridgeMethod.java
|
||||
* @run main BridgeMethod
|
||||
*/
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class BridgeMethod {
|
||||
|
||||
interface H {Object m();}
|
||||
|
||||
interface K<T> {void m(T t);}
|
||||
|
||||
interface L extends K<String> {} //generic substitution
|
||||
|
||||
interface M {void m(String s);}
|
||||
|
||||
interface KM extends K<String>, M{} //generic substitution
|
||||
|
||||
interface N extends H {String m();} //covariant return
|
||||
|
||||
private static void assertTrue(boolean cond) {
|
||||
if (!cond)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
static void bar(String s) {
|
||||
System.out.println("BridgeMethod.bar(String) " + s);
|
||||
}
|
||||
|
||||
String moo() {
|
||||
return "moo";
|
||||
}
|
||||
|
||||
private static Set<String> setOfStringObject() {
|
||||
Set<String> s = new HashSet<>();
|
||||
s.add("java.lang.String");
|
||||
s.add("java.lang.Object");
|
||||
return s;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
L la = BridgeMethod::bar; //static reference
|
||||
la.m("hi");
|
||||
Class<? extends L> c1 = la.getClass();
|
||||
Method[] methods = c1.getDeclaredMethods();
|
||||
Set<String> types = setOfStringObject();
|
||||
System.out.println("methods in SAM conversion of L:");
|
||||
for(Method m : methods) {
|
||||
System.out.println(m.toGenericString());
|
||||
assertTrue(m.getName().equals("m"));
|
||||
Class[] parameterTypes = m.getParameterTypes();
|
||||
assertTrue(parameterTypes.length == 1);
|
||||
assertTrue(types.remove(parameterTypes[0].getName()));
|
||||
}
|
||||
assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
|
||||
|
||||
KM km = BridgeMethod::bar;
|
||||
//km.m("hi"); //will be uncommented when CR7028808 fixed
|
||||
Class<? extends KM> c2 = km.getClass();
|
||||
methods = c2.getDeclaredMethods();
|
||||
types = setOfStringObject();
|
||||
System.out.println("methods in SAM conversion of KM:");
|
||||
for(Method m : methods) {
|
||||
System.out.println(m.toGenericString());
|
||||
assertTrue(m.getName().equals("m"));
|
||||
Class<?>[] parameterTypes = m.getParameterTypes();
|
||||
assertTrue(parameterTypes.length == 1);
|
||||
assertTrue(types.remove(parameterTypes[0].getName()));
|
||||
}
|
||||
assertTrue(types.isEmpty());
|
||||
|
||||
N n = new BridgeMethod()::moo; //instance reference
|
||||
assertTrue( n.m().equals("moo") );
|
||||
assertTrue( ((H)n).m().equals("moo") );
|
||||
Class<? extends N> c3 = n.getClass();
|
||||
methods = c3.getDeclaredMethods();
|
||||
types = setOfStringObject();
|
||||
System.out.println("methods in SAM conversion of N:");
|
||||
for(Method m : methods) {
|
||||
System.out.println(m.toGenericString());
|
||||
assertTrue(m.getName().equals("m"));
|
||||
Class<?> returnType = m.getReturnType();
|
||||
assertTrue(types.remove(returnType.getName()));
|
||||
}
|
||||
assertTrue(types.isEmpty());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test static method reference
|
||||
* @compile MethodRef1.java
|
||||
* @run main MethodRef1
|
||||
*/
|
||||
|
||||
public class MethodRef1 {
|
||||
|
||||
static interface A {void m();}
|
||||
|
||||
static interface B {void m(int i);}
|
||||
|
||||
static interface C {String m(String s);}
|
||||
|
||||
private static void assertTrue(boolean cond) {
|
||||
if (!cond)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
static void foo(int x) {
|
||||
System.out.println("MethodRef1.foo(int) " + x);
|
||||
}
|
||||
|
||||
static void bar() {
|
||||
System.out.println("MethodRef1.bar()");
|
||||
}
|
||||
|
||||
static void bar(int x) {
|
||||
System.out.println("MethodRef1.bar(int) " + x);
|
||||
}
|
||||
|
||||
static String bar(String s) {
|
||||
return "MethodRef1.bar(String) " + s;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
A a = MethodRef1::bar; //static reference to bar()
|
||||
a.m();
|
||||
|
||||
B b = MethodRef1::foo; //static reference to foo(int), (int) omitted because method foo is not overloaded
|
||||
b.m(1);
|
||||
|
||||
b = MethodRef1::foo; //static reference to foo(int)
|
||||
b.m(1);
|
||||
|
||||
b = new MethodRef1()::foo; //instance reference to static methods, supported for now
|
||||
b.m(1);
|
||||
|
||||
b = MethodRef1::bar; //static reference to bar(int)
|
||||
b.m(2);
|
||||
|
||||
C c = MethodRef1::bar; //static reference to bar(String)
|
||||
assertTrue( c.m("hi").equals("MethodRef1.bar(String) hi") );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test instance method reference
|
||||
* @compile MethodRef2.java
|
||||
* @run main MethodRef2
|
||||
*/
|
||||
|
||||
public class MethodRef2 {
|
||||
|
||||
static interface A {String m();}
|
||||
|
||||
static interface B {String m(int i);}
|
||||
|
||||
private static void assertTrue(boolean cond) {
|
||||
if (!cond)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
String moo() {
|
||||
return "moo";
|
||||
}
|
||||
|
||||
String wahoo() {
|
||||
return "wahoo";
|
||||
}
|
||||
|
||||
String wahoo(int x) {
|
||||
return "wahoo " + x;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
MethodRef2 mr = new MethodRef2();
|
||||
|
||||
A a = mr::moo; //instance reference to moo()
|
||||
assertTrue( a.m().equals("moo") );
|
||||
|
||||
a = new MethodRef2()::wahoo; //instance reference to wahoo()
|
||||
assertTrue( a.m().equals("wahoo") );
|
||||
|
||||
B b = mr::wahoo; //instance reference to wahoo(int)
|
||||
assertTrue( b.m(4).equals("wahoo 4") );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test unbound method reference
|
||||
* @compile MethodRef3.java
|
||||
* @run main MethodRef3
|
||||
*/
|
||||
|
||||
public class MethodRef3 {
|
||||
|
||||
static interface A { String m(MethodRef3 mr); }
|
||||
|
||||
static interface B { String m(MethodRef3 mr, String s); }
|
||||
|
||||
private static void assertTrue(boolean cond) {
|
||||
if (!cond)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
String moo() {
|
||||
return "moo";
|
||||
}
|
||||
|
||||
String wahoo(String s) {
|
||||
return "wahoo " + s;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
MethodRef3 mr = new MethodRef3();
|
||||
A a = MethodRef3::moo; //unbound reference to moo()
|
||||
assertTrue( a.m(mr).equals("moo") );
|
||||
B b = MethodRef3::wahoo; //unbound reference to wahoo()
|
||||
assertTrue( b.m(mr, "hi").equals("wahoo hi") );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test constructor reference
|
||||
* @compile MethodRef4.java
|
||||
* @run main MethodRef4
|
||||
*/
|
||||
|
||||
public class MethodRef4 {
|
||||
|
||||
static interface A {Fee<String> m();}
|
||||
|
||||
static interface B {Fee<String> m(String s);}
|
||||
|
||||
static interface C {Object m();}
|
||||
|
||||
static class Fee<T> {
|
||||
|
||||
private T t;
|
||||
|
||||
public Fee() {
|
||||
System.out.println("Fee<T> instantiated");
|
||||
}
|
||||
|
||||
public Fee(T t) {
|
||||
this.t = t;
|
||||
System.out.println("Fee<T> instantiated: " + t);
|
||||
}
|
||||
|
||||
public void make() {
|
||||
System.out.println(this + ": make()");
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertTrue(boolean cond) {
|
||||
if (!cond)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
A a = Fee<String>::new; //constructor reference to Fee<T>()
|
||||
a.m().make();
|
||||
|
||||
B b = Fee<String>::new; //constructor reference to Fee<T>(String)
|
||||
b.m("hi").make();
|
||||
|
||||
C c = MethodRef4::new; //constructor reference to MethodRef4()
|
||||
assertTrue( c.m() instanceof MethodRef4 );
|
||||
c = MethodRef4::new; //constructor reference to MethodRef4()
|
||||
assertTrue( c.m() instanceof MethodRef4 );
|
||||
c = Fee<String>::new; //constructor reference to Fee<T>()
|
||||
assertTrue( c.m() instanceof Fee );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test method reference with SAM interface Comparator<T>
|
||||
* @compile MethodRef5.java
|
||||
* @run main MethodRef5
|
||||
*/
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MethodRef5 {
|
||||
|
||||
static class Person {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private int age;
|
||||
|
||||
public Person() { }
|
||||
|
||||
public Person(String fn, String ln, int a) {
|
||||
firstName = fn;
|
||||
lastName = ln;
|
||||
age = a;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
//the following 2 methods are signature-compatible with Comparator<Person>.compare():
|
||||
public static int compareByAge(Person a, Person b) {
|
||||
return a.age - b.age;
|
||||
}
|
||||
|
||||
public int compareByLastName(Person a, Person b) {
|
||||
return a.lastName.compareToIgnoreCase(b.lastName);
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertTrue(boolean cond) {
|
||||
if (!cond)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Person> persons = new ArrayList<Person>();
|
||||
persons.add(new Person("John", "Smith", 49));
|
||||
persons.add(new Person("Abraham", "Lincoln", 30));
|
||||
persons.add(new Person("George", "Washington", 29));
|
||||
persons.add(new Person("Peter", "Derby", 50));
|
||||
Collections.sort(persons, Person::compareByAge);//static method reference to compareByAge(Person, Person)
|
||||
String age = "";
|
||||
for (Person p : persons) {
|
||||
age += p.getAge() + " ";
|
||||
}
|
||||
assertTrue( (age.equals("29 30 49 50 ")) );
|
||||
Collections.sort(persons, new Person()::compareByLastName);//instance method reference to compareByLastName(Person, Person)
|
||||
String lastName = "";
|
||||
for (Person p : persons) {
|
||||
lastName += p.getLastName() + " ";
|
||||
}
|
||||
assertTrue( lastName.equals("Derby Lincoln Smith Washington ") );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test that the most specific reference is selected when method parameters are elided
|
||||
* @compile MethodRef6.java
|
||||
* @run main MethodRef6
|
||||
*/
|
||||
|
||||
public class MethodRef6 {
|
||||
|
||||
static interface A { String make(Integer i); }
|
||||
|
||||
static interface B { String make(Number i); }
|
||||
|
||||
private static void assertTrue(boolean cond) {
|
||||
if (!cond)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
static String m(Object o) {
|
||||
return "Object " + o;
|
||||
}
|
||||
|
||||
static String m(Number n) {
|
||||
return "Number " + n;
|
||||
}
|
||||
|
||||
static String m(Integer i) {
|
||||
return "Integer " + i;
|
||||
}
|
||||
|
||||
static String m(int i) {
|
||||
return "int " + i;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
A a = MethodRef6::m;
|
||||
assertTrue(a.make(1).equals("Integer 1"));//method parameter type inferred from SAM descriptor, boxing applied
|
||||
B b = MethodRef6::m;
|
||||
assertTrue(b.make(1).equals("Number 1"));//method parameter type inferred from SAM descriptor, boxing and widen applied
|
||||
}
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test that parameter types are inferred from SAM descriptor when method parameters are elided,
|
||||
with different types of method references
|
||||
* @compile MethodRef7.java
|
||||
* @run main MethodRef7
|
||||
*/
|
||||
|
||||
public class MethodRef7 {
|
||||
|
||||
static interface A {void m();}
|
||||
|
||||
static interface A2 {void m(int n);}
|
||||
|
||||
static interface B {String m();}
|
||||
|
||||
static interface B2 {String m(int n);}
|
||||
|
||||
static interface C {String m(MethodRef7 mr);}
|
||||
|
||||
static interface C2 {String m(MethodRef7 mr, int n);}
|
||||
|
||||
static interface D {Fee<String> m();}
|
||||
|
||||
static interface D2 {Fee<String> m(String s);}
|
||||
|
||||
static class Fee<T> {
|
||||
|
||||
public Fee() {
|
||||
System.out.println("Fee<T> instantiated");
|
||||
}
|
||||
|
||||
public Fee(String s) {
|
||||
System.out.println("Fee<T> instantiated: " + s);
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertTrue(boolean cond) {
|
||||
if (!cond)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
static void bar() {
|
||||
System.out.println("MethodRef_neg1.bar()");
|
||||
}
|
||||
|
||||
static void bar(int x) {
|
||||
System.out.println("MethodRef_neg1.bar(int) " + x);
|
||||
}
|
||||
|
||||
String wahoo() {
|
||||
return "wahoo";
|
||||
}
|
||||
|
||||
String wahoo(int x) {
|
||||
return "wahoo " + x;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
A a = MethodRef7::bar; //static reference to bar()
|
||||
a.m();
|
||||
A2 a2 = MethodRef7::bar; //static reference to bar(int x)
|
||||
a2.m(10);
|
||||
|
||||
MethodRef7 mr = new MethodRef7();
|
||||
B b = mr::wahoo; //instance reference to wahoo()
|
||||
assertTrue(b.m().equals("wahoo"));
|
||||
B2 b2 = mr::wahoo; //instance reference to wahoo(int x)
|
||||
assertTrue(b2.m(1).equals("wahoo 1"));
|
||||
|
||||
C c = MethodRef7::wahoo; //unbound reference to wahoo()
|
||||
assertTrue(c.m(mr).equals("wahoo"));
|
||||
C2 c2 = MethodRef7::wahoo; //unbound reference to wahoo(int x)
|
||||
assertTrue(c2.m(mr, 2).equals("wahoo 2"));
|
||||
|
||||
D d = Fee<String>::new; //constructor reference to Fee()
|
||||
D2 d2 = Fee<String>::new; //constructor reference to Fee(String s)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* This is negative test for wrong parameter/return type in method references
|
||||
* @compile/fail/ref=MethodRef_neg.out -XDrawDiagnostics MethodRef_neg.java
|
||||
*/
|
||||
|
||||
public class MethodRef_neg {
|
||||
|
||||
static interface A {void m(Integer i);}
|
||||
|
||||
static interface B {void m(String s);}
|
||||
|
||||
static interface C {Integer m();}
|
||||
|
||||
static interface D {String m();}
|
||||
|
||||
|
||||
static void bar(int x) { }
|
||||
|
||||
int foo() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
static void make() { }
|
||||
|
||||
void method() {
|
||||
A a = MethodRef_neg::bar; //boxing on parameter type is ok
|
||||
B b = MethodRef_neg::bar; //wrong parameter type, required: String, actual: int
|
||||
C c = this::foo; //boxing on return type is ok
|
||||
D d = this::foo; //wrong return type, required: String, actual: int
|
||||
a = MethodRef_neg::make; //missing parameter
|
||||
c = MethodRef_neg::make; //missing return type
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
MethodRef_neg.java:30:15: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, bar, int, java.lang.String, kindname.class, MethodRef_neg, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, int))))
|
||||
MethodRef_neg.java:32:15: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.mref: (compiler.misc.inconvertible.types: int, java.lang.String))
|
||||
MethodRef_neg.java:33:13: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, make, compiler.misc.no.args, java.lang.Integer, kindname.class, MethodRef_neg, (compiler.misc.arg.length.mismatch)))
|
||||
MethodRef_neg.java:34:13: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.mref: (compiler.misc.inconvertible.types: void, java.lang.Integer))
|
||||
4 errors
|
|
@ -0,0 +1,337 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test SAM conversion of method references in contexts of assignment, method/constructor argument,
|
||||
* return statement, array initializer, lambda expression body, conditional expression and cast.
|
||||
* @compile SamConversion.java
|
||||
* @run main SamConversion
|
||||
*/
|
||||
|
||||
public class SamConversion {
|
||||
|
||||
static int assertionCount = 0;
|
||||
|
||||
static interface Foo {
|
||||
Integer m(int i);
|
||||
}
|
||||
|
||||
static interface Bar {
|
||||
int m(Integer i) throws MyException;
|
||||
}
|
||||
|
||||
private static void assertTrue(boolean b) {
|
||||
assertionCount++;
|
||||
if(!b)
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
private static int test1(Foo foo) {
|
||||
return foo.m(1);
|
||||
}
|
||||
|
||||
private static void test2(Bar bar, int result) {
|
||||
try {
|
||||
assertTrue(bar.m(1) == result);
|
||||
} catch (Exception e){
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static Bar test3(int i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
return A::method1;
|
||||
case 1:
|
||||
return new A()::method2;
|
||||
case 2:
|
||||
return A::method3;
|
||||
case 3:
|
||||
return new A()::method4;
|
||||
case 4:
|
||||
return new A()::method5;
|
||||
case 5:
|
||||
return A::method6;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SAM conversion of method reference in assignment context
|
||||
*/
|
||||
private static void testAssignment() {
|
||||
Foo foo = A::method1; //static reference, parameter type matching and return type matching
|
||||
assertTrue(foo.m(1) == 2);
|
||||
|
||||
foo = new A()::method2; //instance reference, parameter type unboxing and return type boxing
|
||||
assertTrue(foo.m(1) == 3);
|
||||
|
||||
foo = A::method3; //static reference, parameter type matching and return type boxing
|
||||
assertTrue(foo.m(1) == 4);
|
||||
|
||||
foo = new A()::method4; //instance reference, parameter type unboxing and return type matching
|
||||
assertTrue(foo.m(1) == 5);
|
||||
|
||||
foo = new A()::method5; //instance reference, parameter type unboxing and return type matching
|
||||
assertTrue(foo.m(1) == 6);
|
||||
|
||||
Bar bar = A::method1;
|
||||
try {
|
||||
assertTrue(bar.m(1) == 2);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
|
||||
bar = new A()::method2;
|
||||
try {
|
||||
assertTrue(bar.m(1) == 3);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
|
||||
bar = A::method3;
|
||||
try {
|
||||
assertTrue(bar.m(1) == 4);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
|
||||
bar = new A()::method4;
|
||||
try {
|
||||
assertTrue(bar.m(1) == 5);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
|
||||
bar = new A()::method5;
|
||||
try {
|
||||
assertTrue(bar.m(1) == 6);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
|
||||
bar = new A()::method6;
|
||||
try {
|
||||
bar.m(1);
|
||||
assertTrue(false);
|
||||
} catch (MyException e) {
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SAM conversion of method reference in method/constructor argument context
|
||||
*/
|
||||
private static void testMethodArgument() {
|
||||
assertTrue(test1(A::method1) == 2);
|
||||
assertTrue(test1(new A()::method2) == 3);
|
||||
assertTrue(test1(A::method3) == 4);
|
||||
assertTrue(test1(new A()::method4) == 5);
|
||||
assertTrue(test1(new A()::method5) == 6);
|
||||
test2(A::method1, 2);
|
||||
test2(new A()::method2, 3);
|
||||
test2(A::method3, 4);
|
||||
test2(new A()::method4, 5);
|
||||
test2(new A()::method5, 6);
|
||||
A a = new A(A::method1); //A(Foo f) called
|
||||
assertTrue(a.method2(1) == 11);
|
||||
assertTrue(a.method4(1) == 11);
|
||||
assertTrue(a.method5(1) == 11);
|
||||
A a2 = new A(new A()::method2); //A(Bar b) called
|
||||
assertTrue(a2.method2(1) == 12);
|
||||
assertTrue(a2.method4(1) == 12);
|
||||
assertTrue(a2.method5(1) == 12);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SAM conversion of method reference in return statement context
|
||||
*/
|
||||
private static void testReturnStatement() {
|
||||
Bar bar = test3(0);
|
||||
try {
|
||||
assertTrue(bar.m(1) == 2);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
bar = test3(1);
|
||||
try {
|
||||
assertTrue(bar.m(1) == 3);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
bar = test3(2);
|
||||
try {
|
||||
assertTrue(bar.m(1) == 4);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
bar = test3(3);
|
||||
try {
|
||||
assertTrue(bar.m(1) == 5);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
bar = test3(4);
|
||||
try {
|
||||
assertTrue(bar.m(1) == 6);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
bar = test3(5);
|
||||
try {
|
||||
bar.m(1);
|
||||
assertTrue(false);
|
||||
} catch (MyException e) {
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SAM conversion of method reference in cast context
|
||||
*/
|
||||
private static void testCast() {
|
||||
assertTrue(((Foo)A::method1).m(1) == 2);
|
||||
try {
|
||||
assertTrue(((Bar)new A()::method2).m(1) == 3);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SAM conversion of method reference in array initializer context
|
||||
*/
|
||||
private static void testArrayInitializer() {
|
||||
Object[] oarray = {"a", 1, (Foo)A::method3}; //last element need a cast
|
||||
Object[] oarray2 = {"a", 1, (Bar)new A()::method4}; //last element need a cast
|
||||
Foo[] farray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5};
|
||||
Bar[] barray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5, A::method6};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SAM conversion of method reference in conditional expression context
|
||||
*/
|
||||
private static void testConditionalExpression(boolean b) {
|
||||
Foo f = b ? A::method3 : new A()::method5;
|
||||
if(b)
|
||||
assertTrue(f.m(1) == 4);
|
||||
else
|
||||
assertTrue(f.m(1) == 6);
|
||||
|
||||
Bar bar = b ? A::method1 : A::method6;
|
||||
if(b) {
|
||||
try {
|
||||
assertTrue(bar.m(1) == 2);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
bar.m(1);
|
||||
assertTrue(false);
|
||||
} catch (MyException e) {
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SAM conversion of method reference in lambda expression body
|
||||
*/
|
||||
private static void testLambdaExpressionBody() {
|
||||
Foo f = n -> ((Foo)A::method3).m(n);
|
||||
assertTrue(f.m(1) == 4);
|
||||
|
||||
Bar b = n -> { return ((Foo)new A()::method5).m(n); };
|
||||
try {
|
||||
assertTrue(b.m(1) == 6);
|
||||
} catch (Exception e) {
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
testAssignment();
|
||||
testMethodArgument();
|
||||
testReturnStatement();
|
||||
testCast();
|
||||
testArrayInitializer();
|
||||
testConditionalExpression(true);
|
||||
testConditionalExpression(false);
|
||||
testLambdaExpressionBody();
|
||||
|
||||
assertTrue(assertionCount == 38);
|
||||
}
|
||||
|
||||
static class MyException extends Exception {}
|
||||
|
||||
static class A {
|
||||
|
||||
int value = 0;
|
||||
|
||||
A() {
|
||||
}
|
||||
|
||||
A(Foo f) {
|
||||
value = f.m(9);
|
||||
}
|
||||
|
||||
A(Bar b) {
|
||||
try {
|
||||
value = b.m(9);
|
||||
} catch (MyException e){}
|
||||
}
|
||||
|
||||
static Integer method1(int n) {
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
int method2(Integer n) {
|
||||
return value == 0 ? n + 2 : n + value;
|
||||
}
|
||||
|
||||
static int method3(int n) {
|
||||
return n + 3;
|
||||
}
|
||||
|
||||
Integer method4(Integer n) {
|
||||
return value == 0 ? n + 4 : n + value;
|
||||
}
|
||||
|
||||
Integer method5(Integer n) {
|
||||
return value == 0 ? new Integer(n + 5) : new Integer(n + value);
|
||||
}
|
||||
|
||||
static int method6(Integer n) throws MyException{
|
||||
throw new MyException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,265 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* Test SAM conversion of method references in combinations of different contexts,
|
||||
* lambda body types(statement/expression), boxing/unboxing etc, to verify
|
||||
* SAM conversion being conducted successfully as expected.
|
||||
*/
|
||||
|
||||
import com.sun.source.util.JavacTask;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.SimpleJavaFileObject;
|
||||
import javax.tools.ToolProvider;
|
||||
import javax.tools.StandardJavaFileManager;
|
||||
|
||||
public class SamConversionComboTest {
|
||||
|
||||
enum FInterface {
|
||||
A("A", "interface A { Integer m(int i); }"),
|
||||
B("B", "interface B { int m(Integer i); }"),
|
||||
C("C", "interface C { int m(int i) throws Exception; }");
|
||||
|
||||
String interfaceType;
|
||||
String interfaceDef;
|
||||
|
||||
FInterface(String interfaceType, String interfaceDef) {
|
||||
this.interfaceType = interfaceType;
|
||||
this.interfaceDef = interfaceDef;
|
||||
}
|
||||
}
|
||||
|
||||
enum Context {
|
||||
ASSIGNMENT("#FType f = #MR;"),
|
||||
METHOD_CALL("void method1(#FType f) { }\n" +
|
||||
"void method2() {\n" +
|
||||
" method1(#MR);\n" +
|
||||
"}"),
|
||||
CONSTRUCTOR("X x = new X(#MR);"),
|
||||
RETURN_OF_METHOD("#FType method1() {\n" +
|
||||
" return #MR;\n" +
|
||||
"}"),
|
||||
ARRAY_INITIALIZER("#FType[] oarray = {#MR};"),
|
||||
LAMBDA_BODY("#FType f = n -> ((#FType)#MR).m(n);"),
|
||||
CAST("void test() throws Exception { int n = ((#FType)#MR).m(1); }"),
|
||||
CONDITIONAL_EXPRESSION("#FType f = 2 > 1 ? #MR : null;");
|
||||
|
||||
String context;
|
||||
|
||||
Context(String context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
String getContext(FInterface f, MethodReference mr) {
|
||||
return context.replace("#FType", f.interfaceType).replace("#MR", mr.mrValue);
|
||||
}
|
||||
}
|
||||
|
||||
enum MethodReference {
|
||||
METHOD1("X::method1"),
|
||||
METHOD2("new X()::method2"),
|
||||
METHOD3("X::method3"),
|
||||
METHOD4("new X()::method4"),
|
||||
METHOD5("new X()::method5"),
|
||||
METHOD6("X::method6"),
|
||||
METHOD7("X::method7"),
|
||||
METHOD8("X::method8");
|
||||
|
||||
String mrValue;
|
||||
|
||||
MethodReference(String mr) {
|
||||
mrValue = mr;
|
||||
}
|
||||
}
|
||||
|
||||
enum MethodDef {
|
||||
METHOD1(" static Integer method1(int n) {\n" +
|
||||
" return n + 1;\n" +
|
||||
" }\n", 0),
|
||||
METHOD2(" int method2(Integer n) {\n" +
|
||||
" return value == 0 ? n + 2 : n + value;\n" +
|
||||
" }\n", 1),
|
||||
METHOD3(" static int method3(int n) {\n" +
|
||||
" return n + 3;\n" +
|
||||
" }\n", 2),
|
||||
METHOD4(" Integer method4(Integer n) {\n" +
|
||||
" return value == 0 ? n + 4 : n + value;\n" +
|
||||
" }\n", 3),
|
||||
METHOD5(" Integer method5(Integer n) {\n" +
|
||||
" return value == 0 ? new Integer(n + 5) : new Integer(n + value);\n" +
|
||||
" }\n", 4),
|
||||
METHOD6(" static int method6(Integer n) throws Exception{\n" +
|
||||
" throw new Exception();\n" +
|
||||
" }\n", 5),
|
||||
METHOD7(" static int method7(String s){\n" +
|
||||
" return s.length();\n" +
|
||||
" }\n", 6),
|
||||
METHOD8(" static String method8(Integer n){\n" +
|
||||
" return n + \"\";\n" +
|
||||
" }\n", 7);
|
||||
|
||||
String methodStr;
|
||||
int index;
|
||||
|
||||
MethodDef(String ms, int i) {
|
||||
methodStr = ms;
|
||||
index = i;
|
||||
}
|
||||
|
||||
MethodReference getMethodReference() {
|
||||
return MethodReference.values()[index];
|
||||
}
|
||||
}
|
||||
|
||||
SourceFile samSourceFile = new SourceFile("FInterface.java", "#C") {
|
||||
public String toString() {
|
||||
String interfaces = "";
|
||||
for(FInterface fi : FInterface.values())
|
||||
interfaces += fi.interfaceDef + "\n";
|
||||
return template.replace("#C", interfaces);
|
||||
}
|
||||
};
|
||||
|
||||
String clientTemplate = "class Client {\n" +
|
||||
" #Context\n" +
|
||||
"}\n\n" +
|
||||
|
||||
"class X {\n" +
|
||||
" int value = 0;\n\n" +
|
||||
|
||||
" X() {\n" +
|
||||
" }\n\n" +
|
||||
|
||||
" X(A a) {\n" +
|
||||
" value = a.m(9);\n" +
|
||||
" }\n\n" +
|
||||
|
||||
" X(B b) {\n" +
|
||||
" value = b.m(9);\n" +
|
||||
" }\n\n" +
|
||||
|
||||
" X(C c) {\n" +
|
||||
" try {\n" +
|
||||
" value = c.m(9);\n" +
|
||||
" } catch (Exception e){}\n" +
|
||||
" }\n\n" +
|
||||
|
||||
"#MethodDef" +
|
||||
"}";
|
||||
|
||||
SourceFile clientSourceFile = new SourceFile("Client.java", clientTemplate) {
|
||||
public String toString() {
|
||||
return template.replace("#Context", context.getContext(fInterface, methodReference)).replace("#MethodDef", methodDef.methodStr);
|
||||
}
|
||||
};
|
||||
|
||||
boolean checkSamConversion() {
|
||||
if(methodDef == MethodDef.METHOD7 || methodDef == MethodDef.METHOD8)//method signature mismatch
|
||||
return false;
|
||||
if(context != Context.CONSTRUCTOR && fInterface != FInterface.C && methodDef == MethodDef.METHOD6)
|
||||
//method that throws exceptions not thrown by the interface method is a mismatch
|
||||
return false;
|
||||
if(context == Context.CONSTRUCTOR &&
|
||||
methodReference != MethodReference.METHOD1 &&
|
||||
methodReference != MethodReference.METHOD2 &&
|
||||
methodReference != MethodReference.METHOD3)//ambiguous reference
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void test() throws Exception {
|
||||
System.out.println("\n====================================");
|
||||
System.out.println(fInterface + ", " + context + ", " + methodReference);
|
||||
System.out.println(samSourceFile + "\n" + clientSourceFile);
|
||||
|
||||
DiagnosticChecker dc = new DiagnosticChecker();
|
||||
JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
|
||||
ct.analyze();
|
||||
if (dc.errorFound == checkSamConversion()) {
|
||||
throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile);
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
abstract class SourceFile extends SimpleJavaFileObject {
|
||||
|
||||
protected String template;
|
||||
|
||||
public SourceFile(String filename, String template) {
|
||||
super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
|
||||
return toString();
|
||||
}
|
||||
|
||||
public abstract String toString();
|
||||
}
|
||||
|
||||
static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
|
||||
|
||||
boolean errorFound = false;
|
||||
|
||||
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
||||
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
|
||||
errorFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FInterface fInterface;
|
||||
Context context;
|
||||
MethodDef methodDef;
|
||||
MethodReference methodReference;
|
||||
static int count = 0;
|
||||
|
||||
static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
|
||||
static StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
|
||||
|
||||
SamConversionComboTest(FInterface f, Context c, MethodDef md) {
|
||||
fInterface = f;
|
||||
context = c;
|
||||
methodDef = md;
|
||||
methodReference = md.getMethodReference();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
for(Context ct : Context.values()) {
|
||||
for (FInterface fi : FInterface.values()) {
|
||||
for (MethodDef md: MethodDef.values()) {
|
||||
new SamConversionComboTest(fi, ct, md).test();
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("total tests: " + count);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue