Esempio n. 1
0
  @Override
  public Function<Object[], ?> visit(LambdaExpression<?> e) {

    final Function<Object[], ?> f = e.getBody().accept(this);

    int size = e.getParameters().size();
    List<Function<Object[], ?>> ppe = new ArrayList<>(size);
    for (ParameterExpression p : e.getParameters()) ppe.add(p.accept(this));

    Function<Object[], Object[]> params =
        pp -> {
          Object[] r = new Object[ppe.size()];
          int index = 0;
          for (Function<Object[], ?> pe : ppe) {
            r[index++] = pe.apply(pp);
          }
          return r;
        };

    return f.compose(params);
  }
Esempio n. 2
0
  @Override
  public Function<Object[], ?> visit(MemberExpression e) {
    final Member m = e.getMember();
    Expression ei = e.getInstance();
    final Function<Object[], ?> instance = ei != null ? ei.accept(this) : null;

    int size = e.getParameters().size();
    List<Function<Object[], ?>> ppe = new ArrayList<>(size);
    for (ParameterExpression p : e.getParameters()) ppe.add(p.accept(this));

    Function<Object[], Object[]> params =
        pp -> {
          Object[] r = new Object[ppe.size()];
          int index = 0;
          for (Function<Object[], ?> pe : ppe) {
            r[index++] = pe.apply(pp);
          }

          return r;
        };

    Function<Object[], ?> field =
        t -> {
          try {
            return ((Field) m).get(instance == null ? null : instance.apply(t));
          } catch (IllegalArgumentException | IllegalAccessException ex) {
            throw new RuntimeException(ex);
          }
        };

    Function<Object[], ?> method =
        t -> {
          Object inst;
          if (instance != null) {
            inst = instance.apply(t);
          } else inst = null;
          try {
            Object[] pp = params.apply(t);
            return ((Method) m).invoke(inst, pp);
          } catch (IllegalAccessException
              | IllegalArgumentException
              | InvocationTargetException ex) {
            throw new RuntimeException(ex);
          }
        };

    Function<Object[], ?> ctor =
        t -> {
          try {
            return ((Constructor<?>) m).newInstance(params.apply(t));
          } catch (InstantiationException
              | IllegalAccessException
              | IllegalArgumentException
              | InvocationTargetException ex) {
            throw new RuntimeException(ex);
          }
        };

    Function<Object[], ?> member;

    if (m instanceof Field) member = field;
    else if (m instanceof Method) member = method;
    else member = ctor;

    return member; // .compose(params);
  }