| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 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.observer.ObservableProperty; |
| 25 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 26 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 27 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 28 | import com.github.javaparser.ast.Node; |
| 29 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 30 | import com.github.javaparser.metamodel.AssignExprMetaModel; |
| 31 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 32 | import com.github.javaparser.printer.Printable; |
| 33 | import com.github.javaparser.TokenRange; |
| 34 | import java.util.function.Consumer; |
| 35 | import java.util.Optional; |
| 36 | import com.github.javaparser.ast.Generated; |
| 37 | |
| 38 | /** |
| 39 | * An assignment expression. It supports the operators that are found the the AssignExpr.Operator enum. |
| 40 | * <br>{@code a=5} |
| 41 | * <br>{@code time+=500} |
| 42 | * <br>{@code watch.time+=500} |
| 43 | * <br>{@code (((time)))=100*60} |
| 44 | * <br>{@code peanut[a]=true} |
| 45 | * |
| 46 | * @author Julio Vilmar Gesser |
| 47 | */ |
| 48 | public class AssignExpr extends Expression { |
| 49 | |
| 50 | public enum Operator implements Printable { |
| 51 | |
| 52 | ASSIGN("="), |
| 53 | PLUS("+="), |
| 54 | MINUS("-="), |
| 55 | MULTIPLY("*="), |
| 56 | DIVIDE("/="), |
| 57 | BINARY_AND("&="), |
| 58 | BINARY_OR("|="), |
| 59 | XOR("^="), |
| 60 | REMAINDER("%="), |
| 61 | LEFT_SHIFT("<<="), |
| 62 | SIGNED_RIGHT_SHIFT(">>="), |
| 63 | UNSIGNED_RIGHT_SHIFT(">>>="); |
| 64 | |
| 65 | private final String codeRepresentation; |
| 66 | |
| 67 | Operator(String codeRepresentation) { |
| 68 | this.codeRepresentation = codeRepresentation; |
| 69 | } |
| 70 | |
| 71 | public String asString() { |
| 72 | return codeRepresentation; |
| 73 | } |
| 74 | |
| 75 | public Optional<BinaryExpr.Operator> toBinaryOperator() { |
| 76 | switch(this) { |
| 77 | case PLUS: |
| 78 | return Optional.of(BinaryExpr.Operator.PLUS); |
| 79 | case MINUS: |
| 80 | return Optional.of(BinaryExpr.Operator.MINUS); |
| 81 | case MULTIPLY: |
| 82 | return Optional.of(BinaryExpr.Operator.MULTIPLY); |
| 83 | case DIVIDE: |
| 84 | return Optional.of(BinaryExpr.Operator.DIVIDE); |
| 85 | case BINARY_AND: |
| 86 | return Optional.of(BinaryExpr.Operator.BINARY_AND); |
| 87 | case BINARY_OR: |
| 88 | return Optional.of(BinaryExpr.Operator.BINARY_OR); |
| 89 | case XOR: |
| 90 | return Optional.of(BinaryExpr.Operator.XOR); |
| 91 | case REMAINDER: |
| 92 | return Optional.of(BinaryExpr.Operator.REMAINDER); |
| 93 | case LEFT_SHIFT: |
| 94 | return Optional.of(BinaryExpr.Operator.LEFT_SHIFT); |
| 95 | case SIGNED_RIGHT_SHIFT: |
| 96 | return Optional.of(BinaryExpr.Operator.SIGNED_RIGHT_SHIFT); |
| 97 | case UNSIGNED_RIGHT_SHIFT: |
| 98 | return Optional.of(BinaryExpr.Operator.UNSIGNED_RIGHT_SHIFT); |
| 99 | default: |
| 100 | return Optional.empty(); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private Expression target; |
| 106 | |
| 107 | private Expression value; |
| 108 | |
| 109 | private Operator operator; |
| 110 | |
| 111 | public AssignExpr() { |
| 112 | this(null, new NameExpr(), new StringLiteralExpr(), Operator.ASSIGN); |
| 113 | } |
| 114 | |
| 115 | @AllFieldsConstructor |
| 116 | public AssignExpr(Expression target, Expression value, Operator operator) { |
| 117 | this(null, target, value, operator); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * This constructor is used by the parser and is considered private. |
| 122 | */ |
| 123 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 124 | public AssignExpr(TokenRange tokenRange, Expression target, Expression value, Operator operator) { |
| 125 | super(tokenRange); |
| 126 | setTarget(target); |
| 127 | setValue(value); |
| 128 | setOperator(operator); |
| 129 | customInitialization(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 134 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 135 | return v.visit(this, arg); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 140 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 141 | v.visit(this, arg); |
| 142 | } |
| 143 | |
| 144 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 145 | public Operator getOperator() { |
| 146 | return operator; |
| 147 | } |
| 148 | |
| 149 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 150 | public Expression getTarget() { |
| 151 | return target; |
| 152 | } |
| 153 | |
| 154 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 155 | public Expression getValue() { |
| 156 | return value; |
| 157 | } |
| 158 | |
| 159 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 160 | public AssignExpr setOperator(final Operator operator) { |
| 161 | assertNotNull(operator); |
| 162 | if (operator == this.operator) { |
| 163 | return (AssignExpr) this; |
| 164 | } |
| 165 | notifyPropertyChange(ObservableProperty.OPERATOR, this.operator, operator); |
| 166 | this.operator = operator; |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 171 | public AssignExpr setTarget(final Expression target) { |
| 172 | assertNotNull(target); |
| 173 | if (target == this.target) { |
| 174 | return (AssignExpr) this; |
| 175 | } |
| 176 | notifyPropertyChange(ObservableProperty.TARGET, this.target, target); |
| 177 | if (this.target != null) |
| 178 | this.target.setParentNode(null); |
| 179 | this.target = target; |
| 180 | setAsParentNodeOf(target); |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 185 | public AssignExpr setValue(final Expression value) { |
| 186 | assertNotNull(value); |
| 187 | if (value == this.value) { |
| 188 | return (AssignExpr) this; |
| 189 | } |
| 190 | notifyPropertyChange(ObservableProperty.VALUE, this.value, value); |
| 191 | if (this.value != null) |
| 192 | this.value.setParentNode(null); |
| 193 | this.value = value; |
| 194 | setAsParentNodeOf(value); |
| 195 | return this; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 200 | public boolean remove(Node node) { |
| 201 | if (node == null) |
| 202 | return false; |
| 203 | return super.remove(node); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 208 | public AssignExpr clone() { |
| 209 | return (AssignExpr) accept(new CloneVisitor(), null); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 214 | public AssignExprMetaModel getMetaModel() { |
| 215 | return JavaParserMetaModel.assignExprMetaModel; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 220 | public boolean replace(Node node, Node replacementNode) { |
| 221 | if (node == null) |
| 222 | return false; |
| 223 | if (node == target) { |
| 224 | setTarget((Expression) replacementNode); |
| 225 | return true; |
| 226 | } |
| 227 | if (node == value) { |
| 228 | setValue((Expression) replacementNode); |
| 229 | return true; |
| 230 | } |
| 231 | return super.replace(node, replacementNode); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 236 | public boolean isAssignExpr() { |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 242 | public AssignExpr asAssignExpr() { |
| 243 | return this; |
| 244 | } |
| 245 | |
| 246 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 247 | public void ifAssignExpr(Consumer<AssignExpr> action) { |
| 248 | action.accept(this); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 253 | public Optional<AssignExpr> toAssignExpr() { |
| 254 | return Optional.of(this); |
| 255 | } |
| 256 | } |
| 257 |
Members