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 
 60 package jdk.internal.org.objectweb.asm.signature;
 61 
 62 /**
 63  * A parser for signature literals, as defined in the Java Virtual Machine Specification (JVMS), to
 64  * visit them with a SignatureVisitor.
 65  *
 66  * @see <a href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.9.1">JVMS
 67  *     4.7.9.1</a>
 68  * @author Thomas Hallgren
 69  * @author Eric Bruneton
 70  */
 71 public class SignatureReader {
 72 
 73     /** The JVMS signature to be read. */
 74     private final String signatureValue;
 75 
 76     /**
 77       * Constructs a {@link SignatureReader} for the given signature.
 78       *
 79       * @param signature A <i>JavaTypeSignature</i>, <i>ClassSignature</i> or <i>MethodSignature</i>.
 80       */
 81     public SignatureReader(final String signature) {
 82         this.signatureValue = signature;
 83     }
 84 
 85     /**
 86       * Makes the given visitor visit the signature of this {@link SignatureReader}. This signature is
 87       * the one specified in the constructor (see {@link #SignatureReader}). This method is intended to
 88       * be called on a {@link SignatureReader} that was created using a <i>ClassSignature</i> (such as
 89       * the <code>signature</code> parameter of the {@link jdk.internal.org.objectweb.asm.ClassVisitor#visit}
 90       * method) or a <i>MethodSignature</i> (such as the <code>signature</code> parameter of the {@link
 91       * jdk.internal.org.objectweb.asm.ClassVisitor#visitMethod} method).
 92       *
 93       * @param signatureVistor the visitor that must visit this signature.
 94       */
 95     public void accept(final SignatureVisitor signatureVistor) {
 96         String signature = this.signatureValue;
 97         int length = signature.length();
 98         int offset; // Current offset in the parsed signature (parsed from left to right).
 99         char currentChar; // The signature character at 'offset', or just before.
100 
101         // If the signature starts with '<', it starts with TypeParameters, i.e. a formal type parameter
102         // identifier, followed by one or more pair ':',ReferenceTypeSignature (for its class bound and
103         // interface bounds).
104         if (signature.charAt(0) == '<') {
105             // Invariant: offset points to the second character of a formal type parameter name at the
106             // beginning of each iteration of the loop below.
107             offset = 2;
108             do {
109                 // The formal type parameter name is everything between offset - 1 and the first ':'.
110                 int classBoundStartOffset = signature.indexOf(':', offset);
111                 signatureVistor.visitFormalTypeParameter(
112                         signature.substring(offset - 1, classBoundStartOffset));
113 
114                 // If the character after the ':' class bound marker is not the start of a
115                 // ReferenceTypeSignature, it means the class bound is empty (which is a valid case).
116                 offset = classBoundStartOffset + 1;
117                 currentChar = signature.charAt(offset);
118                 if (currentChar == 'L' || currentChar == '[' || currentChar == 'T') {
119                     offset = parseType(signature, offset, signatureVistor.visitClassBound());
120                 }
121 
122                 // While the character after the class bound or after the last parsed interface bound
123                 // is ':', we need to parse another interface bound.
124                 while ((currentChar = signature.charAt(offset++)) == ':') {
125                     offset = parseType(signature, offset, signatureVistor.visitInterfaceBound());
126                 }
127 
128                 // At this point a TypeParameter has been fully parsed, and we need to parse the next one
129                 // (note that currentChar is now the first character of the next TypeParameter, and that
130                 // offset points to the second character), unless the character just after this
131                 // TypeParameter signals the end of the TypeParameters.
132             } while (currentChar != '>');
133         } else {
134             offset = 0;
135         }
136 
137         // If the (optional) TypeParameters is followed by '(' this means we are parsing a
138         // MethodSignature, which has JavaTypeSignature type inside parentheses, followed by a Result
139         // type and optional ThrowsSignature types.
140         if (signature.charAt(offset) == '(') {
141             offset++;
142             while (signature.charAt(offset) != ')') {
143                 offset = parseType(signature, offset, signatureVistor.visitParameterType());
144             }
145             // Use offset + 1 to skip ')'.
146             offset = parseType(signature, offset + 1, signatureVistor.visitReturnType());
147             while (offset < length) {
148                 // Use offset + 1 to skip the first character of a ThrowsSignature, i.e. '^'.
149                 offset = parseType(signature, offset + 1, signatureVistor.visitExceptionType());
150             }
151         } else {
152             // Otherwise we are parsing a ClassSignature (by hypothesis on the method input), which has
153             // one or more ClassTypeSignature for the super class and the implemented interfaces.
154             offset = parseType(signature, offset, signatureVistor.visitSuperclass());
155             while (offset < length) {
156                 offset = parseType(signature, offset, signatureVistor.visitInterface());
157             }
158         }
159     }
160 
161     /**
162       * Makes the given visitor visit the signature of this {@link SignatureReader}. This signature is
163       * the one specified in the constructor (see {@link #SignatureReader}). This method is intended to
164       * be called on a {@link SignatureReader} that was created using a <i>JavaTypeSignature</i>, such
165       * as the <code>signature</code> parameter of the {@link
166       * jdk.internal.org.objectweb.asm.ClassVisitor#visitField} or {@link
167       * jdk.internal.org.objectweb.asm.MethodVisitor#visitLocalVariable} methods.
168       *
169       * @param signatureVisitor the visitor that must visit this signature.
170       */
171     public void acceptType(final SignatureVisitor signatureVisitor) {
172         parseType(signatureValue, 0, signatureVisitor);
173     }
174 
175     /**
176       * Parses a JavaTypeSignature and makes the given visitor visit it.
177       *
178       * @param signature a string containing the signature that must be parsed.
179       * @param startOffset index of the first character of the signature to parsed.
180       * @param signatureVisitor the visitor that must visit this signature.
181       * @return the index of the first character after the parsed signature.
182       */
183     private static int parseType(
184             final String signature, final int startOffset, final SignatureVisitor signatureVisitor) {
185         int offset = startOffset; // Current offset in the parsed signature.
186         char currentChar = signature.charAt(offset++); // The signature character at 'offset'.
187 
188         // Switch based on the first character of the JavaTypeSignature, which indicates its kind.
189         switch (currentChar) {
190             case 'Z':
191             case 'C':
192             case 'B':
193             case 'S':
194             case 'I':
195             case 'F':
196             case 'J':
197             case 'D':
198             case 'V':
199                 // Case of a BaseType or a VoidDescriptor.
200                 signatureVisitor.visitBaseType(currentChar);
201                 return offset;
202 
203             case '[':
204                 // Case of an ArrayTypeSignature, a '[' followed by a JavaTypeSignature.
205                 return parseType(signature, offset, signatureVisitor.visitArrayType());
206 
207             case 'T':
208                 // Case of TypeVariableSignature, an identifier between 'T' and ';'.
209                 int endOffset = signature.indexOf(';', offset);
210                 signatureVisitor.visitTypeVariable(signature.substring(offset, endOffset));
211                 return endOffset + 1;
212 
213             case 'L':
214                 // Case of a ClassTypeSignature, which ends with ';'.
215                 // These signatures have a main class type followed by zero or more inner class types
216                 // (separated by '.'). Each can have type arguments, inside '<' and '>'.
217                 int start = offset; // The start offset of the currently parsed main or inner class name.
218                 boolean visited = false; // Whether the currently parsed class name has been visited.
219                 boolean inner = false; // Whether we are currently parsing an inner class type.
220                 // Parses the signature, one character at a time.
221                 while (true) {
222                     currentChar = signature.charAt(offset++);
223                     if (currentChar == '.' || currentChar == ';') {
224                         // If a '.' or ';' is encountered, this means we have fully parsed the main class name
225                         // or an inner class name. This name may already have been visited it is was followed by
226                         // type arguments between '<' and '>'. If not, we need to visit it here.
227                         if (!visited) {
228                             String name = signature.substring(start, offset - 1);
229                             if (inner) {
230                                 signatureVisitor.visitInnerClassType(name);
231                             } else {
232                                 signatureVisitor.visitClassType(name);
233                             }
234                         }
235                         // If we reached the end of the ClassTypeSignature return, otherwise start the parsing
236                         // of a new class name, which is necessarily an inner class name.
237                         if (currentChar == ';') {
238                             signatureVisitor.visitEnd();
239                             break;
240                         }
241                         start = offset;
242                         visited = false;
243                         inner = true;
244                     } else if (currentChar == '<') {
245                         // If a '<' is encountered, this means we have fully parsed the main class name or an
246                         // inner class name, and that we now need to parse TypeArguments. First, we need to
247                         // visit the parsed class name.
248                         String name = signature.substring(start, offset - 1);
249                         if (inner) {
250                             signatureVisitor.visitInnerClassType(name);
251                         } else {
252                             signatureVisitor.visitClassType(name);
253                         }
254                         visited = true;
255                         // Now, parse the TypeArgument(s), one at a time.
256                         while ((currentChar = signature.charAt(offset)) != '>') {
257                             switch (currentChar) {
258                                 case '*':
259                                     // Unbounded TypeArgument.
260                                     ++offset;
261                                     signatureVisitor.visitTypeArgument();
262                                     break;
263                                 case '+':
264                                 case '-':
265                                     // Extends or Super TypeArgument. Use offset + 1 to skip the '+' or '-'.
266                                     offset =
267                                             parseType(
268                                                     signature, offset + 1, signatureVisitor.visitTypeArgument(currentChar));
269                                     break;
270                                 default:
271                                     // Instanceof TypeArgument. The '=' is implicit.
272                                     offset = parseType(signature, offset, signatureVisitor.visitTypeArgument('='));
273                                     break;
274                             }
275                         }
276                     }
277                 }
278                 return offset;
279 
280             default:
281                 throw new IllegalArgumentException();
282         }
283     }
284 }
285