• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3  * Copyright (C) 2011, 2013-2016 The JavaParser Team.
4  *
5  * This file is part of JavaParser.
6  *
7  * JavaParser can be used either under the terms of
8  * a) the GNU Lesser General Public License as published by
9  *     the Free Software Foundation, either version 3 of the License, or
10  *     (at your option) any later version.
11  * b) the terms of the Apache License
12  *
13  * You should have received a copy of both licenses in LICENCE.LGPL and
14  * LICENCE.APACHE. Please refer to those files for details.
15  *
16  * JavaParser is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  */
21 package com.github.javaparser.ast.expr;
22 
23 import com.github.javaparser.ast.AllFieldsConstructor;
24 import com.github.javaparser.ast.nodeTypes.NodeWithExpression;
25 import com.github.javaparser.ast.observer.ObservableProperty;
26 import com.github.javaparser.ast.visitor.GenericVisitor;
27 import com.github.javaparser.ast.visitor.VoidVisitor;
28 import static com.github.javaparser.utils.Utils.assertNotNull;
29 import com.github.javaparser.ast.Node;
30 import com.github.javaparser.ast.visitor.CloneVisitor;
31 import com.github.javaparser.metamodel.DerivedProperty;
32 import com.github.javaparser.metamodel.UnaryExprMetaModel;
33 import com.github.javaparser.metamodel.JavaParserMetaModel;
34 import com.github.javaparser.printer.Printable;
35 import javax.annotation.Generated;
36 import com.github.javaparser.TokenRange;
37 import java.util.function.Consumer;
38 import java.util.Optional;
39 
40 /**
41  * An expression where an operator is applied to a single expression.
42  * It supports the operators that are found the the UnaryExpr.Operator enum.
43  * <br/><code>11++</code>
44  * <br/><code>++11</code>
45  * <br/><code>~1</code>
46  * <br/><code>-333</code>
47  *
48  * @author Julio Vilmar Gesser
49  */
50 public final class UnaryExpr extends Expression implements NodeWithExpression<UnaryExpr> {
51 
52     public enum Operator implements Printable {
53 
54         PLUS("+", false),
55         MINUS("-", false),
56         PREFIX_INCREMENT("++", false),
57         PREFIX_DECREMENT("--", false),
58         LOGICAL_COMPLEMENT("!", false),
59         BITWISE_COMPLEMENT("~", false),
60         POSTFIX_INCREMENT("++", true),
61         POSTFIX_DECREMENT("--", true);
62 
63         private final String codeRepresentation;
64 
65         private final boolean isPostfix;
66 
Operator(String codeRepresentation, boolean isPostfix)67         Operator(String codeRepresentation, boolean isPostfix) {
68             this.codeRepresentation = codeRepresentation;
69             this.isPostfix = isPostfix;
70         }
71 
asString()72         public String asString() {
73             return codeRepresentation;
74         }
75 
isPostfix()76         public boolean isPostfix() {
77             return isPostfix;
78         }
79 
isPrefix()80         public boolean isPrefix() {
81             return !isPostfix();
82         }
83     }
84 
85     private Expression expression;
86 
87     private Operator operator;
88 
UnaryExpr()89     public UnaryExpr() {
90         this(null, new IntegerLiteralExpr(), Operator.POSTFIX_INCREMENT);
91     }
92 
93     @AllFieldsConstructor
UnaryExpr(final Expression expression, final Operator operator)94     public UnaryExpr(final Expression expression, final Operator operator) {
95         this(null, expression, operator);
96     }
97 
98     /**
99      * This constructor is used by the parser and is considered private.
100      */
101     @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
UnaryExpr(TokenRange tokenRange, Expression expression, Operator operator)102     public UnaryExpr(TokenRange tokenRange, Expression expression, Operator operator) {
103         super(tokenRange);
104         setExpression(expression);
105         setOperator(operator);
106         customInitialization();
107     }
108 
109     @Override
110     @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
accept(final GenericVisitor<R, A> v, final A arg)111     public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) {
112         return v.visit(this, arg);
113     }
114 
115     @Override
116     @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
accept(final VoidVisitor<A> v, final A arg)117     public <A> void accept(final VoidVisitor<A> v, final A arg) {
118         v.visit(this, arg);
119     }
120 
121     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
getExpression()122     public Expression getExpression() {
123         return expression;
124     }
125 
126     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
getOperator()127     public Operator getOperator() {
128         return operator;
129     }
130 
131     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
setExpression(final Expression expression)132     public UnaryExpr setExpression(final Expression expression) {
133         assertNotNull(expression);
134         if (expression == this.expression) {
135             return (UnaryExpr) this;
136         }
137         notifyPropertyChange(ObservableProperty.EXPRESSION, this.expression, expression);
138         if (this.expression != null)
139             this.expression.setParentNode(null);
140         this.expression = expression;
141         setAsParentNodeOf(expression);
142         return this;
143     }
144 
145     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
setOperator(final Operator operator)146     public UnaryExpr setOperator(final Operator operator) {
147         assertNotNull(operator);
148         if (operator == this.operator) {
149             return (UnaryExpr) this;
150         }
151         notifyPropertyChange(ObservableProperty.OPERATOR, this.operator, operator);
152         this.operator = operator;
153         return this;
154     }
155 
156     @Override
157     @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
remove(Node node)158     public boolean remove(Node node) {
159         if (node == null)
160             return false;
161         return super.remove(node);
162     }
163 
164     @DerivedProperty
isPostfix()165     public boolean isPostfix() {
166         return operator.isPostfix();
167     }
168 
169     @DerivedProperty
isPrefix()170     public boolean isPrefix() {
171         return !isPostfix();
172     }
173 
174     @Override
175     @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
clone()176     public UnaryExpr clone() {
177         return (UnaryExpr) accept(new CloneVisitor(), null);
178     }
179 
180     @Override
181     @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
getMetaModel()182     public UnaryExprMetaModel getMetaModel() {
183         return JavaParserMetaModel.unaryExprMetaModel;
184     }
185 
186     @Override
187     @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
replace(Node node, Node replacementNode)188     public boolean replace(Node node, Node replacementNode) {
189         if (node == null)
190             return false;
191         if (node == expression) {
192             setExpression((Expression) replacementNode);
193             return true;
194         }
195         return super.replace(node, replacementNode);
196     }
197 
198     @Override
199     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
isUnaryExpr()200     public boolean isUnaryExpr() {
201         return true;
202     }
203 
204     @Override
205     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
asUnaryExpr()206     public UnaryExpr asUnaryExpr() {
207         return this;
208     }
209 
210     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
ifUnaryExpr(Consumer<UnaryExpr> action)211     public void ifUnaryExpr(Consumer<UnaryExpr> action) {
212         action.accept(this);
213     }
214 
215     @Override
216     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
toUnaryExpr()217     public Optional<UnaryExpr> toUnaryExpr() {
218         return Optional.of(this);
219     }
220 }
221