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
138
langtools/test/tools/javac/lambda/funcInterfaces/Helper.java
Normal file
138
langtools/test/tools/javac/lambda/funcInterfaces/Helper.java
Normal file
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*SAM types:
|
||||
1. An interface that has a single abstract method
|
||||
2. Having more than one distinct methods, but only one is "real", the others are overriden public methods in Object - example: Comparator<T>
|
||||
3. Having more than one methods due to inheritance, but they have the same signature
|
||||
4. Having more than one methods due to inheritance, but one of them has a subsignature of all other methods
|
||||
a) parameter types compatible
|
||||
b) return type substitutable
|
||||
c) thrown type not conflicting with the thrown clause of any other method
|
||||
d) mixed up
|
||||
5. Type-dependent SAM types
|
||||
non-SAM types:
|
||||
6. An interface that has a single abstract method, which is also public method in Object
|
||||
7. Having more than one methods due to inheritance, and none of them has a subsignature of all other methods
|
||||
*/
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLTransientException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.io.*;
|
||||
|
||||
interface A {int getOldest(List<Number> list);}
|
||||
interface B {int getOldest(List list);}
|
||||
interface C {int getOldest(List<?> list);}
|
||||
interface D {int getOldest(List<Integer> list);}
|
||||
interface E {int getOldest(Collection<?> collection);}
|
||||
//Not SAM type, case #7
|
||||
interface DE extends D, E {}
|
||||
|
||||
interface Foo {int getAge(Number n);}
|
||||
interface Bar {int getAge(Integer i);}
|
||||
//Not SAM type, case #7
|
||||
interface FooBar extends Foo, Bar {}
|
||||
|
||||
//Not SAM type, case #6
|
||||
interface Planet {boolean equals(Object o);}
|
||||
|
||||
// SAM type interfaces:
|
||||
// type #2:
|
||||
//only one abstract non-Ojbect method getAge()
|
||||
interface Mars<T> extends Planet {int getAge(T t);}
|
||||
//only one abstract non-Ojbect method increment()
|
||||
interface Jupiter {
|
||||
boolean equals(Object o);
|
||||
String toString();
|
||||
int increment(int i);
|
||||
}
|
||||
|
||||
// type #3:
|
||||
interface X {int getTotal(List<String> arg);}
|
||||
interface Y {int getTotal(List<String> strs);}
|
||||
//SAM type ([List<String>], int, {})
|
||||
interface XY extends X, Y {}
|
||||
//SAM type ([List<String>], int, {})
|
||||
interface XYZ extends X, Y, XY {}
|
||||
|
||||
// type #4 a):
|
||||
//SAM type ([List], int, {})
|
||||
interface AB extends A, B {}
|
||||
|
||||
// type #4 b):
|
||||
interface F {Number getValue(String str);}
|
||||
interface G {Integer getValue(String str);}
|
||||
interface H {Serializable getValue(String str);}
|
||||
interface I {Object getValue(String str);}
|
||||
//SAM type ([String], Integer, {})
|
||||
interface FGHI extends F, G, H, I {}
|
||||
|
||||
interface J {List<Number> getAll(String str);}
|
||||
interface K {List<?> getAll(String str);}
|
||||
interface L {List getAll(String str);}
|
||||
interface M {Collection getAll(String str);}
|
||||
//SAM type ([String], List<Number>/List, {}) - the return type is flexible to some degree
|
||||
interface JK extends J, K {}
|
||||
//SAM type ([String], List<Number>/List, {})
|
||||
interface JL extends J, L {}
|
||||
//SAM type ([String], List<Number>/List, {})
|
||||
interface JKL extends J, K, L {}
|
||||
//SAM type ([String], List<Number>/List, {})
|
||||
interface JKLM extends J, K, L, M {}
|
||||
|
||||
// type #4 c):
|
||||
interface N {String getText(File f) throws IOException;}
|
||||
interface O {String getText(File f) throws FileNotFoundException;}
|
||||
interface P {String getText(File f) throws NullPointerException;}
|
||||
//SAM type ([File], String, {FileNotFoundException})
|
||||
interface NO extends N, O {}
|
||||
//SAM type ([File], String, {})
|
||||
interface NOP extends N, O, P {}
|
||||
|
||||
interface Boo {int getAge(String s) throws IOException;}
|
||||
interface Doo {int getAge(String s) throws SQLException;}
|
||||
//SAM type ([String], int, {})
|
||||
interface BooDoo extends Boo, Doo {}
|
||||
|
||||
// type #4 d):
|
||||
interface Q {Iterable m(Iterable<String> arg);}
|
||||
interface R {Iterable<String> m(Iterable arg);}
|
||||
//SAM type ([Iterable], Iterable<String>/Iterable, {})
|
||||
interface QR extends Q, R {}
|
||||
|
||||
interface U {Collection foo(List<String> arg) throws IOException, SQLTransientException;}
|
||||
interface V {List<?> foo(List<String> arg) throws EOFException, SQLException, TimeoutException;}
|
||||
interface W {List<String> foo(List arg) throws Exception;}
|
||||
//SAM type ([List<String>], List<String>/List, {EOFException, SQLTransientException})
|
||||
interface UV extends U, V {}
|
||||
// SAM type ([List], List<String>/List, {EOFException, SQLTransientException})
|
||||
interface UVW extends U, V, W {}
|
||||
|
||||
// type #5:
|
||||
// Not a SAM because sam-ness depends on instantiation of type-variables
|
||||
interface Qoo<T> {void m(T arg);}
|
||||
interface Roo<S extends Number> {void m(S arg);}
|
||||
interface QooRoo<T1, T2 extends Number, T3> extends Qoo<T1>, Roo<T2> {}
|
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* 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
|
||||
* This test is for lambda expressions
|
||||
* @compile LambdaTest1.java
|
||||
* @run main LambdaTest1
|
||||
*/
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LambdaTest1 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
LambdaTest1 test = new LambdaTest1();
|
||||
|
||||
test.method2((int n) -> { });
|
||||
test.method2((int n) -> { });
|
||||
test.method2((int n) -> { return; }); // ";" is mandatory here
|
||||
test.method2((int n) -> { System.out.println(n); }); // ";" is optional here
|
||||
test.method2(n -> { System.out.println(n); }); //warning, explict type required for n?
|
||||
|
||||
test.method3(()-> { System.out.println("implementing VoidVoid.vvMethod()"); });
|
||||
test.method3(() -> {});
|
||||
|
||||
test.method4(()-> 42);
|
||||
test.method4(()-> { return 42; });//";" is mandatory here
|
||||
|
||||
test.method5((int n)-> n+1);
|
||||
test.method5((int n) -> 42);
|
||||
test.method5((int n) -> { return 42; });
|
||||
test.method5(
|
||||
(int n) -> { //"{" optional here
|
||||
if(n > 0)
|
||||
return n++;
|
||||
else
|
||||
return n--;
|
||||
}
|
||||
);
|
||||
|
||||
Runnable r = ()-> { System.out.println("Runnable.run() method implemented"); };
|
||||
r.run();
|
||||
((Runnable)()-> { System.out.println("Runnable.run() method implemented"); }).run();
|
||||
}
|
||||
|
||||
void method2(VoidInt a) {
|
||||
System.out.println("method2()");
|
||||
final int N = 1;
|
||||
int n = 2; //effectively final variable
|
||||
System.out.println("method2() \"this\":" + this);
|
||||
((Runnable)
|
||||
()->{
|
||||
System.out.println("inside lambda \"this\":" + this);
|
||||
System.out.println("inside lambda accessing final variable N:" + N);
|
||||
System.out.println("inside lambda accessing effectively final variable n:" + n);
|
||||
}
|
||||
).run();
|
||||
//n++; //compile error if n is modified
|
||||
a.viMethod(2);
|
||||
}
|
||||
|
||||
void method3(VoidVoid a) {
|
||||
System.out.println("method3()");
|
||||
a.vvMethod();
|
||||
}
|
||||
|
||||
void method4(IntVoid a) {
|
||||
System.out.println("method4()");
|
||||
System.out.println(a.ivMethod());
|
||||
}
|
||||
|
||||
void method5(IntInt a) {
|
||||
System.out.println("method5()");
|
||||
System.out.println(a.iiMethod(5));
|
||||
}
|
||||
|
||||
|
||||
//SAM type interfaces
|
||||
interface VoidInt {
|
||||
void viMethod(int n);
|
||||
}
|
||||
|
||||
interface VoidVoid {
|
||||
void vvMethod();
|
||||
}
|
||||
|
||||
interface IntVoid {
|
||||
int ivMethod();
|
||||
}
|
||||
|
||||
interface IntInt {
|
||||
int iiMethod(int n);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* This test is to verify invalid lambda expressions
|
||||
* @compile/fail/ref=LambdaTest1_neg1.out -XDrawDiagnostics LambdaTest1_neg1.java
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class LambdaTest1_neg1 {
|
||||
void method() {
|
||||
Comparator<Number> c = (Number n1, Number n2) -> { 42; } //compile error, not a statement
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
LambdaTest1_neg1.java:13:60: compiler.err.not.stmt
|
||||
LambdaTest1_neg1.java:13:65: compiler.err.expected: ';'
|
||||
2 errors
|
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* This test is to verify mis-use of accessing "this" from within lambda expression
|
||||
* @compile/fail/ref=LambdaTest1_neg2.out -XDrawDiagnostics LambdaTest1_neg2.java
|
||||
*/
|
||||
|
||||
public class LambdaTest1_neg2 {
|
||||
static void method() {
|
||||
((Runnable)
|
||||
()-> {
|
||||
Object o = this; //use "this" inside lambda expression which is inside a static method, not allowed
|
||||
}
|
||||
).run();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
LambdaTest1_neg2.java:13:28: compiler.err.non-static.cant.be.ref: kindname.variable, this
|
||||
1 error
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* This test is to verify mis-use of capturing local variable within lambda expression
|
||||
* @compile/fail/ref=LambdaTest1_neg3.out -XDrawDiagnostics LambdaTest1_neg3.java
|
||||
*/
|
||||
|
||||
public class LambdaTest1_neg3 {
|
||||
void method() {
|
||||
int n = 2; //effectively final variable
|
||||
((Runnable)
|
||||
()-> {
|
||||
int n2 = n; //inside lambda accessing effectively final variable;
|
||||
}
|
||||
).run();
|
||||
n++; //compile error if n is modified
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
LambdaTest1_neg3.java:14:26: compiler.err.cant.ref.non.effectively.final.var: n, (compiler.misc.lambda)
|
||||
1 error
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* 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
|
||||
* This test is for identifying SAM types 2 and 3, see Helper.java for SAM types
|
||||
* @compile LambdaTest2_SAM1.java Helper.java
|
||||
* @run main LambdaTest2_SAM1
|
||||
*/
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.io.*;
|
||||
|
||||
public class LambdaTest2_SAM1 {
|
||||
private static List<String> strs = new ArrayList<String>();
|
||||
private static List<File> files = new ArrayList<File>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
strs.add("copy");
|
||||
strs.add("paste");
|
||||
strs.add("delete");
|
||||
strs.add("rename");
|
||||
|
||||
files.add(new File("a.txt"));
|
||||
files.add(new File("c.txt"));
|
||||
files.add(new File("b.txt"));
|
||||
|
||||
//type #2: Comparator<T>
|
||||
Collections.sort(files, (File f1, File f2) -> f1.getName().compareTo(f2.getName()));
|
||||
for(File f : files)
|
||||
System.out.println(f.getName());
|
||||
System.out.println();
|
||||
Collections.sort(files, (File f1, File f2) -> (int)(f1.length() - f2.length()));
|
||||
for(File f : files)
|
||||
System.out.println(f.getName() + " " + f.length());
|
||||
System.out.println();
|
||||
|
||||
LambdaTest2_SAM1 test = new LambdaTest2_SAM1();
|
||||
|
||||
//type #2:
|
||||
test.methodMars((File f) -> {
|
||||
System.out.println("implementing Mars<File>.getAge(File f)...");
|
||||
return (int)f.length();
|
||||
});
|
||||
test.methodJupiter((int n) -> n+1);
|
||||
|
||||
//type #3:
|
||||
test.methodXY((List<String> strList) -> strList.size() );
|
||||
test.methodXYZ((List<String> strList) -> 20 );
|
||||
}
|
||||
|
||||
//type #2:
|
||||
void methodMars(Mars<File> m) {
|
||||
System.out.println("methodMars(): SAM type interface Mars object instantiated: " + m);
|
||||
System.out.println(m.getAge(new File("a.txt")));
|
||||
}
|
||||
|
||||
//type #2:
|
||||
void methodJupiter(Jupiter j) {
|
||||
System.out.println("methodJupiter(): SAM type interface Jupiter object instantiated: " + j);
|
||||
System.out.println(j.increment(33));
|
||||
}
|
||||
|
||||
//type #3:
|
||||
void methodXY(XY xy) {
|
||||
System.out.println("methodXY(): SAM type interface XY object instantiated: " + xy);
|
||||
System.out.println(xy.getTotal(strs));
|
||||
}
|
||||
|
||||
//type #3:
|
||||
void methodXYZ(XYZ xyz) {
|
||||
System.out.println("methodXYZ(): SAM type interface XYZ object instantiated: " + xyz);
|
||||
System.out.println(xyz.getTotal(strs));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,225 @@
|
|||
/*
|
||||
* 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
|
||||
* This test is for identifying SAM types #4, see Helper.java for SAM types
|
||||
* @compile LambdaTest2_SAM2.java Helper.java
|
||||
* @run main LambdaTest2_SAM2
|
||||
*/
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.io.*;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLTransientException;
|
||||
|
||||
public class LambdaTest2_SAM2 {
|
||||
private static List<String> strs = new ArrayList<String>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
strs.add("copy");
|
||||
strs.add("paste");
|
||||
strs.add("delete");
|
||||
strs.add("rename");
|
||||
|
||||
LambdaTest2_SAM2 test = new LambdaTest2_SAM2();
|
||||
|
||||
//type #4 a):
|
||||
test.methodAB((List list) -> 100);
|
||||
|
||||
//type #4 b):
|
||||
test.methodFGHI((String s) -> new Integer(22));
|
||||
//type #4 b):
|
||||
test.methodJK((String s) -> new ArrayList<Number>());
|
||||
test.methodJK((String s) -> new ArrayList());
|
||||
//type #4 b):
|
||||
test.methodJL((String s) -> new ArrayList<Number>());
|
||||
test.methodJL((String s) -> new ArrayList());
|
||||
//type #4 b):
|
||||
test.methodJKL((String s) -> new ArrayList<Number>());
|
||||
test.methodJKL((String s) -> new ArrayList());
|
||||
//type #4 b):
|
||||
test.methodJKLM((String s) -> new ArrayList<Number>());
|
||||
test.methodJKLM((String s) -> new ArrayList());
|
||||
|
||||
// tyep #4 c):
|
||||
test.methodNO((File f) -> {
|
||||
String temp = null;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
try
|
||||
{
|
||||
BufferedReader br = new BufferedReader(new FileReader(f));
|
||||
while((temp=br.readLine()) != null)
|
||||
sb.append(temp).append("\n");
|
||||
}
|
||||
catch(FileNotFoundException fne){throw fne;}
|
||||
catch(IOException e){e.printStackTrace();}
|
||||
return sb.toString();
|
||||
});
|
||||
// tyep #4 c):
|
||||
test.methodNOP((File f) -> {
|
||||
String temp = null;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
try
|
||||
{
|
||||
BufferedReader br = new BufferedReader(new FileReader(f));
|
||||
while((temp=br.readLine()) != null)
|
||||
sb.append(temp).append("\n");
|
||||
}
|
||||
catch(IOException e){e.printStackTrace();}
|
||||
return sb.toString();
|
||||
});
|
||||
// type #4 c):
|
||||
test.methodBooDoo((String s) -> s.length());
|
||||
|
||||
//type #4 d):
|
||||
test.methodQR((Iterable i) -> new ArrayList<String>());
|
||||
test.methodQR((Iterable i) -> new ArrayList());
|
||||
//type #4 d):
|
||||
test.methodUV((List<String> list) -> {
|
||||
test.exceptionMethod1();
|
||||
test.exceptionMethod2();
|
||||
return new ArrayList<String>();
|
||||
});
|
||||
test.methodUV((List<String> list) -> {
|
||||
test.exceptionMethod1();
|
||||
test.exceptionMethod2();
|
||||
return new ArrayList();
|
||||
});
|
||||
//type #4 d):
|
||||
test.methodUVW((List list) -> {
|
||||
test.exceptionMethod1();
|
||||
test.exceptionMethod2();
|
||||
return new ArrayList<String>();
|
||||
});
|
||||
test.methodUVW((List list) -> {
|
||||
test.exceptionMethod1();
|
||||
test.exceptionMethod2();
|
||||
return new ArrayList();
|
||||
});
|
||||
}
|
||||
|
||||
private void exceptionMethod1() throws EOFException{
|
||||
}
|
||||
|
||||
private void exceptionMethod2() throws SQLTransientException{
|
||||
}
|
||||
|
||||
//type #4 a): SAM type ([List], int, {})
|
||||
void methodAB (AB ab) {
|
||||
System.out.println("methodAB(): SAM type interface AB object instantiated: " + ab);
|
||||
System.out.println(ab.getOldest(strs));
|
||||
}
|
||||
|
||||
//type #4 b): SAM type ([String], Integer, {})
|
||||
void methodFGHI(FGHI f) {
|
||||
System.out.println("methodFGHI(): SAM type interface FGHI object instantiated: " + f);
|
||||
System.out.println(f.getValue("str"));
|
||||
}
|
||||
|
||||
//type #4 b): SAM type ([String], List<Number>, {})
|
||||
void methodJK(JK jk) {
|
||||
System.out.println("methodJK(): SAM type interface JK object instantiated: " + jk);
|
||||
for(Number n : jk.getAll("in"))
|
||||
System.out.println(n);
|
||||
}
|
||||
|
||||
//type #4 b): SAM type ([String], List<Number>, {})
|
||||
void methodJL(JL jl) {
|
||||
System.out.println("methodJL(): SAM type interface JL object instantiated: " + jl);
|
||||
for(Number n : ((J)jl).getAll("in")) //cast should be redundant - see 7062745
|
||||
System.out.println(n);
|
||||
}
|
||||
|
||||
//type #4 b): SAM type ([String], List<Number>, {})
|
||||
void methodJKL(JKL jkl) { //commented - see 7062745
|
||||
System.out.println("methodJKL(): SAM type interface JKL object instantiated: " + jkl);
|
||||
for(Number n : ((J)jkl).getAll("in"))
|
||||
System.out.println(n);
|
||||
}
|
||||
|
||||
//type #4 b): SAM type ([String], List<Number>, {})
|
||||
void methodJKLM(JKLM jklm) { //commented - see 7062745
|
||||
System.out.println("methodJKLM(): SAM type interface JKLM object instantiated: " + jklm);
|
||||
for(Number n : ((J)jklm).getAll("in"))
|
||||
System.out.println(n);
|
||||
}
|
||||
|
||||
//type #4 c): SAM type ([File], String, {FileNotFoundException})
|
||||
void methodNO(NO no) {
|
||||
System.out.println("methodNO(): SAM type interface \"NO\" object instantiated: " + no);
|
||||
try {
|
||||
System.out.println("text=" + no.getText(new File("a.txt")));
|
||||
System.out.println("got here, no exception thrown");
|
||||
}
|
||||
catch(FileNotFoundException e){e.printStackTrace();}
|
||||
}
|
||||
|
||||
//type #4 c): SAM type ([File]), String, {})
|
||||
void methodNOP(NOP nop) {
|
||||
System.out.println("methodNOP(): SAM type interface \"NOP\" object instantiated: " + nop);
|
||||
System.out.println("text=" + nop.getText(new File("a.txt")));
|
||||
}
|
||||
|
||||
//type #4 c): SAM type ([String], int, {})
|
||||
void methodBooDoo(BooDoo bd) {
|
||||
System.out.println("methodBooDoo(): SAM type interface BooDoo object instantiated: " + bd);
|
||||
System.out.println("result=" + bd.getAge("lambda"));
|
||||
}
|
||||
|
||||
//type #4 d): SAM type ([Iterable], Iterable<String>, {})
|
||||
void methodQR(QR qr) {
|
||||
System.out.println("methodQR(): SAM type interface QR object instantiated: " + qr);
|
||||
System.out.println("Iterable returned: " + qr.m(new SQLException()));
|
||||
}
|
||||
|
||||
//type #4 d): SAM type ([List<String>], List<String>/List, {EOFException, SQLTransientException})
|
||||
void methodUV(UV uv) {
|
||||
System.out.println("methodUV(): SAM type interface UV object instantiated: " + uv);
|
||||
try{
|
||||
System.out.println("result returned: " + uv.foo(strs));
|
||||
}catch(EOFException e){
|
||||
System.out.println(e.getMessage());
|
||||
}catch(SQLTransientException ex){
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
//type #4 d): SAM type ([List], List<String>/List, {EOFException, SQLTransientException})
|
||||
void methodUVW(UVW uvw) {
|
||||
System.out.println("methodUVW(): SAM type interface UVW object instantiated: " + uvw);
|
||||
try{
|
||||
System.out.println("passing List<String>: " + uvw.foo(strs));
|
||||
System.out.println("passing List: " + uvw.foo(new ArrayList()));
|
||||
}catch(EOFException e){
|
||||
System.out.println(e.getMessage());
|
||||
}catch(SQLTransientException ex){
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* 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
|
||||
* This test is for identifying SAM types #5 and instantiating non-SAM types #7 through inner class,
|
||||
see Helper.java for SAM types
|
||||
* @compile LambdaTest2_SAM3.java Helper.java
|
||||
* @run main LambdaTest2_SAM3
|
||||
*/
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LambdaTest2_SAM3 {
|
||||
private static List<String> strs = new ArrayList<String>();
|
||||
private static List<Integer> integers = new ArrayList<Integer>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
LambdaTest2_SAM3 test = new LambdaTest2_SAM3();
|
||||
|
||||
//type #7, Not SAM-convertible, through inner class only:
|
||||
test.methodFooBar(new FooBar() {
|
||||
public int getAge(Number n) {
|
||||
System.out.println("getAge(Number n) called");
|
||||
return 100;
|
||||
}
|
||||
public int getAge(Integer i) {
|
||||
System.out.println("getAge(Integer i) called");
|
||||
return 200;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
//type #7:
|
||||
test.methodDE(new DE(){
|
||||
public int getOldest(List<Integer > list) {
|
||||
System.out.println("getOldest(List<Integer> list) called");
|
||||
return 100;
|
||||
}
|
||||
public int getOldest(Collection<?> collection) {
|
||||
System.out.println("getOldest(Collection<?> collection) called");
|
||||
return 200;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
//type #7: Not SAM type
|
||||
void methodFooBar(FooBar fb) {
|
||||
System.out.println("methodFooBar(): interface FooBar object instantiated: " + fb);
|
||||
System.out.println("result=" + fb.getAge(new Byte("10")));
|
||||
System.out.println("result=" + fb.getAge(new Integer(10)));
|
||||
}
|
||||
|
||||
//type #7: Not SAM type
|
||||
void methodDE (DE de) {
|
||||
System.out.println("methodDE(): interface DE object instantiated: " + de);
|
||||
System.out.println(de.getOldest(integers));
|
||||
System.out.println(de.getOldest(strs));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* This test is for identifying SAM types #5 and instantiating non-SAM types #7 through inner class,
|
||||
see Helper.java for SAM types
|
||||
* @compile/fail/ref=LambdaTest2_neg1.out -XDrawDiagnostics LambdaTest2_neg1.java Helper.java
|
||||
*/
|
||||
|
||||
public class LambdaTest2_neg1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
LambdaTest2_neg1 test = new LambdaTest2_neg1();
|
||||
//not convertible - QooRoo is not a SAM
|
||||
test.methodQooRoo((Integer i) -> { });
|
||||
}
|
||||
|
||||
void methodQooRoo(QooRoo<Integer, Integer, Void> qooroo) { }
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
LambdaTest2_neg1.java:15:13: compiler.err.cant.apply.symbol: kindname.method, methodQooRoo, QooRoo<java.lang.Integer,java.lang.Integer,java.lang.Void>, @531, kindname.class, LambdaTest2_neg1, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, QooRoo)))
|
||||
1 error
|
|
@ -0,0 +1,14 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* This test is for identifying a non-SAM type 6: An interface that has a single abstract method, which is also public method in Object
|
||||
* @compile/fail/ref=NonSAM1.out -XDrawDiagnostics NonSAM1.java Helper.java
|
||||
*/
|
||||
|
||||
public class NonSAM1 {
|
||||
void method() {
|
||||
Planet n = (Object o) -> true;
|
||||
System.out.println("never reach here " + n);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
NonSAM1.java:11:20: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.no.abstracts: kindname.interface, Planet))
|
||||
1 error
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* This test is for identifying a non-SAM type: Having more than one methods due to inheritance, and none of them has a subsignature of all other methods
|
||||
* @compile/fail/ref=NonSAM2.out -XDrawDiagnostics NonSAM2.java Helper.java
|
||||
*/
|
||||
|
||||
import java.util.List;
|
||||
|
||||
interface Foo1 { int getAge(String s);}
|
||||
interface Bar1 { Integer getAge(String s);}
|
||||
interface Foo1Bar1 extends Foo1, Bar1 {} //types Bar1 and Foo1 are incompatible; both define getAge(String), but with unrelated return types
|
||||
|
||||
interface AC extends A, C {} //name clash: getOldest(List<?>) in C and getOldest(List<Number>) in A have the same erasure, yet neither overrides the other
|
||||
interface ABC extends A, B, C {} //name clash: getOldest(List<?>) in C and getOldest(List<Number>) in A have the same erasure, yet neither overrides the other
|
||||
interface AD extends A, D {} //name clash: getOldest(List<Integer>) in D and getOldest(List<Number>) in A have the same erasure, yet neither overrides the other
|
||||
|
||||
interface Foo2<T> { void m(T arg);}
|
||||
interface Bar2<S> { void m(S arg);}
|
||||
interface Foo2Bar2<T1, T2> extends Foo2<T1>, Bar2<T2> {} //name clash: m(S) in Bar and m(T) in Foo have the same erasure, yet neither overrides the other
|
|
@ -0,0 +1,6 @@
|
|||
NonSAM2.java:13:1: compiler.err.types.incompatible.diff.ret: Bar1, Foo1, getAge(java.lang.String)
|
||||
NonSAM2.java:15:1: compiler.err.name.clash.same.erasure.no.override: getOldest(java.util.List<?>), C, getOldest(java.util.List<java.lang.Number>), A
|
||||
NonSAM2.java:16:1: compiler.err.name.clash.same.erasure.no.override: getOldest(java.util.List<?>), C, getOldest(java.util.List<java.lang.Number>), A
|
||||
NonSAM2.java:17:1: compiler.err.name.clash.same.erasure.no.override: getOldest(java.util.List<java.lang.Integer>), D, getOldest(java.util.List<java.lang.Number>), A
|
||||
NonSAM2.java:21:1: compiler.err.name.clash.same.erasure.no.override: m(S), Bar2, m(T), Foo2
|
||||
5 errors
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8003280
|
||||
* @summary Add lambda tests
|
||||
* This test is for identifying a non-SAM type: Having overloaded methods due to inheritance
|
||||
* @compile/fail/ref=NonSAM3.out -XDrawDiagnostics NonSAM3.java Helper.java
|
||||
*/
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class NonSAM3 {
|
||||
void method() {
|
||||
//all of the following will have compile error: "the target type of a lambda conversion has multiple non-overriding abstract methods"
|
||||
FooBar fb = (Number n) -> 100;
|
||||
FooBar fb2 = (Integer i) -> 100;
|
||||
DE de = (List<Integer> list) -> 100;
|
||||
DE de2 = (List<?> list) -> 100;
|
||||
DE de3 = (List list) -> 100;
|
||||
DE de4 = (Collection<Integer> collection) -> 100;
|
||||
DE de5 = (Collection<?> collection) -> 100;
|
||||
DE de6 = (Collection collection) -> 100;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
NonSAM3.java:15:21: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, FooBar))
|
||||
NonSAM3.java:16:22: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, FooBar))
|
||||
NonSAM3.java:17:17: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, DE))
|
||||
NonSAM3.java:18:18: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, DE))
|
||||
NonSAM3.java:19:18: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, DE))
|
||||
NonSAM3.java:20:18: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, DE))
|
||||
NonSAM3.java:21:18: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, DE))
|
||||
NonSAM3.java:22:18: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, DE))
|
||||
8 errors
|
Loading…
Add table
Add a link
Reference in a new issue