/* * Copyright (c) 2009, 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.
*/
/* @test * @summary example code used in javadoc for java.lang.invoke API * @compile JavaDocExamplesTest.java * @run testng/othervm test.java.lang.invoke.JavaDocExamplesTest
*/
/** * @author jrose
*/ publicclass JavaDocExamplesTest { /** Wrapper for running the TestNG tests in this module. * Put TestNG on the classpath!
*/ publicstaticvoid main(String... ignore) throws Throwable { new JavaDocExamplesTest().run();
}
staticint one(int k) { return 1; } staticint inc(int i, int acc, int k) { return i + 1; } staticint mult(int i, int acc, int k) { return i * acc; } staticboolean pred(int i, int acc, int k) { return i < k; } staticint fin(int i, int acc, int k) { return acc; }
@Test publicvoid testLoop() throws Throwable {
MethodHandle MH_inc, MH_one, MH_mult, MH_pred, MH_fin; Class<?> I = int.class;
MH_inc = LOOKUP.findStatic(THIS_CLASS, "inc", methodType(I, I, I, I));
MH_one = LOOKUP.findStatic(THIS_CLASS, "one", methodType(I, I));
MH_mult = LOOKUP.findStatic(THIS_CLASS, "mult", methodType(I, I, I, I));
MH_pred = LOOKUP.findStatic(THIS_CLASS, "pred", methodType(boolean.class, I, I, I));
MH_fin = LOOKUP.findStatic(THIS_CLASS, "fin", methodType(I, I, I, I));
{{
{} /// JAVADOC // iterative implementation of the factorial function as a loop handle // null initializer for counter, should initialize to 0
MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(120, loop.invoke(5));
{}
}}
}
staticint inc(int i) { return i + 1; } // drop acc, k staticint mult(int i, int acc) { return i * acc; } //drop k staticboolean cmp(int i, int k) { return i < k; }
@Test publicvoid testSimplerLoop() throws Throwable {
MethodHandle MH_inc, MH_mult, MH_cmp; Class<?> I = int.class;
MH_inc = LOOKUP.findStatic(THIS_CLASS, "inc", methodType(I, I));
MH_mult = LOOKUP.findStatic(THIS_CLASS, "mult", methodType(I, I, I));
MH_cmp = LOOKUP.findStatic(THIS_CLASS, "cmp", methodType(boolean.class, I, I));
{{
{} /// JAVADOC // simplified implementation of the factorial function as a loop handle // null initializer for counter, should initialize to 0
MethodHandle MH_one = MethodHandles.constant(int.class, 1);
MethodHandle MH_pred = MethodHandles.dropArguments(MH_cmp, 1, int.class); // drop acc
MethodHandle MH_fin = MethodHandles.dropArguments(MethodHandles.identity(int.class), 0, int.class); // drop i
MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(720, loop.invoke(6));
{}
}}
}
// for testFacLoop
{} staticclass FacLoop { finalint k;
FacLoop(int k) { this.k = k; } int inc(int i) { return i + 1; } int mult(int i, int acc) { return i * acc; } boolean pred(int i) { return i < k; } int fin(int i, int acc) { return acc; }
}
{}
// assume MH_inc, MH_mult, and MH_pred are handles to the above methods
@Test publicvoid testFacLoop() throws Throwable {
MethodHandle MH_FacLoop, MH_inc, MH_mult, MH_pred, MH_fin; Class<?> I = int.class;
MH_FacLoop = LOOKUP.findConstructor(FacLoop.class, methodType(void.class, I));
MH_inc = LOOKUP.findVirtual(FacLoop.class, "inc", methodType(I, I));
MH_mult = LOOKUP.findVirtual(FacLoop.class, "mult", methodType(I, I, I));
MH_pred = LOOKUP.findVirtual(FacLoop.class, "pred", methodType(boolean.class, I));
MH_fin = LOOKUP.findVirtual(FacLoop.class, "fin", methodType(I, I, I));
{{
{} /// JAVADOC // instance-based implementation of the factorial function as a loop handle // null initializer for counter, should initialize to 0
MethodHandle MH_one = MethodHandles.constant(int.class, 1);
MethodHandle[] instanceClause = new MethodHandle[]{MH_FacLoop};
MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
MethodHandle loop = MethodHandles.loop(instanceClause, counterClause, accumulatorClause);
assertEquals(5040, loop.invoke(7));
{}
}}
}
static List<String> initZip(Iterator<String> a, Iterator<String> b) { returnnew ArrayList<>(); } staticboolean zipPred(List<String> zip, Iterator<String> a, Iterator<String> b) { return a.hasNext() && b.hasNext(); } static List<String> zipStep(List<String> zip, Iterator<String> a, Iterator<String> b) {
zip.add(a.next());
zip.add(b.next()); return zip;
}
@Test publicvoid testWhileLoop() throws Throwable {
MethodHandle MH_initZip, MH_zipPred, MH_zipStep; Class<?> IT = Iterator.class; Class<?> L = List.class;
MH_initZip = LOOKUP.findStatic(THIS_CLASS, "initZip", methodType(L, IT, IT));
MH_zipPred = LOOKUP.findStatic(THIS_CLASS, "zipPred", methodType(boolean.class, L, IT, IT));
MH_zipStep = LOOKUP.findStatic(THIS_CLASS, "zipStep", methodType(L, L, IT, IT));
{{
{} /// JAVADOC // implement the zip function for lists as a loop handle
MethodHandle loop = MethodHandles.whileLoop(MH_initZip, MH_zipPred, MH_zipStep);
List<String> a = Arrays.asList("a", "b", "c", "d");
List<String> b = Arrays.asList("e", "f", "g", "h");
List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h");
assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator()));
{}
}}
}
staticint zero(int limit) { return 0; } staticint step(int i, int limit) { return i + 1; } staticboolean pred(int i, int limit) { return i < limit; }
@Test publicvoid testDoWhileLoop() throws Throwable {
MethodHandle MH_zero, MH_step, MH_pred; Class<?> I = int.class;
MH_zero = LOOKUP.findStatic(THIS_CLASS, "zero", methodType(I, I));
MH_step = LOOKUP.findStatic(THIS_CLASS, "step", methodType(I, I, I));
MH_pred = LOOKUP.findStatic(THIS_CLASS, "pred", methodType(boolean.class, I, I));
{{
{} /// JAVADOC // int i = 0; while (i < limit) { ++i; } return i; => limit
MethodHandle loop = MethodHandles.doWhileLoop(MH_zero, MH_step, MH_pred);
assertEquals(23, loop.invoke(23));
{}
}}
}
static String step(String v, int counter, String start_) { return"na " + v; } //#0 static String step(String v, int counter ) { return"na " + v; } //#1 static String step(String v, int counter, int iterations_, String pre, String start_) { return pre + " " + v; } //#2 static String step3(String v, int counter, String pre) { return pre + " " + v; } //#3
@Test publicvoid testCountedLoop() throws Throwable {
MethodHandle MH_step; Class<?> S = String.class, I = int.class; // Theme:
MH_step = LOOKUP.findStatic(THIS_CLASS, "step", methodType(S, S, I, S));
{{
{} /// JAVADOC // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; // => a variation on a well known theme
MethodHandle fit13 = MethodHandles.constant(int.class, 13);
MethodHandle start = MethodHandles.identity(String.class);
MethodHandle loop = MethodHandles.countedLoop(fit13, start, MH_step); // (v, i, _) -> "na " + v
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
{}
}} // Variation #1:
MH_step = LOOKUP.findStatic(THIS_CLASS, "step", methodType(S, S, I));
{{
{} /// JAVADOC // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; // => a variation on a well known theme
MethodHandle count = MethodHandles.dropArguments(MethodHandles.identity(int.class), 1, String.class);
MethodHandle start = MethodHandles.dropArguments(MethodHandles.identity(String.class), 0, int.class);
MethodHandle loop = MethodHandles.countedLoop(count, start, MH_step); // (v, i) -> "na " + v
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke(13, "Lambdaman!"));
{}
assertEquals("na na Lambdaman!", loop.invoke(2, "Lambdaman!"));
assertEquals("Lambdaman!", loop.invoke(0, "Lambdaman!"));
assertEquals("Lambdaman!", loop.invoke(-1, "Lambdaman!"));
assertEquals("Lambdaman!", loop.invoke(Integer.MIN_VALUE, "Lambdaman!"));
}} // Variation #2:
MH_step = LOOKUP.findStatic(THIS_CLASS, "step", methodType(S, S, I, I, S, S));
{{
{} /// JAVADOC // String s = "Lambdaman!", t = "na"; for (int i = 0; i < 13; ++i) { s = t + " " + s; } return s; // => a variation on a well known theme
MethodHandle count = MethodHandles.identity(int.class);
MethodHandle start = MethodHandles.dropArguments(MethodHandles.identity(String.class), 0, int.class, String.class);
MethodHandle loop = MethodHandles.countedLoop(count, start, MH_step); // (v, i, _, pre, _) -> pre + " " + v
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke(13, "na", "Lambdaman!"));
{}
}} // Variation #3:
MH_step = LOOKUP.findStatic(THIS_CLASS, "step3", methodType(S, S, I, S));
{{
{} /// JAVADOC // String s = "Lambdaman!", t = "na"; for (int i = 0; i < 13; ++i) { s = t + " " + s; } return s; // => a variation on a well known theme
MethodType loopType = methodType(String.class, String.class, int.class, String.class);
MethodHandle count = MethodHandles.dropArgumentsToMatch(MethodHandles.identity(int.class), 0, loopType.parameterList(), 1);
MethodHandle start = MethodHandles.dropArgumentsToMatch(MethodHandles.identity(String.class), 0, loopType.parameterList(), 2);
MethodHandle body = MethodHandles.dropArgumentsToMatch(MH_step, 2, loopType.parameterList(), 0);
MethodHandle loop = MethodHandles.countedLoop(count, start, body); // (v, i, pre, _, _) -> pre + " " + v
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("na", 13, "Lambdaman!"));
{}
}}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.23Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.