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 A <i>ClassSignature</i>, <i>MethodTypeSignature</i>, 79 * or <i>FieldTypeSignature</i>. 80 */ 81 public SignatureReader(final String signature) { 82 this.signature = signature; 83 } 84 85 /** 86 * Makes the given visitor visit the signature of this 87 * {@link SignatureReader}. This signature is the one specified in the 88 * constructor (see {@link #SignatureReader(String) SignatureReader}). This 89 * method is intended to be called on a {@link SignatureReader} that was 90 * created using a <i>ClassSignature</i> (such as the 91 * <code>signature</code> parameter of the 92 * {@link jdk.internal.org.objectweb.asm.ClassVisitor#visit ClassVisitor.visit} method) 93 * or a <i>MethodTypeSignature</i> (such as the <code>signature</code> 94 * parameter of the 95 * {@link jdk.internal.org.objectweb.asm.ClassVisitor#visitMethod ClassVisitor.visitMethod} 96 * method). 97 * 98 * @param v the visitor that must visit this signature. 99 */ 100 public void accept(final SignatureVisitor v) { 101 String signature = this.signature; 102 int len = signature.length(); 103 int pos; 104 char c; 105 106 if (signature.charAt(0) == '<') { 107 pos = 2; 108 do { 109 int end = signature.indexOf(':', pos); 110 v.visitFormalTypeParameter(signature.substring(pos - 1, end)); 111 pos = end + 1; 112 113 c = signature.charAt(pos); 114 if (c == 'L' || c == '[' || c == 'T') { 115 pos = parseType(signature, pos, v.visitClassBound()); 116 } 117 118 while ((c = signature.charAt(pos++)) == ':') { 119 pos = parseType(signature, pos, v.visitInterfaceBound()); 120 } 121 } while (c != '>'); 122 } else { 123 pos = 0; 124 } 125 126 if (signature.charAt(pos) == '(') { 127 pos++; 128 while (signature.charAt(pos) != ')') { 129 pos = parseType(signature, pos, v.visitParameterType()); 130 } 131 pos = parseType(signature, pos + 1, v.visitReturnType()); 132 while (pos < len) { 133 pos = parseType(signature, pos + 1, v.visitExceptionType()); 134 } 135 } else { 136 pos = parseType(signature, pos, v.visitSuperclass()); 137 while (pos < len) { 138 pos = parseType(signature, pos, v.visitInterface()); 139 } 140 } 141 } 142 143 /** 144 * Makes the given visitor visit the signature of this 145 * {@link SignatureReader}. This signature is the one specified in the 146 * constructor (see {@link #SignatureReader(String) SignatureReader}). This 147 * method is intended to be called on a {@link SignatureReader} that was 148 * created using a <i>FieldTypeSignature</i>, such as the 149 * <code>signature</code> parameter of the 150 * {@link jdk.internal.org.objectweb.asm.ClassVisitor#visitField 151 * ClassVisitor.visitField} or {@link 152 * jdk.internal.org.objectweb.asm.MethodVisitor#visitLocalVariable 153 * MethodVisitor.visitLocalVariable} methods. 154 * 155 * @param v the visitor that must visit this signature. 156 */ 157 public void acceptType(final SignatureVisitor v) { 158 parseType(this.signature, 0, v); 159 } 160 161 /** 162 * Parses a field type signature and makes the given visitor visit it. 163 * 164 * @param signature a string containing the signature that must be parsed. 165 * @param pos index of the first character of the signature to parsed. 166 * @param v the visitor that must visit this signature. 167 * @return the index of the first character after the parsed signature. 168 */ 169 private static int parseType( 170 final String signature, 171 int pos, 172 final SignatureVisitor v) 173 { 174 char c; 175 int start, end; 176 boolean visited, inner; 177 String name; 178 179 switch (c = signature.charAt(pos++)) { 180 case 'Z': 181 case 'C': 182 case 'B': 183 case 'S': 184 case 'I': 185 case 'F': 186 case 'J': 187 case 'D': 188 case 'V': 189 v.visitBaseType(c); 190 return pos; 191 192 case '[': 193 return parseType(signature, pos, v.visitArrayType()); 194 195 case 'T': 196 end = signature.indexOf(';', pos); 197 v.visitTypeVariable(signature.substring(pos, end)); 198 return end + 1; 199 200 default: // case 'L': 201 start = pos; 202 visited = false; 203 inner = false; 204 for (;;) { 205 switch (c = signature.charAt(pos++)) { 206 case '.': 207 case ';': 208 if (!visited) { 209 name = signature.substring(start, pos - 1); 210 if (inner) { 211 v.visitInnerClassType(name); 212 } else { 213 v.visitClassType(name); 214 } 215 } 216 if (c == ';') { 217 v.visitEnd(); 218 return pos; 219 } 220 start = pos; 221 visited = false; 222 inner = true; 223 break; 224 225 case '<': 226 name = signature.substring(start, pos - 1); 227 if (inner) { 228 v.visitInnerClassType(name); 229 } else { 230 v.visitClassType(name); 231 } 232 visited = true; 233 top: for (;;) { 234 switch (c = signature.charAt(pos)) { 235 case '>': 236 break top; 237 case '*': 238 ++pos; 239 v.visitTypeArgument(); 240 break; 241 case '+': 242 case '-': 243 pos = parseType(signature, 244 pos + 1, 245 v.visitTypeArgument(c)); 246 break; 247 default: 248 pos = parseType(signature, 249 pos, 250 v.visitTypeArgument('=')); 251 break; 252 } 253 } 254 } 255 } 256 } 257 } 258 }