Home | History | Annotate | Download | only in nodeTypes
      1 package com.github.javaparser.ast.nodeTypes;
      2 
      3 import java.util.List;
      4 
      5 import com.github.javaparser.ast.Node;
      6 import com.github.javaparser.ast.body.Parameter;
      7 import com.github.javaparser.ast.body.VariableDeclaratorId;
      8 import com.github.javaparser.ast.type.ClassOrInterfaceType;
      9 import com.github.javaparser.ast.type.Type;
     10 
     11 public interface NodeWithParameters<T> {
     12     List<Parameter> getParameters();
     13 
     14     T setParameters(List<Parameter> parameters);
     15 
     16     default T addParameter(Type type, String name) {
     17         return addParameter(new Parameter(type, new VariableDeclaratorId(name)));
     18     }
     19 
     20     default T addParameter(Class<?> paramClass, String name) {
     21         ((Node) this).tryAddImportToParentCompilationUnit(paramClass);
     22         return addParameter(new ClassOrInterfaceType(paramClass.getSimpleName()), name);
     23     }
     24 
     25     /**
     26      * Remember to import the class in the compilation unit yourself
     27      *
     28      * @param className the name of the class, ex : org.test.Foo or Foo if you added manually the import
     29      * @param name the name of the parameter
     30      */
     31     default T addParameter(String className, String name) {
     32         return addParameter(new ClassOrInterfaceType(className), name);
     33     }
     34 
     35     @SuppressWarnings("unchecked")
     36     default T addParameter(Parameter parameter) {
     37         getParameters().add(parameter);
     38         parameter.setParentNode((Node) this);
     39         return (T) this;
     40     }
     41 
     42     default Parameter addAndGetParameter(Type type, String name) {
     43         return addAndGetParameter(new Parameter(type, new VariableDeclaratorId(name)));
     44     }
     45 
     46     default Parameter addAndGetParameter(Class<?> paramClass, String name) {
     47         ((Node) this).tryAddImportToParentCompilationUnit(paramClass);
     48         return addAndGetParameter(new ClassOrInterfaceType(paramClass.getSimpleName()), name);
     49     }
     50 
     51     /**
     52      * Remember to import the class in the compilation unit yourself
     53      *
     54      * @param className the name of the class, ex : org.test.Foo or Foo if you added manually the import
     55      * @param name the name of the parameter
     56      * @return the {@link Parameter} created
     57      */
     58     default Parameter addAndGetParameter(String className, String name) {
     59         return addAndGetParameter(new ClassOrInterfaceType(className), name);
     60     }
     61 
     62     default Parameter addAndGetParameter(Parameter parameter) {
     63         getParameters().add(parameter);
     64         parameter.setParentNode((Node) this);
     65         return parameter;
     66     }
     67 
     68     /**
     69      * Try to find a {@link Parameter} by its name
     70      *
     71      * @param name the name of the param
     72      * @return null if not found, the param found otherwise
     73      */
     74     default Parameter getParamByName(String name) {
     75         return getParameters().stream()
     76                 .filter(p -> p.getName().equals(name)).findFirst().orElse(null);
     77     }
     78 
     79     /**
     80      * Try to find a {@link Parameter} by its type
     81      *
     82      * @param type the type of the param
     83      * @return null if not found, the param found otherwise
     84      */
     85     default Parameter getParamByType(String type) {
     86         return getParameters().stream()
     87                 .filter(p -> p.getType().toString().equals(type)).findFirst().orElse(null);
     88     }
     89 
     90     /**
     91      * Try to find a {@link Parameter} by its type
     92      *
     93      * @param type the type of the param <b>take care about generics, it wont work</b>
     94      * @return null if not found, the param found otherwise
     95      */
     96     default Parameter getParamByType(Class<?> type) {
     97         return getParameters().stream()
     98                 .filter(p -> p.getType().toString().equals(type.getSimpleName())).findFirst().orElse(null);
     99     }
    100 }
    101