конвертировать индекс на основе цикла в Java 8 - PullRequest
0 голосов
/ 21 февраля 2019

возможно ли преобразовать приведенный ниже цикл for в код Java 8?

 Object[] args = pjp.getArgs();
    MethodSignature methodSignature = (MethodSignature) pjp.getStaticPart()
            .getSignature();
    Method method = methodSignature.getMethod();
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    StringBuilder methodArgs = new StringBuilder();
    for (int argIndex = 0; argIndex < args.length; argIndex++) {
        for (Annotation annotation : parameterAnnotations[argIndex]) {
            if ((annotation instanceof RequestParam) || (annotation instanceof PathVariable) || (annotation instanceof RequestHeader)) {
                methodArgs.append(args[argIndex] + "|");
            } else if ((annotation instanceof RequestBody)) {
                methodArgs.append(mapper.writeValueAsString(args[argIndex]) + "|");
            }
        }
    }

Я пытался использовать приведенный ниже код Java 8.Имя функции берется случайным образом

public void some() {
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    Arrays.stream(parameterAnnotations)
            .map(f -> asd(f));
}

private Object asd(Annotation[] annotations) {
    Arrays.stream(annotations)
            .map(a -> change(a)); //here is problem...how i can access args[argIndex]
    return null;
}

1 Ответ

0 голосов
/ 21 февраля 2019

Вам нужно будет открыть InsStream для перебора индекса args, затем создать SimpleEntry каждого arg с соответствующим annotaton (согласно вашему коду), затем вы можетеприменить свою бизнес-логику.

IntStream.range(0, args.length)
        .mapToObj(argIndex -> new AbstractMap.SimpleEntry<>(args[argIndex], parameterAnnotations[argIndex]))
        .flatMap(objectSimpleEntry -> Arrays.stream(objectSimpleEntry.getValue()).map(annotation -> new AbstractMap.SimpleEntry<>(objectSimpleEntry.getKey(), annotation)))
        .forEach(objectAnnotationSimpleEntry -> {
          Annotation annotation = objectAnnotationSimpleEntry.getValue();
          Object arg = objectAnnotationSimpleEntry.getKey();
          if ((annotation instanceof RequestParam) || (annotation instanceof PathVariable) || (annotation instanceof RequestHeader)) {
            methodArgs.append(arg + "|");
          } else if ((annotation instanceof RequestBody)) {
            methodArgs.append(arg + "|");
          }
        });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...