Skip to content

Commit aa08c11

Browse files
author
Rob Harrop
committed
[SPR-5859] @ManagedOperation can now be used on getters/setters
1 parent 2a0d68c commit aa08c11

File tree

7 files changed

+23
-11
lines changed

7 files changed

+23
-11
lines changed

org.springframework.context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ public ManagedMetric getManagedMetric(Method method) throws InvalidMetadataExcep
8787
}
8888

8989
public ManagedOperation getManagedOperation(Method method) throws InvalidMetadataException {
90-
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
91-
if (pd != null) {
92-
throw new InvalidMetadataException(
93-
"The ManagedOperation attribute is not valid for JavaBean properties. Use ManagedAttribute instead.");
94-
}
9590
Annotation ann = AnnotationUtils.findAnnotation(method, org.springframework.jmx.export.annotation.ManagedOperation.class);
9691
if (ann == null) {
9792
return null;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ protected ModelMBeanOperationInfo[] getOperationInfo(Object managedBean, String
355355
info.setDescriptor(desc);
356356
}
357357
}
358-
else if (includeOperation(method, beanKey)) {
358+
359+
// allow getters and setters to be marked as operations directly
360+
if (info == null && includeOperation(method, beanKey)) {
359361
info = createModelMBeanOperationInfo(method, method.getName(), beanKey);
360362
Descriptor desc = info.getDescriptor();
361363
desc.setField(FIELD_ROLE, ROLE_OPERATION);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ protected boolean includeWriteAttribute(Method method, String beanKey) {
156156
protected boolean includeOperation(Method method, String beanKey) {
157157
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
158158
if (pd != null) {
159-
return hasManagedAttribute(method);
160-
}
161-
else {
162-
return hasManagedOperation(method);
159+
if(hasManagedAttribute(method)) {
160+
return true;
161+
}
163162
}
163+
return hasManagedOperation(method);
164164
}
165165

166166
/**

org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/AnnotationMetadataAssemblerTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public void testOperationFromInterface() throws Exception {
4242
assertNotNull(op);
4343
}
4444

45+
public void testOperationOnGetter() throws Exception {
46+
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
47+
ModelMBeanOperationInfo op = inf.getOperation("getExpensiveToCalculate");
48+
assertNotNull(op);
49+
}
50+
4551
protected JmxAttributeSource getAttributeSource() {
4652
return new AnnotationJmxAttributeSource();
4753
}
@@ -65,6 +71,6 @@ protected int getExpectedAttributeCount() {
6571

6672
@Override
6773
protected int getExpectedOperationCount() {
68-
return super.getExpectedOperationCount() + 3;
74+
return super.getExpectedOperationCount() + 4;
6975
}
7076
}

org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/AnnotationTestBean.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,6 @@ public int getCacheEntries() {
110110
return 3;
111111
}
112112

113+
114+
113115
}

org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/AnnotationTestSubBean.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ public void setColour(String colour) {
4848

4949
public void fromInterface() {
5050
}
51+
52+
public int getExpensiveToCalculate() {
53+
return Integer.MAX_VALUE;
54+
}
5155
}

org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/IAnnotationTestBean.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ public interface IAnnotationTestBean {
2929

3030
@ManagedOperation
3131
void fromInterface();
32+
33+
@ManagedOperation
34+
int getExpensiveToCalculate();
3235
}

0 commit comments

Comments
 (0)