/* * Copyright (c) 2011, 2018, 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 unit tests for method handles which permute their arguments * @library /test/lib /java/lang/invoke/common * @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -ea -esa -DPermuteArgsTest.MAX_ARITY=8 test.java.lang.invoke.PermuteArgsTest
*/
static Object list2I(int x, int y) { return Arrays.asList(x, y);
} static Object list3I(int x, int y, int z) { return Arrays.asList(x, y, z);
} static Object list4I(int w, int x, int y, int z) { return Arrays.asList(w, x, y, z);
} static Object list2J(long x, long y) { return Arrays.asList(x, y);
} static Object list3J(long x, long y, long z) { return Arrays.asList(x, y, z);
} static Object list4J(long w, long x, long y, long z) { return Arrays.asList(w, x, y, z);
} static Object list2I2J(int w, int x, long y, long z) { return Arrays.asList(w, x, y, z);
} static Object list2J2I(long w, long x, int y, int z) { return Arrays.asList(w, x, y, z);
} static Object listLJJ(Object x, long y, long z) { return Arrays.asList(x, y, z);
} static Object listJLJ(long x, Object y, long z) { return Arrays.asList(x, y, z);
} static Object listJJL(long x, long y, Object z) { return Arrays.asList(x, y, z);
} static Object listJLL(long x, Object y, Object z) { return Arrays.asList(x, y, z);
} static Object listLJL(Object x, long y, Object z) { return Arrays.asList(x, y, z);
} static Object listLLJ(Object x, Object y, long z) { return Arrays.asList(x, y, z);
} static Object listJLLJ(long w, Object x, Object y, long z) { return Arrays.asList(w, x, y, z);
} static Object listLJJL(Object w, long x, long y, Object z) { return Arrays.asList(w, x, y, z);
} static Object listI_etc(int... va) {
ArrayList<Object> res = new ArrayList<>(); for (int x : va) res.add(x); return res;
} static Object listIJL_etc(int x, long y, Object z, Object... va) {
ArrayList<Object> res = new ArrayList<>();
res.addAll(Arrays.asList(x, y, z));
res.addAll(Arrays.asList(va)); return res;
}
publicstaticvoid main(String argv[]) throws Throwable { if (argv.length > 0) { for (String arg : argv) { // arg ::= name[n,...] int k = arg.indexOf('[');
String mhName = arg.substring(0, k).trim();
String permString = arg.substring(k);
testOnePermutation(mhName, permString);
} return;
} new PermuteArgsTest().test();
}
publicvoid test0() throws Throwable {
testCases = 0;
Lookup lookup = lookup(); for (Method m : lookup.lookupClass().getDeclaredMethods()) { if (m.getName().startsWith("list") &&
Modifier.isStatic(m.getModifiers())) {
test(m.getName(), lookup.unreflect(m));
}
}
System.out.println("ran a total of "+testCases+" test cases");
}
staticint jump(int i, int min, int max) { if (i >= min && i <= max-1) { // jump faster int len = max-min; if (i < min + len/2)
i = min + len/2; else
i = max-1;
} return i;
}
staticvoid test(String name, MethodHandle mh) throws Throwable { if (VERBOSE)
System.out.println("mh = "+name+" : "+mh+" { "
+Arrays.toString(junkArgs(mh.type().parameterArray()))); int testCases0 = testCases; if (!mh.isVarargsCollector()) { // normal case
testPermutations(mh);
} else { // varargs case; add params up to MAX_ARITY
MethodType mt = mh.type(); int posArgs = mt.parameterCount() - 1; int arity0 = Math.max(3, posArgs); for (int arity = arity0; arity <= MAX_ARITY; arity++) {
MethodHandle mh1; try {
mh1 = adjustArity(mh, arity);
} catch (IllegalArgumentException ex) {
System.out.println("*** mh = "+name+" : "+mh+"; arity = "+arity+" => "+ex);
ex.printStackTrace(System.out); break; // cannot get this arity for this type
}
test("("+arity+")"+name, mh1);
arity = jump(arity, arity0*2, MAX_ARITY);
}
} if (VERBOSE)
System.out.println("ran "+(testCases - testCases0)+" test cases for "+name+" }");
}
static MethodHandle adjustArity(MethodHandle mh, int arity) {
MethodType mt = mh.type(); int posArgs = mt.parameterCount() - 1; Class<?> reptype = mt.parameterType(posArgs).getComponentType();
MethodType mt1 = mt.dropParameterTypes(posArgs, posArgs+1); while (mt1.parameterCount() < arity) { Class<?> pt = reptype; if (pt == Object.class && posArgs > 0) // repeat types cyclically if possible:
pt = mt1.parameterType(mt1.parameterCount() - posArgs);
mt1 = mt1.appendParameterTypes(pt);
} try { return mh.asType(mt1);
} catch (WrongMethodTypeException | IllegalArgumentException ex) { thrownew IllegalArgumentException("cannot convert to type "+mt1+" from "+mh, ex);
}
} static MethodHandle findTestMH(String name, int[] perm) throws ReflectiveOperationException { int arity = perm.length;
Lookup lookup = lookup(); for (Method m : lookup.lookupClass().getDeclaredMethods()) { if (m.getName().equals(name) &&
Modifier.isStatic(m.getModifiers())) {
MethodHandle mh = lookup.unreflect(m); int mhArity = mh.type().parameterCount(); if (mh.isVarargsCollector()) { if (mhArity-1 <= arity) return adjustArity(mh, arity);
} elseif (mhArity == arity) { return mh;
}
}
} thrownew RuntimeException("no such method for arity "+arity+": "+name);
}
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 und die Messung sind noch experimentell.