Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 46-51
Link Here
|
46 |
|
46 |
|
47 |
public class Util implements SuffixConstants { |
47 |
public class Util implements SuffixConstants { |
48 |
|
48 |
|
|
|
49 |
/** |
50 |
* Character constant indicating the primitive type boolean in a signature. |
51 |
* Value is <code>'Z'</code>. |
52 |
*/ |
53 |
public static final char C_BOOLEAN = 'Z'; |
54 |
|
55 |
/** |
56 |
* Character constant indicating the primitive type byte in a signature. |
57 |
* Value is <code>'B'</code>. |
58 |
*/ |
59 |
public static final char C_BYTE = 'B'; |
60 |
|
61 |
/** |
62 |
* Character constant indicating the primitive type char in a signature. |
63 |
* Value is <code>'C'</code>. |
64 |
*/ |
65 |
public static final char C_CHAR = 'C'; |
66 |
|
67 |
/** |
68 |
* Character constant indicating the primitive type double in a signature. |
69 |
* Value is <code>'D'</code>. |
70 |
*/ |
71 |
public static final char C_DOUBLE = 'D'; |
72 |
|
73 |
/** |
74 |
* Character constant indicating the primitive type float in a signature. |
75 |
* Value is <code>'F'</code>. |
76 |
*/ |
77 |
public static final char C_FLOAT = 'F'; |
78 |
|
79 |
/** |
80 |
* Character constant indicating the primitive type int in a signature. |
81 |
* Value is <code>'I'</code>. |
82 |
*/ |
83 |
public static final char C_INT = 'I'; |
84 |
|
85 |
/** |
86 |
* Character constant indicating the semicolon in a signature. |
87 |
* Value is <code>';'</code>. |
88 |
*/ |
89 |
public static final char C_SEMICOLON = ';'; |
90 |
|
91 |
/** |
92 |
* Character constant indicating the colon in a signature. |
93 |
* Value is <code>':'</code>. |
94 |
* @since 3.0 |
95 |
*/ |
96 |
public static final char C_COLON = ':'; |
97 |
|
98 |
/** |
99 |
* Character constant indicating the primitive type long in a signature. |
100 |
* Value is <code>'J'</code>. |
101 |
*/ |
102 |
public static final char C_LONG = 'J'; |
103 |
|
104 |
/** |
105 |
* Character constant indicating the primitive type short in a signature. |
106 |
* Value is <code>'S'</code>. |
107 |
*/ |
108 |
public static final char C_SHORT = 'S'; |
109 |
|
110 |
/** |
111 |
* Character constant indicating result type void in a signature. |
112 |
* Value is <code>'V'</code>. |
113 |
*/ |
114 |
public static final char C_VOID = 'V'; |
115 |
|
116 |
/** |
117 |
* Character constant indicating the start of a resolved type variable in a |
118 |
* signature. Value is <code>'T'</code>. |
119 |
* @since 3.0 |
120 |
*/ |
121 |
public static final char C_TYPE_VARIABLE = 'T'; |
122 |
|
123 |
/** |
124 |
* Character constant indicating an unbound wildcard type argument |
125 |
* in a signature. |
126 |
* Value is <code>'*'</code>. |
127 |
* @since 3.0 |
128 |
*/ |
129 |
public static final char C_STAR = '*'; |
130 |
|
131 |
/** |
132 |
* Character constant indicating an exception in a signature. |
133 |
* Value is <code>'^'</code>. |
134 |
* @since 3.1 |
135 |
*/ |
136 |
public static final char C_EXCEPTION_START = '^'; |
137 |
|
138 |
/** |
139 |
* Character constant indicating a bound wildcard type argument |
140 |
* in a signature with extends clause. |
141 |
* Value is <code>'+'</code>. |
142 |
* @since 3.1 |
143 |
*/ |
144 |
public static final char C_EXTENDS = '+'; |
145 |
|
146 |
/** |
147 |
* Character constant indicating a bound wildcard type argument |
148 |
* in a signature with super clause. |
149 |
* Value is <code>'-'</code>. |
150 |
* @since 3.1 |
151 |
*/ |
152 |
public static final char C_SUPER = '-'; |
153 |
|
154 |
/** |
155 |
* Character constant indicating the dot in a signature. |
156 |
* Value is <code>'.'</code>. |
157 |
*/ |
158 |
public static final char C_DOT = '.'; |
159 |
|
160 |
/** |
161 |
* Character constant indicating the dollar in a signature. |
162 |
* Value is <code>'$'</code>. |
163 |
*/ |
164 |
public static final char C_DOLLAR = '$'; |
165 |
|
166 |
/** |
167 |
* Character constant indicating an array type in a signature. |
168 |
* Value is <code>'['</code>. |
169 |
*/ |
170 |
public static final char C_ARRAY = '['; |
171 |
|
172 |
/** |
173 |
* Character constant indicating the start of a resolved, named type in a |
174 |
* signature. Value is <code>'L'</code>. |
175 |
*/ |
176 |
public static final char C_RESOLVED = 'L'; |
177 |
|
178 |
/** |
179 |
* Character constant indicating the start of an unresolved, named type in a |
180 |
* signature. Value is <code>'Q'</code>. |
181 |
*/ |
182 |
public static final char C_UNRESOLVED = 'Q'; |
183 |
|
184 |
/** |
185 |
* Character constant indicating the end of a named type in a signature. |
186 |
* Value is <code>';'</code>. |
187 |
*/ |
188 |
public static final char C_NAME_END = ';'; |
189 |
|
190 |
/** |
191 |
* Character constant indicating the start of a parameter type list in a |
192 |
* signature. Value is <code>'('</code>. |
193 |
*/ |
194 |
public static final char C_PARAM_START = '('; |
195 |
|
196 |
/** |
197 |
* Character constant indicating the end of a parameter type list in a |
198 |
* signature. Value is <code>')'</code>. |
199 |
*/ |
200 |
public static final char C_PARAM_END = ')'; |
201 |
|
202 |
/** |
203 |
* Character constant indicating the start of a formal type parameter |
204 |
* (or type argument) list in a signature. Value is <code>'<'</code>. |
205 |
* @since 3.0 |
206 |
*/ |
207 |
public static final char C_GENERIC_START = '<'; |
208 |
|
209 |
/** |
210 |
* Character constant indicating the end of a generic type list in a |
211 |
* signature. Value is <code>'>'</code>. |
212 |
* @since 3.0 |
213 |
*/ |
214 |
public static final char C_GENERIC_END = '>'; |
215 |
|
216 |
/** |
217 |
* Character constant indicating a capture of a wildcard type in a |
218 |
* signature. Value is <code>'!'</code>. |
219 |
* @since 3.1 |
220 |
*/ |
221 |
public static final char C_CAPTURE = '!'; |
222 |
|
49 |
public interface Displayable { |
223 |
public interface Displayable { |
50 |
String displayString(Object o); |
224 |
String displayString(Object o); |
51 |
} |
225 |
} |
Lines 977-980
Link Here
|
977 |
} |
1151 |
} |
978 |
} |
1152 |
} |
979 |
} |
1153 |
} |
|
|
1154 |
public static int getParameterCount(char[] methodSignature) { |
1155 |
try { |
1156 |
int count = 0; |
1157 |
int i = CharOperation.indexOf(C_PARAM_START, methodSignature); |
1158 |
if (i < 0) { |
1159 |
throw new IllegalArgumentException(); |
1160 |
} else { |
1161 |
i++; |
1162 |
} |
1163 |
for (;;) { |
1164 |
if (methodSignature[i] == C_PARAM_END) { |
1165 |
return count; |
1166 |
} |
1167 |
int e= Util.scanTypeSignature(methodSignature, i); |
1168 |
if (e < 0) { |
1169 |
throw new IllegalArgumentException(); |
1170 |
} else { |
1171 |
i = e + 1; |
1172 |
} |
1173 |
count++; |
1174 |
} |
1175 |
} catch (ArrayIndexOutOfBoundsException e) { |
1176 |
throw new IllegalArgumentException(); |
1177 |
} |
1178 |
} |
1179 |
|
1180 |
/** |
1181 |
* Scans the given string for a type signature starting at the given index |
1182 |
* and returns the index of the last character. |
1183 |
* <pre> |
1184 |
* TypeSignature: |
1185 |
* | BaseTypeSignature |
1186 |
* | ArrayTypeSignature |
1187 |
* | ClassTypeSignature |
1188 |
* | TypeVariableSignature |
1189 |
* </pre> |
1190 |
* |
1191 |
* @param string the signature string |
1192 |
* @param start the 0-based character index of the first character |
1193 |
* @return the 0-based character index of the last character |
1194 |
* @exception IllegalArgumentException if this is not a type signature |
1195 |
*/ |
1196 |
public static int scanTypeSignature(char[] string, int start) { |
1197 |
// need a minimum 1 char |
1198 |
if (start >= string.length) { |
1199 |
throw new IllegalArgumentException(); |
1200 |
} |
1201 |
char c = string[start]; |
1202 |
switch (c) { |
1203 |
case C_ARRAY : |
1204 |
return scanArrayTypeSignature(string, start); |
1205 |
case C_RESOLVED : |
1206 |
case C_UNRESOLVED : |
1207 |
return scanClassTypeSignature(string, start); |
1208 |
case C_TYPE_VARIABLE : |
1209 |
return scanTypeVariableSignature(string, start); |
1210 |
case C_BOOLEAN : |
1211 |
case C_BYTE : |
1212 |
case C_CHAR : |
1213 |
case C_DOUBLE : |
1214 |
case C_FLOAT : |
1215 |
case C_INT : |
1216 |
case C_LONG : |
1217 |
case C_SHORT : |
1218 |
case C_VOID : |
1219 |
return scanBaseTypeSignature(string, start); |
1220 |
case C_CAPTURE : |
1221 |
return scanCaptureTypeSignature(string, start); |
1222 |
case C_EXTENDS: |
1223 |
case C_SUPER: |
1224 |
case C_STAR: |
1225 |
return scanTypeBoundSignature(string, start); |
1226 |
default : |
1227 |
throw new IllegalArgumentException(); |
1228 |
} |
1229 |
} |
1230 |
|
1231 |
/** |
1232 |
* Scans the given string for a base type signature starting at the given index |
1233 |
* and returns the index of the last character. |
1234 |
* <pre> |
1235 |
* BaseTypeSignature: |
1236 |
* <b>B</b> | <b>C</b> | <b>D</b> | <b>F</b> | <b>I</b> |
1237 |
* | <b>J</b> | <b>S</b> | <b>V</b> | <b>Z</b> |
1238 |
* </pre> |
1239 |
* Note that although the base type "V" is only allowed in method return types, |
1240 |
* there is no syntactic ambiguity. This method will accept them anywhere |
1241 |
* without complaint. |
1242 |
* |
1243 |
* @param string the signature string |
1244 |
* @param start the 0-based character index of the first character |
1245 |
* @return the 0-based character index of the last character |
1246 |
* @exception IllegalArgumentException if this is not a base type signature |
1247 |
*/ |
1248 |
public static int scanBaseTypeSignature(char[] string, int start) { |
1249 |
// need a minimum 1 char |
1250 |
if (start >= string.length) { |
1251 |
throw new IllegalArgumentException(); |
1252 |
} |
1253 |
char c = string[start]; |
1254 |
if ("BCDFIJSVZ".indexOf(c) >= 0) { //$NON-NLS-1$ |
1255 |
return start; |
1256 |
} else { |
1257 |
throw new IllegalArgumentException(); |
1258 |
} |
1259 |
} |
1260 |
|
1261 |
/** |
1262 |
* Scans the given string for an array type signature starting at the given |
1263 |
* index and returns the index of the last character. |
1264 |
* <pre> |
1265 |
* ArrayTypeSignature: |
1266 |
* <b>[</b> TypeSignature |
1267 |
* </pre> |
1268 |
* |
1269 |
* @param string the signature string |
1270 |
* @param start the 0-based character index of the first character |
1271 |
* @return the 0-based character index of the last character |
1272 |
* @exception IllegalArgumentException if this is not an array type signature |
1273 |
*/ |
1274 |
public static int scanArrayTypeSignature(char[] string, int start) { |
1275 |
int length = string.length; |
1276 |
// need a minimum 2 char |
1277 |
if (start >= length - 1) { |
1278 |
throw new IllegalArgumentException(); |
1279 |
} |
1280 |
char c = string[start]; |
1281 |
if (c != C_ARRAY) { |
1282 |
throw new IllegalArgumentException(); |
1283 |
} |
1284 |
|
1285 |
c = string[++start]; |
1286 |
while(c == C_ARRAY) { |
1287 |
// need a minimum 2 char |
1288 |
if (start >= length - 1) { |
1289 |
throw new IllegalArgumentException(); |
1290 |
} |
1291 |
c = string[++start]; |
1292 |
} |
1293 |
return scanTypeSignature(string, start); |
1294 |
} |
1295 |
|
1296 |
/** |
1297 |
* Scans the given string for a capture of a wildcard type signature starting at the given |
1298 |
* index and returns the index of the last character. |
1299 |
* <pre> |
1300 |
* CaptureTypeSignature: |
1301 |
* <b>!</b> TypeBoundSignature |
1302 |
* </pre> |
1303 |
* |
1304 |
* @param string the signature string |
1305 |
* @param start the 0-based character index of the first character |
1306 |
* @return the 0-based character index of the last character |
1307 |
* @exception IllegalArgumentException if this is not a capture type signature |
1308 |
*/ |
1309 |
public static int scanCaptureTypeSignature(char[] string, int start) { |
1310 |
// need a minimum 2 char |
1311 |
if (start >= string.length - 1) { |
1312 |
throw new IllegalArgumentException(); |
1313 |
} |
1314 |
char c = string[start]; |
1315 |
if (c != C_CAPTURE) { |
1316 |
throw new IllegalArgumentException(); |
1317 |
} |
1318 |
return scanTypeBoundSignature(string, start + 1); |
1319 |
} |
1320 |
|
1321 |
/** |
1322 |
* Scans the given string for a type variable signature starting at the given |
1323 |
* index and returns the index of the last character. |
1324 |
* <pre> |
1325 |
* TypeVariableSignature: |
1326 |
* <b>T</b> Identifier <b>;</b> |
1327 |
* </pre> |
1328 |
* |
1329 |
* @param string the signature string |
1330 |
* @param start the 0-based character index of the first character |
1331 |
* @return the 0-based character index of the last character |
1332 |
* @exception IllegalArgumentException if this is not a type variable signature |
1333 |
*/ |
1334 |
public static int scanTypeVariableSignature(char[] string, int start) { |
1335 |
// need a minimum 3 chars "Tx;" |
1336 |
if (start >= string.length - 2) { |
1337 |
throw new IllegalArgumentException(); |
1338 |
} |
1339 |
// must start in "T" |
1340 |
char c = string[start]; |
1341 |
if (c != C_TYPE_VARIABLE) { |
1342 |
throw new IllegalArgumentException(); |
1343 |
} |
1344 |
int id = scanIdentifier(string, start + 1); |
1345 |
c = string[id + 1]; |
1346 |
if (c == C_SEMICOLON) { |
1347 |
return id + 1; |
1348 |
} else { |
1349 |
throw new IllegalArgumentException(); |
1350 |
} |
1351 |
} |
1352 |
|
1353 |
/** |
1354 |
* Scans the given string for an identifier starting at the given |
1355 |
* index and returns the index of the last character. |
1356 |
* Stop characters are: ";", ":", "<", ">", "/", ".". |
1357 |
* |
1358 |
* @param string the signature string |
1359 |
* @param start the 0-based character index of the first character |
1360 |
* @return the 0-based character index of the last character |
1361 |
* @exception IllegalArgumentException if this is not an identifier |
1362 |
*/ |
1363 |
public static int scanIdentifier(char[] string, int start) { |
1364 |
// need a minimum 1 char |
1365 |
if (start >= string.length) { |
1366 |
throw new IllegalArgumentException(); |
1367 |
} |
1368 |
int p = start; |
1369 |
while (true) { |
1370 |
char c = string[p]; |
1371 |
if (c == '<' || c == '>' || c == ':' || c == ';' || c == '.' || c == '/') { |
1372 |
return p - 1; |
1373 |
} |
1374 |
p++; |
1375 |
if (p == string.length) { |
1376 |
return p - 1; |
1377 |
} |
1378 |
} |
1379 |
} |
1380 |
|
1381 |
/** |
1382 |
* Scans the given string for a class type signature starting at the given |
1383 |
* index and returns the index of the last character. |
1384 |
* <pre> |
1385 |
* ClassTypeSignature: |
1386 |
* { <b>L</b> | <b>Q</b> } Identifier |
1387 |
* { { <b>/</b> | <b>.</b> Identifier [ <b><</b> TypeArgumentSignature* <b>></b> ] } |
1388 |
* <b>;</b> |
1389 |
* </pre> |
1390 |
* Note that although all "/"-identifiers most come before "."-identifiers, |
1391 |
* there is no syntactic ambiguity. This method will accept them without |
1392 |
* complaint. |
1393 |
* |
1394 |
* @param string the signature string |
1395 |
* @param start the 0-based character index of the first character |
1396 |
* @return the 0-based character index of the last character |
1397 |
* @exception IllegalArgumentException if this is not a class type signature |
1398 |
*/ |
1399 |
public static int scanClassTypeSignature(char[] string, int start) { |
1400 |
// need a minimum 3 chars "Lx;" |
1401 |
if (start >= string.length - 2) { |
1402 |
throw new IllegalArgumentException(); |
1403 |
} |
1404 |
// must start in "L" or "Q" |
1405 |
char c = string[start]; |
1406 |
if (c != C_RESOLVED && c != C_UNRESOLVED) { |
1407 |
return -1; |
1408 |
} |
1409 |
int p = start + 1; |
1410 |
while (true) { |
1411 |
if (p >= string.length) { |
1412 |
throw new IllegalArgumentException(); |
1413 |
} |
1414 |
c = string[p]; |
1415 |
if (c == C_SEMICOLON) { |
1416 |
// all done |
1417 |
return p; |
1418 |
} else if (c == C_GENERIC_START) { |
1419 |
int e = scanTypeArgumentSignatures(string, p); |
1420 |
p = e; |
1421 |
} else if (c == C_DOT || c == '/') { |
1422 |
int id = scanIdentifier(string, p + 1); |
1423 |
p = id; |
1424 |
} |
1425 |
p++; |
1426 |
} |
1427 |
} |
1428 |
|
1429 |
/** |
1430 |
* Scans the given string for a type bound signature starting at the given |
1431 |
* index and returns the index of the last character. |
1432 |
* <pre> |
1433 |
* TypeBoundSignature: |
1434 |
* <b>[-+]</b> TypeSignature <b>;</b> |
1435 |
* <b>*</b></b> |
1436 |
* </pre> |
1437 |
* |
1438 |
* @param string the signature string |
1439 |
* @param start the 0-based character index of the first character |
1440 |
* @return the 0-based character index of the last character |
1441 |
* @exception IllegalArgumentException if this is not a type variable signature |
1442 |
*/ |
1443 |
public static int scanTypeBoundSignature(char[] string, int start) { |
1444 |
// need a minimum 1 char for wildcard |
1445 |
if (start >= string.length) { |
1446 |
throw new IllegalArgumentException(); |
1447 |
} |
1448 |
char c = string[start]; |
1449 |
switch (c) { |
1450 |
case C_STAR : |
1451 |
return start; |
1452 |
case C_SUPER : |
1453 |
case C_EXTENDS : |
1454 |
// need a minimum 3 chars "+[I" |
1455 |
if (start >= string.length - 2) { |
1456 |
throw new IllegalArgumentException(); |
1457 |
} |
1458 |
break; |
1459 |
default : |
1460 |
// must start in "+/-" |
1461 |
throw new IllegalArgumentException(); |
1462 |
|
1463 |
} |
1464 |
c = string[++start]; |
1465 |
switch (c) { |
1466 |
case C_CAPTURE : |
1467 |
return scanCaptureTypeSignature(string, start); |
1468 |
case C_SUPER : |
1469 |
case C_EXTENDS : |
1470 |
return scanTypeBoundSignature(string, start); |
1471 |
case C_RESOLVED : |
1472 |
case C_UNRESOLVED : |
1473 |
return scanClassTypeSignature(string, start); |
1474 |
case C_TYPE_VARIABLE : |
1475 |
return scanTypeVariableSignature(string, start); |
1476 |
case C_ARRAY : |
1477 |
return scanArrayTypeSignature(string, start); |
1478 |
case C_STAR: |
1479 |
return start; |
1480 |
default: |
1481 |
throw new IllegalArgumentException(); |
1482 |
} |
1483 |
} |
1484 |
|
1485 |
/** |
1486 |
* Scans the given string for a list of type argument signatures starting at |
1487 |
* the given index and returns the index of the last character. |
1488 |
* <pre> |
1489 |
* TypeArgumentSignatures: |
1490 |
* <b><</b> TypeArgumentSignature* <b>></b> |
1491 |
* </pre> |
1492 |
* Note that although there is supposed to be at least one type argument, there |
1493 |
* is no syntactic ambiguity if there are none. This method will accept zero |
1494 |
* type argument signatures without complaint. |
1495 |
* |
1496 |
* @param string the signature string |
1497 |
* @param start the 0-based character index of the first character |
1498 |
* @return the 0-based character index of the last character |
1499 |
* @exception IllegalArgumentException if this is not a list of type arguments |
1500 |
* signatures |
1501 |
*/ |
1502 |
public static int scanTypeArgumentSignatures(char[] string, int start) { |
1503 |
// need a minimum 2 char "<>" |
1504 |
if (start >= string.length - 1) { |
1505 |
throw new IllegalArgumentException(); |
1506 |
} |
1507 |
char c = string[start]; |
1508 |
if (c != C_GENERIC_START) { |
1509 |
throw new IllegalArgumentException(); |
1510 |
} |
1511 |
int p = start + 1; |
1512 |
while (true) { |
1513 |
if (p >= string.length) { |
1514 |
throw new IllegalArgumentException(); |
1515 |
} |
1516 |
c = string[p]; |
1517 |
if (c == C_GENERIC_END) { |
1518 |
return p; |
1519 |
} |
1520 |
int e = scanTypeArgumentSignature(string, p); |
1521 |
p = e + 1; |
1522 |
} |
1523 |
} |
1524 |
|
1525 |
/** |
1526 |
* Scans the given string for a type argument signature starting at the given |
1527 |
* index and returns the index of the last character. |
1528 |
* <pre> |
1529 |
* TypeArgumentSignature: |
1530 |
* <b>*</b> |
1531 |
* | <b>+</b> TypeSignature |
1532 |
* | <b>-</b> TypeSignature |
1533 |
* | TypeSignature |
1534 |
* </pre> |
1535 |
* Note that although base types are not allowed in type arguments, there is |
1536 |
* no syntactic ambiguity. This method will accept them without complaint. |
1537 |
* |
1538 |
* @param string the signature string |
1539 |
* @param start the 0-based character index of the first character |
1540 |
* @return the 0-based character index of the last character |
1541 |
* @exception IllegalArgumentException if this is not a type argument signature |
1542 |
*/ |
1543 |
public static int scanTypeArgumentSignature(char[] string, int start) { |
1544 |
// need a minimum 1 char |
1545 |
if (start >= string.length) { |
1546 |
throw new IllegalArgumentException(); |
1547 |
} |
1548 |
char c = string[start]; |
1549 |
switch (c) { |
1550 |
case C_STAR : |
1551 |
return start; |
1552 |
case C_EXTENDS : |
1553 |
case C_SUPER : |
1554 |
return scanTypeBoundSignature(string, start); |
1555 |
default : |
1556 |
return scanTypeSignature(string, start); |
1557 |
} |
1558 |
} |
980 |
} |
1559 |
} |