1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * ASM: a very small and fast Java bytecode manipulation framework 32 * Copyright (c) 2000-2011 INRIA, France Telecom 33 * All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the copyright holders nor the names of its 44 * contributors may be used to endorse or promote products derived from 45 * this software without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 57 * THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 package jdk.internal.org.objectweb.asm.signature; 60 61 /** 62 * A type signature parser to make a signature visitor visit an existing 63 * signature. 64 * 65 * @author Thomas Hallgren 66 * @author Eric Bruneton 67 */ 68 public class SignatureReader { 69 70 /** 71 * The signature to be read. 72 */ 73 private final String signature; 74 75 /** 76 * Constructs a {@link SignatureReader} for the given signature. 77 * 78 * @param signature 79 * A <i>ClassSignature</i>, <i>MethodTypeSignature</i>, or 80 * <i>FieldTypeSignature</i>. 81 */ 82 public SignatureReader(final String signature) { 83 this.signature = signature; 84 } 85 86 /** 87 * Makes the given visitor visit the signature of this 88 * {@link SignatureReader}. This signature is the one specified in the 89 * constructor (see {@link #SignatureReader(String) SignatureReader}). This 90 * method is intended to be called on a {@link SignatureReader} that was 91 * created using a <i>ClassSignature</i> (such as the <code>signature</code> 92 * parameter of the {@link jdk.internal.org.objectweb.asm.ClassVisitor#visit 93 * ClassVisitor.visit} method) or a <i>MethodTypeSignature</i> (such as the 94 * <code>signature</code> parameter of the 95 * {@link jdk.internal.org.objectweb.asm.ClassVisitor#visitMethod 96 * ClassVisitor.visitMethod} method). 97 * 98 * @param v 99 * the visitor that must visit this signature. 100 */ 101 public void accept(final SignatureVisitor v) { 102 String signature = this.signature; 103 int len = signature.length(); 104 int pos; 105 char c; 106 107 if (signature.charAt(0) == '<') { 108 pos = 2; 109 do { 110 int end = signature.indexOf(':', pos); 111 v.visitFormalTypeParameter(signature.substring(pos - 1, end)); 112 pos = end + 1; 113 114 c = signature.charAt(pos); 115 if (c == 'L' || c == '[' || c == 'T') { 116 pos = parseType(signature, pos, v.visitClassBound()); 117 } 118 119 while ((c = signature.charAt(pos++)) == ':') { 120 pos = parseType(signature, pos, v.visitInterfaceBound()); 121 } 122 } while (c != '>'); 123 } else { 124 pos = 0; 125 } 126 127 if (signature.charAt(pos) == '(') { 128 pos++; 129 while (signature.charAt(pos) != ')') { 130 pos = parseType(signature, pos, v.visitParameterType()); 131 } 132 pos = parseType(signature, pos + 1, v.visitReturnType()); 133 while (pos < len) { 134 pos = parseType(signature, pos + 1, v.visitExceptionType()); 135 } 136 } else { 137 pos = parseType(signature, pos, v.visitSuperclass()); 138 while (pos < len) { 139 pos = parseType(signature, pos, v.visitInterface()); 140 } 141 } 142 } 143 144 /** 145 * Makes the given visitor visit the signature of this 146 * {@link SignatureReader}. This signature is the one specified in the 147 * constructor (see {@link #SignatureReader(String) SignatureReader}). This 148 * method is intended to be called on a {@link SignatureReader} that was 149 * created using a <i>FieldTypeSignature</i>, such as the 150 * <code>signature</code> parameter of the 151 * {@link jdk.internal.org.objectweb.asm.ClassVisitor#visitField ClassVisitor.visitField} 152 * or {@link jdk.internal.org.objectweb.asm.MethodVisitor#visitLocalVariable 153 * MethodVisitor.visitLocalVariable} methods. 154 * 155 * @param v 156 * the visitor that must visit this signature. 157 */ 158 public void acceptType(final SignatureVisitor v) { 159 parseType(this.signature, 0, v); 160 } 161 162 /** 163 * Parses a field type signature and makes the given visitor visit it. 164 * 165 * @param signature 166 * a string containing the signature that must be parsed. 167 * @param pos 168 * index of the first character of the signature to parsed. 169 * @param v 170 * the visitor that must visit this signature. 171 * @return the index of the first character after the parsed signature. 172 */ 173 private static int parseType(final String signature, int pos, 174 final SignatureVisitor v) { 175 char c; 176 int start, end; 177 boolean visited, inner; 178 String name; 179 180 switch (c = signature.charAt(pos++)) { 181 case 'Z': 182 case 'C': 183 case 'B': 184 case 'S': 185 case 'I': 186 case 'F': 187 case 'J': 188 case 'D': 189 case 'V': 190 v.visitBaseType(c); 191 return pos; 192 193 case '[': 194 return parseType(signature, pos, v.visitArrayType()); 195 196 case 'T': 197 end = signature.indexOf(';', pos); 198 v.visitTypeVariable(signature.substring(pos, end)); 199 return end + 1; 200 201 default: // case 'L': 202 start = pos; 203 visited = false; 204 inner = false; 205 for (;;) { 206 switch (c = signature.charAt(pos++)) { 207 case '.': 208 case ';': 209 if (!visited) { 210 name = signature.substring(start, pos - 1); 211 if (inner) { 212 v.visitInnerClassType(name); 213 } else { 214 v.visitClassType(name); 215 } 216 } 217 if (c == ';') { 218 v.visitEnd(); 219 return pos; 220 } 221 start = pos; 222 visited = false; 223 inner = true; 224 break; 225 226 case '<': 227 name = signature.substring(start, pos - 1); 228 if (inner) { 229 v.visitInnerClassType(name); 230 } else { 231 v.visitClassType(name); 232 } 233 visited = true; 234 top: for (;;) { 235 switch (c = signature.charAt(pos)) { 236 case '>': 237 break top; 238 case '*': 239 ++pos; 240 v.visitTypeArgument(); 241 break; 242 case '+': 243 case '-': 244 pos = parseType(signature, pos + 1, 245 v.visitTypeArgument(c)); 246 break; 247 default: 248 pos = parseType(signature, pos, 249 v.visitTypeArgument('=')); 250 break; 251 } 252 } 253 } 254 } 255 } 256 } 257 }