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