Skip to content

Commit

Permalink
Improved logging for @MessageMapping methods
Browse files Browse the repository at this point in the history
Closes gh-20564
  • Loading branch information
rstoyanchev committed Mar 12, 2019
1 parent b88aad6 commit 19c024f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -26,6 +27,8 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -309,12 +312,27 @@ protected final void detectHandlerMethods(final Object handler) {
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
(MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
if (logger.isDebugEnabled()) {
logger.debug(methods.size() + " message handler methods found on " + userType + ": " + methods);
logger.debug(formatMappings(userType, methods));
}
methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
}
}

private String formatMappings(Class<?> userType, Map<Method, T> methods) {
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
.map(p -> p.substring(0, 1))
.collect(Collectors.joining(".", "", "." + userType.getSimpleName()));
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName)
.collect(Collectors.joining(",", "(", ")"));
return methods.entrySet().stream()
.map(e -> {
Method method = e.getKey();
return e.getValue() + ": " + method.getName() + methodFormatter.apply(method);
})
.collect(Collectors.joining("\n\t", "\n\t" + formattedType + ":" + "\n\t", ""));
}

/**
* Provide the mapping for a handler method.
* @param method the method to provide a mapping for
Expand Down Expand Up @@ -344,9 +362,6 @@ protected void registerHandlerMethod(Object handler, Method method, T mapping) {
}

this.handlerMethods.put(mapping, newHandlerMethod);
if (logger.isTraceEnabled()) {
logger.trace("Mapped \"" + mapping + "\" onto " + newHandlerMethod);
}

for (String pattern : getDirectLookupDestinations(mapping)) {
this.destinationLookup.add(pattern, mapping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -284,12 +287,27 @@ protected final void detectHandlerMethods(Object handler) {
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
(MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
if (logger.isDebugEnabled()) {
logger.debug(methods.size() + " message handler methods found on " + userType + ": " + methods);
logger.debug(formatMappings(userType, methods));
}
methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
}
}

private String formatMappings(Class<?> userType, Map<Method, T> methods) {
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
.map(p -> p.substring(0, 1))
.collect(Collectors.joining(".", "", "." + userType.getSimpleName()));
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName)
.collect(Collectors.joining(",", "(", ")"));
return methods.entrySet().stream()
.map(e -> {
Method method = e.getKey();
return e.getValue() + ": " + method.getName() + methodFormatter.apply(method);
})
.collect(Collectors.joining("\n\t", "\n\t" + formattedType + ":" + "\n\t", ""));
}

/**
* Obtain the mapping for the given method, if any.
* @param method the method to check
Expand Down Expand Up @@ -322,9 +340,6 @@ protected final void registerHandlerMethod(Object handler, Method method, T mapp
}

this.handlerMethods.put(mapping, newHandlerMethod);
if (logger.isTraceEnabled()) {
logger.trace("Mapped \"" + mapping + "\" onto " + newHandlerMethod);
}

for (String pattern : getDirectLookupMappings(mapping)) {
this.destinationLookup.add(pattern, mapping);
Expand Down

0 comments on commit 19c024f

Please sign in to comment.