/Users/lyon/j4p/src/javassist/compiler/ast/Declarator.java

1    /* 
2     * Javassist, a Java-bytecode translator toolkit. 
3     * Copyright (C) 1999-2003 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 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 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           d.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() { 
71           return varType; 
72       } 
73    
74       public int getArrayDim() { 
75           return arrayDim; 
76       } 
77    
78       public void addArrayDim(int d) { 
79           arrayDim += d; 
80       } 
81    
82       public String getClassName() { 
83           return qualifiedClass; 
84       } 
85    
86       public void setClassName(String s) { 
87           qualifiedClass = s; 
88       } 
89    
90       public Symbol getVariable() { 
91           return (Symbol) getLeft(); 
92       } 
93    
94       public void setVariable(Symbol sym) { 
95           setLeft(sym); 
96       } 
97    
98       public ASTree getInitializer() { 
99           ASTList t = tail(); 
100          if (t != null) 
101              return t.head(); 
102          else 
103              return null; 
104      } 
105   
106      public void setLocalVar(int n) { 
107          localVar = n; 
108      } 
109   
110      public int getLocalVar() { 
111          return localVar; 
112      } 
113   
114      public String getTag() { 
115          return "decl"; 
116      } 
117   
118      public void accept(Visitor v) throws CompileError { 
119          v.atDeclarator(this); 
120      } 
121   
122      public static String astToClassName(ASTList name, char sep) { 
123          if (name == null) 
124              return null; 
125   
126          StringBuffer sbuf = new StringBuffer(); 
127          for (; ;) { 
128              sbuf.append(((Symbol) name.head()).get()); 
129              name = name.tail(); 
130              if (name == null) 
131                  break; 
132   
133              sbuf.append(sep); 
134          } 
135   
136          return sbuf.toString(); 
137      } 
138  } 
139