--- /dev/null 2016-10-25 08:46:44.038854975 +0200 +++ new/src/share/classes/sun/evtracing/parser/metadata/JvmNamePrettyPrinter.java 2016-10-25 10:40:43.012799922 +0200 @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved. + * + * This file is part of the Lock Contention Tracing Subsystem for the HotSpot + * Virtual Machine, which is developed at Christian Doppler Laboratory on + * Monitoring and Evolution of Very-Large-Scale Software Systems. Please + * contact us at if you need additional information + * or have any questions. + * + * 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, see . + * + */ +package sun.evtracing.parser.metadata; + +import java.io.EOFException; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +public class JvmNamePrettyPrinter { + + public static String prettyClassName(String className, boolean compact) { + try { + Reader reader = new StringReader(className); + StringBuilder sb = new StringBuilder(); + int c = peek(reader); + if (c == '[') { + prettyPrintNextType(reader, sb, compact); + } else { + int startLength = sb.length(); + c = reader.read(); + while (c != -1) { + if (c == '/') { + if (compact) { + sb.setLength(startLength); + } else { + sb.append('.'); + } + } else { + sb.append((char) c); + } + c = reader.read(); + } + } + return sb.toString(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + public static String prettyMethodName(String className, String methodName, boolean compact) { + return prettyClassName(className, compact) + "." + methodName; + } + + public static String prettyMethodDescriptor(String className, String methodName, String signature, boolean compact) { + return prettyMethodName(className, methodName, compact) + prettyMethodSignature(signature, compact); + } + + public static String prettyMethodSignature(String signature, boolean compact) { + StringBuilder sb = new StringBuilder(); + StringReader reader = new StringReader(signature); + try { + int c = reader.read(); + assert c == '('; + sb.append('('); + + c = peek(reader); + if (c == -1) + throw new EOFException("closing parenthesis"); + + // arguments + while (c != ')') { + prettyPrintNextType(reader, sb, compact); + + c = peek(reader); + if (c == -1) + throw new EOFException("closing parenthesis"); + + if (c != ')') + sb.append(", "); + } + + c = reader.read(); // consume ')' + + sb.append(')'); + + // return type + sb.append(" : "); + prettyPrintNextType(reader, sb, compact); + } catch (Exception e) { + throw new RuntimeException("unparsable signature '" + signature + "'", e); + } + return sb.toString(); + } + + private static int peek(Reader reader) throws IOException { + int c; + reader.mark(1); + c = reader.read(); + reader.reset(); + return c; + } + + private static void prettyPrintNextType(Reader reader, StringBuilder sb, boolean compact) throws Exception { + int arrayDimensions = 0; + boolean atEnd; + do { + int c = reader.read(); + if (c == -1) + throw new EOFException(); + + atEnd = true; + switch (c) { + case '[': + arrayDimensions++; + atEnd = false; + break; + case 'B': sb.append("byte"); break; + case 'C': sb.append("char"); break; + case 'D': sb.append("double"); break; + case 'F': sb.append("float"); break; + case 'I': sb.append("int"); break; + case 'J': sb.append("long"); break; + case 'S': sb.append("short"); break; + case 'Z': sb.append("boolean"); break; + case 'V': sb.append("void"); break; + case 'L': + int startLength = sb.length(); + do { + c = reader.read(); + if (c == -1) + throw new EOFException("in class name"); + if (c != ';') { + if (c == '/') { + if (compact) { + sb.setLength(startLength); + } else { + sb.append('.'); + } + } else { + sb.append((char) c); + } + } + } while (c != ';'); + break; + default: + throw new Exception("invalid type '" + (char) c + "'"); + } + } while (!atEnd); + + for (int i = 0; i < arrayDimensions; i++) { + sb.append("[]"); + } + } + +}