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 
     22 package com.github.javaparser.ast.expr;
     23 
     24 import com.github.javaparser.Range;
     25 import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
     26 import com.github.javaparser.ast.type.Type;
     27 import com.github.javaparser.ast.visitor.GenericVisitor;
     28 import com.github.javaparser.ast.visitor.VoidVisitor;
     29 
     30 import java.util.List;
     31 
     32 import static com.github.javaparser.utils.Utils.ensureNotNull;
     33 
     34 /**
     35  * @author Julio Vilmar Gesser
     36  */
     37 public final class MethodCallExpr extends Expression implements NodeWithTypeArguments<MethodCallExpr> {
     38 
     39     private Expression scope;
     40 
     41     private List<Type<?>> typeArguments;
     42 
     43     private NameExpr name;
     44 
     45     private List<Expression> args;
     46 
     47     public MethodCallExpr() {
     48     }
     49 
     50     public MethodCallExpr(final Expression scope, final String name) {
     51         setScope(scope);
     52         setName(name);
     53     }
     54 
     55     public MethodCallExpr(final Expression scope, final String name, final List<Expression> args) {
     56         setScope(scope);
     57         setName(name);
     58         setArgs(args);
     59     }
     60 
     61 	public MethodCallExpr(final Range range, final Expression scope, final List<Type<?>> typeArguments, final String name, final List<Expression> args) {
     62 		super(range);
     63 		setScope(scope);
     64 		setTypeArguments(typeArguments);
     65 		setName(name);
     66 		setArgs(args);
     67 	}
     68 
     69     /**
     70      * Adds the given argument to the method call.
     71      *
     72      * @param arg
     73      *            argument value
     74      */
     75     public MethodCallExpr addArgument(Expression arg) {
     76         getArgs().add(arg);
     77         arg.setParentNode(this);
     78         return this;
     79     }
     80 
     81     public void addArgument(String arg) {
     82         addArgument(new NameExpr(arg));
     83     }
     84 
     85     @Override
     86     public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) {
     87         return v.visit(this, arg);
     88     }
     89 
     90     @Override
     91     public <A> void accept(final VoidVisitor<A> v, final A arg) {
     92         v.visit(this, arg);
     93     }
     94 
     95     public List<Expression> getArgs() {
     96         args = ensureNotNull(args);
     97         return args;
     98     }
     99 
    100     public String getName() {
    101         return name.getName();
    102     }
    103 
    104     public NameExpr getNameExpr() {
    105         return name;
    106     }
    107 
    108     public Expression getScope() {
    109         return scope;
    110     }
    111 
    112 	public void setArgs(final List<Expression> args) {
    113 		this.args = args;
    114 		setAsParentNodeOf(this.args);
    115 	}
    116 
    117     public MethodCallExpr setName(final String name) {
    118         setNameExpr(new NameExpr(name));
    119         return this;
    120     }
    121 
    122     public MethodCallExpr setNameExpr(NameExpr name) {
    123         this.name = name;
    124         setAsParentNodeOf(this.name);
    125         return this;
    126     }
    127 
    128     public MethodCallExpr setScope(final Expression scope) {
    129         this.scope = scope;
    130         setAsParentNodeOf(this.scope);
    131         return this;
    132     }
    133 
    134     @Override
    135     public List<Type<?>> getTypeArguments() {
    136         return typeArguments;
    137     }
    138 
    139     @Override
    140     public MethodCallExpr setTypeArguments(final List<Type<?>> types) {
    141         this.typeArguments = types;
    142         setAsParentNodeOf(this.typeArguments);
    143         return this;
    144     }
    145 }
    146