8003280: Add lambda tests

Turn on lambda expression, method reference and default method support

Reviewed-by: jjg
This commit is contained in:
Maurizio Cimadamore 2012-11-17 19:01:03 +00:00
parent c39f1d99b4
commit a494f0ab86
451 changed files with 15433 additions and 488 deletions

View file

@ -0,0 +1,30 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that non-static variables are not accessible from static lambdas
* @author Maurizio Cimadamore
* @compile/fail/ref=BadAccess.out -XDrawDiagnostics BadAccess.java
*/
public class BadAccess {
int i;
static int I;
interface SAM {
int m();
}
static void test1() {
int l = 0; //effectively final
final int L = 0;
SAM s = ()-> i + I + l + L;
}
void test2() {
int l = 0; //effectively final
final int L = 0;
SAM s = ()-> i + I + l + L;
}
}

View file

@ -0,0 +1,2 @@
BadAccess.java:22:22: compiler.err.non-static.cant.be.ref: kindname.variable, i
1 error

View file

@ -0,0 +1,31 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check lambda can access only effectively-final locals
* @author Maurizio Cimadamore
* @compile/fail/ref=BadAccess02.out -XDrawDiagnostics BadAccess02.java
*/
public class BadAccess02 {
interface SAM {
int m(int h);
}
static void test1() {
int l = 0; //effectively final
int j = 0; //non-effectively final
j = 2;
final int L = 0;
SAM s = (int h) -> { int k = 0; return h + j + l + L; };
}
void test2() {
int l = 0; //effectively final
int j = 0; //non-effectively final
j = 2;
final int L = 0;
SAM s = (int h) -> { int k = 0; return h + k + j + l + L; };
}
}

View file

@ -0,0 +1,3 @@
BadAccess02.java:21:52: compiler.err.cant.ref.non.effectively.final.var: j, (compiler.misc.lambda)
BadAccess02.java:29:56: compiler.err.cant.ref.non.effectively.final.var: j, (compiler.misc.lambda)
2 errors

View file

@ -0,0 +1,15 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check lambda cannot assign non-effectively final locals
* @compile/fail/ref=BadAccess03.out -XDrawDiagnostics BadAccess03.java
*/
class BadAccess03 {
void test() {
int k = 0;
int n = 2; //effectively final variable
Runnable r = ()-> { k = n; }; //error
}
}

View file

@ -0,0 +1,2 @@
BadAccess03.java:13:29: compiler.err.cant.ref.non.effectively.final.var: k, (compiler.misc.lambda)
1 error

View file

@ -0,0 +1,44 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that break/continue is disallowed in lambda expressions
* @author Maurizio Cimadamore
* @compile/fail/ref=BadBreakContinue.out -XDrawDiagnostics BadBreakContinue.java
*/
class BadBreakContinue {
static interface SAM {
void m();
}
SAM s1 = ()-> { break; };
SAM s2 = ()-> { continue; };
SAM s3 = ()-> {
SAM s3_1 = ()-> { break; };
SAM s3_2 = ()-> { continue; };
};
void testLabelled() {
loop: while (true) {
SAM s1 = ()-> { break loop; };
SAM s2 = ()-> { continue loop; };
SAM s3 = ()-> {
SAM s3_1 = ()-> { break loop; };
SAM s3_2 = ()-> { continue loop; };
};
}
}
void testNonLabelled() {
while (true) {
SAM s1 = ()-> { break; };
SAM s2 = ()-> { continue; };
SAM s3 = ()-> {
SAM s3_1 = ()-> { break; };
SAM s3_2 = ()-> { continue; };
};
}
}
}

View file

@ -0,0 +1,13 @@
BadBreakContinue.java:16:21: compiler.err.break.outside.switch.loop
BadBreakContinue.java:17:21: compiler.err.cont.outside.loop
BadBreakContinue.java:19:27: compiler.err.break.outside.switch.loop
BadBreakContinue.java:20:27: compiler.err.cont.outside.loop
BadBreakContinue.java:25:29: compiler.err.undef.label: loop
BadBreakContinue.java:26:29: compiler.err.undef.label: loop
BadBreakContinue.java:28:35: compiler.err.undef.label: loop
BadBreakContinue.java:29:35: compiler.err.undef.label: loop
BadBreakContinue.java:36:29: compiler.err.break.outside.switch.loop
BadBreakContinue.java:37:29: compiler.err.cont.outside.loop
BadBreakContinue.java:39:35: compiler.err.break.outside.switch.loop
BadBreakContinue.java:40:35: compiler.err.cont.outside.loop
12 errors

View file

@ -0,0 +1,20 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* NPE while checking if subinterface is a SAM type
* @compile/fail/ref=BadConv03.out -XDrawDiagnostics BadConv03.java
*/
class BadConv03 {
interface A {
void a();
}
interface B extends A { //not a SAM (2 non-override equivalent abstracts!)
void a(int i);
}
B b = ()-> { };
}

View file

@ -0,0 +1,2 @@
BadConv03.java:19:11: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.incompatible.abstracts: kindname.interface, BadConv03.B))
1 error

View file

@ -0,0 +1,22 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that ill-formed SAM type generates right diagnostic when SAM converted
* @compile/fail/ref=BadConv04.out -XDrawDiagnostics BadConv04.java
*/
class BadConv04 {
interface I1 {
int m();
}
interface I2 {
long m();
}
interface SAM extends I1, I2 {}
SAM s = ()-> { };
}

View file

@ -0,0 +1,3 @@
BadConv04.java:19:5: compiler.err.types.incompatible.diff.ret: BadConv04.I2, BadConv04.I1, m()
BadConv04.java:21:13: compiler.err.prob.found.req: (compiler.misc.incompatible.descs.in.functional.intf: kindname.interface, BadConv04.SAM,{(compiler.misc.descriptor: m, , long, ),(compiler.misc.descriptor: m, , int, )})
2 errors

View file

@ -0,0 +1,21 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that a conditonal can't be void
* @compile/fail/ref=BadExpressionLambda.out -XDrawDiagnostics BadExpressionLambda.java
*/
class BadExpressionLambda {
interface SAM {
void invoke();
}
public static void m() {}
void test() {
SAM sam1 = () -> m(); //ok
SAM sam2 = () -> true ? m() : m(); //not ok
}
}

View file

@ -0,0 +1,2 @@
BadExpressionLambda.java:19:31: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.conditional.target.cant.be.void))
1 error

View file

@ -0,0 +1,191 @@
/*
* 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
* compile crashes on partial lambda expressions
*/
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.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class BadLambdaExpr {
static int checkCount = 0;
enum ParameterListKind {
ZERO_ARY("()"),
UNARY("(#P)"),
TWO_ARY("(#P, #P)"),
THREE_ARY("(#P, #P, #P)");
String parametersTemplateStr;
ParameterListKind(String parametersTemplateStr) {
this.parametersTemplateStr = parametersTemplateStr;
}
String getParameterString(ParameterKind pk) {
return parametersTemplateStr.replaceAll("#P", pk.parameterStr);
}
}
enum ParameterKind {
IMPLICIT("a"),
EXPLIICT("A a");
String parameterStr;
ParameterKind(String parameterStr) {
this.parameterStr = parameterStr;
}
}
enum ArrowKind {
NONE(""),
SEMI("-"),
FULL("->");
String arrowStr;
ArrowKind(String arrowStr) {
this.arrowStr = arrowStr;
}
}
enum ExprKind {
NONE("#P#A"),
METHOD_CALL("m(#P#A)"),
CONSTR_CALL("new Foo(#P#A)");
String expressionTemplate;
ExprKind(String expressionTemplate) {
this.expressionTemplate = expressionTemplate;
}
String expressionString(ParameterListKind plk, ParameterKind pk,
ArrowKind ak) {
return expressionTemplate.replaceAll("#P", plk.getParameterString(pk))
.replaceAll("#A", ak.arrowStr);
}
}
public static void main(String... args) throws Exception {
//create default shared JavaCompiler - reused across multiple compilations
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
for (ParameterListKind plk : ParameterListKind.values()) {
for (ParameterKind pk : ParameterKind.values()) {
for (ArrowKind ak : ArrowKind.values()) {
for (ExprKind ek : ExprKind.values()) {
new BadLambdaExpr(plk, pk, ak, ek).run(comp, fm);
}
}
}
}
System.out.println("Total check executed: " + checkCount);
}
ParameterListKind plk;
ParameterKind pk;
ArrowKind ak;
ExprKind ek;
JavaSource source;
DiagnosticChecker diagChecker;
BadLambdaExpr(ParameterListKind plk, ParameterKind pk, ArrowKind ak, ExprKind ek) {
this.plk = plk;
this.pk = pk;
this.ak = ak;
this.ek = ek;
this.source = new JavaSource();
this.diagChecker = new DiagnosticChecker();
}
class JavaSource extends SimpleJavaFileObject {
String template = "class Test {\n" +
" SAM s = #E;\n" +
"}";
String source;
public JavaSource() {
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
source = template.replaceAll("#E", ek.expressionString(plk, pk, ak));
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return source;
}
}
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
null, null, Arrays.asList(source));
try {
ct.parse();
} catch (Throwable ex) {
throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true));
}
check();
}
void check() {
boolean errorExpected =
ak != ArrowKind.NONE ||
plk != ParameterListKind.UNARY ||
pk != ParameterKind.IMPLICIT;
if (errorExpected != diagChecker.errorFound) {
throw new Error("bad diag for source:\n" +
source.getCharContent(true));
}
checkCount++;
}
static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
boolean errorFound;
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
errorFound = true;
}
}
}
}

View file

@ -0,0 +1,31 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that lambda is only allowed in argument/cast/assignment context
* @author Maurizio Cimadamore
* @compile/fail/ref=BadLambdaPos.out -XDrawDiagnostics BadLambdaPos.java
*/
interface SAM {
void m(Integer x);
}
class Test {
void test(Object x) {}
void test1() {
test((int x)-> { } + (int x)-> { } );
test((int x)-> { } instanceof Object );
}
void test2() {
int i2 = (int x)-> { } + (int x)-> { };
boolean b = (int x)-> { } instanceof Object;
}
void test3() {
test((Object)(int x)-> { });
Object o = (Object)(int x)-> { };
}
}

View file

@ -0,0 +1,9 @@
BadLambdaPos.java:18:14: compiler.err.unexpected.lambda
BadLambdaPos.java:18:30: compiler.err.unexpected.lambda
BadLambdaPos.java:19:14: compiler.err.unexpected.lambda
BadLambdaPos.java:23:18: compiler.err.unexpected.lambda
BadLambdaPos.java:23:34: compiler.err.unexpected.lambda
BadLambdaPos.java:24:21: compiler.err.unexpected.lambda
BadLambdaPos.java:28:22: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
BadLambdaPos.java:29:28: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
8 errors

View file

@ -0,0 +1,16 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that diagnostics on nested erroneous deferred types are flushed
* @compile/fail/ref=BadMethodCall.out -XDrawDiagnostics BadMethodCall.java
*/
import java.util.*;
class BadMethodCall {
<I> List<I> id(List<I> z) { return null; };
List<String> cons(String s, List<String> ls) { return null; }
void test(List<Object> lo) { Object t = cons(id(""),lo); }
}

View file

@ -0,0 +1,2 @@
BadMethodCall.java:15:50: compiler.err.cant.apply.symbol: kindname.method, id, java.util.List<I>, java.lang.String, kindname.class, BadMethodCall, (compiler.misc.infer.no.conforming.assignment.exists: I, (compiler.misc.inconvertible.types: java.lang.String, java.util.List<I>))
1 error

View file

@ -0,0 +1,19 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that recovery of speculative types is not attempted if receiver is erroneous
* @compile/fail/ref=BadRecovery.out -XDrawDiagnostics BadRecovery.java
*/
class BadRecovery {
interface SAM1 {
void m(Object o);
}
void m(SAM1 m) { };
void test() {
m((receiver, t) -> { receiver.someMemberOfReceiver(()->{ Object x = f; }); });
}
}

View file

@ -0,0 +1,3 @@
BadRecovery.java:17:9: compiler.err.cant.apply.symbol: kindname.method, m, BadRecovery.SAM1, @369, kindname.class, BadRecovery, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.incompatible.arg.types.in.lambda))
BadRecovery.java:17:77: compiler.err.cant.resolve.location: kindname.variable, f, , , (compiler.misc.location: kindname.class, BadRecovery, null)
2 errors

View file

@ -0,0 +1,38 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that incompatible return types in lambdas are flagged with error
* @author Maurizio Cimadamore
* @compile/fail/ref=BadReturn.out -XDrawDiagnostics BadReturn.java
*/
class BadReturn {
interface SAM {
Comparable<?> m();
}
static void testNeg1() {
SAM s = ()-> {
if (true) {
return "";
} else {
return System.out.println("");
}};
}
static void testNeg2() {
SAM s = ()-> { return System.out.println(""); };
}
static void testPos() {
SAM s = ()-> {
if (false) {
return 10;
}
else {
return true;
}};
}
}

View file

@ -0,0 +1,3 @@
BadReturn.java:21:42: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: void, java.lang.Comparable<?>))
BadReturn.java:26:49: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: void, java.lang.Comparable<?>))
2 errors

View file

@ -0,0 +1,19 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that the compiler emits meaningful diagnostics when the lambda body contains bad statements
* @author Maurizio Cimadamore
* @compile/fail/ref=BadStatementInLambda.out -XDrawDiagnostics BadStatementInLambda.java
*/
class BadStatementInLambda {
interface SAM{
Object m();
}
SAM t1 = ()-> { null; };
SAM t2 = ()-> { 1; };
SAM t3 = ()-> { 1 + 5; };
}

View file

@ -0,0 +1,4 @@
BadStatementInLambda.java:16:21: compiler.err.not.stmt
BadStatementInLambda.java:17:21: compiler.err.not.stmt
BadStatementInLambda.java:18:23: compiler.err.not.stmt
3 errors

View file

@ -0,0 +1,19 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that the compiler emits meaningful diagnostics when the lambda body contains bad statements
* @author Maurizio Cimadamore
* @compile/fail/ref=BadStatementInLambda02.out -XDrawDiagnostics BadStatementInLambda02.java
*/
class BadStatementInLambda02 {
interface SAM {
void m();
}
{ call(()-> { System.out.println(new NonExistentClass() + ""); }); }
void call(SAM s) { }
}

View file

@ -0,0 +1,2 @@
BadStatementInLambda02.java:16:42: compiler.err.cant.resolve.location: kindname.class, NonExistentClass, , , (compiler.misc.location: kindname.class, BadStatementInLambda02, null)
1 error

View file

@ -0,0 +1,23 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that only SAM are allowed as target types for lambda expressions
* @author Jan Lahoda
* @author Maurizio Cimadamore
* @compile/fail/ref=BadTargetType.out -XDrawDiagnostics BadTargetType.java
*/
class BadTargetType {
static void m1(Object o) {}
void m2(Object o) {}
static Object l1 = (int pos)-> { };
Object l2 = (int pos)-> { };
{
m1((int pos)-> { });
m2((int pos)-> { });
}
}

View file

@ -0,0 +1,5 @@
BadTargetType.java:16:24: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
BadTargetType.java:17:17: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
BadTargetType.java:20:9: compiler.err.cant.apply.symbol: kindname.method, m1, java.lang.Object, @460, kindname.class, BadTargetType, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.not.a.functional.intf))
BadTargetType.java:21:9: compiler.err.cant.apply.symbol: kindname.method, m2, java.lang.Object, @489, kindname.class, BadTargetType, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.not.a.functional.intf))
4 errors

View file

@ -0,0 +1,45 @@
/*
* 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
* conditional and varargs
* @compile -XDcomplexinference Conditional01.java
*/
import java.util.*;
class Conditional01 {
void varargs(Object ... args) { }
void test(boolean flag, List<String> ls) {
varargs(flag ? "" : ls);
varargs(null, flag ? "" : ls);
varargs(flag ? "" : ls());
varargs(null, flag ? "" : ls());
}
List<String> ls() { return null; }
}

View file

@ -0,0 +1,42 @@
/*
* 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
* inference and conditionals
* @compile -XDcomplexinference Conditional02.java
*/
class Conditional02 {
<Z> void m1(Z z) { }
<Z> void m2(Z... z) { }
void test(boolean flag) {
m1(flag ? "" : "");
m2(flag ? "" : "");
m2("", flag ? "" : "");
}
}

View file

@ -0,0 +1,43 @@
/*
* 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
* conditionals and boxing
* @compile -XDcomplexinference Conditional03.java
*/
class Conditional03 {
void m1(Object o) { }
void m2(int i) { }
void test(boolean cond) {
m1((cond ? 1 : 1));
m1((cond ? box(1) : box(1)));
}
Integer box(int i) { return i; }
}

View file

@ -0,0 +1,36 @@
/*
* 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
* lambda compiler regression with uninferred type-variables in generic constructor call
* @compile Conformance01.java
*/
class Conformance01 {
<T1, T2> Conformance01(T1 t) { }
Conformance01 c01 = new Conformance01(null);
}

View file

@ -0,0 +1,45 @@
/*
* 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
* routine that checks for SAM types should skip defender methods in extended interfaces
* @author Maurizio Cimadamore
* @compile Defender01.java
*/
class Defender01 {
interface A{
Object m();
default void n() { E.n(this); }
}
static class E{
static void n(A a){};
}
A t = ()-> null;
}

View file

@ -0,0 +1,52 @@
/*
* 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
* check that subtyping between disjunctive and non disjunctive type works
* @author Maurizio Cimadamore
* @compile DisjunctiveTypeTest.java
*/
class DisjunctiveTypeTest {
static class A extends IllegalArgumentException {
A(String a) { super(a); }
}
class B extends IllegalArgumentException {
B(String b) { super(b); }
}
void m() throws A,B {}
void test() {
try {
m();
} catch (A|B e) {
throw new IllegalArgumentException(e);
}
}
}

View file

@ -0,0 +1,18 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* effectively final check fails on method parameter
* @compile/fail/ref=EffectivelyFinal01.out -XDrawDiagnostics EffectivelyFinal01.java
*/
class EffectivelyFinal01 {
interface SAM {
Integer m(Integer i);
}
void test(Integer nefPar) {
SAM s = (Integer h) -> { Integer k = 0; return k + h + nefPar; };
nefPar++; //non-effectively final
}
}

View file

@ -0,0 +1,2 @@
EffectivelyFinal01.java:15:65: compiler.err.cant.ref.non.effectively.final.var: nefPar, (compiler.misc.lambda)
1 error

View file

@ -1,30 +1,9 @@
/*
* 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
* @summary Integrate efectively final check with DA/DU analysis
* @compile/fail/ref=EffectivelyFinalTest01.out -XDallowEffectivelyFinalInInnerClasses -XDrawDiagnostics EffectivelyFinalTest.java
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* Integrate effectively final check with DA/DU analysis
* @compile/fail/ref=EffectivelyFinalTest01.out -XDrawDiagnostics EffectivelyFinalTest.java
* @compile/fail/ref=EffectivelyFinalTest02.out -source 7 -Xlint:-options -XDrawDiagnostics EffectivelyFinalTest.java
*/
class EffectivelyFinalTest {
@ -62,7 +41,7 @@ class EffectivelyFinalTest {
void m6(int x) {
new Object() { { System.out.println(x+1); } }; //error - x not EF
x++;
x++; // Illegal: x is not effectively final.
}
void m7(int x) {

View file

@ -1,6 +1,6 @@
EffectivelyFinalTest.java:46:47: compiler.err.var.might.not.have.been.initialized: y
EffectivelyFinalTest.java:60:47: compiler.err.cant.ref.non.effectively.final.var: y, (compiler.misc.inner.cls)
EffectivelyFinalTest.java:64:45: compiler.err.cant.ref.non.effectively.final.var: x, (compiler.misc.inner.cls)
EffectivelyFinalTest.java:69:45: compiler.err.cant.ref.non.effectively.final.var: x, (compiler.misc.inner.cls)
EffectivelyFinalTest.java:74:45: compiler.err.cant.ref.non.effectively.final.var: y, (compiler.misc.inner.cls)
EffectivelyFinalTest.java:25:47: compiler.err.var.might.not.have.been.initialized: y
EffectivelyFinalTest.java:39:47: compiler.err.cant.ref.non.effectively.final.var: y, (compiler.misc.inner.cls)
EffectivelyFinalTest.java:43:45: compiler.err.cant.ref.non.effectively.final.var: x, (compiler.misc.inner.cls)
EffectivelyFinalTest.java:48:45: compiler.err.cant.ref.non.effectively.final.var: x, (compiler.misc.inner.cls)
EffectivelyFinalTest.java:53:45: compiler.err.cant.ref.non.effectively.final.var: y, (compiler.misc.inner.cls)
5 errors

View file

@ -1,14 +1,14 @@
EffectivelyFinalTest.java:46:47: compiler.err.var.might.not.have.been.initialized: y
EffectivelyFinalTest.java:34:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:34:47: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:40:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:40:47: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:46:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:53:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:53:47: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:60:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:60:47: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:64:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:69:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:74:45: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:25:47: compiler.err.var.might.not.have.been.initialized: y
EffectivelyFinalTest.java:13:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:13:47: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:19:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:19:47: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:25:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:32:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:32:47: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:39:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:39:47: compiler.err.local.var.accessed.from.icls.needs.final: y
EffectivelyFinalTest.java:43:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:48:45: compiler.err.local.var.accessed.from.icls.needs.final: x
EffectivelyFinalTest.java:53:45: compiler.err.local.var.accessed.from.icls.needs.final: y
13 errors

View file

@ -0,0 +1,36 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* speculative cache mismatches between Resolve.access and Attr.checkId leads to compiler crashes
* @compile/fail/ref=ErroneousArg.out -XDrawDiagnostics ErroneousArg.java
*/
class ErroneousArg {
private static class Foo {
static int j() { return 1; }
}
static Foo foo = new Foo();
static void m(String s) { }
static void m(Integer i) { }
static int f(String s) { return 1; }
static int g(String s) { return 1; }
static int g(Double s) { return 1; }
int h() { return 1; }
}
class TestErroneousArg extends ErroneousArg {
static void test() {
m(unknown()); //method not found
m(f(1)); //inapplicable method
m(g(1)); //inapplicable methods
m(g(null)); //ambiguous
m(h()); //static error
m(foo.j()); //inaccessible method
}
}

View file

@ -0,0 +1,7 @@
ErroneousArg.java:29:11: compiler.err.cant.resolve.location.args: kindname.method, unknown, , , (compiler.misc.location: kindname.class, TestErroneousArg, null)
ErroneousArg.java:30:11: compiler.err.cant.apply.symbol: kindname.method, f, java.lang.String, int, kindname.class, ErroneousArg, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String))
ErroneousArg.java:31:11: compiler.err.cant.apply.symbols: kindname.method, g, int,{(compiler.misc.inapplicable.method: kindname.method, ErroneousArg, g(java.lang.String), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String))),(compiler.misc.inapplicable.method: kindname.method, ErroneousArg, g(java.lang.Double), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.Double)))}
ErroneousArg.java:32:11: compiler.err.ref.ambiguous: g, kindname.method, g(java.lang.String), ErroneousArg, kindname.method, g(java.lang.Double), ErroneousArg
ErroneousArg.java:33:11: compiler.err.non-static.cant.be.ref: kindname.method, h()
ErroneousArg.java:34:14: compiler.err.not.def.access.class.intf.cant.access: j(), ErroneousArg.Foo
6 errors

View file

@ -0,0 +1,65 @@
/*
* 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
* stale state after speculative attribution round leads to missing classfiles
*/
public class ErroneousLambdaExpr<T> {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface SAM1<X> {
X m(X t, String s);
}
interface SAM2 {
void m(String s, int i);
}
interface SAM3<X> {
X m(X t, String s, int i);
}
void call(SAM1<T> s1) { assertTrue(true); }
void call(SAM2 s2) { assertTrue(false); }
void call(SAM3<T> s3) { assertTrue(false); }
public static void main(String[] args) {
ErroneousLambdaExpr<StringBuilder> test =
new ErroneousLambdaExpr<>();
test.call((builder, string) -> { builder.append(string); return builder; });
assertTrue(assertionCount == 1);
}
}

View file

@ -23,15 +23,20 @@
/*
* @test
* @summary Regression test JDK-8003306 inner class constructor in lambda
* @bug 8003280
* @summary Add lambda tests
* Regression test JDK-8003306 inner class constructor in lambda
* @author Robert Field
* @compile -XDallowLambda InnerConstructor.java
*/
class InnerConstructor {
public class InnerConstructor {
public void testLambdaWithInnerConstructor() {
System.out.printf("%s should be %s\n", seq1().m().toString(), "Cbl:nada");
public static void main(String... args) {
InnerConstructor ic = new InnerConstructor();
String res = ic.seq1().m().toString();
if (!res.equals("Cbl.toString")) {
throw new AssertionError(String.format("Unexpected result: %s", res));
}
}
Ib1 seq1() {
@ -40,6 +45,9 @@ class InnerConstructor {
class Cbl {
Cbl() { }
public String toString() {
return "Cbl.toString";
}
}
interface Ib1 {

View file

@ -0,0 +1,107 @@
/*
* 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
* basic test for capture of non-mutable locals
* @author Brian Goetz
* @author Maurizio Cimadamore
* @run main LambdaCapture01
*/
public class LambdaCapture01 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface Tester {
void test();
}
interface TU<T, U> {
public T foo(U u);
}
public static <T, U> T exec(TU<T, U> lambda, U x) {
return lambda.foo(x);
}
public int n = 5;
//Simple local capture
void test1() {
final int N = 1;
int res = LambdaCapture01.<Integer,Integer>exec((Integer x) -> x + N, 3);
assertTrue(4 == res);
}
//Local capture with multiple scopes (anon class)
void test2() {
final int N = 1;
new Tester() {
public void test() {
final int M = 2;
int res = LambdaCapture01.<Integer,Integer>exec((Integer x) -> x + N + M, 3);
assertTrue(6 == res);
}
}.test();
}
//Local capture with multiple scopes (local class)
void test3() {
final int N = 1;
class MyTester implements Tester {
public void test() {
final int M = 2;
int res = LambdaCapture01.<Integer,Integer>exec((Integer x) -> x + N + M, 3);
assertTrue(6 == res);
}
}
new MyTester().test();
}
//access to field from enclosing scope
void test4() {
final int N = 4;
int res1 = LambdaCapture01.<Integer,Integer>exec((Integer x) -> x + n + N, 3);
assertTrue(12 == res1);
int res2 = LambdaCapture01.<Integer,Integer>exec((Integer x) -> x + LambdaCapture01.this.n + N, 3);
assertTrue(12 == res2);
}
public static void main(String[] args) {
LambdaCapture01 t = new LambdaCapture01();
t.test1();
t.test2();
t.test3();
t.test4();
assertTrue(assertionCount == 5);
}
}

View file

@ -0,0 +1,107 @@
/*
* 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
* basic test for capture of non-mutable locals
* @author Brian Goetz
* @author Maurizio Cimadamore
* @run main LambdaCapture02
*/
public class LambdaCapture02 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface Tester {
void test();
}
interface TU<T, U> {
public T foo(U u);
}
public static <T, U> T exec(TU<T, U> lambda, U x) {
return lambda.foo(x);
}
public Integer n = 5;
//Simple local capture
void test1() {
final Integer N = 1;
int res = LambdaCapture02.<Integer,Integer>exec((Integer x) -> x + N, 3);
assertTrue(4 == res);
}
//Local capture with multiple scopes (anon class)
void test2() {
final Integer N = 1;
new Tester() {
public void test() {
final Integer M = 2;
int res = LambdaCapture02.<Integer,Integer>exec((Integer x) -> x + N + M, 3);
assertTrue(6 == res);
}
}.test();
}
//Local capture with multiple scopes (local class)
void test3() {
final Integer N = 1;
class MyTester implements Tester {
public void test() {
final Integer M = 2;
int res = LambdaCapture02.<Integer,Integer>exec((Integer x) -> x + N + M, 3);
assertTrue(6 == res);
}
}
new MyTester().test();
}
//access to field from enclosing scope
void test4() {
final Integer N = 4;
int res1 = LambdaCapture02.<Integer,Integer>exec((Integer x) -> x + n + N, 3);
assertTrue(12 == res1);
int res2 = LambdaCapture02.<Integer,Integer>exec((Integer x) -> x + LambdaCapture02.this.n + N, 3);
assertTrue(12 == res2);
}
public static void main(String[] args) {
LambdaCapture02 t = new LambdaCapture02();
t.test1();
t.test2();
t.test3();
t.test4();
assertTrue(assertionCount == 5);
}
}

View file

@ -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
* test for capture of non-mutable locals/outer fields in multiple scopes
* @author Maurizio Cimadamore
* @run main LambdaCapture03
*/
public class LambdaCapture03 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface Tester {
void test();
}
interface TU<T, U> {
public T foo(U u);
}
public static <T, U> T exec(TU<T, U> lambda, U x) {
return lambda.foo(x);
}
Integer n1 = 10;
void test1() {
final Integer N1 = 1;
class A {
Integer n2 = 20;
void test() {
final Integer N2 = 2;
class B {
void test() {
final Integer N3 = 3;
int res = LambdaCapture03.exec((Integer x) -> x + n1 + n2 + N1 + N2 + N3, 30);
assertTrue(res == 66);
}
}
new B().test();
}
}
new A().test();
}
void test2() {
final Integer N1 = 1;
new Tester() {
Integer n2 = 20;
public void test() {
final Integer N2 = 2;
new Tester() {
public void test() {
final Integer N3 = 3;
int res = LambdaCapture03.exec((Integer x) -> x + n1 + n2 + N1 + N2 + N3, 30);
assertTrue(res == 66);
}
}.test();
}
}.test();
}
public static void main(String[] args) {
LambdaCapture03 t = new LambdaCapture03();
t.test1();
t.test2();
assertTrue(assertionCount == 2);
}
}

View file

@ -0,0 +1,143 @@
/*
* 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 for capture of non-mutable locals/outer fields in multiple scopes
* @author Maurizio Cimadamore
* @run main LambdaCapture04
*/
public class LambdaCapture04 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface Tester {
void test();
}
interface TU<U> {
public void foo(U u);
}
public static <U> void exec(TU<U> lambda, U x) {
lambda.foo(x);
}
Integer n1 = 10;
void test1() {
final Integer N1 = 1;
class A {
Integer n2 = 20;
void test() {
final Integer N2 = 2;
class B {
void test() {
final Integer N3 = 3;
exec((final Integer x) -> new Tester() { public void test() { assertTrue(x + n1 + n2 + N1 + N2 + N3 == 66); } }.test(),30);
}
}
new B().test();
}
}
new A().test();
}
void test2() {
final Integer N1 = 1;
class A {
Integer n2 = 20;
void test() {
final Integer N2 = 2;
class B {
void test() {
final Integer N3 = 3;
exec((final Integer x) -> {
class LocTester implements Tester {
public void test() { assertTrue(x + n1 + n2 + N1 + N2 + N3 == 66); }
};
new LocTester().test();
},30);
}
}
new B().test();
}
}
new A().test();
}
void test3() {
final Integer N1 = 1;
new Tester() {
Integer n2 = 20;
public void test() {
final Integer N2 = 2;
new Tester() {
public void test() {
final Integer N3 = 3;
exec((final Integer x) -> new Tester() { public void test() { assertTrue(x + n1 + n2 + N1 + N2 + N3 == 66); } }.test(),30);
}
}.test();
}
}.test();
}
void test4() {
final Integer N1 = 1;
new Tester() {
Integer n2 = 20;
public void test() {
final Integer N2 = 2;
new Tester() {
public void test() {
final Integer N3 = 3;
exec((final Integer x) -> {
class LocTester implements Tester {
public void test() { assertTrue(x + n1 + n2 + N1 + N2 + N3 == 66); }
};
new LocTester().test();
},30);
}
}.test();
}
}.test();
}
public static void main(String[] args) {
LambdaCapture04 t = new LambdaCapture04();
t.test1();
t.test2();
t.test3();
t.test4();
assertTrue(assertionCount == 4);
}
}

View file

@ -0,0 +1,81 @@
/*
* 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 for capture in nested lambda expressions
* @author Maurizio Cimadamore
* @run main LambdaCapture05
*/
public class LambdaCapture05 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface TU<T, U> {
public T foo(U u);
}
public static <T, U> T exec(TU<T, U> lambda, U x) {
return lambda.foo(x);
}
int i = 40;
void test1(final int a0) {
exec((final Integer a1) -> {
final Integer x2 = 10; exec((final Integer a2) -> {
final Integer x3 = 20;
exec((final Integer a3) -> { assertTrue(106 == (a0 + a1 + a2 + a3 + x2 + x3 + i)); return null; }, 3);
return null;
},2);
return null;
},1);
}
static void test2(final int a0) {
exec((final Integer a1) -> {
final Integer x2 = 10; exec((final Integer a2) -> {
final Integer x3 = 20;
exec((final Integer a3) -> { assertTrue(66 == (a0 + a1 + a2 + a3 + x2 + x3)); return null; }, 3);
return null;
}, 2);
return null;
}, 1);
}
public static void main(String[] args) {
LambdaCapture05 t = new LambdaCapture05();
t.test1(30);
test2(30);
assertTrue(assertionCount == 2);
}
}

View file

@ -0,0 +1,57 @@
/*
* 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
* @ignore investigate as to whether code generation fails
* @bug 8003280
* @summary Add lambda tests
* Compiler crash when local inner class nested inside lambda captures local variables from enclosing scope
*/
public class LambdaCapture06 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface SAM {
void m(int n);
}
public static void main(String[] args) {
int n = 5;
SAM s = k -> {
new Object() {
void test() { int j = n; assertTrue(j == 5); }
}.test();
};
s.m(42);
assertTrue(assertionCount == 1);
}
}

View file

@ -0,0 +1,145 @@
/*
* 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
* basic test for lambda conversion
* @author Brian Goetz
* @author Maurizio Cimadamore
* @run main LambdaConv01
*/
public class LambdaConv01 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface IntToInt {
public int foo(int x);
}
interface IntToVoid {
public void foo(int x);
}
interface VoidToInt {
public int foo();
}
interface TU<T, U> {
public T foo(U u);
}
public static <T, U> T exec(TU<T, U> lambda, U x) {
return lambda.foo(x);
}
static {
//Assignment conversion:
VoidToInt f1 = ()-> 3;
assertTrue(3 == f1.foo());
//Covariant returns:
TU<Number, Integer> f2 = (Integer x) -> x;
assertTrue(3 == f2.foo(3));
//Method resolution with boxing:
int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
assertTrue(3 == res);
//Runtime exception transparency:
try {
LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
}
catch (RuntimeException e) {
assertTrue(true);
}
}
{
//Assignment conversion:
VoidToInt f1 = ()-> 3;
assertTrue(3 == f1.foo());
//Covariant returns:
TU<Number, Integer> f2 = (Integer x) -> x;
assertTrue(3 == f2.foo(3));
//Method resolution with boxing:
int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
assertTrue(3 == res);
//Runtime exception transparency:
try {
LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
}
catch (RuntimeException e) {
assertTrue(true);
}
}
public static void test1() {
//Assignment conversion:
VoidToInt f1 = ()-> 3;
assertTrue(3 == f1.foo());
//Covariant returns:
TU<Number, Integer> f2 = (Integer x) -> x;
assertTrue(3 == f2.foo(3));
//Method resolution with boxing:
int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
assertTrue(3 == res);
//Runtime exception transparency:
try {
LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
}
catch (RuntimeException e) {
assertTrue(true);
}
}
public void test2() {
//Assignment conversion:
VoidToInt f1 = ()-> 3;
assertTrue(3 == f1.foo());
//Covariant returns:
TU<Number, Integer> f2 = (Integer x) -> x;
assertTrue(3 == f2.foo(3));
//Method resolution with boxing:
int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
assertTrue(3 == res);
//Runtime exception transparency:
try {
LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
}
catch (RuntimeException e) {
assertTrue(true);
}
}
public static void main(String[] args) {
test1();
new LambdaConv01().test2();
assertTrue(assertionCount == 16);
}
}

View file

@ -0,0 +1,121 @@
/*
* 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
* SAM types and method type inference
* @author Brian Goetz
* @author Maurizio Cimadamore
* @run main LambdaConv03
*/
public class LambdaConv03 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface TU<T, U> {
public T foo(U u);
}
public static <T, U> T exec(TU<T, U> lambda, U x) {
return lambda.foo(x);
}
static {
//Covariant returns:
int i1 = exec((Integer x) -> { return x; }, 3);
assertTrue(3 == i1);
//Method resolution with boxing:
int i2 = exec((Integer x) -> { return x; }, 3);
assertTrue(3 == i2);
//Runtime exception transparency:
try {
exec((Object x) -> { return x.hashCode(); }, null);
}
catch (RuntimeException e) {
assertTrue(true);
}
}
{
//Covariant returns:
int i1 = exec((Integer x) -> { return x; }, 3);
assertTrue(3 == i1);
//Method resolution with boxing:
int i2 = exec((Integer x) -> { return x; }, 3);
assertTrue(3 == i2);
//Runtime exception transparency:
try {
exec((Object x) -> { return x.hashCode(); }, null);
}
catch (RuntimeException e) {
assertTrue(true);
}
}
public static void test1() {
//Covariant returns:
int i1 = exec((Integer x) -> { return x; }, 3);
assertTrue(3 == i1);
//Method resolution with boxing:
int i2 = exec((Integer x) -> { return x; }, 3);
assertTrue(3 == i2);
//Runtime exception transparency:
try {
exec((Object x) -> { return x.hashCode(); }, null);
}
catch (RuntimeException e) {
assertTrue(true);
}
}
public void test2() {
//Covariant returns:
int i1 = exec((Integer x) -> { return x; }, 3);
assertTrue(3 == i1);
//Method resolution with boxing:
int i2 = exec((Integer x) -> { return x; }, 3);
assertTrue(3 == i2);
//Runtime exception transparency:
try {
exec((Object x) -> { return x.hashCode(); }, null);
}
catch (RuntimeException e) {
assertTrue(true);
}
}
public static void main(String[] args) {
test1();
new LambdaConv03().test2();
assertTrue(assertionCount == 12);
}
}

View file

@ -0,0 +1,62 @@
/*
* 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
* function type and method type inference
* @author Alex Buckley
* @author Maurizio Cimadamore
* @run main LambdaConv05
*/
import java.util.*;
public class LambdaConv05 {
static void assertTrue(boolean cond) {
if (!cond)
throw new AssertionError();
}
int count = 0;
void sort(List<String> data) {
Collections.sort(data,
(String a, String b) -> { LambdaConv05.this.count++; return a.length()-b.length(); });
}
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<>();
arr.add("Three");
arr.add("Four");
arr.add("One");
LambdaConv05 sorter = new LambdaConv05();
sorter.sort(arr);
assertTrue(arr.get(0).equals("One"));
assertTrue(arr.get(1).equals("Four"));
assertTrue(arr.get(2).equals("Three"));
assertTrue(sorter.count == 2);
}
}

View file

@ -0,0 +1,52 @@
/*
* 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
* ensure that definite assignment analysis doesn't mess up with lambda attribution
* @author Jan Lahoda
* @author Maurizio Cimadamore
* @compile LambdaConv06.java
*/
class LambdaConv06 {
private int t() {
return a((final Object indexed) -> {
return b(new R() {
public String build(final Object index) {
return "";
}
});
});
}
private int a(R r) {return 0;}
private String b(R r) {return null;}
public static interface R {
public String build(Object o);
}
}

View file

@ -0,0 +1,57 @@
/*
* 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
* check that SAM conversion handles covarinat return types correctly
* @author Peter Levart
* @author Maurizio Cimadamore
* @run main LambdaConv08
*/
public class LambdaConv08 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
public interface ObjectF { Object invoke(); }
public interface StringF extends ObjectF { String invoke(); }
public static void call(StringF stringFunc) {
assertTrue(true);
}
public static void call(ObjectF objectFunc) { }
public static void main(String[] args) {
call(()-> "Hello");
assertTrue(assertionCount == 1);
}
}

View file

@ -0,0 +1,50 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that SAM conversion handles Object members correctly
* @author Alex Buckley
* @author Maurizio Cimadamore
* @compile/fail/ref=LambdaConv09.out -XDrawDiagnostics LambdaConv09.java
*/
class LambdaConv09 {
// Not a SAM type; not enough abstract methods
interface Foo1 {}
// SAM type; Foo has no abstract methods
interface Foo2 { boolean equals(Object object); }
// Not a SAM type; Foo still has no abstract methods
interface Foo3 extends Foo2 { public abstract String toString(); }
// SAM type; Bar has one abstract non-Object method
interface Foo4<T> extends Foo2 { int compare(T o1, T o2); }
// Not a SAM type; still no valid abstract methods
interface Foo5 {
boolean equals(Object object);
String toString();
}
// SAM type; Foo6 has one abstract non-Object method
interface Foo6<T> {
boolean equals(Object obj);
int compare(T o1, T o2);
}
// SAM type; Foo6 has one abstract non-Object method
interface Foo7<T> extends Foo2, Foo6<T> { }
void test() {
Foo1 f1 = ()-> { };
Foo2 f2 = ()-> { };
Foo3 f3 = x -> true;
Foo4 f4 = (x, y) -> 1;
Foo5 f5 = x -> true;
Foo6 f6 = (x, y) -> 1;
Foo7 f7 = (x, y) -> 1;
}
}

View file

@ -0,0 +1,5 @@
LambdaConv09.java:42:19: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.no.abstracts: kindname.interface, LambdaConv09.Foo1))
LambdaConv09.java:43:19: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.no.abstracts: kindname.interface, LambdaConv09.Foo2))
LambdaConv09.java:44:19: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.no.abstracts: kindname.interface, LambdaConv09.Foo3))
LambdaConv09.java:46:19: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf.1: (compiler.misc.no.abstracts: kindname.interface, LambdaConv09.Foo5))
4 errors

View file

@ -0,0 +1,17 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that lambda conversion does not allow boxing of lambda parameters
* @author Maurizio Cimadamore
* @compile/fail/ref=LambdaConv10.out -XDrawDiagnostics LambdaConv10.java
*/
class LambdaConv10 {
interface Method1<R, A1> { public R call( A1 a1 ); }
public static void main( final String... notUsed ) {
Method1<Integer,Integer> m1 = (int i) -> 2 * i;
}
}

View file

@ -0,0 +1,2 @@
LambdaConv10.java:15:39: compiler.err.prob.found.req: (compiler.misc.incompatible.arg.types.in.lambda)
1 error

View file

@ -0,0 +1,42 @@
/*
* 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
* issues with lambda conversion involving generic class hierarchies
* @author Maurizio Cimadamore
* @compile LambdaConv11.java
*/
import java.util.Comparator;
class LambdaConv11<T> {
interface SAM<X> extends Comparator<X> {
public int compare(X left, X right);
}
SAM<T> y = (l, r) -> 0;
}

View file

@ -0,0 +1,45 @@
/*
* 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
* instance creation expression should allow lambda expressions as constrcutor arguments
* @author Maurizio Cimadamore
* @compile LambdaConv12.java
*/
class LambdaConv12 {
LambdaConv12(SAM s) {}
interface SAM {
public abstract void m();
}
void test() {
new LambdaConv12(()-> { });
new LambdaConv12(()-> { }) {};
}
}

View file

@ -0,0 +1,48 @@
/*
* 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
* interface methods in diamond shaped inheritance trees shouldn't be counted twice
* @author Maurizio Cimadamore
* @compile LambdaConv13.java
*/
class LambdaConv13 {
interface I {
void m();
}
interface A extends I {}
interface B extends I {}
interface C extends A, B {}
interface D extends A, I {}
interface E extends B, I {}
C c = ()-> { };
D d = ()-> { };
D e = ()-> { };
}

View file

@ -0,0 +1,63 @@
/*
* 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
* SAM conversion and raw types in argument/return types
* @author Maurizio Cimadamore
* @run main LambdaConv16
*/
import java.util.*;
public class LambdaConv16 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface A {
Iterable m(List<String> ls);
}
interface B {
Iterable<String> m(List l);
}
interface AB extends A, B {} //SAM type ([List], Iterable<String>, {})
static void test(AB ab, List l) { ab.m(l); }
public static void main(String[] args) {
AB ab = (List list) -> { assertTrue(true); return new ArrayList<String>(); };
ab.m(null);
test((List list) -> { assertTrue(true); return new ArrayList<String>(); }, null);
assertTrue(assertionCount == 2);
}
}

View file

@ -0,0 +1,38 @@
/*
* 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
* lambda compiler crashes if lambda has try-with-resources
* @compile LambdaConv17.java
*/
class LambdaConv17 {
interface SAM {
void m() throws Exception;
}
SAM s = ()-> { try (AutoCloseable ac = null){ } };
}

View file

@ -0,0 +1,24 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* simple test for lambda candidate check
* @compile/fail/ref=LambdaConv18.out -XDrawDiagnostics -XDidentifyLambdaCandidate=true LambdaConv18.java
*/
class LambdaConv18 {
interface SAM {
void m();
}
interface NonSAM {
void m1();
void m2();
}
SAM s1 = new SAM() { public void m() {} };
NonSAM s2 = new NonSAM() { public void m1() {}
public void m2() {} };
NonExistent s3 = new NonExistent() { public void m() {} };
}

View file

@ -0,0 +1,4 @@
LambdaConv18.java:23:5: compiler.err.cant.resolve.location: kindname.class, NonExistent, , , (compiler.misc.location: kindname.class, LambdaConv18, null)
LambdaConv18.java:20:24: compiler.note.potential.lambda.found
LambdaConv18.java:23:26: compiler.err.cant.resolve.location: kindname.class, NonExistent, , , (compiler.misc.location: kindname.class, LambdaConv18, null)
2 errors

View file

@ -0,0 +1,39 @@
/*
* 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
* check that redundant cast warnings are not generated for SAM conversions
* @compile -Xlint:cast -Werror LambdaConv19.java
*/
class LambdaConv19 {
interface SAM {
void m();
}
SAM s = (SAM)()-> { };
}

View file

@ -0,0 +1,56 @@
/*
* 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
* check that synthetic casts are added when erased type of lambda body
* ends up being too general
* @run main LambdaConv20
*/
import java.util.*;
public class LambdaConv20 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface SAM<X> {
X m(List<X> l);
}
public static void main(String[] args) {
SAM<Integer> si1 = l -> l.get(0);
assertTrue(si1.m(Arrays.asList(1)) == 1);
SAM<Integer> si2 = l -> { return l.get(0); };
assertTrue(si2.m(Arrays.asList(1)) == 1);
assertTrue(assertionCount == 2);
}
}

View file

@ -0,0 +1,38 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that code generation handles void-compatibility correctly
* @compile/fail/ref=LambdaConv21.out -XDrawDiagnostics LambdaConv21.java
*/
class LambdaConv21 {
interface SAM_void<X> {
void m();
}
interface SAM_java_lang_Void {
Void m();
}
static void m_void() { }
static Void m_java_lang_Void() { return null; }
static void testExpressionLambda() {
SAM_void s1 = ()->m_void(); //ok
SAM_java_lang_Void s2 = ()->m_void(); //no - incompatible target
SAM_void s3 = ()->m_java_lang_Void(); //no - incompatible target
SAM_java_lang_Void s4 = ()->m_java_lang_Void(); //ok
}
static void testStatementLambda() {
SAM_void s1 = ()-> { m_void(); }; //ok
SAM_java_lang_Void s2 = ()-> { m_void(); }; //no - missing return value
SAM_void s3 = ()-> { return m_java_lang_Void(); }; //no - unexpected return value
SAM_java_lang_Void s4 = ()-> { return m_java_lang_Void(); }; //ok
SAM_void s5 = ()-> { m_java_lang_Void(); }; //ok
SAM_java_lang_Void s6 = ()-> { m_java_lang_Void(); }; //no - missing return value
}
}

View file

@ -0,0 +1,6 @@
LambdaConv21.java:25:43: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: void, java.lang.Void))
LambdaConv21.java:26:43: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: java.lang.Void, void))
LambdaConv21.java:32:33: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.missing.ret.val: java.lang.Void))
LambdaConv21.java:33:53: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.unexpected.ret.val))
LambdaConv21.java:36:33: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.missing.ret.val: java.lang.Void))
5 errors

View file

@ -0,0 +1,42 @@
/*
* 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
* inner class translator fails with spurious method clash errors
* @compile LambdaConv22.java
*/
class LambdaConv22<U> {
interface Factory<T> { T make(); }
U make() { return null; }
void test(U u) {
Factory<U> fu1 = () -> u;
Factory<U> fu2 = this::make;
}
}

View file

@ -0,0 +1,62 @@
/*
* 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
* check super varargs reference is handled correctly
* @run main LambdaConv23
*/
public class LambdaConv23 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface SAM { void m(Integer a, Integer b); }
static class Super {
void m(Object... vi) { assertTrue(true); }
}
static class Sub extends Super {
void m(Object... vi) { assertTrue(false); }
public void test() {
SAM q = super::m;
q.m(1, 2);
}
}
public static void main(String[] args) {
new Sub().test();
assertTrue(assertionCount == 1);
}
}

View file

@ -0,0 +1,64 @@
/*
* 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
* check that lambda inside 'this' call is handled properly
* @run main LambdaConv24
*/
public class LambdaConv24 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface SAM<X> {
boolean m(X x);
}
LambdaConv24(SAM<String> p) {
assertTrue(p.m("42"));
}
LambdaConv24(int i) {
this(s->true);
}
LambdaConv24(int i1, int i2) {
this(LambdaConv24::m);
}
static boolean m(String s) { return true; }
public static void main(String[] args) {
new LambdaConv24(1);
new LambdaConv24(1,2);
assertTrue(assertionCount == 2);
}
}

View file

@ -0,0 +1,246 @@
/*
* 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
* perform several automated checks in lambda conversion, esp. around accessibility
* @author Maurizio Cimadamore
* @run main LambdaConversionTest
*/
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;
public class LambdaConversionTest {
enum PackageKind {
NO_PKG(""),
PKG_A("a");
String pkg;
PackageKind(String pkg) {
this.pkg = pkg;
}
String getPkgDecl() {
return this == NO_PKG ?
"" :
"package " + pkg + ";";
}
String getImportStat() {
return this == NO_PKG ?
"" :
"import " + pkg + ".*;";
}
}
enum SamKind {
CLASS("public class Sam { }"),
ABSTACT_CLASS("public abstract class Sam { }"),
ANNOTATION("public @interface Sam { }"),
ENUM("public enum Sam { }"),
INTERFACE("public interface Sam { \n #METH; \n }");
String sam_str;
SamKind(String sam_str) {
this.sam_str = sam_str;
}
String getSam(String methStr) {
return sam_str.replaceAll("#METH", methStr);
}
}
enum ModifierKind {
PUBLIC("public"),
PACKAGE("");
String modifier_str;
ModifierKind(String modifier_str) {
this.modifier_str = modifier_str;
}
boolean stricterThan(ModifierKind that) {
return this.ordinal() > that.ordinal();
}
}
enum TypeKind {
EXCEPTION("Exception"),
PKG_CLASS("PackageClass");
String typeStr;
private TypeKind(String typeStr) {
this.typeStr = typeStr;
}
}
enum MethodKind {
NONE(""),
NON_GENERIC("public #R m(#ARG s) throws #T;"),
GENERIC("public <X> #R m(#ARG s) throws #T;");
String methodTemplate;
private MethodKind(String methodTemplate) {
this.methodTemplate = methodTemplate;
}
String getMethod(TypeKind retType, TypeKind argType, TypeKind thrownType) {
return methodTemplate.replaceAll("#R", retType.typeStr).
replaceAll("#ARG", argType.typeStr).
replaceAll("#T", thrownType.typeStr);
}
}
public static void main(String[] args) throws Exception {
for (PackageKind samPkg : PackageKind.values()) {
for (ModifierKind modKind : ModifierKind.values()) {
for (SamKind samKind : SamKind.values()) {
for (MethodKind meth : MethodKind.values()) {
for (TypeKind retType : TypeKind.values()) {
for (TypeKind argType : TypeKind.values()) {
for (TypeKind thrownType : TypeKind.values()) {
new LambdaConversionTest(samPkg, modKind, samKind,
meth, retType, argType, thrownType).test();
}
}
}
}
}
}
}
}
PackageKind samPkg;
ModifierKind modKind;
SamKind samKind;
MethodKind meth;
TypeKind retType;
TypeKind argType;
TypeKind thrownType;
SourceFile samSourceFile = new SourceFile("Sam.java", "#P \n #C") {
public String toString() {
return template.replaceAll("#P", samPkg.getPkgDecl()).
replaceAll("#C", samKind.getSam(meth.getMethod(retType, argType, thrownType)));
}
};
SourceFile pkgClassSourceFile = new SourceFile("PackageClass.java",
"#P\n #M class PackageClass extends Exception { }") {
public String toString() {
return template.replaceAll("#P", samPkg.getPkgDecl()).
replaceAll("#M", modKind.modifier_str);
}
};
SourceFile clientSourceFile = new SourceFile("Client.java",
"#I\n class Client { Sam s = x -> null; }") {
public String toString() {
return template.replaceAll("#I", samPkg.getImportStat());
}
};
LambdaConversionTest(PackageKind samPkg, ModifierKind modKind, SamKind samKind,
MethodKind meth, TypeKind retType, TypeKind argType, TypeKind thrownType) {
this.samPkg = samPkg;
this.modKind = modKind;
this.samKind = samKind;
this.meth = meth;
this.retType = retType;
this.argType = argType;
this.thrownType = thrownType;
}
void test() throws Exception {
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
DiagnosticChecker dc = new DiagnosticChecker();
JavacTask ct = (JavacTask)tool.getTask(null, null, dc,
null, null, Arrays.asList(samSourceFile, pkgClassSourceFile, clientSourceFile));
ct.analyze();
if (dc.errorFound == checkSamConversion()) {
throw new AssertionError(samSourceFile + "\n\n" + pkgClassSourceFile + "\n\n" + clientSourceFile);
}
}
boolean checkSamConversion() {
if (samKind != SamKind.INTERFACE) {
//sam type must be an interface
return false;
} else if (meth != MethodKind.NON_GENERIC) {
//target method must be non-generic
return false;
} else if (samPkg != PackageKind.NO_PKG &&
modKind != ModifierKind.PUBLIC &&
(retType == TypeKind.PKG_CLASS ||
argType == TypeKind.PKG_CLASS ||
thrownType == TypeKind.PKG_CLASS)) {
//target must not contain inaccessible types
return false;
} else {
return true;
}
}
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;
}
}
}
}

View file

@ -0,0 +1,60 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* Integrate efectively final check with DA/DU analysis
* @compile/fail/ref=LambdaEffectivelyFinalTest.out -XDrawDiagnostics LambdaEffectivelyFinalTest.java
*/
class LambdaEffectivelyFinalTest {
interface SAM {
int m();
}
void foo(LambdaEffectivelyFinalTest.SAM s) { }
void m1(int x) {
int y = 1;
foo(() -> x+y); // Legal: x and y are both effectively final.
}
void m2(int x) {
int y;
y = 1;
foo(() -> x+y); // Legal: x and y are both effectively final.
}
void m3(int x, boolean cond) {
int y;
if (cond) y = 1;
foo(() -> x+y); // Illegal: y is effectively final, but not definitely assigned.
}
void m4(int x, boolean cond) {
int y;
if (cond) y = 1;
else y = 2;
foo(() -> x+y); // Legal: x and y are both effectively final.
}
void m5(int x, boolean cond) {
int y;
if (cond) y = 1;
y = 2;
foo(() -> x+y); // Illegal: y is not effectively final.t EF
}
void m6(int x) {
foo(() -> x+1);
x++; // Illegal: x is not effectively final.
}
void m7(int x) {
foo(() -> x=1); // Illegal: x in the assignment does not denote a variable (see 6.5.6.1)
}
void m8() {
int y;
foo(() -> y=1); // Illegal: y in the assignment does not denote a variable (see 6.5.6.1)
}
}

View file

@ -0,0 +1,6 @@
LambdaEffectivelyFinalTest.java:30:21: compiler.err.var.might.not.have.been.initialized: y
LambdaEffectivelyFinalTest.java:44:21: compiler.err.cant.ref.non.effectively.final.var: y, (compiler.misc.lambda)
LambdaEffectivelyFinalTest.java:48:19: compiler.err.cant.ref.non.effectively.final.var: x, (compiler.misc.lambda)
LambdaEffectivelyFinalTest.java:53:19: compiler.err.cant.ref.non.effectively.final.var: x, (compiler.misc.lambda)
LambdaEffectivelyFinalTest.java:58:19: compiler.err.cant.ref.non.effectively.final.var: y, (compiler.misc.lambda)
5 errors

View file

@ -0,0 +1,133 @@
/*
* 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
* basic test for simple lambda expressions in multiple scopes
* @author Brian Goetz
* @author Maurizio Cimadamore
* @run main LambdaExpr01
*/
public class LambdaExpr01 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface S_int {
int m();
}
interface S_Integer {
Integer m();
}
interface S_int_int {
int m(int i);
}
interface S_Integer_int {
int m(Integer i);
}
interface S_int_Integer {
Integer m(int i);
}
interface S_Integer_Integer {
Integer m(Integer i);
}
static {
S_int s_i = ()-> 3;
assertTrue(3 == s_i.m());
S_Integer s_I = ()-> 3;
assertTrue(3 == s_I.m());
S_int_int s_i_i = (int x)-> x+1;
assertTrue(4 == s_i_i.m(3));
S_int_Integer s_i_I = (int x)-> x+1;
assertTrue(4 == s_i_I.m(3));
S_Integer_int s_I_i = (Integer x) -> x.intValue() + 1;
assertTrue(4 == s_I_i.m(3));
S_Integer_Integer s_I_I = (Integer x) -> x.intValue() + 1;
assertTrue(4 == s_I_I.m(3));
}
{
S_int s_i = ()-> 3;
assertTrue(3 == s_i.m());
S_Integer s_I = ()-> 3;
assertTrue(3 == s_I.m());
S_int_int s_i_i = (int x)-> x+1;
assertTrue(4 == s_i_i.m(3));
S_int_Integer s_i_I = (int x)-> x+1;
assertTrue(4 == s_i_I.m(3));
S_Integer_int s_I_i = (Integer x) -> x.intValue() + 1;
assertTrue(4 == s_I_i.m(3));
S_Integer_Integer s_I_I = (Integer x) -> x.intValue() + 1;
assertTrue(4 == s_I_I.m(3));
}
static void test1() {
S_int s_i = ()-> 3;
assertTrue(3 == s_i.m());
S_Integer s_I = ()-> 3;
assertTrue(3 == s_I.m());
S_int_int s_i_i = (int x)-> x+1;
assertTrue(4 == s_i_i.m(3));
S_int_Integer s_i_I = (int x)-> x+1;
assertTrue(4 == s_i_I.m(3));
S_Integer_int s_I_i = (Integer x) -> x.intValue() + 1;
assertTrue(4 == s_I_i.m(3));
S_Integer_Integer s_I_I = (Integer x) -> x.intValue() + 1;
assertTrue(4 == s_I_I.m(3));
}
void test2() {
S_int s_i = ()-> 3;
assertTrue(3 == s_i.m());
S_Integer s_I = ()-> 3;
assertTrue(3 == s_I.m());
S_int_int s_i_i = (int x)-> x+1;
assertTrue(4 == s_i_i.m(3));
S_int_Integer s_i_I = (int x)-> x+1;
assertTrue(4 == s_i_I.m(3));
S_Integer_int s_I_i = (Integer x) -> x.intValue() + 1;
assertTrue(4 == s_I_i.m(3));
S_Integer_Integer s_I_I = (Integer x) -> x.intValue() + 1;
assertTrue(4 == s_I_I.m(3));
}
public static void main(String[] args) {
test1();
new LambdaExpr01().test2();
assertTrue(assertionCount == 24);
}
}

View file

@ -0,0 +1,133 @@
/*
* 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
* basic test for simple lambda expressions in multiple scopes
* @author Brian Goetz
* @author Maurizio Cimadamore
* @run main LambdaExpr01
*/
public class LambdaExpr02 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface S_int {
int m();
}
interface S_Integer {
Integer m();
}
interface S_int_int {
int m(int i);
}
interface S_Integer_int {
int m(Integer i);
}
interface S_int_Integer {
Integer m(int i);
}
interface S_Integer_Integer {
Integer m(Integer i);
}
static {
S_int s_i = ()-> { return 3; };
assertTrue(3 == s_i.m());
S_Integer s_I = ()-> { return 3; };
assertTrue(3 == s_I.m());
S_int_int s_i_i = (int x) -> { return x + 1; };
assertTrue(4 == s_i_i.m(3));
S_int_Integer s_i_I = (int x) -> { return x + 1; };
assertTrue(4 == s_i_I.m(3));
S_Integer_int s_I_i = (Integer x) -> { return x.intValue() + 1; };
assertTrue(4 == s_I_i.m(3));
S_Integer_Integer s_I_I = (Integer x) -> { return x.intValue() + 1; };
assertTrue(4 == s_I_I.m(3));
}
{
S_int s_i = ()-> { return 3; };
assertTrue(3 == s_i.m());
S_Integer s_I = ()-> { return 3; };
assertTrue(3 == s_I.m());
S_int_int s_i_i = (int x) -> { return x + 1; };
assertTrue(4 == s_i_i.m(3));
S_int_Integer s_i_I = (int x) -> { return x + 1; };
assertTrue(4 == s_i_I.m(3));
S_Integer_int s_I_i = (Integer x) -> { return x.intValue() + 1; };
assertTrue(4 == s_I_i.m(3));
S_Integer_Integer s_I_I = (Integer x) -> { return x.intValue() + 1; };
assertTrue(4 == s_I_I.m(3));
}
static void test1() {
S_int s_i = ()-> { return 3; };
assertTrue(3 == s_i.m());
S_Integer s_I = ()-> { return 3; };
assertTrue(3 == s_I.m());
S_int_int s_i_i = (int x) -> { return x + 1; };
assertTrue(4 == s_i_i.m(3));
S_int_Integer s_i_I = (int x) -> { return x + 1; };
assertTrue(4 == s_i_I.m(3));
S_Integer_int s_I_i = (Integer x) -> { return x.intValue() + 1; };
assertTrue(4 == s_I_i.m(3));
S_Integer_Integer s_I_I = (Integer x) -> { return x.intValue() + 1; };
assertTrue(4 == s_I_I.m(3));
}
void test2() {
S_int s_i = ()-> { return 3; };
assertTrue(3 == s_i.m());
S_Integer s_I = ()-> { return 3; };
assertTrue(3 == s_I.m());
S_int_int s_i_i = (int x) -> { return x + 1; };
assertTrue(4 == s_i_i.m(3));
S_int_Integer s_i_I = (int x) -> { return x + 1; };
assertTrue(4 == s_i_I.m(3));
S_Integer_int s_I_i = (Integer x) -> { return x.intValue() + 1; };
assertTrue(4 == s_I_i.m(3));
S_Integer_Integer s_I_I = (Integer x) -> { return x.intValue() + 1; };
assertTrue(4 == s_I_I.m(3));
}
public static void main(String[] args) {
test1();
new LambdaExpr02().test2();
assertTrue(assertionCount == 24);
}
}

View file

@ -0,0 +1,46 @@
/*
* 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
* check that lambda initializers compile w/o problems
* @author Jan Lahoda
* @author Maurizio Cimadamore
* @compile LambdaExpr04.java
*/
class LambdaExpr04 {
interface SAM {
void m(int i);
}
static SAM lambda_01 = (int pos) -> { };
static final SAM lambda_02 = (int pos) -> { };
SAM lambda_03 = (int pos) -> { };
final SAM lambda_04 = (int pos) -> { };
}

View file

@ -0,0 +1,39 @@
/*
* 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
* check that binary expression in lambda expression is parsed correctly
* @author Maurizio Cimadamore
* @compile LambdaExpr05.java
*/
class LambdaExpr05 {
interface SAM { int foo(int i); }
SAM s1 = i -> i * 2;
SAM s2 = i -> 2 * i;
}

View file

@ -0,0 +1,51 @@
/*
* 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
* parser test for nested parenthesized lambda expression
* @run main LambdaExpr06
*/
public class LambdaExpr06 {
static void assertTrue(boolean cond) {
if (!cond)
throw new AssertionError();
}
interface A {
int m();
}
interface B {
int dup(int i);
}
public static void main(String[] args) {
A a = ()-> ((B)i -> i * 2).dup(3);
assertTrue(a.m() == 6);
}
}

View file

@ -0,0 +1,57 @@
/*
* 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
* check access to effectively final local variable from doubly nested lambda
* @run main LambdaExpr07
*/
public class LambdaExpr07 {
interface Block<A, R> {
R apply(A x);
}
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
String S = "A";
void test() {
Block<String, Block<String, String>> o = s1 -> s2 -> S + s1 + s2;
assertTrue(o.apply("B").apply("C").equals("ABC"));
}
public static void main(String[] args) {
new LambdaExpr07().test();
assertTrue(assertionCount == 1);
}
}

View file

@ -0,0 +1,43 @@
/*
* 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
* check that reference to local final variable w/o initializer is accepted
* @compile LambdaExpr08.java
*/
class LambdaExpr08 {
interface SAM {
String m();
}
void test() {
final String s;
s = "";
SAM sam = () -> s;
}
}

View file

@ -0,0 +1,54 @@
/*
* 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
* check that lambda in array initializers is correctly accepted
* @compile LambdaExpr09.java
*/
class LambdaExpr09 {
interface Block<T> {
void m(T t);
}
void apply(Object[] obj_arr) { }
void test1() {
Block<?>[] arr1 = { t -> { }, t -> { } };
Block<?>[][] arr2 = { { t -> { }, t -> { } }, { t -> { }, t -> { } } };
}
void test2() {
Block<?>[] arr1 = new Block<?>[]{ t -> { }, t -> { } };
Block<?>[][] arr2 = new Block<?>[][]{ { t -> { }, t -> { } }, { t -> { }, t -> { } } };
}
void test3() {
apply(new Block<?>[]{ t -> { }, t -> { } });
apply(new Block<?>[][]{ { t -> { }, t -> { } }, { t -> { }, t -> { } } });
}
}

View file

@ -0,0 +1,37 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that lambda in array initializers (with wrong type) are correctly rejected
* @compile/fail/ref=LambdaExpr10.out -XDrawDiagnostics LambdaExpr10.java
*/
class LambdaExpr10 {
interface Block<T> {
void m(T t);
}
void apply(Object[] obj_arr) { }
void test1() {
Object[] arr1 = { t -> { } };
Object[][] arr2 = { { t -> { } } };
}
void test2() {
Object[] arr1 = new Object[]{ t -> { } };
Object[][] arr2 = new Object[][]{ { t -> { } } };
}
void test3() {
apply(new Object[]{ t -> { } });
apply(new Object[][]{ { t -> { } } });
}
void test4() {
Block<?>[] arr1 = { t -> t };
Block<?>[] arr2 = new Block<?>[]{ t -> t };
apply(new Block<?>[]{ t -> { }, t -> { } });
}
}

View file

@ -0,0 +1,9 @@
LambdaExpr10.java:18:28: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
LambdaExpr10.java:19:32: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
LambdaExpr10.java:23:40: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
LambdaExpr10.java:24:46: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
LambdaExpr10.java:28:29: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
LambdaExpr10.java:29:33: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf)
LambdaExpr10.java:33:35: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: java.lang.Object, void))
LambdaExpr10.java:34:49: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: java.lang.Object, void))
8 errors

View file

@ -0,0 +1,89 @@
/*
* 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
* check that creating an inner class from a lambda does add a captured this
* @run main LambdaExpr11
*/
public class LambdaExpr11 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
class Inner {
Inner() { assertTrue(true); }
}
void test() {
Runnable r1 = ()-> { new Inner(); };
r1.run();
Runnable r2 = ()-> { new Inner() {}; };
r2.run();
Runnable r3 = ()-> { class SubInner extends Inner {}; new SubInner(); };
r3.run();
Runnable r4 = ()-> { class SubInner extends Inner {}; new SubInner() {}; };
r4.run();
new Inner2().test();
}
class Inner2 {
void test() {
Runnable r1 = ()-> { new Inner(); };
r1.run();
Runnable r2 = ()-> { new Inner() {}; };
r2.run();
Runnable r3 = ()-> { class SubInner extends Inner {}; new SubInner(); };
r3.run();
Runnable r4 = ()-> { class SubInner extends Inner {}; new SubInner() {}; };
r4.run();
new Inner3().test();
}
class Inner3 {
void test() {
Runnable r1 = ()-> { new Inner(); };
r1.run();
Runnable r2 = ()-> { new Inner() {}; };
r2.run();
Runnable r3 = ()-> { class SubInner extends Inner {}; new SubInner(); };
r3.run();
Runnable r4 = ()-> { class SubInner extends Inner {}; new SubInner() {}; };
r4.run();
}
}
}
public static void main(String[] args) {
new LambdaExpr11().test();
assertTrue(assertionCount == 12);
}
}

View file

@ -0,0 +1,66 @@
/*
* 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
* check that creating an inner class from a lambda does add a captured this
* @run main LambdaExpr12
*/
public class LambdaExpr12 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface Getter<X> {
X get();
}
interface Mapper<X,Y> {
Y map(X x);
}
void test() {
Mapper<String, Getter<Character>> mapper =
(final String s) -> new Getter<Character>() {
@Override
public Character get() {
return s.charAt(0);
}
};
assertTrue(mapper.map("First").get() == 'F');
}
public static void main(String[] args) {
new LambdaExpr12().test();
assertTrue(assertionCount == 1);
}
}

View file

@ -0,0 +1,47 @@
/*
* 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
* check that recursive lambda (through field ref) is accepted in all contexts
* @compile LambdaExpr13.java
*/
class LambdaExpr13 {
Runnable ir = () -> { ir.run(); };;
static Runnable sr = () -> { sr.run(); };
{ ir = () -> { ir.run(); }; }
static { sr = () -> { sr.run(); }; }
static void m1() {
sr = () -> { sr.run(); };
}
void m2() {
ir = () -> { ir.run(); };
}
}

View file

@ -0,0 +1,43 @@
/*
* 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
* check that recursion from doubly nested lambda is handled correctly
*/
public class LambdaExpr14 {
interface SAM {
SAM invoke();
}
static SAM local;
public static void main(String[] args) {
local = () -> () -> local.invoke();
local.invoke().invoke(); // Not a recursive lambda - exec should terminate
}
}

View file

@ -0,0 +1,67 @@
/*
* 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
* @ignore investigate as to whether code generation fails
* @bug 8003280
* @summary Add lambda tests
* check that nested inner class in statement lambdas don't get corrupted return statements
* @run main LambdaExpr15
*/
public class LambdaExpr15 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface Block<T> {
void apply(T t);
}
public static void main(String[] args) {
//anon class
Block<Object> ba1 = t -> {
new Object() {
String get() { return ""; }
};
assertTrue(t == 1);
};
ba1.apply(1);
//local class
Block<Object> ba2 = t -> {
class A {
String get() { return ""; }
};
new A();
assertTrue(t == 2);
};
ba2.apply(2);
assertTrue(assertionCount == 2);
}
}

View file

@ -0,0 +1,76 @@
/*
* 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
* check that super inside lambda is handled correctly
* @run main LambdaExpr16
*/
public class LambdaExpr16 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface A { void m(); }
static class Sup {
void m() {
assertTrue(true);
}
}
static class Sub extends Sup {
void testLambda1() {
A a = ()->{ super.m(); };
a.m();
}
void testLambda2() {
A a = () -> { A a1 = () -> { super.m(); }; a1.m(); };
a.m();
}
void testRef1() {
A a = () -> { A a1 = super::m; a1.m(); };
a.m();
}
void testRef2() {
A a = () -> { A a1 = () -> { A a2 = super::m; a2.m(); }; a1.m(); };
a.m();
}
}
public static void main(String[] args) {
Sub s = new Sub();
s.testLambda1();
s.testLambda2();
s.testRef1();
s.testRef2();
assertTrue(assertionCount == 4);
}
}

View file

@ -0,0 +1,63 @@
/*
* 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
* check that super in argument position inside lambda is handled correctly
* @run main LambdaExpr17
*/
public class LambdaExpr17 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface SAM {
void m();
}
static class Sup {
protected String m() {
assertTrue(true);
return "Hello!";
}
}
static class Sub extends Sup {
void test() {
SAM s = () -> { System.out.println(super.m()); };
s.m();
}
}
public static void main(String[] args) {
new Sub().test();
assertTrue(assertionCount == 1);
}
}

View file

@ -0,0 +1,62 @@
/*
* 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
* check that synthetic casts from outer environment are not inserted twice
* @run main LambdaExpr18
*/
public class LambdaExpr18 {
static int assertionCount = 0;
static void assertTrue(boolean cond) {
assertionCount++;
if (!cond)
throw new AssertionError();
}
interface SAM<R> {
R eval();
}
static void test(){
SAM<Integer> sam1 = () -> {
assertTrue(true);
SAM<String> sam2 = () -> {
assertTrue(true);
return "";
};
sam2.eval();
return 1;
};
sam1.eval();
}
public static void main(String[] args) {
test();
assertTrue(assertionCount == 2);
}
}

View file

@ -0,0 +1,53 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that inner scopes are left after a lambda check exception has been thrown
* @compile/fail/ref=LambdaExpr19.out -XDrawDiagnostics LambdaExpr19.java
*/
class LambdaExpr19 {
interface SAM {
String m();
}
void m(SAM s) { }
void testTry() {
m(() -> {
try { return 1; }
catch (Exception e) { }
});
}
void testTryWithResources() {
m(() -> {
try (AutoCloseable c = null) { return 1; }
catch (Exception e) { }
});
}
void testSwitch() {
m(() -> {
switch (1) {
default: return 1;
}
});
}
void testFor() {
m(() -> {
for (;;) {
return 1;
}
});
}
void testForeach() {
m(() -> {
for (Object o : new Object[] { null , null }) {
return 1;
}
});
}
}

View file

@ -0,0 +1,6 @@
LambdaExpr19.java:17:9: compiler.err.cant.apply.symbol: kindname.method, m, LambdaExpr19.SAM, @363, kindname.class, LambdaExpr19, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: int, java.lang.String)))
LambdaExpr19.java:24:9: compiler.err.cant.apply.symbol: kindname.method, m, LambdaExpr19.SAM, @512, kindname.class, LambdaExpr19, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: int, java.lang.String)))
LambdaExpr19.java:31:9: compiler.err.cant.apply.symbol: kindname.method, m, LambdaExpr19.SAM, @676, kindname.class, LambdaExpr19, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: int, java.lang.String)))
LambdaExpr19.java:39:9: compiler.err.cant.apply.symbol: kindname.method, m, LambdaExpr19.SAM, @824, kindname.class, LambdaExpr19, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: int, java.lang.String)))
LambdaExpr19.java:47:9: compiler.err.cant.apply.symbol: kindname.method, m, LambdaExpr19.SAM, @965, kindname.class, LambdaExpr19, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: int, java.lang.String)))
5 errors

View file

@ -0,0 +1,45 @@
/*
* 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
* check that default super call from lambda expression is compiled successfully
* @compile LambdaExpr20.java
*/
class LambdaExpr20 {
interface K {
default void m() { }
}
static class Test implements K {
@Override
public void m() {
Runnable r = () -> { K.super.m(); };
r.run();
}
}
}

View file

@ -0,0 +1,16 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @summary Add lambda tests
* check that lambda expression body (when not a block) cannot be void
* @author Maurizio Cimadamore
* @compile/fail/ref=LambdaExprNotVoid.out -XDlambdaInferenceDiags=false -XDrawDiagnostics LambdaExprNotVoid.java
*/
class LambdaExpr05 {
interface SAM { void foo(int i); }
SAM s1 = i -> i * 2;
SAM s2 = i -> 2 * i;
}

View file

@ -0,0 +1,3 @@
LambdaExprNotVoid.java:14:21: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: int, void))
LambdaExprNotVoid.java:15:21: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.inconvertible.types: int, void))
2 errors

View file

@ -24,7 +24,9 @@
/*
* @test
* @bug 7115050
* @summary Add parser support for lambda expressions
* @bug 8003280
* @summary Add lambda tests
* Add parser support for lambda expressions
*/
import com.sun.source.util.JavacTask;
@ -234,7 +236,7 @@ public class LambdaParserTest {
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
Arrays.asList("-XDallowLambda"), null, Arrays.asList(source));
null, null, Arrays.asList(source));
try {
ct.parse();
} catch (Throwable ex) {

Some files were not shown because too many files have changed in this diff Show more