1 /* 2 * Javassist, a Java-bytecode translator toolkit. 3 * Copyright (C) 1999-2007 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 NewExpr(ASTList className, ASTList args)28 public NewExpr(ASTList className, ASTList args) { 29 super(className, new ASTList(args)); 30 newArray = false; 31 arrayType = CLASS; 32 } 33 NewExpr(int type, ASTList arraySize, ArrayInit init)34 public NewExpr(int type, ASTList arraySize, ArrayInit init) { 35 super(null, new ASTList(arraySize)); 36 newArray = true; 37 arrayType = type; 38 if (init != null) 39 append(this, init); 40 } 41 makeObjectArray(ASTList className, ASTList arraySize, ArrayInit init)42 public static NewExpr makeObjectArray(ASTList className, 43 ASTList arraySize, ArrayInit 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 isArray()52 public boolean isArray() { return newArray; } 53 54 /* TokenId.CLASS, TokenId.INT, ... 55 */ getArrayType()56 public int getArrayType() { return arrayType; } 57 getClassName()58 public ASTList getClassName() { return (ASTList)getLeft(); } 59 getArguments()60 public ASTList getArguments() { return (ASTList)getRight().getLeft(); } 61 getArraySize()62 public ASTList getArraySize() { return getArguments(); } 63 getInitializer()64 public ArrayInit getInitializer() { 65 ASTree t = getRight().getRight(); 66 if (t == null) 67 return null; 68 else 69 return (ArrayInit)t.getLeft(); 70 } 71 accept(Visitor v)72 public void accept(Visitor v) throws CompileError { v.atNewExpr(this); } 73 getTag()74 protected String getTag() { 75 return newArray ? "new[]" : "new"; 76 } 77 } 78