1 /* 2 * Copyright (c) 2015, 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.internal.jimage.decompressor; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * 32 * A descriptor parser used to extract java type strings from 33 * UTF_8 descriptors. 34 * 35 * @implNote This class needs to maintain JDK 8 source compatibility. 36 * 37 * It is used internally in the JDK to implement jimage/jrtfs access, 38 * but also compiled and delivered as part of the jrtfs.jar to support access 39 * to the jimage file provided by the shipped JDK by tools running on JDK 8. 40 */ 41 public class SignatureParser { 42 43 public static class ParseResult { 44 45 public final List<String> types = new ArrayList<>(); 46 public String formatted; 47 private ParseResult() {} 48 } 49 50 private SignatureParser() {} 51 52 public static String reconstruct(String formatted, List<String> arguments) { 53 int arg_index = 0; 54 char[] chars = formatted.toCharArray(); 55 StringBuilder out = new StringBuilder(); 56 57 for (int i = 0; i < chars.length; i++) { 58 char c = chars[i]; 59 out.append(c); 60 switch (c) { 61 case 'L': { 62 String pkg = arguments.get(arg_index); 63 if(!pkg.isEmpty()) { 64 out.append(pkg).append("/"); 65 } 66 arg_index+=1; 67 out.append(arguments.get(arg_index)); 68 arg_index+=1; 69 break; 70 } 71 default: { 72 break; 73 } 74 } 75 } 76 return out.toString(); 77 } 78 79 public static ParseResult parseSignatureDescriptor(String str) { 80 ParseResult res = new ParseResult(); 81 char[] chars = str.toCharArray(); 82 StringBuilder type = null; 83 StringBuilder formatted = new StringBuilder(); 84 for (int i = 0; i < chars.length; i++) { 85 char c = chars[i]; 86 switch (c) { 87 case ';': 88 case ':': 89 case '<': { 90 if(type != null) { 91 String fullName = type.toString(); 92 int endIndex = fullName.lastIndexOf("/"); 93 String clazz = fullName; 94 String pkg = ""; 95 if(endIndex != -1) { 96 pkg = fullName.substring(0, endIndex); 97 clazz = fullName.substring(endIndex+1); 98 } 99 res.types.add(pkg); 100 res.types.add(clazz); 101 } 102 formatted.append(c); 103 104 type = null; 105 break; 106 } 107 case 'L': { 108 if(type == null) { 109 type = new StringBuilder(); 110 formatted.append(c); 111 } else { 112 type.append(c); 113 } 114 break; 115 } 116 default: { 117 if(type == null) { 118 formatted.append(c); 119 } else { 120 type.append(c); 121 } 122 break; 123 } 124 } 125 } 126 res.formatted = formatted.toString(); 127 return res; 128 } 129 }