1 /* 2 * Javassist, a Java-bytecode translator toolkit. 3 * Copyright (C) 1999- 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 * or the Apache License Version 2.0. 10 * 11 * Software distributed under the License is distributed on an "AS IS" basis, 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 * for the specific language governing rights and limitations under the 14 * License. 15 */ 16 17 package javassist.compiler.ast; 18 19 import javassist.compiler.CompileError; 20 import javassist.compiler.TokenId; 21 22 /** 23 * New Expression. 24 */ 25 public class NewExpr extends ASTList implements TokenId { 26 /** default serialVersionUID */ 27 private static final long serialVersionUID = 1L; 28 protected boolean newArray; 29 protected int arrayType; 30 NewExpr(ASTList className, ASTList args)31 public NewExpr(ASTList className, ASTList args) { 32 super(className, new ASTList(args)); 33 newArray = false; 34 arrayType = CLASS; 35 } 36 NewExpr(int type, ASTList arraySize, ArrayInit init)37 public NewExpr(int type, ASTList arraySize, ArrayInit init) { 38 super(null, new ASTList(arraySize)); 39 newArray = true; 40 arrayType = type; 41 if (init != null) 42 append(this, init); 43 } 44 makeObjectArray(ASTList className, ASTList arraySize, ArrayInit init)45 public static NewExpr makeObjectArray(ASTList className, 46 ASTList arraySize, ArrayInit init) { 47 NewExpr e = new NewExpr(className, arraySize); 48 e.newArray = true; 49 if (init != null) 50 append(e, init); 51 52 return e; 53 } 54 isArray()55 public boolean isArray() { return newArray; } 56 57 /* TokenId.CLASS, TokenId.INT, ... 58 */ getArrayType()59 public int getArrayType() { return arrayType; } 60 getClassName()61 public ASTList getClassName() { return (ASTList)getLeft(); } 62 getArguments()63 public ASTList getArguments() { return (ASTList)getRight().getLeft(); } 64 getArraySize()65 public ASTList getArraySize() { return getArguments(); } 66 getInitializer()67 public ArrayInit getInitializer() { 68 ASTree t = getRight().getRight(); 69 if (t == null) 70 return null; 71 return (ArrayInit)t.getLeft(); 72 } 73 74 @Override accept(Visitor v)75 public void accept(Visitor v) throws CompileError { v.atNewExpr(this); } 76 77 @Override getTag()78 protected String getTag() { 79 return newArray ? "new[]" : "new"; 80 } 81 } 82