/Users/lyon/j4p/src/javassist/compiler/ast/NewExpr.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    * New Expression. 
23    */ 
24   public class NewExpr extends ASTList implements TokenId { 
25       protected boolean newArray; 
26       protected int arrayType; 
27    
28       public NewExpr(ASTList className, ASTList args) { 
29           super(className, new ASTList(args)); 
30           newArray = false; 
31           arrayType = CLASS; 
32       } 
33    
34       public NewExpr(int type, ASTList arraySize, ASTree init) { 
35           super(null, new ASTList(arraySize)); 
36           newArray = true; 
37           arrayType = type; 
38           if (init != null) 
39               append(this, init); 
40       } 
41    
42       public static NewExpr makeObjectArray(ASTList className, 
43                                             ASTList arraySize, ASTree init) { 
44           NewExpr e = new NewExpr(className, arraySize); 
45           e.newArray = true; 
46           if (init != null) 
47               append(e, init); 
48    
49           return e; 
50       } 
51    
52       public boolean isArray() { 
53           return newArray; 
54       } 
55    
56       /* TokenId.CLASS, TokenId.INT, ... 
57        */ 
58       public int getArrayType() { 
59           return arrayType; 
60       } 
61    
62       public ASTList getClassName() { 
63           return (ASTList) getLeft(); 
64       } 
65    
66       public ASTList getArguments() { 
67           return (ASTList) getRight().getLeft(); 
68       } 
69    
70       public ASTList getArraySize() { 
71           return getArguments(); 
72       } 
73    
74       public ASTree getInitializer() { 
75           ASTree t = getRight().getRight(); 
76           if (t == null) 
77               return null; 
78           else 
79               return t.getLeft(); 
80       } 
81    
82       public void accept(Visitor v) throws CompileError { 
83           v.atNewExpr(this); 
84       } 
85    
86       protected String getTag() { 
87           return newArray ? "new[]" : "new"; 
88       } 
89   } 
90