| 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.ArrayAccessExprMetaModel; |
| 31 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 32 | import com.github.javaparser.TokenRange; |
| 33 | import java.util.function.Consumer; |
| 34 | import java.util.Optional; |
| 35 | import com.github.javaparser.ast.Generated; |
| 36 | |
| 37 | /** |
| 38 | * Array brackets [] being used to get a value from an array. |
| 39 | * In <br>{@code getNames()[15*15]} the name expression is getNames() and the index expression is 15*15. |
| 40 | * |
| 41 | * @author Julio Vilmar Gesser |
| 42 | */ |
| 43 | public class ArrayAccessExpr extends Expression { |
| 44 | |
| 45 | private Expression name; |
| 46 | |
| 47 | private Expression index; |
| 48 | |
| 49 | public ArrayAccessExpr() { |
| 50 | this(null, new NameExpr(), new IntegerLiteralExpr()); |
| 51 | } |
| 52 | |
| 53 | @AllFieldsConstructor |
| 54 | public ArrayAccessExpr(Expression name, Expression index) { |
| 55 | this(null, name, index); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * This constructor is used by the parser and is considered private. |
| 60 | */ |
| 61 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 62 | public ArrayAccessExpr(TokenRange tokenRange, Expression name, Expression index) { |
| 63 | super(tokenRange); |
| 64 | setName(name); |
| 65 | setIndex(index); |
| 66 | customInitialization(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 71 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 72 | return v.visit(this, arg); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 77 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 78 | v.visit(this, arg); |
| 79 | } |
| 80 | |
| 81 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 82 | public Expression getIndex() { |
| 83 | return index; |
| 84 | } |
| 85 | |
| 86 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 87 | public Expression getName() { |
| 88 | return name; |
| 89 | } |
| 90 | |
| 91 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 92 | public ArrayAccessExpr setIndex(final Expression index) { |
| 93 | assertNotNull(index); |
| 94 | if (index == this.index) { |
| 95 | return (ArrayAccessExpr) this; |
| 96 | } |
| 97 | notifyPropertyChange(ObservableProperty.INDEX, this.index, index); |
| 98 | if (this.index != null) |
| 99 | this.index.setParentNode(null); |
| 100 | this.index = index; |
| 101 | setAsParentNodeOf(index); |
| 102 | return this; |
| 103 | } |
| 104 | |
| 105 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 106 | public ArrayAccessExpr setName(final Expression name) { |
| 107 | assertNotNull(name); |
| 108 | if (name == this.name) { |
| 109 | return (ArrayAccessExpr) this; |
| 110 | } |
| 111 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
| 112 | if (this.name != null) |
| 113 | this.name.setParentNode(null); |
| 114 | this.name = name; |
| 115 | setAsParentNodeOf(name); |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 121 | public boolean remove(Node node) { |
| 122 | if (node == null) |
| 123 | return false; |
| 124 | return super.remove(node); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 129 | public ArrayAccessExpr clone() { |
| 130 | return (ArrayAccessExpr) accept(new CloneVisitor(), null); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 135 | public ArrayAccessExprMetaModel getMetaModel() { |
| 136 | return JavaParserMetaModel.arrayAccessExprMetaModel; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 141 | public boolean replace(Node node, Node replacementNode) { |
| 142 | if (node == null) |
| 143 | return false; |
| 144 | if (node == index) { |
| 145 | setIndex((Expression) replacementNode); |
| 146 | return true; |
| 147 | } |
| 148 | if (node == name) { |
| 149 | setName((Expression) replacementNode); |
| 150 | return true; |
| 151 | } |
| 152 | return super.replace(node, replacementNode); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 157 | public boolean isArrayAccessExpr() { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 163 | public ArrayAccessExpr asArrayAccessExpr() { |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 168 | public void ifArrayAccessExpr(Consumer<ArrayAccessExpr> action) { |
| 169 | action.accept(this); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 174 | public Optional<ArrayAccessExpr> toArrayAccessExpr() { |
| 175 | return Optional.of(this); |
| 176 | } |
| 177 | } |
| 178 |
Members