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++)) == ':') { 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()); 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 } | 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++)) == ':') { 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()); 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 } |