KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > TypeSignatureParser


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.bytecode;
31
32 import java.util.ArrayList JavaDoc;
33
34 /**
35  * Manages an introspected java classes.
36  */

37 public class TypeSignatureParser {
38   private final JClassLoader _loader;
39   private final String JavaDoc _sig;
40   private final int _length;
41   private int _index;
42   
43   public TypeSignatureParser(JClassLoader loader, String JavaDoc sig)
44   {
45     this(loader, sig, 0);
46   }
47   
48   public TypeSignatureParser(JClassLoader loader, String JavaDoc sig, int index)
49   {
50     _loader = loader;
51     _sig = sig;
52     _length = sig.length();
53     _index = index;
54   }
55
56   JType nextType()
57   {
58     int ch = read();
59
60     switch (ch) {
61     case -1:
62       return null;
63       
64     case '>':
65     case ')':
66       _index--;
67       return null;
68
69     case 'V':
70       return JClass.VOID;
71     case 'Z':
72       return JClass.BOOLEAN;
73     case 'B':
74       return JClass.BYTE;
75     case 'S':
76       return JClass.SHORT;
77     case 'I':
78       return JClass.INT;
79     case 'J':
80       return JClass.LONG;
81     case 'F':
82       return JClass.FLOAT;
83     case 'D':
84       return JClass.DOUBLE;
85     case 'C':
86       return JClass.CHAR;
87
88     case 'L':
89       return parseClass();
90
91     default:
92       throw new IllegalStateException JavaDoc("Can't parse: " + _sig);
93     }
94   }
95
96   private JType parseClass()
97   {
98     int begin = _index;
99     int end = begin;
100
101     int ch;
102     
103     for (ch = read();
104      ch >= 0 &&
105        ch != ';' && ch != ')' &&
106        ch != ',' && ch != '<' && ch != '>';
107      ch = read()) {
108       end = _index;
109     }
110
111     String JavaDoc className = _sig.substring(begin, end).replace('/', '.');
112     JClass rawClass = _loader.forName(className);
113
114     if (ch == '<')
115       return parseParameterizedType(rawClass);
116     else
117       return rawClass;
118   }
119
120   private JType parseParameterizedType(JClass rawClass)
121   {
122     ArrayList JavaDoc<JType> argList = new ArrayList JavaDoc<JType>();
123
124     JType type;
125     while ((type = nextType()) != null) {
126       argList.add(type);
127     }
128
129     int ch = read();
130     if (ch != '>')
131       throw new IllegalStateException JavaDoc("expected '>' at " + (char) ch);
132
133     JType []args = new JType[argList.size()];
134     argList.toArray(args);
135
136     return new JavaParameterizedType(_loader, rawClass, args);
137   }
138
139   /**
140    * Reads the next character.
141    */

142   private int read()
143   {
144     if (_index < _length)
145       return _sig.charAt(_index++);
146     else
147       return -1;
148   }
149 }
150
Popular Tags