KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > compiler > ast > Declarator


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist.compiler.ast;
17
18 import javassist.compiler.TokenId;
19 import javassist.compiler.CompileError;
20
21 /**
22  * Variable declarator.
23  */

24 public class Declarator extends ASTList implements TokenId {
25     protected int varType;
26     protected int arrayDim;
27     protected int localVar;
28     protected String JavaDoc qualifiedClass; // JVM-internal representation
29

30     public Declarator(int type, int dim) {
31         super(null);
32         varType = type;
33         arrayDim = dim;
34         localVar = -1;
35         qualifiedClass = null;
36     }
37
38     public Declarator(ASTList className, int dim) {
39         super(null);
40         varType = CLASS;
41         arrayDim = dim;
42         localVar = -1;
43         qualifiedClass = astToClassName(className, '/');
44     }
45
46     /* For declaring a pre-defined? local variable.
47      */

48     public Declarator(int type, String JavaDoc jvmClassName, int dim,
49                       int var, Symbol sym) {
50         super(null);
51         varType = type;
52         arrayDim = dim;
53         localVar = var;
54         qualifiedClass = jvmClassName;
55         setLeft(sym);
56         append(this, null); // initializer
57
}
58
59     public Declarator make(Symbol sym, int dim, ASTree init) {
60         Declarator d = new Declarator(this.varType, this.arrayDim + dim);
61         d.qualifiedClass = this.qualifiedClass;
62         d.setLeft(sym);
63         append(d, init);
64         return d;
65     }
66
67     /* Returns CLASS, BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT,
68      * or DOUBLE (or VOID)
69      */

70     public int getType() { return varType; }
71
72     public int getArrayDim() { return arrayDim; }
73
74     public void addArrayDim(int d) { arrayDim += d; }
75
76     public String JavaDoc getClassName() { return qualifiedClass; }
77
78     public void setClassName(String JavaDoc s) { qualifiedClass = s; }
79
80     public Symbol getVariable() { return (Symbol)getLeft(); }
81
82     public void setVariable(Symbol sym) { setLeft(sym); }
83
84     public ASTree getInitializer() {
85         ASTList t = tail();
86         if (t != null)
87             return t.head();
88         else
89             return null;
90     }
91
92     public void setLocalVar(int n) { localVar = n; }
93
94     public int getLocalVar() { return localVar; }
95
96     public String JavaDoc getTag() { return "decl"; }
97
98     public void accept(Visitor v) throws CompileError {
99         v.atDeclarator(this);
100     }
101
102     public static String JavaDoc astToClassName(ASTList name, char sep) {
103         if (name == null)
104             return null;
105
106         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
107         astToClassName(sbuf, name, sep);
108         return sbuf.toString();
109     }
110
111     private static void astToClassName(StringBuffer JavaDoc sbuf, ASTList name,
112                                        char sep) {
113         for (;;) {
114             ASTree h = name.head();
115             if (h instanceof Symbol)
116                 sbuf.append(((Symbol)h).get());
117             else if (h instanceof ASTList)
118                 astToClassName(sbuf, (ASTList)h, sep);
119
120             name = name.tail();
121             if (name == null)
122                 break;
123
124             sbuf.append(sep);
125         }
126     }
127 }
128
Popular Tags