Home | History | Annotate | Download | only in expr
      1 /*
      2  * Copyright (C) 2007-2010 Jlio 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.NodeList;
     25 import com.github.javaparser.ast.nodeTypes.NodeWithArguments;
     26 import com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope;
     27 import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
     28 import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
     29 import com.github.javaparser.ast.observer.ObservableProperty;
     30 import com.github.javaparser.ast.type.Type;
     31 import com.github.javaparser.ast.visitor.GenericVisitor;
     32 import com.github.javaparser.ast.visitor.VoidVisitor;
     33 import java.util.Optional;
     34 import static com.github.javaparser.utils.Utils.assertNotNull;
     35 import com.github.javaparser.ast.Node;
     36 import com.github.javaparser.ast.visitor.CloneVisitor;
     37 import com.github.javaparser.metamodel.MethodCallExprMetaModel;
     38 import com.github.javaparser.metamodel.JavaParserMetaModel;
     39 import javax.annotation.Generated;
     40 import com.github.javaparser.TokenRange;
     41 import com.github.javaparser.metamodel.OptionalProperty;
     42 import com.github.javaparser.resolution.SymbolResolver;
     43 import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
     44 import java.util.function.Consumer;
     45 
     46 /**
     47  * A method call on an object. <br/><code>circle.circumference()</code> <br/>In <code>a.&lt;String&gt;bb(15);</code> a
     48  * is the scope, String is a type argument, bb is the name and 15 is an argument.
     49  *
     50  * @author Julio Vilmar Gesser
     51  */
     52 public final class MethodCallExpr extends Expression implements NodeWithTypeArguments<MethodCallExpr>, NodeWithArguments<MethodCallExpr>, NodeWithSimpleName<MethodCallExpr>, NodeWithOptionalScope<MethodCallExpr> {
     53 
     54     @OptionalProperty
     55     private Expression scope;
     56 
     57     @OptionalProperty
     58     private NodeList<Type> typeArguments;
     59 
     60     private SimpleName name;
     61 
     62     private NodeList<Expression> arguments;
     63 
     64     public MethodCallExpr() {
     65         this(null, null, new NodeList<>(), new SimpleName(), new NodeList<>());
     66     }
     67 
     68     public MethodCallExpr(String name, Expression... arguments) {
     69         this(null, null, new NodeList<>(), new SimpleName(name), new NodeList<>(arguments));
     70     }
     71 
     72     public MethodCallExpr(final Expression scope, final String name) {
     73         this(null, scope, new NodeList<>(), new SimpleName(name), new NodeList<>());
     74     }
     75 
     76     public MethodCallExpr(final Expression scope, final SimpleName name) {
     77         this(null, scope, new NodeList<>(), name, new NodeList<>());
     78     }
     79 
     80     public MethodCallExpr(final Expression scope, final String name, final NodeList<Expression> arguments) {
     81         this(null, scope, new NodeList<>(), new SimpleName(name), arguments);
     82     }
     83 
     84     public MethodCallExpr(final Expression scope, final SimpleName name, final NodeList<Expression> arguments) {
     85         this(null, scope, new NodeList<>(), name, arguments);
     86     }
     87 
     88     @AllFieldsConstructor
     89     public MethodCallExpr(final Expression scope, final NodeList<Type> typeArguments, final SimpleName name, final NodeList<Expression> arguments) {
     90         this(null, scope, typeArguments, name, arguments);
     91     }
     92 
     93     /**
     94      * This constructor is used by the parser and is considered private.
     95      */
     96     @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
     97     public MethodCallExpr(TokenRange tokenRange, Expression scope, NodeList<Type> typeArguments, SimpleName name, NodeList<Expression> arguments) {
     98         super(tokenRange);
     99         setScope(scope);
    100         setTypeArguments(typeArguments);
    101         setName(name);
    102         setArguments(arguments);
    103         customInitialization();
    104     }
    105 
    106     @Override
    107     @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
    108     public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) {
    109         return v.visit(this, arg);
    110     }
    111 
    112     @Override
    113     @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
    114     public <A> void accept(final VoidVisitor<A> v, final A arg) {
    115         v.visit(this, arg);
    116     }
    117 
    118     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
    119     public NodeList<Expression> getArguments() {
    120         return arguments;
    121     }
    122 
    123     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
    124     public SimpleName getName() {
    125         return name;
    126     }
    127 
    128     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
    129     public Optional<Expression> getScope() {
    130         return Optional.ofNullable(scope);
    131     }
    132 
    133     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
    134     public MethodCallExpr setArguments(final NodeList<Expression> arguments) {
    135         assertNotNull(arguments);
    136         if (arguments == this.arguments) {
    137             return (MethodCallExpr) this;
    138         }
    139         notifyPropertyChange(ObservableProperty.ARGUMENTS, this.arguments, arguments);
    140         if (this.arguments != null)
    141             this.arguments.setParentNode(null);
    142         this.arguments = arguments;
    143         setAsParentNodeOf(arguments);
    144         return this;
    145     }
    146 
    147     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
    148     public MethodCallExpr setName(final SimpleName name) {
    149         assertNotNull(name);
    150         if (name == this.name) {
    151             return (MethodCallExpr) this;
    152         }
    153         notifyPropertyChange(ObservableProperty.NAME, this.name, name);
    154         if (this.name != null)
    155             this.name.setParentNode(null);
    156         this.name = name;
    157         setAsParentNodeOf(name);
    158         return this;
    159     }
    160 
    161     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
    162     public MethodCallExpr setScope(final Expression scope) {
    163         if (scope == this.scope) {
    164             return (MethodCallExpr) this;
    165         }
    166         notifyPropertyChange(ObservableProperty.SCOPE, this.scope, scope);
    167         if (this.scope != null)
    168             this.scope.setParentNode(null);
    169         this.scope = scope;
    170         setAsParentNodeOf(scope);
    171         return this;
    172     }
    173 
    174     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
    175     public Optional<NodeList<Type>> getTypeArguments() {
    176         return Optional.ofNullable(typeArguments);
    177     }
    178 
    179     /**
    180      * Sets the typeArguments
    181      *
    182      * @param typeArguments the typeArguments, can be null
    183      * @return this, the MethodCallExpr
    184      */
    185     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
    186     public MethodCallExpr setTypeArguments(final NodeList<Type> typeArguments) {
    187         if (typeArguments == this.typeArguments) {
    188             return (MethodCallExpr) this;
    189         }
    190         notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments);
    191         if (this.typeArguments != null)
    192             this.typeArguments.setParentNode(null);
    193         this.typeArguments = typeArguments;
    194         setAsParentNodeOf(typeArguments);
    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         for (int i = 0; i < arguments.size(); i++) {
    204             if (arguments.get(i) == node) {
    205                 arguments.remove(i);
    206                 return true;
    207             }
    208         }
    209         if (scope != null) {
    210             if (node == scope) {
    211                 removeScope();
    212                 return true;
    213             }
    214         }
    215         if (typeArguments != null) {
    216             for (int i = 0; i < typeArguments.size(); i++) {
    217                 if (typeArguments.get(i) == node) {
    218                     typeArguments.remove(i);
    219                     return true;
    220                 }
    221             }
    222         }
    223         return super.remove(node);
    224     }
    225 
    226     @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
    227     public MethodCallExpr removeScope() {
    228         return setScope((Expression) null);
    229     }
    230 
    231     @Override
    232     @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
    233     public MethodCallExpr clone() {
    234         return (MethodCallExpr) accept(new CloneVisitor(), null);
    235     }
    236 
    237     @Override
    238     @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
    239     public MethodCallExprMetaModel getMetaModel() {
    240         return JavaParserMetaModel.methodCallExprMetaModel;
    241     }
    242 
    243     @Override
    244     @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
    245     public boolean replace(Node node, Node replacementNode) {
    246         if (node == null)
    247             return false;
    248         for (int i = 0; i < arguments.size(); i++) {
    249             if (arguments.get(i) == node) {
    250                 arguments.set(i, (Expression) replacementNode);
    251                 return true;
    252             }
    253         }
    254         if (node == name) {
    255             setName((SimpleName) replacementNode);
    256             return true;
    257         }
    258         if (scope != null) {
    259             if (node == scope) {
    260                 setScope((Expression) replacementNode);
    261                 return true;
    262             }
    263         }
    264         if (typeArguments != null) {
    265             for (int i = 0; i < typeArguments.size(); i++) {
    266                 if (typeArguments.get(i) == node) {
    267                     typeArguments.set(i, (Type) replacementNode);
    268                     return true;
    269                 }
    270             }
    271         }
    272         return super.replace(node, replacementNode);
    273     }
    274 
    275     @Override
    276     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
    277     public boolean isMethodCallExpr() {
    278         return true;
    279     }
    280 
    281     @Override
    282     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
    283     public MethodCallExpr asMethodCallExpr() {
    284         return this;
    285     }
    286 
    287     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
    288     public void ifMethodCallExpr(Consumer<MethodCallExpr> action) {
    289         action.accept(this);
    290     }
    291 
    292     public ResolvedMethodDeclaration resolveInvokedMethod() {
    293         return getSymbolResolver().resolveDeclaration(this, ResolvedMethodDeclaration.class);
    294     }
    295 
    296     @Override
    297     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
    298     public Optional<MethodCallExpr> toMethodCallExpr() {
    299         return Optional.of(this);
    300     }
    301 }
    302