Reputation:
is it possible to convert below given for loop into java 8 code?
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]) + "|");
}
}
}
i tried with below given java 8 code. function name is randomly taken
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;
}
Upvotes: 1
Views: 52
Reputation: 16043
You'll have to open an InsStream
to iterate over the args
index, then create a SimpleEntry
of each arg
with it's corresponding annotaton
(acc. to your code), then you can apply your business logic.
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 + "|");
}
});
Upvotes: 1