| 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.BinaryExprMetaModel; |
| 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 expression with an expression on the left, an expression on the right, and an operator in the middle. |
| 40 | * It supports the operators that are found the the BinaryExpr.Operator enum. |
| 41 | * <br>{@code a && b} |
| 42 | * <br>{@code 155 * 33} |
| 43 | * |
| 44 | * @author Julio Vilmar Gesser |
| 45 | */ |
| 46 | public class BinaryExpr extends Expression { |
| 47 | |
| 48 | public enum Operator implements Printable { |
| 49 | |
| 50 | OR("||"), |
| 51 | AND("&&"), |
| 52 | BINARY_OR("|"), |
| 53 | BINARY_AND("&"), |
| 54 | XOR("^"), |
| 55 | EQUALS("=="), |
| 56 | NOT_EQUALS("!="), |
| 57 | LESS("<"), |
| 58 | GREATER(">"), |
| 59 | LESS_EQUALS("<="), |
| 60 | GREATER_EQUALS(">="), |
| 61 | LEFT_SHIFT("<<"), |
| 62 | SIGNED_RIGHT_SHIFT(">>"), |
| 63 | UNSIGNED_RIGHT_SHIFT(">>>"), |
| 64 | PLUS("+"), |
| 65 | MINUS("-"), |
| 66 | MULTIPLY("*"), |
| 67 | DIVIDE("/"), |
| 68 | REMAINDER("%"); |
| 69 | |
| 70 | private final String codeRepresentation; |
| 71 | |
| 72 | Operator(String codeRepresentation) { |
| 73 | this.codeRepresentation = codeRepresentation; |
| 74 | } |
| 75 | |
| 76 | public String asString() { |
| 77 | return codeRepresentation; |
| 78 | } |
| 79 | |
| 80 | public Optional<AssignExpr.Operator> toAssignOperator() { |
| 81 | switch(this) { |
| 82 | case BINARY_OR: |
| 83 | return Optional.of(AssignExpr.Operator.BINARY_OR); |
| 84 | case BINARY_AND: |
| 85 | return Optional.of(AssignExpr.Operator.BINARY_AND); |
| 86 | case XOR: |
| 87 | return Optional.of(AssignExpr.Operator.XOR); |
| 88 | case LEFT_SHIFT: |
| 89 | return Optional.of(AssignExpr.Operator.LEFT_SHIFT); |
| 90 | case SIGNED_RIGHT_SHIFT: |
| 91 | return Optional.of(AssignExpr.Operator.SIGNED_RIGHT_SHIFT); |
| 92 | case UNSIGNED_RIGHT_SHIFT: |
| 93 | return Optional.of(AssignExpr.Operator.UNSIGNED_RIGHT_SHIFT); |
| 94 | case PLUS: |
| 95 | return Optional.of(AssignExpr.Operator.PLUS); |
| 96 | case MINUS: |
| 97 | return Optional.of(AssignExpr.Operator.MINUS); |
| 98 | case MULTIPLY: |
| 99 | return Optional.of(AssignExpr.Operator.MULTIPLY); |
| 100 | case DIVIDE: |
| 101 | return Optional.of(AssignExpr.Operator.DIVIDE); |
| 102 | case REMAINDER: |
| 103 | return Optional.of(AssignExpr.Operator.REMAINDER); |
| 104 | default: |
| 105 | return Optional.empty(); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private Expression left; |
| 111 | |
| 112 | private Expression right; |
| 113 | |
| 114 | private Operator operator; |
| 115 | |
| 116 | public BinaryExpr() { |
| 117 | this(null, new BooleanLiteralExpr(), new BooleanLiteralExpr(), Operator.EQUALS); |
| 118 | } |
| 119 | |
| 120 | @AllFieldsConstructor |
| 121 | public BinaryExpr(Expression left, Expression right, Operator operator) { |
| 122 | this(null, left, right, operator); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * This constructor is used by the parser and is considered private. |
| 127 | */ |
| 128 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 129 | public BinaryExpr(TokenRange tokenRange, Expression left, Expression right, Operator operator) { |
| 130 | super(tokenRange); |
| 131 | setLeft(left); |
| 132 | setRight(right); |
| 133 | setOperator(operator); |
| 134 | customInitialization(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 139 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 140 | return v.visit(this, arg); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 145 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 146 | v.visit(this, arg); |
| 147 | } |
| 148 | |
| 149 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 150 | public Expression getLeft() { |
| 151 | return left; |
| 152 | } |
| 153 | |
| 154 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 155 | public Operator getOperator() { |
| 156 | return operator; |
| 157 | } |
| 158 | |
| 159 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 160 | public Expression getRight() { |
| 161 | return right; |
| 162 | } |
| 163 | |
| 164 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 165 | public BinaryExpr setLeft(final Expression left) { |
| 166 | assertNotNull(left); |
| 167 | if (left == this.left) { |
| 168 | return (BinaryExpr) this; |
| 169 | } |
| 170 | notifyPropertyChange(ObservableProperty.LEFT, this.left, left); |
| 171 | if (this.left != null) |
| 172 | this.left.setParentNode(null); |
| 173 | this.left = left; |
| 174 | setAsParentNodeOf(left); |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 179 | public BinaryExpr setOperator(final Operator operator) { |
| 180 | assertNotNull(operator); |
| 181 | if (operator == this.operator) { |
| 182 | return (BinaryExpr) this; |
| 183 | } |
| 184 | notifyPropertyChange(ObservableProperty.OPERATOR, this.operator, operator); |
| 185 | this.operator = operator; |
| 186 | return this; |
| 187 | } |
| 188 | |
| 189 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 190 | public BinaryExpr setRight(final Expression right) { |
| 191 | assertNotNull(right); |
| 192 | if (right == this.right) { |
| 193 | return (BinaryExpr) this; |
| 194 | } |
| 195 | notifyPropertyChange(ObservableProperty.RIGHT, this.right, right); |
| 196 | if (this.right != null) |
| 197 | this.right.setParentNode(null); |
| 198 | this.right = right; |
| 199 | setAsParentNodeOf(right); |
| 200 | return this; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 205 | public boolean remove(Node node) { |
| 206 | if (node == null) |
| 207 | return false; |
| 208 | return super.remove(node); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 213 | public BinaryExpr clone() { |
| 214 | return (BinaryExpr) accept(new CloneVisitor(), null); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 219 | public BinaryExprMetaModel getMetaModel() { |
| 220 | return JavaParserMetaModel.binaryExprMetaModel; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 225 | public boolean replace(Node node, Node replacementNode) { |
| 226 | if (node == null) |
| 227 | return false; |
| 228 | if (node == left) { |
| 229 | setLeft((Expression) replacementNode); |
| 230 | return true; |
| 231 | } |
| 232 | if (node == right) { |
| 233 | setRight((Expression) replacementNode); |
| 234 | return true; |
| 235 | } |
| 236 | return super.replace(node, replacementNode); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 241 | public boolean isBinaryExpr() { |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 247 | public BinaryExpr asBinaryExpr() { |
| 248 | return this; |
| 249 | } |
| 250 | |
| 251 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 252 | public void ifBinaryExpr(Consumer<BinaryExpr> action) { |
| 253 | action.accept(this); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 258 | public Optional<BinaryExpr> toBinaryExpr() { |
| 259 | return Optional.of(this); |
| 260 | } |
| 261 | } |
| 262 |
Members