1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package jdk.vm.ci.hotspot.test; 24 25 import java.io.ByteArrayOutputStream; 26 import java.io.PrintStream; 27 import java.lang.reflect.InvocationTargetException; 28 import java.lang.reflect.Method; 29 30 import org.junit.Assert; 31 import org.junit.Test; 32 33 public class TestTranslatedException { 34 35 private static String printToString(Throwable throwable) { 36 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 37 try (PrintStream ps = new PrintStream(baos)) { 38 throwable.printStackTrace(ps); 39 } 40 return baos.toString(); 41 } 42 43 @SuppressWarnings("serial") 44 public static class Untranslatable extends RuntimeException { 45 public Untranslatable(String message, Throwable cause) { 46 super(message, cause); 47 } 48 } 49 50 @SuppressWarnings("unchecked") 51 @Test 52 public void encodeDecodeTest() throws Exception { 53 54 Class<?> translatedExceptionClass = Class.forName("jdk.vm.ci.hotspot.TranslatedException"); 55 56 Method encode = translatedExceptionClass.getDeclaredMethod("encodeThrowable", Throwable.class); 57 Method decode = translatedExceptionClass.getDeclaredMethod("decodeThrowable", String.class); 58 encode.setAccessible(true); 59 decode.setAccessible(true); 60 61 Throwable throwable = new ExceptionInInitializerError(new InvocationTargetException(new Untranslatable("test exception", new NullPointerException()), "invoke")); 62 for (int i = 0; i < 10; i++) { 63 throwable = new ExceptionInInitializerError(new InvocationTargetException(new RuntimeException(String.valueOf(i), throwable), "invoke")); 64 } 65 String before = printToString(throwable); 66 String encoding = (String) encode.invoke(null, throwable); 67 Throwable decoded = (Throwable) decode.invoke(null, encoding); 68 String after = printToString(decoded); 69 70 after = after.replace( 71 "jdk.vm.ci.hotspot.TranslatedException: [java.lang.ClassNotFoundException: jdk/vm/ci/hotspot/test/TestTranslatedException$Untranslatable]", 72 "jdk.vm.ci.hotspot.test.TestTranslatedException$Untranslatable: test exception"); 73 74 Assert.assertEquals("before:\n" + before + "\nafter:\n" + after, before, after); 75 } 76 }