Home | History | Annotate | Download | only in nodeTypes
      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.nodeTypes;
     23 
     24 import com.github.javaparser.ast.Node;
     25 import com.github.javaparser.ast.NodeList;
     26 import com.github.javaparser.ast.body.Parameter;
     27 import com.github.javaparser.ast.type.Type;
     28 
     29 import java.util.Optional;
     30 import java.util.stream.Stream;
     31 
     32 import static com.github.javaparser.JavaParser.parseType;
     33 import static java.util.stream.Collectors.toSet;
     34 
     35 public interface NodeWithParameters<N extends Node> {
     36     NodeList<Parameter> getParameters();
     37 
     38     default Parameter getParameter(int i) {
     39         return getParameters().get(i);
     40     }
     41 
     42     void tryAddImportToParentCompilationUnit(Class<?> clazz);
     43 
     44     @SuppressWarnings("unchecked")
     45     default N setParameter(int i, Parameter parameter) {
     46         getParameters().set(i, parameter);
     47         return (N) this;
     48     }
     49 
     50     N setParameters(NodeList<Parameter> parameters);
     51 
     52     default N addParameter(Type type, String name) {
     53         return addParameter(new Parameter(type, name));
     54     }
     55 
     56     default N addParameter(Class<?> paramClass, String name) {
     57         tryAddImportToParentCompilationUnit(paramClass);
     58         return addParameter(parseType(paramClass.getSimpleName()), name);
     59     }
     60 
     61     /**
     62      * Remember to import the class in the compilation unit yourself
     63      *
     64      * @param className the name of the class, ex : org.test.Foo or Foo if you added manually the import
     65      * @param name the name of the parameter
     66      */
     67     default N addParameter(String className, String name) {
     68         return addParameter(parseType(className), name);
     69     }
     70 
     71     @SuppressWarnings("unchecked")
     72     default N addParameter(Parameter parameter) {
     73         getParameters().add(parameter);
     74         return (N) this;
     75     }
     76 
     77     default Parameter addAndGetParameter(Type type, String name) {
     78         return addAndGetParameter(new Parameter(type, name));
     79     }
     80 
     81     default Parameter addAndGetParameter(Class<?> paramClass, String name) {
     82         tryAddImportToParentCompilationUnit(paramClass);
     83         return addAndGetParameter(parseType(paramClass.getSimpleName()), name);
     84     }
     85 
     86     /**
     87      * Remember to import the class in the compilation unit yourself
     88      *
     89      * @param className the name of the class, ex : org.test.Foo or Foo if you added manually the import
     90      * @param name the name of the parameter
     91      * @return the {@link Parameter} created
     92      */
     93     default Parameter addAndGetParameter(String className, String name) {
     94         return addAndGetParameter(parseType(className), name);
     95     }
     96 
     97     default Parameter addAndGetParameter(Parameter parameter) {
     98         getParameters().add(parameter);
     99         return parameter;
    100     }
    101 
    102     /**
    103      * Try to find a {@link Parameter} by its name
    104      *
    105      * @param name the name of the param
    106      * @return null if not found, the param found otherwise
    107      */
    108     default Optional<Parameter> getParameterByName(String name) {
    109         return getParameters().stream()
    110                 .filter(p -> p.getNameAsString().equals(name)).findFirst();
    111     }
    112 
    113     /**
    114      * Try to find a {@link Parameter} by its type
    115      *
    116      * @param type the type of the param
    117      * @return null if not found, the param found otherwise
    118      */
    119     default Optional<Parameter> getParameterByType(String type) {
    120         return getParameters().stream()
    121                 .filter(p -> p.getType().toString().equals(type)).findFirst();
    122     }
    123 
    124     /**
    125      * Try to find a {@link Parameter} by its type
    126      *
    127      * @param type the type of the param <b>take care about generics, it wont work</b>
    128      * @return null if not found, the param found otherwise
    129      */
    130     default Optional<Parameter> getParameterByType(Class<?> type) {
    131         return getParameters().stream()
    132                 .filter(p -> p.getType().toString().equals(type.getSimpleName())).findFirst();
    133     }
    134 
    135     /**
    136      * Check if the parameters have certain types.
    137      *
    138      * @param paramTypes the types of parameters like "Map&lt;Integer,String&gt;","int" to match<br> void
    139      * foo(Map&lt;Integer,String&gt; myMap,int number)
    140      * @return true if all parameters match
    141      */
    142     default boolean hasParametersOfType(String... paramTypes) {
    143         return getParameters().stream()
    144                 .map(p -> p.getType().toString())
    145                 .collect(toSet())
    146                 .equals(Stream.of(paramTypes).collect(toSet()));
    147     }
    148 
    149     /**
    150      * Check if the parameters have certain types. Note that this is a match in SimpleName, so "java.awt.List" and
    151      * "java.util.List" are identical to this algorithm.
    152      *
    153      * @param paramTypes the types of parameters like "Map&lt;Integer,String&gt;","int" to match<br> void
    154      * foo(Map&lt;Integer,String&gt; myMap,int number)
    155      * @return true if all parameters match
    156      */
    157     default boolean hasParametersOfType(Class<?>... paramTypes) {
    158         return getParameters().stream().map(p -> p.getType().toString())
    159                 .collect(toSet())
    160                 .equals(Stream.of(paramTypes).map(Class::getSimpleName).collect(toSet()));
    161     }
    162 }
    163