169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.compiler.ast;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.TokenId;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.CompileError;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * New Expression.
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class NewExpr extends ASTList implements TokenId {
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected boolean newArray;
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected int arrayType;
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public NewExpr(ASTList className, ASTList args) {
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(className, new ASTList(args));
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        newArray = false;
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        arrayType = CLASS;
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public NewExpr(int type, ASTList arraySize, ArrayInit init) {
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(null, new ASTList(arraySize));
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        newArray = true;
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        arrayType = type;
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (init != null)
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            append(this, init);
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static NewExpr makeObjectArray(ASTList className,
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                          ASTList arraySize, ArrayInit init) {
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        NewExpr e = new NewExpr(className, arraySize);
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        e.newArray = true;
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (init != null)
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            append(e, init);
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return e;
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public boolean isArray() { return newArray; }
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /* TokenId.CLASS, TokenId.INT, ...
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int getArrayType() { return arrayType; }
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ASTList getClassName() { return (ASTList)getLeft(); }
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ASTList getArguments() { return (ASTList)getRight().getLeft(); }
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ASTList getArraySize() { return getArguments(); }
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ArrayInit getInitializer() {
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ASTree t = getRight().getRight();
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (t == null)
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return null;
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return (ArrayInit)t.getLeft();
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void accept(Visitor v) throws CompileError { v.atNewExpr(this); }
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected String getTag() {
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return newArray ? "new[]" : "new";
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
78