Skip to content

Commit 4dde7c4

Browse files
jhoellerunknown
authored andcommitted
MBeanInfoAssembler impls expose actual method parameter names if possible
Issue: SPR-9985
1 parent 2ee70d6 commit 4dde7c4

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractReflectiveMBeanInfoAssembler.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,8 @@
3030
import org.springframework.aop.framework.AopProxyUtils;
3131
import org.springframework.aop.support.AopUtils;
3232
import org.springframework.beans.BeanUtils;
33+
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
34+
import org.springframework.core.ParameterNameDiscoverer;
3335
import org.springframework.jmx.support.JmxUtils;
3436

3537
/**
@@ -49,6 +51,7 @@
4951
*
5052
* @author Rob Harrop
5153
* @author Juergen Hoeller
54+
* @author David Boden
5255
* @since 1.2
5356
* @see #includeOperation
5457
* @see #includeReadAttribute
@@ -177,6 +180,8 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
177180

178181
private boolean exposeClassDescriptor = false;
179182

183+
private ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
184+
180185

181186
/**
182187
* Set the default for the JMX field "currencyTimeLimit".
@@ -254,6 +259,23 @@ protected boolean isExposeClassDescriptor() {
254259
return this.exposeClassDescriptor;
255260
}
256261

262+
/**
263+
* Set the ParameterNameDiscoverer to use for resolving method parameter
264+
* names if needed (e.g. for parameter names of MBean operation methods).
265+
* <p>The default is {@link LocalVariableTableParameterNameDiscoverer}.
266+
*/
267+
public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) {
268+
this.parameterNameDiscoverer = parameterNameDiscoverer;
269+
}
270+
271+
/**
272+
* Return the ParameterNameDiscoverer to use for resolving method parameter
273+
* names if needed (may be {@code null} in order to skip parameter detection).
274+
*/
275+
protected ParameterNameDiscoverer getParameterNameDiscoverer() {
276+
return this.parameterNameDiscoverer;
277+
}
278+
257279

258280
/**
259281
* Iterate through all properties on the MBean class and gives subclasses
@@ -381,7 +403,8 @@ protected ModelMBeanOperationInfo[] getOperationInfo(Object managedBean, String
381403
* Creates an instance of {@code ModelMBeanOperationInfo} for the
382404
* given method. Populates the parameter info for the operation.
383405
* @param method the {@code Method} to create a {@code ModelMBeanOperationInfo} for
384-
* @param name the name for the operation info
406+
* @param name the logical name for the operation (method name or property name);
407+
* not used by the default implementation but possibly by subclasses
385408
* @param beanKey the key associated with the MBean in the beans map
386409
* of the {@code MBeanExporter}
387410
* @return the {@code ModelMBeanOperationInfo}
@@ -392,7 +415,7 @@ protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, S
392415
return new ModelMBeanOperationInfo(getOperationDescription(method, beanKey), method);
393416
}
394417
else {
395-
return new ModelMBeanOperationInfo(name,
418+
return new ModelMBeanOperationInfo(method.getName(),
396419
getOperationDescription(method, beanKey),
397420
getOperationParameters(method, beanKey),
398421
method.getReturnType().getName(),
@@ -476,16 +499,27 @@ protected String getOperationDescription(Method method, String beanKey) {
476499

477500
/**
478501
* Create parameter info for the given method.
479-
* <p>The default implementation returns an empty arry of {@code MBeanParameterInfo}.
502+
* <p>The default implementation returns an empty array of {@code MBeanParameterInfo}.
480503
* @param method the {@code Method} to get the parameter information for
481504
* @param beanKey the key associated with the MBean in the beans map
482505
* of the {@code MBeanExporter}
483506
* @return the {@code MBeanParameterInfo} array
484507
*/
485508
protected MBeanParameterInfo[] getOperationParameters(Method method, String beanKey) {
486-
return new MBeanParameterInfo[0];
487-
}
509+
ParameterNameDiscoverer paramNameDiscoverer = getParameterNameDiscoverer();
510+
String[] paramNames = (paramNameDiscoverer != null ? paramNameDiscoverer.getParameterNames(method) : null);
511+
if (paramNames == null) {
512+
return new MBeanParameterInfo[0];
513+
}
488514

515+
MBeanParameterInfo[] info = new MBeanParameterInfo[paramNames.length];
516+
Class<?>[] typeParameters = method.getParameterTypes();
517+
for(int i = 0; i < info.length; i++) {
518+
info[i] = new MBeanParameterInfo(paramNames[i], typeParameters[i].getName(), paramNames[i]);
519+
}
520+
521+
return info;
522+
}
489523

490524
/**
491525
* Allows subclasses to add extra fields to the {@code Descriptor} for an MBean.

spring-context/src/main/java/org/springframework/jmx/export/assembler/MetadataMBeanInfoAssembler.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
import org.springframework.jmx.export.metadata.ManagedResource;
3737
import org.springframework.jmx.support.MetricType;
3838
import org.springframework.util.Assert;
39+
import org.springframework.util.ObjectUtils;
3940
import org.springframework.util.StringUtils;
4041

4142
/**
@@ -255,19 +256,17 @@ protected String getOperationDescription(Method method, String beanKey) {
255256
@Override
256257
protected MBeanParameterInfo[] getOperationParameters(Method method, String beanKey) {
257258
ManagedOperationParameter[] params = this.attributeSource.getManagedOperationParameters(method);
258-
if (params == null || params.length == 0) {
259-
return new MBeanParameterInfo[0];
259+
if (ObjectUtils.isEmpty(params)) {
260+
return super.getOperationParameters(method, beanKey);
260261
}
261262

262263
MBeanParameterInfo[] parameterInfo = new MBeanParameterInfo[params.length];
263264
Class[] methodParameters = method.getParameterTypes();
264-
265265
for (int i = 0; i < params.length; i++) {
266266
ManagedOperationParameter param = params[i];
267267
parameterInfo[i] =
268268
new MBeanParameterInfo(param.getName(), methodParameters[i].getName(), param.getDescription());
269269
}
270-
271270
return parameterInfo;
272271
}
273272

spring-context/src/test/java/org/springframework/jmx/export/assembler/MethodNameBasedMBeanInfoAssemblerTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -16,11 +16,13 @@
1616

1717
package org.springframework.jmx.export.assembler;
1818

19+
import javax.management.MBeanOperationInfo;
1920
import javax.management.modelmbean.ModelMBeanAttributeInfo;
2021
import javax.management.modelmbean.ModelMBeanInfo;
2122

2223
/**
2324
* @author Rob Harrop
25+
* @author David Boden
2426
*/
2527
public class MethodNameBasedMBeanInfoAssemblerTests extends AbstractJmxAssemblerTests {
2628

@@ -56,6 +58,13 @@ public void testGetAgeIsReadOnly() throws Exception {
5658
assertFalse(attr.isWritable());
5759
}
5860

61+
public void testSetNameParameterIsNamed() throws Exception {
62+
ModelMBeanInfo info = getMBeanInfoFromAssembler();
63+
64+
MBeanOperationInfo operationSetAge = info.getOperation("setName");
65+
assertEquals("name", operationSetAge.getSignature()[0].getName());
66+
}
67+
5968
@Override
6069
protected String getApplicationContextPath() {
6170
return "org/springframework/jmx/export/assembler/methodNameAssembler.xml";

0 commit comments

Comments
 (0)