01: /*******************************************************************************
02: * Copyright (c) 2004, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdi.internal;
11:
12: import java.util.ArrayList;
13: import java.util.Collections;
14: import java.util.List;
15:
16: /**
17: * Utility class to perform operations on generic signatures
18: */
19: public class GenericSignature {
20:
21: private static final char C_CLASS_TYPE = 'L';
22: private static final char C_TYPE_VARIABLE = 'T';
23: private static final char C_ARRAY = '[';
24: private static final char C_WILDCARD_PLUS = '+';
25: private static final char C_WILDCARD_MINUS = '-';
26:
27: private static final char C_TYPE_END = ';';
28: private static final char C_PARAMETERS_START = '(';
29: private static final char C_PARAMETERS_END = ')';
30: private static final char C_TYPE_ARGUMENTS_START = '<';
31: private static final char C_TYPE_ARGUMENTS_END = '>';
32:
33: public static List getParameterTypes(String methodSignature) {
34: int parameterStart = methodSignature
35: .indexOf(C_PARAMETERS_START);
36: int parametersEnd = methodSignature
37: .lastIndexOf(C_PARAMETERS_END);
38: if (parameterStart == -1 || parametersEnd == -1) {
39: // used to throw illegal argument exception, but now return empty list if we can't parse it
40: return Collections.EMPTY_LIST;
41: }
42: return getTypeSignatureList(methodSignature.substring(
43: parameterStart + 1, parametersEnd));
44: }
45:
46: private static List getTypeSignatureList(String typeSignatureList) {
47: List list = new ArrayList();
48: int pos = 0;
49: while (pos < typeSignatureList.length()) {
50: int signatureLength = nextTypeSignatureLength(
51: typeSignatureList, pos);
52: list.add(typeSignatureList.substring(pos,
53: pos += signatureLength));
54: }
55: return list;
56: }
57:
58: private static int nextTypeSignatureLength(String signature,
59: int startPos) {
60: int inclusionLevel = 0;
61: for (int i = startPos, length = signature.length(); i < length; i++) {
62: if (inclusionLevel == 0) {
63: switch (signature.charAt(i)) {
64: case C_CLASS_TYPE:
65: case C_TYPE_VARIABLE:
66: case C_WILDCARD_PLUS:
67: case C_WILDCARD_MINUS:
68: inclusionLevel = 1;
69: break;
70: case C_ARRAY:
71: break;
72: default:
73: return i - startPos + 1;
74: }
75: } else {
76: switch (signature.charAt(i)) {
77: case C_TYPE_END:
78: if (inclusionLevel == 1) {
79: return i - startPos + 1;
80: }
81: break;
82: case C_TYPE_ARGUMENTS_START:
83: inclusionLevel++;
84: break;
85: case C_TYPE_ARGUMENTS_END:
86: inclusionLevel--;
87: break;
88: }
89: }
90: }
91: throw new IllegalArgumentException();
92: }
93: }
|