KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > JavaVariable


1 /*
2  * JavaVariable.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: JavaVariable.java,v 1.1.1.1 2002/09/24 16:08:09 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.StringTokenizer JavaDoc;
25
26 public final class JavaVariable
27 {
28     // what
29
public static final int FIELD = 1;
30     public static final int PARAMETER = 2;
31     public static final int LOCAL = 3;
32
33     private final String JavaDoc type;
34     private final String JavaDoc name;
35     private final int what;
36
37     public JavaVariable(String JavaDoc s, int what)
38     {
39         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s);
40         FastStringBuffer sb = new FastStringBuffer();
41         while (st.hasMoreTokens()) {
42             sb.append(st.nextToken());
43             sb.append(' ');
44         }
45         s = sb.toString();
46         int length = s.length();
47         for (int i = length-1; i >= 0; i--) {
48             if (" \t=;,".indexOf(s.charAt(i)) >= 0)
49                 --length;
50             else
51                 break;
52         }
53         s = s.substring(0, length);
54         int index = s.lastIndexOf(' ');
55         if (index < 0)
56             index = s.lastIndexOf('\t');
57         if (index >= 0) {
58             type = s.substring(0, index);
59             name = s.substring(index+1).trim();
60         } else {
61             type = "";
62             name = s;
63         }
64         this.what = what;
65     }
66
67     public final String JavaDoc getName()
68     {
69         return name;
70     }
71
72     public String JavaDoc toString()
73     {
74         FastStringBuffer sb = new FastStringBuffer(type);
75         sb.append(' ');
76         sb.append(name);
77         sb.append(" (");
78         switch (what) {
79             case FIELD:
80                 sb.append("field");
81                 break;
82             case PARAMETER:
83                 sb.append("parameter");
84                 break;
85             case LOCAL:
86                 sb.append("local variable");
87                 break;
88         }
89         sb.append(")");
90         return sb.toString();
91     }
92 }
93
Popular Tags