Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/NewExpression.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.ast;
8
9 import org.mozilla.javascript.Token;
10
11 /**
12 * New expression. Node type is {@link Token#NEW}.<p>
13 *
14 * <pre><i>NewExpression</i> :
15 * MemberExpression
16 * <b>new</b> NewExpression</pre>
17 *
18 * This node is a subtype of {@link FunctionCall}, mostly for internal code
19 * sharing. Structurally a {@code NewExpression} node is very similar to a
20 * {@code FunctionCall}, so it made a certain amount of sense.
21 */
22 public class NewExpression extends FunctionCall {
23
24 private ObjectLiteral initializer;
25
26 {
27 type = Token.NEW;
28 }
29
30 public NewExpression() {
31 }
32
33 public NewExpression(int pos) {
34 super(pos);
35 }
36
37 public NewExpression(int pos, int len) {
38 super(pos, len);
39 }
40
41 /**
42 * Returns initializer object, if any.
43 * @return extra initializer object-literal expression, or {@code null} if
44 * not specified.
45 */
46 public ObjectLiteral getInitializer() {
47 return initializer;
48 }
49
50 /**
51 * Sets initializer object. Rhino supports an experimental syntax
52 * of the form {@code new expr [ ( arglist ) ] [initializer]},
53 * in which initializer is an object literal that is used to set
54 * additional properties on the newly-created {@code expr} object.
55 *
56 * @param initializer extra initializer object.
57 * Can be {@code null}.
58 */
59 public void setInitializer(ObjectLiteral initializer) {
60 this.initializer = initializer;
61 if (initializer != null)
62 initializer.setParent(this);
63 }
64
65 @Override
66 public String toSource(int depth) {
67 StringBuilder sb = new StringBuilder();
68 sb.append(makeIndent(depth));
69 sb.append("new ");
70 sb.append(target.toSource(0));
71 sb.append("(");
72 if (arguments != null) {
73 printList(arguments, sb);
74 }
75 sb.append(")");
76 if (initializer != null) {
77 sb.append(" ");
78 sb.append(initializer.toSource(0));
79 }
80 return sb.toString();
81 }
82
83 /**
84 * Visits this node, the target, and each argument. If there is
85 * a trailing initializer node, visits that last.
86 */
87 @Override
88 public void visit(NodeVisitor v) {
89 if (v.visit(this)) {
90 target.visit(v);
91 for (AstNode arg : getArguments()) {
92 arg.visit(v);
93 }
94 if (initializer != null) {
95 initializer.visit(v);
96 }
97 }
98 }
99 }