undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2025, 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 8003639
|
||||
* @summary convert lambda testng tests to jtreg and add them
|
||||
* @run junit InInterface
|
||||
*/
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
interface LTII {
|
||||
|
||||
interface ILsp1 {
|
||||
String m();
|
||||
}
|
||||
|
||||
interface ILsp2 {
|
||||
String m(String x);
|
||||
}
|
||||
|
||||
default ILsp1 t1() {
|
||||
return () -> { return "yo"; };
|
||||
}
|
||||
|
||||
default ILsp2 t2() {
|
||||
return (x) -> { return "snur" + x; };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class InInterface implements LTII {
|
||||
|
||||
@Test
|
||||
public void testLambdaInDefaultMethod() {
|
||||
assertEquals("yo", t1().m());
|
||||
assertEquals("snurp", t2().m("p"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2025, 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 8003639
|
||||
* @summary convert lambda testng tests to jtreg and add them
|
||||
* @run junit InnerConstructor
|
||||
*/
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class InnerConstructor {
|
||||
|
||||
@Test
|
||||
public void testLambdaWithInnerConstructor() {
|
||||
assertEquals("Cbl:nada", seq1().m().toString());
|
||||
assertEquals("Cbl:rats", seq2().m("rats").toString());
|
||||
}
|
||||
|
||||
Ib1 seq1() {
|
||||
return () -> { return new Cbl(); };
|
||||
}
|
||||
|
||||
Ib2 seq2() {
|
||||
return (x) -> { return new Cbl(x); };
|
||||
}
|
||||
|
||||
class Cbl {
|
||||
String val;
|
||||
|
||||
Cbl() {
|
||||
this.val = "nada";
|
||||
}
|
||||
|
||||
Cbl(String z) {
|
||||
this.val = z;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Cbl:" + val;
|
||||
}
|
||||
}
|
||||
|
||||
interface Ib1 {
|
||||
Object m();
|
||||
}
|
||||
|
||||
interface Ib2 {
|
||||
Object m(String x);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,233 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2025, 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 8003639
|
||||
* @summary convert lambda testng tests to jtreg and add them
|
||||
* @run junit/othervm -Duser.language=en -Duser.country=US LambdaTranslationTest1
|
||||
*/
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class LambdaTranslationTest1 extends LT1Sub {
|
||||
|
||||
String cntxt = "blah";
|
||||
|
||||
private static final ThreadLocal<Object> result = new ThreadLocal<>();
|
||||
|
||||
private static void setResult(Object s) { result.set(s); }
|
||||
private static void appendResult(Object s) { result.set(result.get().toString() + s); }
|
||||
|
||||
private static void assertResult(String expected) {
|
||||
assertEquals(expected, result.get().toString());
|
||||
}
|
||||
|
||||
static Integer count(String s) {
|
||||
return s.length();
|
||||
}
|
||||
|
||||
static int icount(String s) {
|
||||
return s.length();
|
||||
}
|
||||
|
||||
static void eye(Integer i) {
|
||||
setResult(String.format("I:%d", i));
|
||||
}
|
||||
|
||||
static void ieye(int i) {
|
||||
setResult(String.format("i:%d", i));
|
||||
}
|
||||
|
||||
static void deye(double d) {
|
||||
setResult(String.format("d:%f", d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLambdas() {
|
||||
TBlock<Object> b = t -> {setResult("Sink0::" + t);};
|
||||
b.apply("Howdy");
|
||||
assertResult("Sink0::Howdy");
|
||||
|
||||
TBlock<String> b1 = t -> {setResult("Sink1::" + t);};
|
||||
b1.apply("Rowdy");
|
||||
assertResult("Sink1::Rowdy");
|
||||
|
||||
for (int i = 5; i < 10; ++i) {
|
||||
TBlock<Integer> b2 = t -> {setResult("Sink2::" + t);};
|
||||
b2.apply(i);
|
||||
assertResult("Sink2::" + i);
|
||||
}
|
||||
|
||||
TBlock<Integer> b3 = t -> {setResult("Sink3::" + t);};
|
||||
for (int i = 900; i > 0; i -= 100) {
|
||||
b3.apply(i);
|
||||
assertResult("Sink3::" + i);
|
||||
}
|
||||
|
||||
cntxt = "blah";
|
||||
TBlock<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));};
|
||||
b4.apply("Yor");
|
||||
assertResult("b4: blah .. Yor");
|
||||
|
||||
String flaw = "flaw";
|
||||
TBlock<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));};
|
||||
b5.apply("BB");
|
||||
assertResult("b5: flaw .. BB");
|
||||
|
||||
cntxt = "flew";
|
||||
TBlock<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));};
|
||||
b6.apply("flee");
|
||||
assertResult("b6: flee .. flew .. flaw");
|
||||
|
||||
TBlock<String> b7 = t -> {setResult(String.format("b7: %s %s", t, this.protectedSuperclassMethod()));};
|
||||
b7.apply("this:");
|
||||
assertResult("b7: this: instance:flew");
|
||||
|
||||
TBlock<String> b8 = t -> {setResult(String.format("b8: %s %s", t, super.protectedSuperclassMethod()));};
|
||||
b8.apply("super:");
|
||||
assertResult("b8: super: I'm the sub");
|
||||
|
||||
TBlock<String> b7b = t -> {setResult(String.format("b9: %s %s", t, protectedSuperclassMethod()));};
|
||||
b7b.apply("implicit this:");
|
||||
assertResult("b9: implicit this: instance:flew");
|
||||
|
||||
TBlock<Object> b10 = t -> {setResult(String.format("b10: new LT1Thing: %s", (new LT1Thing(t)).str));};
|
||||
b10.apply("thing");
|
||||
assertResult("b10: new LT1Thing: thing");
|
||||
|
||||
TBlock<Object> b11 = t -> {setResult(String.format("b11: %s", (new LT1Thing(t) {
|
||||
String get() {
|
||||
return "*" + str.toString() + "*";
|
||||
}
|
||||
}).get()));};
|
||||
b11.apply(999);
|
||||
assertResult("b11: *999*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodRefs() {
|
||||
LT1IA ia = LambdaTranslationTest1::eye;
|
||||
ia.doit(1234);
|
||||
assertResult("I:1234");
|
||||
|
||||
LT1IIA iia = LambdaTranslationTest1::ieye;
|
||||
iia.doit(1234);
|
||||
assertResult("i:1234");
|
||||
|
||||
LT1IA da = LambdaTranslationTest1::deye;
|
||||
da.doit(1234);
|
||||
assertResult("d:1234.000000");
|
||||
|
||||
LT1SA a = LambdaTranslationTest1::count;
|
||||
assertEquals((Integer) 5, a.doit("howdy"));
|
||||
|
||||
a = LambdaTranslationTest1::icount;
|
||||
assertEquals((Integer) 6, a.doit("shower"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInner() throws Exception {
|
||||
(new In()).doInner();
|
||||
}
|
||||
|
||||
protected String protectedSuperclassMethod() {
|
||||
return "instance:" + cntxt;
|
||||
}
|
||||
|
||||
private class In {
|
||||
|
||||
private int that = 1234;
|
||||
|
||||
void doInner() {
|
||||
TBlock<String> i4 = t -> {setResult(String.format("i4: %d .. %s", that, t));};
|
||||
i4.apply("=1234");
|
||||
assertResult("i4: 1234 .. =1234");
|
||||
|
||||
TBlock<String> i5 = t -> {setResult(""); appendResult(t); appendResult(t);};
|
||||
i5.apply("fruit");
|
||||
assertResult("fruitfruit");
|
||||
|
||||
cntxt = "human";
|
||||
TBlock<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));};
|
||||
b4.apply("bin");
|
||||
assertResult("b4: human .. bin");
|
||||
|
||||
final String flaw = "flaw";
|
||||
|
||||
/**
|
||||
Callable<String> c5 = () -> "["+flaw+"]" ;
|
||||
System.out.printf("c5: %s\n", c5.call() );
|
||||
**/
|
||||
|
||||
TBlock<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));};
|
||||
b5.apply("BB");
|
||||
assertResult("b5: flaw .. BB");
|
||||
|
||||
cntxt = "borg";
|
||||
TBlock<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));};
|
||||
b6.apply("flee");
|
||||
assertResult("b6: flee .. borg .. flaw");
|
||||
|
||||
TBlock<String> b7b = t -> {setResult(String.format("b7b: %s %s", t, protectedSuperclassMethod()));};
|
||||
b7b.apply("implicit outer this");
|
||||
assertResult("b7b: implicit outer this instance:borg");
|
||||
|
||||
/**
|
||||
TBlock<Object> b9 = t -> { System.out.printf("New: %s\n", (new LT1Thing(t)).str); };
|
||||
b9.apply("thing");
|
||||
|
||||
TBlock<Object> ba = t -> { System.out.printf("Def: %s\n", (new LT1Thing(t) { String get() { return "*" + str.toString() +"*";}}).get() ); };
|
||||
ba.apply(999);
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LT1Sub {
|
||||
protected String protectedSuperclassMethod() {
|
||||
return "I'm the sub";
|
||||
}
|
||||
}
|
||||
|
||||
class LT1Thing {
|
||||
final Object str;
|
||||
|
||||
LT1Thing(Object s) {
|
||||
str = s;
|
||||
}
|
||||
}
|
||||
|
||||
interface LT1SA {
|
||||
Integer doit(String s);
|
||||
}
|
||||
|
||||
interface LT1IA {
|
||||
void doit(int i);
|
||||
}
|
||||
|
||||
interface LT1IIA {
|
||||
void doit(Integer i);
|
||||
}
|
||||
|
|
@ -0,0 +1,368 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2025, 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 8003639
|
||||
* @summary convert lambda testng tests to jtreg and add them
|
||||
* @run junit/othervm -Duser.language=en -Duser.country=US LambdaTranslationTest2
|
||||
*/
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* LambdaTranslationTest2 -- end-to-end smoke tests for lambda evaluation
|
||||
*/
|
||||
|
||||
public class LambdaTranslationTest2 {
|
||||
|
||||
final String dummy = "dummy";
|
||||
|
||||
@Test
|
||||
public void testLambdas() {
|
||||
TPredicate<String> isEmpty = s -> s.isEmpty();
|
||||
assertTrue(isEmpty.test(""));
|
||||
assertTrue(!isEmpty.test("foo"));
|
||||
|
||||
TPredicate<Object> oIsEmpty = s -> ((String) s).isEmpty();
|
||||
assertTrue(oIsEmpty.test(""));
|
||||
assertTrue(!oIsEmpty.test("foo"));
|
||||
|
||||
TPredicate<Object> alwaysTrue = o -> true;
|
||||
assertTrue(alwaysTrue.test(""));
|
||||
assertTrue(alwaysTrue.test(null));
|
||||
|
||||
TPredicate<Object> alwaysFalse = o -> false;
|
||||
assertTrue(!alwaysFalse.test(""));
|
||||
assertTrue(!alwaysFalse.test(null));
|
||||
|
||||
// tests local capture
|
||||
String foo = "foo";
|
||||
TPredicate<String> equalsFoo = s -> s.equals(foo);
|
||||
assertTrue(!equalsFoo.test(""));
|
||||
assertTrue(equalsFoo.test("foo"));
|
||||
|
||||
// tests instance capture
|
||||
TPredicate<String> equalsDummy = s -> s.equals(dummy);
|
||||
assertTrue(!equalsDummy.test(""));
|
||||
assertTrue(equalsDummy.test("dummy"));
|
||||
|
||||
TMapper<Object, Object> ident = s -> s;
|
||||
|
||||
assertEquals("blarf", ident.map("blarf"));
|
||||
assertEquals("wooga", ident.map("wooga"));
|
||||
assertTrue("wooga" == ident.map("wooga"));
|
||||
|
||||
// constant capture
|
||||
TMapper<Object, Object> prefixer = s -> "p" + s;
|
||||
assertEquals("pblarf", prefixer.map("blarf"));
|
||||
assertEquals("pwooga", prefixer.map("wooga"));
|
||||
|
||||
// instance capture
|
||||
TMapper<Object, Object> prefixer2 = s -> dummy + s;
|
||||
assertEquals("dummyblarf", prefixer2.map("blarf"));
|
||||
assertEquals("dummywooga", prefixer2.map("wooga"));
|
||||
}
|
||||
|
||||
interface Factory<T> {
|
||||
T make();
|
||||
}
|
||||
|
||||
interface StringFactory extends Factory<String> { }
|
||||
|
||||
interface StringFactory2 extends Factory<String> {
|
||||
String make();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBridges() {
|
||||
Factory<String> of = () -> "y";
|
||||
Factory<?> ef = () -> "z";
|
||||
|
||||
assertEquals("y", of.make());
|
||||
assertEquals("y", ((Factory<?>) of).make());
|
||||
assertEquals("y", ((Factory) of).make());
|
||||
|
||||
assertEquals("z", ef.make());
|
||||
assertEquals("z", ((Factory) ef).make());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBridgesImplicitSpecialization() {
|
||||
StringFactory sf = () -> "x";
|
||||
|
||||
assertEquals("x", sf.make());
|
||||
assertEquals("x", ((Factory<String>) sf).make());
|
||||
assertEquals("x", ((Factory<?>) sf).make());
|
||||
assertEquals("x", ((Factory) sf).make());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBridgesExplicitSpecialization() {
|
||||
StringFactory2 sf = () -> "x";
|
||||
|
||||
assertEquals("x", sf.make());
|
||||
assertEquals("x", ((Factory<String>) sf).make());
|
||||
assertEquals("x", ((Factory<?>) sf).make());
|
||||
assertEquals("x", ((Factory) sf).make());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuperCapture() {
|
||||
class A {
|
||||
String make() { return "x"; }
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
void testSuperCapture() {
|
||||
StringFactory sf = () -> super.make();
|
||||
assertEquals("x", sf.make());
|
||||
}
|
||||
}
|
||||
|
||||
new B().testSuperCapture();
|
||||
}
|
||||
|
||||
interface WidenD {
|
||||
public String m(float a0, double a1);
|
||||
}
|
||||
|
||||
interface WidenS {
|
||||
public String m(byte a0, short a1);
|
||||
}
|
||||
|
||||
interface WidenI {
|
||||
public String m(byte a0, short a1, char a2, int a3);
|
||||
}
|
||||
|
||||
interface WidenL {
|
||||
public String m(byte a0, short a1, char a2, int a3, long a4);
|
||||
}
|
||||
|
||||
interface Box {
|
||||
public String m(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7);
|
||||
}
|
||||
|
||||
static String pb(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7) {
|
||||
return String.format("b%d s%d c%c i%d j%d z%b f%f d%f", a0, a1, a2, a3, a4, a5, a6, a7);
|
||||
}
|
||||
|
||||
static String pwI1(int a0, int a1, int a2, int a3) {
|
||||
return String.format("b%d s%d c%d i%d", a0, a1, a2, a3);
|
||||
}
|
||||
|
||||
static String pwI2(Integer a0, Integer a1, Integer a2, Integer a3) {
|
||||
return String.format("b%d s%d c%d i%d", a0, a1, a2, a3);
|
||||
}
|
||||
|
||||
static String pwL1(long a0, long a1, long a2, long a3, long a4) {
|
||||
return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4);
|
||||
}
|
||||
|
||||
static String pwL2(Long a0, Long a1, Long a2, Long a3, Long a4) {
|
||||
return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4);
|
||||
}
|
||||
|
||||
static String pwS1(short a0, short a1) {
|
||||
return String.format("b%d s%d", a0, a1);
|
||||
}
|
||||
|
||||
static String pwS2(Short a0, Short a1) {
|
||||
return String.format("b%d s%d", a0, a1);
|
||||
}
|
||||
|
||||
static String pwD1(double a0, double a1) {
|
||||
return String.format("f%f d%f", a0, a1);
|
||||
}
|
||||
|
||||
static String pwD2(Double a0, Double a1) {
|
||||
return String.format("f%f d%f", a0, a1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveWidening() {
|
||||
WidenS ws1 = LambdaTranslationTest2::pwS1;
|
||||
assertEquals("b1 s2", ws1.m((byte) 1, (short) 2));
|
||||
|
||||
WidenD wd1 = LambdaTranslationTest2::pwD1;
|
||||
assertEquals("f1.000000 d2.000000", wd1.m(1.0f, 2.0));
|
||||
|
||||
WidenI wi1 = LambdaTranslationTest2::pwI1;
|
||||
assertEquals("b1 s2 c3 i4", wi1.m((byte) 1, (short) 2, (char) 3, 4));
|
||||
|
||||
WidenL wl1 = LambdaTranslationTest2::pwL1;
|
||||
assertEquals("b1 s2 c3 i4 j5", wl1.m((byte) 1, (short) 2, (char) 3, 4, 5L));
|
||||
|
||||
// @@@ TODO: clarify spec on widen+box conversion
|
||||
}
|
||||
|
||||
interface Unbox {
|
||||
public String m(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7);
|
||||
}
|
||||
|
||||
static String pu(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7) {
|
||||
return String.format("b%d s%d c%c i%d j%d z%b f%f d%f", a0, a1, a2, a3, a4, a5, a6, a7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnboxing() {
|
||||
Unbox u = LambdaTranslationTest2::pu;
|
||||
assertEquals("b1 s2 cA i4 j5 ztrue f6.000000 d7.000000", u.m((byte)1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoxing() {
|
||||
Box b = LambdaTranslationTest2::pb;
|
||||
assertEquals("b1 s2 cA i4 j5 ztrue f6.000000 d7.000000", b.m((byte) 1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0));
|
||||
}
|
||||
|
||||
static boolean cc(Object o) {
|
||||
return ((String) o).equals("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArgCastingAdaptation() {
|
||||
TPredicate<String> p = LambdaTranslationTest2::cc;
|
||||
assertTrue(p.test("foo"));
|
||||
assertTrue(!p.test("bar"));
|
||||
}
|
||||
|
||||
interface SonOfPredicate<T> extends TPredicate<T> { }
|
||||
|
||||
@Test
|
||||
public void testExtendsSAM() {
|
||||
SonOfPredicate<String> p = s -> s.isEmpty();
|
||||
assertTrue(p.test(""));
|
||||
assertTrue(!p.test("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorRef() {
|
||||
Factory<List<String>> lf = ArrayList<String>::new;
|
||||
List<String> list = lf.make();
|
||||
assertTrue(list instanceof ArrayList);
|
||||
assertTrue(list != lf.make());
|
||||
list.add("a");
|
||||
assertEquals("[a]", list.toString());
|
||||
}
|
||||
|
||||
private static String privateMethod() {
|
||||
return "private";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrivateMethodRef() {
|
||||
Factory<String> sf = LambdaTranslationTest2::privateMethod;
|
||||
assertEquals("private", sf.make());
|
||||
}
|
||||
|
||||
private interface PrivateIntf {
|
||||
String make();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrivateIntf() {
|
||||
PrivateIntf p = () -> "foo";
|
||||
assertEquals("foo", p.make());
|
||||
}
|
||||
|
||||
interface Op<T> {
|
||||
public T op(T a, T b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoxToObject() {
|
||||
Op<Integer> maxer = Math::max;
|
||||
for (int i=-100000; i < 100000; i += 100)
|
||||
for (int j=-100000; j < 100000; j += 99) {
|
||||
assertEquals(Math.max(i,j), (int) maxer.op(i,j));
|
||||
}
|
||||
}
|
||||
|
||||
protected static String protectedMethod() {
|
||||
return "protected";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProtectedMethodRef() {
|
||||
Factory<String> sf = LambdaTranslationTest2::protectedMethod;
|
||||
assertEquals("protected", sf.make());
|
||||
}
|
||||
|
||||
class Inner1 {
|
||||
String m1() {
|
||||
return "Inner1.m1()";
|
||||
}
|
||||
|
||||
class Inner2 {
|
||||
public String m1() {
|
||||
return "Inner1.Inner2.m1()";
|
||||
}
|
||||
|
||||
protected String m2() {
|
||||
return "Inner1.Inner2.m2()";
|
||||
}
|
||||
|
||||
String m3() {
|
||||
return "Inner1.Inner2.m3()";
|
||||
}
|
||||
|
||||
class Inner3<T> {
|
||||
T t = null;
|
||||
Inner3(T t) {
|
||||
this.t = t;
|
||||
}
|
||||
T m1() {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInnerClassMethodRef() {
|
||||
Factory<String> fs = new Inner1()::m1;
|
||||
assertEquals("Inner1.m1()", fs.make());
|
||||
|
||||
fs = new Inner1().new Inner2()::m1;
|
||||
assertEquals("Inner1.Inner2.m1()", fs.make());
|
||||
|
||||
fs = new Inner1().new Inner2()::m2;
|
||||
assertEquals("Inner1.Inner2.m2()", fs.make());
|
||||
|
||||
fs = new Inner1().new Inner2()::m3;
|
||||
assertEquals("Inner1.Inner2.m3()", fs.make());
|
||||
|
||||
fs = new Inner1().new Inner2().new Inner3<String>("Inner1.Inner2.Inner3")::m1;
|
||||
assertEquals("Inner1.Inner2.Inner3", fs.make());
|
||||
|
||||
Factory<Integer> fsi = new Inner1().new Inner2().new Inner3<Integer>(100)::m1;
|
||||
assertEquals(100, (int)fsi.make());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Performs operations upon an input object which may modify that object and/or
|
||||
* external state (other objects).
|
||||
*
|
||||
* <p>All block implementations are expected to:
|
||||
* <ul>
|
||||
* <li>When used for aggregate operations upon many elements blocks
|
||||
* should not assume that the {@code apply} operation will be called upon
|
||||
* elements in any specific order.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <T> The type of input objects to {@code apply}.
|
||||
*/
|
||||
public interface TBlock<T> {
|
||||
|
||||
/**
|
||||
* Performs operations upon the provided object which may modify that object
|
||||
* and/or external state.
|
||||
*
|
||||
* @param t an input object
|
||||
*/
|
||||
void apply(T t);
|
||||
|
||||
/**
|
||||
* Returns a Block which performs in sequence the {@code apply} methods of
|
||||
* multiple Blocks. This Block's {@code apply} method is performed followed
|
||||
* by the {@code apply} method of the specified Block operation.
|
||||
*
|
||||
* @param other an additional Block which will be chained after this Block
|
||||
* @return a Block which performs in sequence the {@code apply} method of
|
||||
* this Block and the {@code apply} method of the specified Block operation
|
||||
*/
|
||||
public default TBlock<T> chain(TBlock<? super T> other) {
|
||||
return (T t) -> { apply(t); other.apply(t); };
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Given an input object maps to an appropriate output object. A mapper may
|
||||
* variously provide a mapping between types, object instances or keys and
|
||||
* values or any other form of transformation upon the input.
|
||||
*
|
||||
* <p/>All mapper implementations are expected to:
|
||||
* <ul>
|
||||
* <li>Provide stable results such that for any {@code t} the result of two
|
||||
* {@code map} operations are always equivalent. ie.<pre>
|
||||
* Foo one = mapper.map(a);
|
||||
* Foo two = mapper.map(a);
|
||||
*
|
||||
* assert one.equals(two) && two.equals(one);
|
||||
* </pre></li>
|
||||
* <li>Equivalent input objects should map to equivalent output objects. ie.<pre>
|
||||
* assert a.equals(b); // a and b are equivalent
|
||||
*
|
||||
* Foo x = mapper.map(a);
|
||||
* Foo y = mapper.map(b);
|
||||
*
|
||||
* assert x.equals(y); // their mapped results should be as equivalent.
|
||||
* </pre></li>
|
||||
* <li>The mapper should not modify the input object in any way that would
|
||||
* change the mapping.</li>
|
||||
* <li>When used for aggregate operations upon many elements mappers
|
||||
* should not assume that the {@code map} operation will be called upon elements
|
||||
* in any specific order.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <R> the type of output objects from {@code map} operation. May be the
|
||||
* @param <T> the type of input objects provided to the {@code map} operation.
|
||||
* same type as {@code <T>}.
|
||||
*/
|
||||
public interface TMapper<R, T> {
|
||||
|
||||
/**
|
||||
* Map the provided input object to an appropriate output object.
|
||||
*
|
||||
* @param t the input object to be mapped.
|
||||
* @return the mapped output object.
|
||||
*/
|
||||
R map(T t);
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Determines if the input object matches some criteria.
|
||||
*
|
||||
* <p>All predicate implementations are expected to:
|
||||
* <ul>
|
||||
* <li>Provide stable results such that for any {@code t} the result of two
|
||||
* {@code eval} operations are always equivalent. ie.<pre>
|
||||
* boolean one = predicate.test(a);
|
||||
* boolean two = predicate.test(a);
|
||||
*
|
||||
* assert one == two;
|
||||
* </pre></li>
|
||||
* <li>Equivalent input objects should map to equivalent output objects. ie.<pre>
|
||||
* assert a.equals(b); // a and b are equivalent
|
||||
*
|
||||
* boolean x = predicate.test(a);
|
||||
* boolean y = predicate.test(ab;
|
||||
*
|
||||
* assert x == y; // their test results should be the same.
|
||||
* </pre></li>
|
||||
* <li>The predicate should not modify the input object in any way that would
|
||||
* change the evaluation.</li>
|
||||
* <li>When used for aggregate operations upon many elements predicates
|
||||
* should not assume that the {@code test} operation will be called upon
|
||||
* elements in any specific order.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <T> the type of input objects provided to {@code test}.
|
||||
*/
|
||||
public interface TPredicate<T> {
|
||||
|
||||
/**
|
||||
* Return {@code true} if the input object matches some criteria.
|
||||
*
|
||||
* @param t the input object.
|
||||
* @return {@code true} if the input object matched some criteria.
|
||||
*/
|
||||
boolean test(T t);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue