001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.drools.asm.signature;
030:
031: /**
032: * A type signature parser to make a signature visitor visit an existing
033: * signature.
034: *
035: * @author Thomas Hallgren
036: * @author Eric Bruneton
037: */
038: public class SignatureReader {
039:
040: /**
041: * The signature to be read.
042: */
043: private final String signature;
044:
045: /**
046: * Constructs a {@link SignatureReader} for the given signature.
047: *
048: * @param signature A <i>ClassSignature</i>, <i>MethodTypeSignature</i>,
049: * or <i>FieldTypeSignature</i>.
050: */
051: public SignatureReader(final String signature) {
052: this .signature = signature;
053: }
054:
055: /**
056: * Makes the given visitor visit the signature of this
057: * {@link SignatureReader}. This signature is the one specified in the
058: * constructor (see {@link #SignatureReader(String) SignatureReader}). This
059: * method is intended to be called on a {@link SignatureReader} that was
060: * created using a <i>ClassSignature</i> (such as the
061: * <code>signature</code> parameter of the
062: * {@link org.drools.asm.ClassVisitor#visit ClassVisitor.visit} method)
063: * or a <i>MethodTypeSignature</i> (such as the <code>signature</code>
064: * parameter of the
065: * {@link org.drools.asm.ClassVisitor#visitMethod ClassVisitor.visitMethod}
066: * method).
067: *
068: * @param v the visitor that must visit this signature.
069: */
070: public void accept(final SignatureVisitor v) {
071: final String signature = this .signature;
072: final int len = signature.length();
073: int pos;
074: char c;
075:
076: if (signature.charAt(0) == '<') {
077: pos = 2;
078: do {
079: final int end = signature.indexOf(':', pos);
080: v.visitFormalTypeParameter(signature.substring(pos - 1,
081: end));
082: pos = end + 1;
083:
084: c = signature.charAt(pos);
085: if (c == 'L' || c == '[' || c == 'T') {
086: pos = parseType(signature, pos, v.visitClassBound());
087: }
088:
089: for (;;) {
090: if ((c = signature.charAt(pos++)) == ':') {
091: pos = parseType(signature, pos, v
092: .visitInterfaceBound());
093: } else {
094: break;
095: }
096: }
097: } while (c != '>');
098: } else {
099: pos = 0;
100: }
101:
102: if (signature.charAt(pos) == '(') {
103: pos = pos + 1;
104: while (signature.charAt(pos) != ')') {
105: pos = parseType(signature, pos, v.visitParameterType());
106: }
107: pos = parseType(signature, pos + 1, v.visitReturnType());
108: while (pos < len) {
109: pos = parseType(signature, pos + 1, v
110: .visitExceptionType());
111: }
112: } else {
113: pos = parseType(signature, pos, v.visitSuperclass());
114: while (pos < len) {
115: pos = parseType(signature, pos, v.visitInterface());
116: }
117: }
118: }
119:
120: /**
121: * Makes the given visitor visit the signature of this
122: * {@link SignatureReader}. This signature is the one specified in the
123: * constructor (see {@link #SignatureReader(String) SignatureReader}). This
124: * method is intended to be called on a {@link SignatureReader} that was
125: * created using a <i>FieldTypeSignature</i>, such as the
126: * <code>signature</code> parameter of the
127: * {@link org.drools.asm.ClassVisitor#visitField
128: * ClassVisitor.visitField} or {@link
129: * org.drools.asm.MethodVisitor#visitLocalVariable
130: * MethodVisitor.visitLocalVariable} methods.
131: *
132: * @param v the visitor that must visit this signature.
133: */
134: public void acceptType(final SignatureVisitor v) {
135: parseType(this .signature, 0, v);
136: }
137:
138: /**
139: * Parses a field type signature and makes the given visitor visit it.
140: *
141: * @param signature a string containing the signature that must be parsed.
142: * @param pos index of the first character of the signature to parsed.
143: * @param v the visitor that must visit this signature.
144: * @return the index of the first character after the parsed signature.
145: */
146: private static int parseType(final String signature, int pos,
147: final SignatureVisitor v) {
148: char c;
149: int start, end;
150: boolean visited, inner;
151: String name;
152:
153: switch (c = signature.charAt(pos++)) {
154: case 'Z':
155: case 'C':
156: case 'B':
157: case 'S':
158: case 'I':
159: case 'F':
160: case 'J':
161: case 'D':
162: case 'V':
163: v.visitBaseType(c);
164: return pos;
165:
166: case '[':
167: return parseType(signature, pos, v.visitArrayType());
168:
169: case 'T':
170: end = signature.indexOf(';', pos);
171: v.visitTypeVariable(signature.substring(pos, end));
172: return end + 1;
173:
174: default: // case 'L':
175: start = pos;
176: visited = false;
177: inner = false;
178: for (;;) {
179: switch (c = signature.charAt(pos++)) {
180: case '.':
181: case ';':
182: if (!visited) {
183: name = signature.substring(start, pos - 1);
184: if (inner) {
185: v.visitInnerClassType(name);
186: } else {
187: v.visitClassType(name);
188: }
189: }
190: if (c == ';') {
191: v.visitEnd();
192: return pos;
193: }
194: start = pos;
195: visited = false;
196: inner = true;
197: break;
198:
199: case '<':
200: name = signature.substring(start, pos - 1);
201: if (inner) {
202: v.visitInnerClassType(name);
203: } else {
204: v.visitClassType(name);
205: }
206: visited = true;
207: top: for (;;) {
208: switch (c = signature.charAt(pos)) {
209: case '>':
210: break top;
211: case '*':
212: ++pos;
213: v.visitTypeArgument();
214: break;
215: case '+':
216: case '-':
217: pos = parseType(signature, pos + 1, v
218: .visitTypeArgument(c));
219: break;
220: default:
221: pos = parseType(signature, pos, v
222: .visitTypeArgument('='));
223: break;
224: }
225: }
226: }
227: }
228: }
229: }
230: }
|