1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/*
* Copyright (c) 2015, 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.
*/
import java.io.Serializable;
import java.lang.invoke.*;
import java.util.concurrent.Callable;
/**
* @test
* @summary Test input invariants for StringConcatFactory
*
* @compile StringConcatFactoryInvariants.java
*
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED_EXACT StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED_EXACT StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_INLINE_SIZED_EXACT StringConcatFactoryInvariants
*
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB -Djava.lang.invoke.stringConcat.debug=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED -Djava.lang.invoke.stringConcat.debug=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED -Djava.lang.invoke.stringConcat.debug=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED_EXACT -Djava.lang.invoke.stringConcat.debug=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED_EXACT -Djava.lang.invoke.stringConcat.debug=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_INLINE_SIZED_EXACT -Djava.lang.invoke.stringConcat.debug=true StringConcatFactoryInvariants
*
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED_EXACT -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED_EXACT -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_INLINE_SIZED_EXACT -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
*
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB -Djava.lang.invoke.stringConcat.debug=true -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED -Djava.lang.invoke.stringConcat.debug=true -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED -Djava.lang.invoke.stringConcat.debug=true -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED_EXACT -Djava.lang.invoke.stringConcat.debug=true -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED_EXACT -Djava.lang.invoke.stringConcat.debug=true -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
* @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_INLINE_SIZED_EXACT -Djava.lang.invoke.stringConcat.debug=true -Djava.lang.invoke.stringConcat.cache=true StringConcatFactoryInvariants
*
*/
public class StringConcatFactoryInvariants {
private static final char TAG_ARG = '\u0001';
private static final char TAG_CONST = '\u0002';
public static void main(String[] args) throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
String methodName = "foo";
MethodType mt = MethodType.methodType(String.class, String.class, int.class);
String recipe = "" + TAG_ARG + TAG_ARG + TAG_CONST;
Object[][] constants = new Object[][] {
new String[] { "bar" },
new Integer[] { 1 },
new Short[] { 2 },
new Long[] { 3L },
new Boolean[] { true },
new Character[] { 'a' },
new Byte[] { -128 },
new Class[] { String.class },
new MethodHandle[] { MethodHandles.constant(String.class, "constant") },
new MethodType[] { MethodType.methodType(String.class) }
};
// The string representation that should end up if the corresponding
// Object[] in constants is used as an argument to makeConcatWithConstants
String[] constantString = new String[] {
"bar",
"1",
"2",
"3",
"true",
"a",
"-128",
"class java.lang.String",
"MethodHandle()String",
"()String"
};
final int LIMIT = 200;
// Simple factory: check for dynamic arguments overflow
Class<?>[] underThreshold = new Class<?>[LIMIT - 1];
Class<?>[] threshold = new Class<?>[LIMIT];
Class<?>[] overThreshold = new Class<?>[LIMIT + 1];
StringBuilder sbUnderThreshold = new StringBuilder();
sbUnderThreshold.append(TAG_CONST);
for (int c = 0; c < LIMIT - 1; c++) {
underThreshold[c] = int.class;
threshold[c] = int.class;
overThreshold[c] = int.class;
sbUnderThreshold.append(TAG_ARG);
}
threshold[LIMIT - 1] = int.class;
overThreshold[LIMIT - 1] = int.class;
overThreshold[LIMIT] = int.class;
String recipeEmpty = "";
String recipeUnderThreshold = sbUnderThreshold.toString();
String recipeThreshold = sbUnderThreshold.append(TAG_ARG).toString();
String recipeOverThreshold = sbUnderThreshold.append(TAG_ARG).toString();
MethodType mtEmpty = MethodType.methodType(String.class);
MethodType mtUnderThreshold = MethodType.methodType(String.class, underThreshold);
MethodType mtThreshold = MethodType.methodType(String.class, threshold);
MethodType mtOverThreshold = MethodType.methodType(String.class, overThreshold);
// Check the basic functionality is working
{
CallSite cs = StringConcatFactory.makeConcat(lookup, methodName, mt);
test("foo42", (String) cs.getTarget().invokeExact("foo", 42));
}
{
for (int i = 0; i < constants.length; i++) {
CallSite cs = StringConcatFactory.makeConcatWithConstants(lookup, methodName, mt, recipe, constants[i]);
test("foo42".concat(constantString[i]), (String) cs.getTarget().invokeExact("foo", 42));
}
}
// Simple factory, check for nulls:
failNPE("Lookup is null",
() -> StringConcatFactory.makeConcat(null, methodName, mt));
failNPE("Method name is null",
() -> StringConcatFactory.makeConcat(lookup, null, mt));
failNPE("MethodType is null",
() -> StringConcatFactory.makeConcat(lookup, methodName, null));
// Advanced factory, check for nulls:
for (int i = 0; i < constants.length; i++) {
final Object[] consts = constants[i];
failNPE("Lookup is null",
() -> StringConcatFactory.makeConcatWithConstants(null, methodName, mt, recipe, consts));
failNPE("Method name is null",
() -> StringConcatFactory.makeConcatWithConstants(lookup, null, mt, recipe, consts));
failNPE("MethodType is null",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, null, recipe, consts));
failNPE("Recipe is null",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mt, null, consts));
}
failNPE("Constants vararg is null",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mt, recipe, (Object[]) null));
failNPE("Constant argument is null",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mt, recipe, new Object[] { null }));
// Simple factory, check for return type
fail("Return type: void",
() -> StringConcatFactory.makeConcat(lookup, methodName, MethodType.methodType(void.class, String.class, int.class)));
fail("Return type: int",
() -> StringConcatFactory.makeConcat(lookup, methodName, MethodType.methodType(int.class, String.class, int.class)));
fail("Return type: StringBuilder",
() -> StringConcatFactory.makeConcat(lookup, methodName, MethodType.methodType(StringBuilder.class, String.class, int.class)));
ok("Return type: Object",
() -> StringConcatFactory.makeConcat(lookup, methodName, MethodType.methodType(Object.class, String.class, int.class)));
ok("Return type: CharSequence",
() -> StringConcatFactory.makeConcat(lookup, methodName, MethodType.methodType(CharSequence.class, String.class, int.class)));
ok("Return type: Serializable",
() -> StringConcatFactory.makeConcat(lookup, methodName, MethodType.methodType(Serializable.class, String.class, int.class)));
// Advanced factory, check for return types
for (int i = 0; i < constants.length; i++) {
final Object[] consts = constants[i];
fail("Return type: void",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(void.class, String.class, int.class), recipe, consts));
fail("Return type: int",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(int.class, String.class, int.class), recipe, consts));
fail("Return type: StringBuilder",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(StringBuilder.class, String.class, int.class), recipe, consts));
ok("Return type: Object",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(Object.class, String.class, int.class), recipe, consts));
ok("Return type: CharSequence",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(CharSequence.class, String.class, int.class), recipe, consts));
ok("Return type: Serializable",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(Serializable.class, String.class, int.class), recipe, consts));
}
// Simple factory: check for dynamic arguments overflow
ok("Dynamic arguments is under limit",
() -> StringConcatFactory.makeConcat(lookup, methodName, mtUnderThreshold));
ok("Dynamic arguments is at the limit",
() -> StringConcatFactory.makeConcat(lookup, methodName, mtThreshold));
fail("Dynamic arguments is over the limit",
() -> StringConcatFactory.makeConcat(lookup, methodName, mtOverThreshold));
// Advanced factory: check for dynamic arguments overflow
ok("Dynamic arguments is under limit",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtUnderThreshold, recipeUnderThreshold, constants[0]));
ok("Dynamic arguments is at the limit",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeThreshold, constants[0]));
fail("Dynamic arguments is over the limit",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtOverThreshold, recipeOverThreshold, constants[0]));
// Advanced factory: check for mismatched recipe and Constants
ok("Static arguments and recipe match",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeThreshold, "bar"));
fail("Static arguments and recipe mismatch",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeThreshold, "bar", "baz"));
// Advanced factory: check for mismatched recipe and dynamic arguments
fail("Dynamic arguments and recipe mismatch",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeUnderThreshold, constants[0]));
ok("Dynamic arguments and recipe match",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeThreshold, constants[0]));
fail("Dynamic arguments and recipe mismatch",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtThreshold, recipeOverThreshold, constants[0]));
// Test passing array as constant
{
Object[] arg = {"boo", "bar"};
CallSite cs1 = StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(String.class, int.class), "" + TAG_ARG + TAG_CONST + TAG_CONST, arg);
test("42boobar", (String) cs1.getTarget().invokeExact(42));
}
// Test passing null constant
ok("Can pass regular constants",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(String.class, int.class), "" + TAG_ARG + TAG_CONST, "foo"));
failNPE("Cannot pass null constants",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, MethodType.methodType(String.class, int.class), "" + TAG_ARG + TAG_CONST, new Object[]{null}));
// Simple factory: test empty arguments
ok("Ok to pass empty arguments",
() -> StringConcatFactory.makeConcat(lookup, methodName, mtEmpty));
// Advanced factory: test empty arguments
ok("Ok to pass empty arguments",
() -> StringConcatFactory.makeConcatWithConstants(lookup, methodName, mtEmpty, recipeEmpty));
// Simple factory: public Lookup is rejected
fail("Passing public Lookup",
() -> StringConcatFactory.makeConcat(MethodHandles.publicLookup(), methodName, mtEmpty));
// Advanced factory: public Lookup is rejected
fail("Passing public Lookup",
() -> StringConcatFactory.makeConcatWithConstants(MethodHandles.publicLookup(), methodName, mtEmpty, recipeEmpty));
// Zero inputs
{
MethodType zero = MethodType.methodType(String.class);
CallSite cs = StringConcatFactory.makeConcat(lookup, methodName, zero);
test("", (String) cs.getTarget().invokeExact());
cs = StringConcatFactory.makeConcatWithConstants(lookup, methodName, zero, "");
test("", (String) cs.getTarget().invokeExact());
}
// One input
{
MethodType zero = MethodType.methodType(String.class);
MethodType one = MethodType.methodType(String.class, String.class);
CallSite cs = StringConcatFactory.makeConcat(lookup, methodName, one);
test("A", (String) cs.getTarget().invokeExact("A"));
cs = StringConcatFactory.makeConcatWithConstants(lookup, methodName, one, "\1");
test("A", (String) cs.getTarget().invokeExact("A"));
cs = StringConcatFactory.makeConcatWithConstants(lookup, methodName, zero, "\2", "A");
test("A", (String) cs.getTarget().invokeExact());
}
}
public static void ok(String msg, Callable runnable) {
try {
runnable.call();
} catch (Throwable e) {
e.printStackTrace();
throw new IllegalStateException(msg + ", should have passed", e);
}
}
public static void fail(String msg, Callable runnable) {
boolean expected = false;
try {
runnable.call();
} catch (StringConcatException e) {
expected = true;
} catch (Throwable e) {
e.printStackTrace();
}
if (!expected) {
throw new IllegalStateException(msg + ", should have failed with StringConcatException");
}
}
public static void failNPE(String msg, Callable runnable) {
boolean expected = false;
try {
runnable.call();
} catch (NullPointerException e) {
expected = true;
} catch (Throwable e) {
e.printStackTrace();
}
if (!expected) {
throw new IllegalStateException(msg + ", should have failed with NullPointerException");
}
}
public static void test(String expected, String actual) {
// Fingers crossed: String concat should work.
if (!expected.equals(actual)) {
StringBuilder sb = new StringBuilder();
sb.append("Expected = ");
sb.append(expected);
sb.append(", actual = ");
sb.append(actual);
throw new IllegalStateException(sb.toString());
}
}
}
|