Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/ObjectProperty.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 * AST node for a single name:value entry in an Object literal.
13 * For simple entries, the node type is {@link Token#COLON}, and
14 * the name (left side expression) is either a {@link Name}, a
15 * {@link StringLiteral} or a {@link NumberLiteral}.<p>
16 *
17 * This node type is also used for getter/setter properties in object
18 * literals. In this case the node bounds include the "get" or "set"
19 * keyword. The left-hand expression in this case is always a
20 * {@link Name}, and the overall node type is {@link Token#GET} or
21 * {@link Token#SET}, as appropriate.<p>
22 *
23 * The {@code operatorPosition} field is meaningless if the node is
24 * a getter or setter.<p>
25 *
26 * <pre><i>ObjectProperty</i> :
27 * PropertyName <b>:</b> AssignmentExpression
28 * <i>PropertyName</i> :
29 * Identifier
30 * StringLiteral
31 * NumberLiteral</pre>
32 */
33 public class ObjectProperty extends InfixExpression {
34
35 {
36 type = Token.COLON;
37 }
38
39 /**
40 * Sets the node type. Must be one of
41 * {@link Token#COLON}, {@link Token#GET}, or {@link Token#SET}.
42 * @throws IllegalArgumentException if {@code nodeType} is invalid
43 */
44 public void setNodeType(int nodeType) {
45 if (nodeType != Token.COLON
46 && nodeType != Token.GET
47 && nodeType != Token.SET
48 && nodeType != Token.METHOD)
49 throw new IllegalArgumentException("invalid node type: "
50 + nodeType);
51 setType(nodeType);
52 }
53
54 public ObjectProperty() {
55 }
56
57 public ObjectProperty(int pos) {
58 super(pos);
59 }
60
61 public ObjectProperty(int pos, int len) {
62 super(pos, len);
63 }
64
65 /**
66 * Marks this node as a "getter" property.
67 */
68 public void setIsGetterMethod() {
69 type = Token.GET;
70 }
71
72 /**
73 * Returns true if this is a getter function.
74 */
75 public boolean isGetterMethod() {
76 return type == Token.GET;
77 }
78
79 /**
80 * Marks this node as a "setter" property.
81 */
82 public void setIsSetterMethod() {
83 type = Token.SET;
84 }
85
86 /**
87 * Returns true if this is a setter function.
88 */
89 public boolean isSetterMethod() {
90 return type == Token.SET;
91 }
92
93 public void setIsNormalMethod() {
94 type = Token.METHOD;
95 }
96
97 public boolean isNormalMethod() {
98 return type == Token.METHOD;
99 }
100
101 public boolean isMethod() {
102 return isGetterMethod() || isSetterMethod() || isNormalMethod();
103 }
104
105 @Override
106 public String toSource(int depth) {
107 StringBuilder sb = new StringBuilder();
108 sb.append("\n");
109 sb.append(makeIndent(depth+1));
110 if (isGetterMethod()) {
111 sb.append("get ");
112 } else if (isSetterMethod()) {
113 sb.append("set ");
114 }
115 sb.append(left.toSource(getType()==Token.COLON ? 0 : depth));
116 if (type == Token.COLON) {
117 sb.append(": ");
118 }
119 sb.append(right.toSource(getType()==Token.COLON ? 0 : depth+1));
120 return sb.toString();
121 }
122 }