/* * Copyright (c) 2017, 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. */ /* * Startup eval slimming down version of CondyNestedTest */ import java.io.File; import java.io.FileOutputStream; import java.lang.invoke.CallSite; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Base64; public class CondyNestedTest { /** * NOTE: This is a temporary solution until asmtools is updated to support * dynamic constant and jtreg is updated to include a new version of * asmtools. * * These are the class file bytes for a class named CondyNestedTest_Code * whose bytes are 1) generated by the generator() function; 2) the bytes * converted to a jcod file: * * java -jar asmtools.jar jdec CondyNestedTest_Code.class > * CondyNestedTest_Code.jcod * * which was then edited so that dynamic constant declarations are * recursive both for an ldc or invokedynamic (specifically declaring a * BSM+attributes whose static argument is a dynamic constant * that refers to the same BSM+attributes); 3) the jcod file is converted * back to a class file: * * java -jar asmtools.jar jdis [options] CondyNestedTest_Code.jcod * * ;and finally 4) the newly generated class file bytes are converted to * a base64 representation and embedded in the following String. */ static final String CLASS_CondyNestedTest_Code = "yv66vgAAADUAQgEAEGphdmEvbGFuZy9PYmplY3QHAAEBAAY8aW5pdD4BAAMoKVYMAAMABAoAAgAFAQAE" + "Q29kZQEAEGphdmEvbGFuZy9TdHJpbmcHAAgBAAZpbnRlcm4BABQ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luZzsMADYANxEA" + "AAA4EQABADgMAC8AMAoAEQA7DwYAPAwANgALEgACAD4SAAEAPgEAEEJvb3RzdHJhcE1ldGhvZHMAAAAR" + "AAIAAAAAAAcAAQADAAQAAQAHAAAAEQABAAEAAAAFKrcABrEAAAAAAAkAHgAfAAEABwAAAEIAAgACAAAA" + "JioDMrYADUwrEg+mAAe4ABSxKxIWpgAHuAAYsSsSGqYAB7gAHLGxAAAAAQAdAAAACgAD/AARBwAJCQkA" + "CQAtAC4AAQAHAAAALQAEAAQAAAAYLMEAIQOfABG7ACNZEgkruAAptwAssCuwAAAAAQAdAAAAAwABFgAJ" + "AC8AMAABAAcAAAAaAAQABAAAAA67ACNZEgkruAAptwAssAAAAAAACQAOABIAAQAHAAAADwABAAAAAAAD" + "EjqwAAAAAAAJABUAEgABAAcAAAASAAEAAAAAAAa6AD8AALAAAAAAAAkAGQASAAEABwAAABIAAQAAAAAA" + "BroAQAAAsAAAAAAAAQBBAAAAFAADADUAAQAyADUAAQA6AD0AAQA6"; static final Class[] THROWABLES = {InvocationTargetException.class, StackOverflowError.class}; static Class c; static void test(Method m, Class... ts) { Throwable caught = null; try { m.invoke(null); } catch (Throwable t) { caught = t; } if (caught == null) { System.out.println("Throwable expected"); } String actualMessage = null; for (int i = 0; i < ts.length; i++) { actualMessage = caught.getMessage(); System.out.println(caught != null); caught = caught.getCause(); } } public static void generateClass() throws Exception { byte[] ba = Base64.getDecoder().decode(CLASS_CondyNestedTest_Code); ClassLoader l = new ClassLoader(CondyNestedTest.class.getClassLoader()) { @Override protected Class findClass(String name) throws ClassNotFoundException { if (name == "CondyNestedTest_Code") { return defineClass(name, ba, 0, ba.length); } return null; } }; c = l.loadClass("CondyNestedTest_Code"); } /** * Testing an ldc of a dynamic constant, C say, with a BSM whose static * argument is C. */ public static void testCondyBsmCondyBsm() throws Exception { test("condy_bsm_condy_bsm", THROWABLES); } /** * Testing an invokedynamic with a BSM whose static argument is a constant * dynamic, C say, with a BSM whose static argument is C. */ public static void testIndyBsmIndyCondyBsm() throws Exception { test("indy_bsmIndy_condy_bsm", THROWABLES); } /** * Testing an invokedynamic with a BSM, B say, whose static argument is * a dynamic constant, C say, that uses BSM B. */ public static void testIndyBsmCondyBsm() throws Exception { test("indy_bsm_condy_bsm", THROWABLES); } static void test(String methodName, Class... ts) throws Exception { Method m = c.getMethod(methodName); m.setAccessible(true); test(m, ts); } public static void main(String ... args) throws Exception { generateClass(); testIndyBsmCondyBsm(); } }