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  * Method reference expressions introduced in Java 8 specifically designed to simplify lambda Expressions.
     36  * These are some examples:
     37  *
     38  * System.out::println;
     39  *
     40  * (test ? stream.map(String::trim) : stream)::toArray;
     41  * @author Raquel Pau
     42  *
     43  */
     44 public class MethodReferenceExpr extends Expression implements NodeWithTypeArguments<MethodReferenceExpr> {
     45 
     46     private Expression scope;
     47 
     48     private List<Type<?>> typeArguments;
     49 
     50     private String identifier;
     51 
     52     public MethodReferenceExpr() {
     53     }
     54 
     55     public MethodReferenceExpr(Range range, Expression scope,
     56                                List<Type<?>> typeArguments, String identifier) {
     57         super(range);
     58         setIdentifier(identifier);
     59         setScope(scope);
     60         setTypeArguments(typeArguments);
     61     }
     62 
     63     @Override
     64     public <R, A> R accept(GenericVisitor<R, A> v, A arg) {
     65 
     66         return v.visit(this, arg);
     67     }
     68 
     69     @Override
     70     public <A> void accept(VoidVisitor<A> v, A arg) {
     71         v.visit(this, arg);
     72     }
     73 
     74     public Expression getScope() {
     75         return scope;
     76     }
     77 
     78     public MethodReferenceExpr setScope(Expression scope) {
     79         this.scope = scope;
     80         setAsParentNodeOf(this.scope);
     81         return this;
     82     }
     83 
     84     @Override
     85     public List<Type<?>> getTypeArguments() {
     86         return typeArguments;
     87     }
     88 
     89     @Override
     90     public MethodReferenceExpr setTypeArguments(final List<Type<?>> types) {
     91         this.typeArguments = types;
     92         setAsParentNodeOf(this.typeArguments);
     93         return this;
     94     }
     95 
     96     public String getIdentifier() {
     97         return identifier;
     98     }
     99 
    100     public MethodReferenceExpr setIdentifier(String identifier) {
    101         this.identifier = identifier;
    102         return this;
    103     }
    104 
    105 }
    106