From d02cf20a6fcbaad4ccaf8822d0492b0c3e196769 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 19:58:39 +0100 Subject: [PATCH 01/17] Fix breaking change in the method behavior According to the guards in the code, this is expected for both name and description to be null. --- src/main/java/org/weakref/jmx/AnnotationUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/weakref/jmx/AnnotationUtils.java b/src/main/java/org/weakref/jmx/AnnotationUtils.java index c26a3d2..0762ffd 100644 --- a/src/main/java/org/weakref/jmx/AnnotationUtils.java +++ b/src/main/java/org/weakref/jmx/AnnotationUtils.java @@ -212,7 +212,7 @@ public static String getDescription(Annotation... annotations) .stream() ) .findFirst() - .orElse(""); + .orElse(null); } public static String getName(Method annotatedMethod) @@ -231,7 +231,7 @@ public static String getName(Annotation... annotations) .stream() ) .findFirst() - .orElse(""); + .orElse(null); } /** From 6679133ab52921b767a8628f7d746a8f376f9eb5 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 20:01:41 +0100 Subject: [PATCH 02/17] Use pattern matching for instanceof --- .../java/org/weakref/jmx/AnnotationUtils.java | 16 +++++++-------- .../java/org/weakref/jmx/MBeanBuilder.java | 8 ++++---- .../java/org/weakref/jmx/ReflectionUtils.java | 20 +++++++++---------- .../jmx/guice/NamedBindingBuilder.java | 4 ++-- .../weakref/jmx/guice/NamedExportBinder.java | 4 ++-- .../jmx/testing/TestingMBeanServer.java | 4 +--- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/weakref/jmx/AnnotationUtils.java b/src/main/java/org/weakref/jmx/AnnotationUtils.java index 0762ffd..7d50a6e 100644 --- a/src/main/java/org/weakref/jmx/AnnotationUtils.java +++ b/src/main/java/org/weakref/jmx/AnnotationUtils.java @@ -121,11 +121,11 @@ private static void processAnnotation(Annotation annotation, Map // Convert Class and Enum value or array value to String or String array // see DescriptorKey javadocs for more info - if (value instanceof Class) { - value = ((Class) value).getName(); + if (value instanceof Class clazz) { + value = clazz.getName(); } - else if (value instanceof Enum) { - value = ((Enum) value).name(); + else if (value instanceof Enum enumValue) { + value = enumValue.name(); } else if (value.getClass().isArray()) { Class componentType = value.getClass().getComponentType(); @@ -175,8 +175,8 @@ public static String getDescription(Descriptor descriptor, Method... annotatedMe // If that didn't work, look for one in the descriptor object Object descriptionValue = descriptor.getFieldValue("description"); - if (descriptionValue instanceof String) { - return (String) descriptionValue; + if (descriptionValue instanceof String stringValue) { + return stringValue; } return null; } @@ -191,8 +191,8 @@ public static String getDescription(Descriptor descriptor, Annotation... annotat // If that didn't work, look for one in the descriptor object Object descriptionValue = descriptor.getFieldValue("description"); - if (descriptionValue instanceof String) { - return (String) descriptionValue; + if (descriptionValue instanceof String stringValue) { + return stringValue; } return null; } diff --git a/src/main/java/org/weakref/jmx/MBeanBuilder.java b/src/main/java/org/weakref/jmx/MBeanBuilder.java index 45f5040..8141b4d 100644 --- a/src/main/java/org/weakref/jmx/MBeanBuilder.java +++ b/src/main/java/org/weakref/jmx/MBeanBuilder.java @@ -121,11 +121,11 @@ public MBean build() List operations = new ArrayList<>(); for (MBeanAttributeBuilder attributeBuilder : attributeBuilders) { for (MBeanFeature feature : attributeBuilder.build()) { - if (feature instanceof MBeanAttribute) { - attributes.add((MBeanAttribute) feature); + if (feature instanceof MBeanAttribute attribute) { + attributes.add(attribute); } - if (feature instanceof MBeanOperation) { - operations.add((MBeanOperation) feature); + if (feature instanceof MBeanOperation operation) { + operations.add(operation); } } } diff --git a/src/main/java/org/weakref/jmx/ReflectionUtils.java b/src/main/java/org/weakref/jmx/ReflectionUtils.java index 76b7259..2868a16 100644 --- a/src/main/java/org/weakref/jmx/ReflectionUtils.java +++ b/src/main/java/org/weakref/jmx/ReflectionUtils.java @@ -63,27 +63,27 @@ public static Object invoke(Object target, Method method, Object... params) catch (InvocationTargetException e) { // unwrap exception Throwable targetException = e.getTargetException(); - if (targetException instanceof RuntimeException) { + if (targetException instanceof RuntimeException runtimeException) { throw new MBeanException( - (RuntimeException) targetException, + runtimeException, "RuntimeException occurred while invoking " + toSimpleName(method)); } - else if (targetException instanceof ReflectionException) { + else if (targetException instanceof ReflectionException exception) { // allow ReflectionException to passthrough - throw (ReflectionException) targetException; + throw exception; } - else if (targetException instanceof MBeanException) { + else if (targetException instanceof MBeanException exception) { // allow MBeanException to passthrough - throw (MBeanException) targetException; + throw exception; } - else if (targetException instanceof Exception) { + else if (targetException instanceof Exception exception) { throw new MBeanException( - (Exception) targetException, + exception, "Exception occurred while invoking " + toSimpleName(method)); } - else if (targetException instanceof Error) { + else if (targetException instanceof Error error) { throw new RuntimeErrorException( - (Error) targetException, + error, "Error occurred while invoking " + toSimpleName(method)); } else { diff --git a/src/main/java/org/weakref/jmx/guice/NamedBindingBuilder.java b/src/main/java/org/weakref/jmx/guice/NamedBindingBuilder.java index 2a64b87..1eed531 100644 --- a/src/main/java/org/weakref/jmx/guice/NamedBindingBuilder.java +++ b/src/main/java/org/weakref/jmx/guice/NamedBindingBuilder.java @@ -40,8 +40,8 @@ public class NamedBindingBuilder public void withGeneratedName() { if (key.getAnnotation() != null) { - if (key.getAnnotation() instanceof Named) { - as(factory -> factory.generatedNameOf(key.getTypeLiteral().getRawType(), ((Named) key.getAnnotation()).value())); + if (key.getAnnotation() instanceof Named annotation) { + as(factory -> factory.generatedNameOf(key.getTypeLiteral().getRawType(), annotation.value())); } else { as(factory -> factory.generatedNameOf(key.getTypeLiteral().getRawType(), key.getAnnotation().annotationType().getSimpleName())); diff --git a/src/main/java/org/weakref/jmx/guice/NamedExportBinder.java b/src/main/java/org/weakref/jmx/guice/NamedExportBinder.java index f72d184..be15e61 100644 --- a/src/main/java/org/weakref/jmx/guice/NamedExportBinder.java +++ b/src/main/java/org/weakref/jmx/guice/NamedExportBinder.java @@ -39,8 +39,8 @@ public class NamedExportBinder public void withGeneratedName() { if (key.getAnnotation() != null) { - if (key.getAnnotation() instanceof Named) { - as(factory -> factory.generatedNameOf(key.getTypeLiteral().getRawType(), ((Named) key.getAnnotation()).value())); + if (key.getAnnotation() instanceof Named annotation) { + as(factory -> factory.generatedNameOf(key.getTypeLiteral().getRawType(), annotation.value())); } else { as(factory -> factory.generatedNameOf(key.getTypeLiteral().getRawType(), key.getAnnotation().annotationType().getSimpleName())); diff --git a/src/main/java/org/weakref/jmx/testing/TestingMBeanServer.java b/src/main/java/org/weakref/jmx/testing/TestingMBeanServer.java index 9e3df7f..54f197a 100644 --- a/src/main/java/org/weakref/jmx/testing/TestingMBeanServer.java +++ b/src/main/java/org/weakref/jmx/testing/TestingMBeanServer.java @@ -38,12 +38,10 @@ public ObjectInstance registerMBean(Object object, ObjectName name) throw new UnsupportedOperationException("Only explicit name supported at this time"); } - if (!(object instanceof DynamicMBean)) { + if (!(object instanceof DynamicMBean mbean)) { throw new UnsupportedOperationException("Only DynamicMBeans supported at this time"); } - DynamicMBean mbean = (DynamicMBean) object; - if (mbeans.containsKey(name)) { throw new InstanceAlreadyExistsException(format("MBean already registered: %s", name)); } From b3f36524af9739aa25d1d30cc95c3c1493bca8be Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 20:07:10 +0100 Subject: [PATCH 03/17] Convert ManagedAttribute to a record --- .../org/weakref/jmx/ManagedAttribute.java | 35 +++---------------- .../java/org/weakref/jmx/ManagedClass.java | 7 ++-- 2 files changed, 7 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/weakref/jmx/ManagedAttribute.java b/src/main/java/org/weakref/jmx/ManagedAttribute.java index 4fcc128..065d36a 100644 --- a/src/main/java/org/weakref/jmx/ManagedAttribute.java +++ b/src/main/java/org/weakref/jmx/ManagedAttribute.java @@ -17,38 +17,11 @@ import java.lang.reflect.Method; -public class ManagedAttribute +public record ManagedAttribute(Method method, String name, String description, boolean flatten) { - private final Method method; - private final String name; - private final String description; - private final boolean flatten; - - public ManagedAttribute(Method method, String name, String description, boolean flatten) - { - this.method = Preconditions.checkNotNull(method, "method is null"); - this.name = Preconditions.checkNotNull(name, "name is null"); - this.description = description; - this.flatten = flatten; - } - - public Method getMethod() - { - return method; - } - - public String getName() - { - return name; - } - - public String getDescription() - { - return description; - } - - public boolean isFlatten() + public ManagedAttribute { - return flatten; + Preconditions.checkNotNull(method, "method is null"); + Preconditions.checkNotNull(name, "name is null"); } } diff --git a/src/main/java/org/weakref/jmx/ManagedClass.java b/src/main/java/org/weakref/jmx/ManagedClass.java index 8e3e327..aa85d2c 100644 --- a/src/main/java/org/weakref/jmx/ManagedClass.java +++ b/src/main/java/org/weakref/jmx/ManagedClass.java @@ -13,7 +13,6 @@ */ package org.weakref.jmx; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -109,17 +108,17 @@ public Set getAttributeNames() public Object invokeAttribute(String attributeName) throws InvocationTargetException, IllegalAccessException { - return getManagedAttribute(attributeName).getMethod().invoke(getTarget()); + return getManagedAttribute(attributeName).method().invoke(getTarget()); } public String getAttributeDescription(String attributeName) { - return getManagedAttribute(attributeName).getDescription(); + return getManagedAttribute(attributeName).description(); } public boolean isAttributeFlatten(String attributeName) { - return getManagedAttribute(attributeName).isFlatten(); + return getManagedAttribute(attributeName).flatten(); } private ManagedAttribute getManagedAttribute(String attributeName) From ef97e4e74a9b7944258b9f2205dc5b2c9795aa44 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 20:07:59 +0100 Subject: [PATCH 04/17] Drop redundant final --- src/main/java/org/weakref/jmx/Signature.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/weakref/jmx/Signature.java b/src/main/java/org/weakref/jmx/Signature.java index 85d8a53..a835503 100644 --- a/src/main/java/org/weakref/jmx/Signature.java +++ b/src/main/java/org/weakref/jmx/Signature.java @@ -96,7 +96,7 @@ public int hashCode() @Override public String toString() { - final StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(); sb.append(actionName).append('('); boolean first = true; for (String type : parameterTypes) { From afddbac8be7bc2d402f6994ed45da2df6fb9ff9f Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 20:08:35 +0100 Subject: [PATCH 05/17] Do not return raw Class --- src/main/java/org/weakref/jmx/ManagedClass.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/weakref/jmx/ManagedClass.java b/src/main/java/org/weakref/jmx/ManagedClass.java index aa85d2c..4be03fe 100644 --- a/src/main/java/org/weakref/jmx/ManagedClass.java +++ b/src/main/java/org/weakref/jmx/ManagedClass.java @@ -75,7 +75,7 @@ public static ManagedClass fromExportedObject(Object target) return new ManagedClass(target, children.build(), attributes.build()); } - public Class getTargetClass() + public Class getTargetClass() { return getTarget().getClass(); } From fcdaa6ed2aef8b3e1612e85be8c1c20b20e11d65 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 20:09:02 +0100 Subject: [PATCH 06/17] Infer type automatically --- src/main/java/org/weakref/jmx/guice/ExportBinder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/weakref/jmx/guice/ExportBinder.java b/src/main/java/org/weakref/jmx/guice/ExportBinder.java index 1c58c00..2dea6c6 100644 --- a/src/main/java/org/weakref/jmx/guice/ExportBinder.java +++ b/src/main/java/org/weakref/jmx/guice/ExportBinder.java @@ -25,8 +25,8 @@ public class ExportBinder public static ExportBinder newExporter(Binder binder) { - Multibinder> collectionBinder = newSetBinder(binder, new TypeLiteral>() {}); - Multibinder> mapBinder = newSetBinder(binder, new TypeLiteral>() {}); + Multibinder> collectionBinder = newSetBinder(binder, new TypeLiteral<>() {}); + Multibinder> mapBinder = newSetBinder(binder, new TypeLiteral<>() {}); return new ExportBinder(newSetBinder(binder, Mapping.class), collectionBinder, mapBinder); } From 7ab899fbdb996ac071aa06cf1c48c35b14732be5 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 20:13:07 +0100 Subject: [PATCH 07/17] Use String.isEmpty method --- src/main/java/org/weakref/jmx/ManagedClass.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/weakref/jmx/ManagedClass.java b/src/main/java/org/weakref/jmx/ManagedClass.java index 4be03fe..0dd2ca5 100644 --- a/src/main/java/org/weakref/jmx/ManagedClass.java +++ b/src/main/java/org/weakref/jmx/ManagedClass.java @@ -53,7 +53,7 @@ public static ManagedClass fromExportedObject(Object target) String attributeName = AnnotationUtils.getName(annotatedMethod); String description = AnnotationUtils.getDescription(annotatedMethod); - if (attributeName == null || attributeName.equals("")) { + if (attributeName == null || attributeName.isEmpty()) { attributeName = getAttributeName(concreteMethod); } From 038bc143669b243dcaadfcae85743f022771d2ca Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 20:16:24 +0100 Subject: [PATCH 08/17] Remove redundant suppression --- src/main/java/org/weakref/jmx/MBeanExporter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/weakref/jmx/MBeanExporter.java b/src/main/java/org/weakref/jmx/MBeanExporter.java index 2a9f91d..7ef0aad 100644 --- a/src/main/java/org/weakref/jmx/MBeanExporter.java +++ b/src/main/java/org/weakref/jmx/MBeanExporter.java @@ -219,7 +219,6 @@ public Map unexportAllAndReportMissing() toRemove.add(objectName); } catch (MBeanRegistrationException e) { - //noinspection ThrowableResultOfMethodCallIgnored errors.put(objectName.toString(), e); } } From 1004b53d5004471acc064ebec4be0abba7d68a18 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 20:23:56 +0100 Subject: [PATCH 09/17] Convert Signature to record class --- src/main/java/org/weakref/jmx/Signature.java | 68 ++++---------------- 1 file changed, 14 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/weakref/jmx/Signature.java b/src/main/java/org/weakref/jmx/Signature.java index a835503..665595a 100644 --- a/src/main/java/org/weakref/jmx/Signature.java +++ b/src/main/java/org/weakref/jmx/Signature.java @@ -19,27 +19,26 @@ import javax.management.MBeanParameterInfo; import java.lang.reflect.Method; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import static java.util.Collections.unmodifiableList; +import static java.util.stream.Collectors.toUnmodifiableList; -final class Signature +final record Signature(String actionName, List parameterTypes) { - private final String actionName; - private final List parameterTypes; + public Signature + { + actionName = actionName; + parameterTypes = unmodifiableList(parameterTypes); + } public Signature(Method method) { - this.actionName = method.getName(); - - List builder = new ArrayList<>(); - for (Class type : method.getParameterTypes()) { - builder.add(type.getName()); - } - parameterTypes = unmodifiableList(builder); + this(method.getName(), Arrays.stream(method.getParameterTypes()) + .map(Class::getName) + .collect(toUnmodifiableList())); } public Signature(String actionName, String... parameterTypes) @@ -47,50 +46,11 @@ public Signature(String actionName, String... parameterTypes) this(actionName, Arrays.asList(parameterTypes)); } - public Signature(String actionName, List parameterTypes) - { - this.actionName = actionName; - this.parameterTypes = unmodifiableList(parameterTypes); - } - - public Signature(MBeanOperationInfo info) { - this.actionName = info.getName(); - - List parameterTypes = new ArrayList<>(info.getSignature().length); - for (MBeanParameterInfo parameterInfo : info.getSignature()) { - parameterTypes.add(parameterInfo.getType()); - } - this.parameterTypes = unmodifiableList(parameterTypes); - } - - public String getActionName() - { - return actionName; - } - - public List getParameterTypes() - { - return parameterTypes; - } - - @Override - public boolean equals(Object o) - { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Signature signature = (Signature) o; - return Objects.equals(actionName, signature.actionName) && - Objects.equals(parameterTypes, signature.parameterTypes); - } - - @Override - public int hashCode() + public Signature(MBeanOperationInfo info) { - return Objects.hash(actionName, parameterTypes); + this(info.getName(), Arrays.stream(info.getSignature()) + .map(MBeanParameterInfo::getType) + .collect(toUnmodifiableList())); } @Override From 97970d89a13dc6773b040fe8e532f741c4c261a7 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 21:25:15 +0100 Subject: [PATCH 10/17] Simplify condition --- src/main/java/org/weakref/jmx/ReflectionUtils.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/org/weakref/jmx/ReflectionUtils.java b/src/main/java/org/weakref/jmx/ReflectionUtils.java index 2868a16..d97bca8 100644 --- a/src/main/java/org/weakref/jmx/ReflectionUtils.java +++ b/src/main/java/org/weakref/jmx/ReflectionUtils.java @@ -150,10 +150,7 @@ public static boolean isValidSetter(Method setter) if (setter == null) { throw new NullPointerException("setter is null"); } - if (setter.getParameterTypes().length != 1) { - return false; - } - return true; + return setter.getParameterCount() == 1; } public static boolean isAssignable(Object value, Class type) From adcd102f03ec9ae691a27ce40aa68bf7f046c45f Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 21:27:25 +0100 Subject: [PATCH 11/17] Reduce code nesting --- .../weakref/jmx/MBeanAttributeBuilder.java | 71 +++++++++---------- .../java/org/weakref/jmx/ReflectionUtils.java | 5 +- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java b/src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java index b5f772d..807bb53 100644 --- a/src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java +++ b/src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java @@ -181,51 +181,50 @@ else if (nested || AnnotationUtils.isNested(annotatedGetter)) { } return Collections.unmodifiableCollection(features); } + + // We must have a getter or a setter + if (concreteGetter == null && concreteSetter == null) { + throw new IllegalArgumentException("JmxAttribute must have a concrete getter or setter method"); + } + + // Type + Class attributeType; + if (concreteGetter != null) { + attributeType = concreteGetter.getReturnType(); + } else { - // We must have a getter or a setter - if (concreteGetter == null && concreteSetter == null) { - throw new IllegalArgumentException("JmxAttribute must have a concrete getter or setter method"); - } + attributeType = concreteSetter.getParameterTypes()[0]; + } - // Type - Class attributeType; - if (concreteGetter != null) { - attributeType = concreteGetter.getReturnType(); + // Descriptor + Descriptor descriptor = null; + if (annotatedGetter != null) { + descriptor = AnnotationUtils.buildDescriptor(annotatedGetter); + } + if (annotatedSetter != null) { + Descriptor setterDescriptor = AnnotationUtils.buildDescriptor(annotatedSetter); + if (descriptor == null) { + descriptor = setterDescriptor; } else { - attributeType = concreteSetter.getParameterTypes()[0]; - } - - // Descriptor - Descriptor descriptor = null; - if (annotatedGetter != null) { - descriptor = AnnotationUtils.buildDescriptor(annotatedGetter); - } - if (annotatedSetter != null) { - Descriptor setterDescriptor = AnnotationUtils.buildDescriptor(annotatedSetter); - if (descriptor == null) { - descriptor = setterDescriptor; - } - else { - descriptor = ImmutableDescriptor.union(descriptor, setterDescriptor); - } + descriptor = ImmutableDescriptor.union(descriptor, setterDescriptor); } + } - // Description - String description = AnnotationUtils.getDescription(descriptor, annotatedGetter, annotatedSetter); + // Description + String description = AnnotationUtils.getDescription(descriptor, annotatedGetter, annotatedSetter); - MBeanAttributeInfo mbeanAttributeInfo = new MBeanAttributeInfo( - attributeName, - attributeType.getName(), - description, - concreteGetter != null, - concreteSetter != null, - concreteGetter != null && concreteGetter.getName().startsWith("is"), - descriptor); + MBeanAttributeInfo mbeanAttributeInfo = new MBeanAttributeInfo( + attributeName, + attributeType.getName(), + description, + concreteGetter != null, + concreteSetter != null, + concreteGetter != null && concreteGetter.getName().startsWith("is"), + descriptor); - return Collections.singleton(new ReflectionMBeanAttribute(mbeanAttributeInfo, target, concreteGetter, concreteSetter)); - } + return Collections.singleton(new ReflectionMBeanAttribute(mbeanAttributeInfo, target, concreteGetter, concreteSetter)); } private static String getAttributeName(Method... methods) diff --git a/src/main/java/org/weakref/jmx/ReflectionUtils.java b/src/main/java/org/weakref/jmx/ReflectionUtils.java index d97bca8..562f059 100644 --- a/src/main/java/org/weakref/jmx/ReflectionUtils.java +++ b/src/main/java/org/weakref/jmx/ReflectionUtils.java @@ -158,9 +158,8 @@ public static boolean isAssignable(Object value, Class type) if (type.isPrimitive()) { return primitiveToWrapper.get(type).isInstance(value); } - else { - return value == null || type.isInstance(value); - } + + return value == null || type.isInstance(value); } private static void assertNotNull(Object param, String name) From c06d502fb5f9f8bd939b388a0090828bbb73cc05 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 21:34:03 +0100 Subject: [PATCH 12/17] Use Objects' preconditions --- src/main/java/org/weakref/jmx/MBean.java | 19 +++++------- .../weakref/jmx/MBeanAttributeBuilder.java | 27 +++++----------- .../java/org/weakref/jmx/MBeanBuilder.java | 5 ++- .../weakref/jmx/MBeanOperationBuilder.java | 31 +++++-------------- .../weakref/jmx/ReflectionMBeanAttribute.java | 11 ++----- .../java/org/weakref/jmx/ReflectionUtils.java | 23 +++++--------- 6 files changed, 34 insertions(+), 82 deletions(-) diff --git a/src/main/java/org/weakref/jmx/MBean.java b/src/main/java/org/weakref/jmx/MBean.java index 227176d..7812afc 100644 --- a/src/main/java/org/weakref/jmx/MBean.java +++ b/src/main/java/org/weakref/jmx/MBean.java @@ -37,6 +37,8 @@ import java.util.Collections; import java.util.HashMap; +import static java.util.Objects.requireNonNull; + class MBean implements DynamicMBean { private static final Object[] NO_PARAMS = new Object[0]; @@ -92,7 +94,7 @@ public Collection getOperations() public Object invoke(String actionName, Object[] params, String[] argTypes) throws MBeanException, ReflectionException { - assertNotNull("actionName", actionName); + requireNonNull(actionName, "actionName is null"); // params argTypes are allowed to be null and mean no-arg method if (params == null) { @@ -103,7 +105,7 @@ public Object invoke(String actionName, Object[] params, String[] argTypes) } for (int i = 0; i < argTypes.length; i++) { - assertNotNull("argTypes[" + i + "]", argTypes[i]); + requireNonNull(argTypes[i], "argTypes[" + i + "] is null"); } Signature signature = new Signature(actionName, argTypes); @@ -121,7 +123,7 @@ public Object invoke(String actionName, Object[] params, String[] argTypes) public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException { - assertNotNull("attribute", name); + requireNonNull(name, "name is null"); MBeanAttribute mbeanAttribute = attributes.get(name); if (mbeanAttribute == null) { throw new AttributeNotFoundException(name); @@ -134,9 +136,9 @@ public Object getAttribute(String name) public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { - assertNotNull("attribute", attribute); + requireNonNull(attribute, "attribute is null"); String name = attribute.getName(); - assertNotNull("attribute.name", name); + requireNonNull(name, "attribute.name is null"); Object value = attribute.getValue(); MBeanAttribute mbeanAttribute = attributes.get(name); @@ -185,11 +187,4 @@ public AttributeList setAttributes(AttributeList attributes) } return response; } - - private static void assertNotNull(String name, Object value) - { - if (value == null) { - throw new RuntimeOperationsException(new NullPointerException(name + " is null")); - } - } } diff --git a/src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java b/src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java index 807bb53..8894ecc 100644 --- a/src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java +++ b/src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java @@ -26,6 +26,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static java.util.Objects.requireNonNull; import static org.weakref.jmx.ReflectionUtils.isValidGetter; import static org.weakref.jmx.ReflectionUtils.isValidSetter; @@ -43,27 +44,19 @@ public class MBeanAttributeBuilder public MBeanAttributeBuilder onInstance(Object target) { - if (target == null) { - throw new NullPointerException("target is null"); - } - this.target = target; + this.target = requireNonNull(target, "target is null"); return this; } public MBeanAttributeBuilder named(String name) { - if (name == null) { - throw new NullPointerException("name is null"); - } - this.name = name; + this.name = requireNonNull(name, "name is null"); return this; } public MBeanAttributeBuilder withConcreteGetter(Method concreteGetter) { - if (concreteGetter == null) { - throw new NullPointerException("concreteGetter is null"); - } + requireNonNull(concreteGetter, "concreteGetter is null"); if (!isValidGetter(concreteGetter)) { throw new IllegalArgumentException("Method is not a valid getter: " + concreteGetter); } @@ -73,9 +66,7 @@ public MBeanAttributeBuilder withConcreteGetter(Method concreteGetter) public MBeanAttributeBuilder withAnnotatedGetter(Method annotatedGetter) { - if (annotatedGetter == null) { - throw new NullPointerException("annotatedGetter is null"); - } + requireNonNull(annotatedGetter, "annotatedGetter is null"); if (!isValidGetter(annotatedGetter)) { throw new IllegalArgumentException("Method is not a valid getter: " + annotatedGetter); } @@ -85,9 +76,7 @@ public MBeanAttributeBuilder withAnnotatedGetter(Method annotatedGetter) public MBeanAttributeBuilder withConcreteSetter(Method concreteSetter) { - if (concreteSetter == null) { - throw new NullPointerException("concreteSetter is null"); - } + requireNonNull(concreteSetter, "concreteSetter is null"); if (!isValidSetter(concreteSetter)) { throw new IllegalArgumentException("Method is not a valid setter: " + concreteSetter); } @@ -97,9 +86,7 @@ public MBeanAttributeBuilder withConcreteSetter(Method concreteSetter) public MBeanAttributeBuilder withAnnotatedSetter(Method annotatedSetter) { - if (annotatedSetter == null) { - throw new NullPointerException("annotatedSetter is null"); - } + requireNonNull(annotatedSetter, "annotatedSetter is null"); if (!isValidSetter(annotatedSetter)) { throw new IllegalArgumentException("Method is not a valid setter: " + annotatedSetter); } diff --git a/src/main/java/org/weakref/jmx/MBeanBuilder.java b/src/main/java/org/weakref/jmx/MBeanBuilder.java index 8141b4d..4ec90cb 100644 --- a/src/main/java/org/weakref/jmx/MBeanBuilder.java +++ b/src/main/java/org/weakref/jmx/MBeanBuilder.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.TreeMap; +import static java.util.Objects.requireNonNull; import static org.weakref.jmx.ReflectionUtils.getAttributeName; import static org.weakref.jmx.ReflectionUtils.isGetter; import static org.weakref.jmx.ReflectionUtils.isSetter; @@ -49,9 +50,7 @@ public static MBeanBuilder from(Object object) public MBeanBuilder(Object target) { - if (target == null) { - throw new NullPointerException("target is null"); - } + requireNonNull(target, "target is null"); Map attributeBuilders = new TreeMap<>(); diff --git a/src/main/java/org/weakref/jmx/MBeanOperationBuilder.java b/src/main/java/org/weakref/jmx/MBeanOperationBuilder.java index 9db4a8b..f01bf01 100644 --- a/src/main/java/org/weakref/jmx/MBeanOperationBuilder.java +++ b/src/main/java/org/weakref/jmx/MBeanOperationBuilder.java @@ -23,7 +23,9 @@ import java.lang.reflect.Method; import java.util.List; +import static com.google.common.base.Preconditions.checkArgument; import static io.airlift.parameternames.ParameterNames.getParameterNames; +import static java.util.Objects.requireNonNull; public class MBeanOperationBuilder { @@ -34,50 +36,33 @@ public class MBeanOperationBuilder public MBeanOperationBuilder onInstance(Object target) { - if (target == null) { - throw new NullPointerException("target is null"); - } - this.target = target; + this.target = requireNonNull(target, "target is null"); return this; } public MBeanOperationBuilder named(String name) { - if (name == null) { - throw new NullPointerException("name is null"); - } - this.name = name; + this.name = requireNonNull(name, "name is null"); return this; } public MBeanOperationBuilder withConcreteMethod(Method concreteMethod) { - if (concreteMethod == null) { - throw new NullPointerException("concreteMethod is null"); - } - this.concreteMethod = concreteMethod; + this.concreteMethod = requireNonNull(concreteMethod, "concreteMethod is null"); return this; } public MBeanOperationBuilder withAnnotatedMethod(Method annotatedMethod) { - if (annotatedMethod == null) { - throw new NullPointerException("annotatedMethod is null"); - } - this.annotatedMethod = annotatedMethod; + this.annotatedMethod = requireNonNull(annotatedMethod, "annotatedMethod is null"); return this; } public MBeanOperation build() { - if (target == null) { - throw new IllegalArgumentException("JmxOperation must have a target object"); - } - + requireNonNull(target, "JmxOperation must have a target object"); // We must have a method to invoke - if (concreteMethod == null) { - throw new IllegalArgumentException("JmxOperation must have a concrete method"); - } + requireNonNull(concreteMethod, "JmxOperation must have a concrete method"); String operationName = name; if (operationName == null) { diff --git a/src/main/java/org/weakref/jmx/ReflectionMBeanAttribute.java b/src/main/java/org/weakref/jmx/ReflectionMBeanAttribute.java index fc3156c..246cc21 100644 --- a/src/main/java/org/weakref/jmx/ReflectionMBeanAttribute.java +++ b/src/main/java/org/weakref/jmx/ReflectionMBeanAttribute.java @@ -15,6 +15,7 @@ */ package org.weakref.jmx; +import static java.util.Objects.requireNonNull; import static org.weakref.jmx.ReflectionUtils.invoke; import javax.management.AttributeNotFoundException; @@ -34,14 +35,8 @@ class ReflectionMBeanAttribute implements MBeanAttribute public ReflectionMBeanAttribute(MBeanAttributeInfo info, Object target, Method getter, Method setter) { - if (info == null) { - throw new NullPointerException("info is null"); - } - if (target == null) { - throw new NullPointerException("target is null"); - } - this.info = info; - this.target = target; + this.info = requireNonNull(info, "info is null"); + this.target = requireNonNull(target, "target is null"); this.name = info.getName(); this.getter = getter; this.setter = setter; diff --git a/src/main/java/org/weakref/jmx/ReflectionUtils.java b/src/main/java/org/weakref/jmx/ReflectionUtils.java index 562f059..a4fc7c4 100644 --- a/src/main/java/org/weakref/jmx/ReflectionUtils.java +++ b/src/main/java/org/weakref/jmx/ReflectionUtils.java @@ -27,6 +27,8 @@ import java.util.LinkedHashMap; import java.util.Collections; +import static java.util.Objects.requireNonNull; + final class ReflectionUtils { private ReflectionUtils() @@ -52,9 +54,9 @@ private ReflectionUtils() public static Object invoke(Object target, Method method, Object... params) throws MBeanException, ReflectionException { - assertNotNull(target, "target"); - assertNotNull(target, "method"); - assertNotNull(target, "params"); + requireNonNull(target, "target is null"); + requireNonNull(method, "method is ull"); + requireNonNull(params, "params is null"); try { Object result = method.invoke(target, params); @@ -133,9 +135,7 @@ public static String getAttributeName(Method method) public static boolean isValidGetter(Method getter) { - if (getter == null) { - throw new NullPointerException("getter is null"); - } + requireNonNull(getter, "getter is null"); if (getter.getParameterTypes().length != 0) { return false; } @@ -147,9 +147,7 @@ public static boolean isValidGetter(Method getter) public static boolean isValidSetter(Method setter) { - if (setter == null) { - throw new NullPointerException("setter is null"); - } + requireNonNull(setter, "setter is null"); return setter.getParameterCount() == 1; } @@ -161,11 +159,4 @@ public static boolean isAssignable(Object value, Class type) return value == null || type.isInstance(value); } - - private static void assertNotNull(Object param, String name) - { - if (param == null) { - throw new RuntimeOperationsException(new NullPointerException(name + " is null")); - } - } } From defd9021996c8096f57d9b9b12c379a570609d86 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 21:35:04 +0100 Subject: [PATCH 13/17] Simplify condition --- src/main/java/org/weakref/jmx/ReflectionUtils.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/org/weakref/jmx/ReflectionUtils.java b/src/main/java/org/weakref/jmx/ReflectionUtils.java index a4fc7c4..5e1b166 100644 --- a/src/main/java/org/weakref/jmx/ReflectionUtils.java +++ b/src/main/java/org/weakref/jmx/ReflectionUtils.java @@ -136,13 +136,7 @@ public static String getAttributeName(Method method) public static boolean isValidGetter(Method getter) { requireNonNull(getter, "getter is null"); - if (getter.getParameterTypes().length != 0) { - return false; - } - if (getter.getReturnType().equals(Void.TYPE)) { - return false; - } - return true; + return getter.getParameterCount() == 0 && !getter.getReturnType().equals(Void.TYPE); } public static boolean isValidSetter(Method setter) From 50fabf1d7753da7e66cbe12c746664c2c8b139e2 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 21:38:34 +0100 Subject: [PATCH 14/17] Inline variable --- src/main/java/org/weakref/jmx/ReflectionUtils.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/weakref/jmx/ReflectionUtils.java b/src/main/java/org/weakref/jmx/ReflectionUtils.java index 5e1b166..8bdde9e 100644 --- a/src/main/java/org/weakref/jmx/ReflectionUtils.java +++ b/src/main/java/org/weakref/jmx/ReflectionUtils.java @@ -59,8 +59,7 @@ public static Object invoke(Object target, Method method, Object... params) requireNonNull(params, "params is null"); try { - Object result = method.invoke(target, params); - return result; + return method.invoke(target, params); } catch (InvocationTargetException e) { // unwrap exception From 2085cbcfa78a00a2492392a852a617d0de0f7528 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 21:41:23 +0100 Subject: [PATCH 15/17] Replace Matcher with a String manipulations --- .../java/org/weakref/jmx/ReflectionUtils.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/weakref/jmx/ReflectionUtils.java b/src/main/java/org/weakref/jmx/ReflectionUtils.java index 8bdde9e..2274a32 100644 --- a/src/main/java/org/weakref/jmx/ReflectionUtils.java +++ b/src/main/java/org/weakref/jmx/ReflectionUtils.java @@ -21,8 +21,6 @@ import javax.management.RuntimeOperationsException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.Map; import java.util.LinkedHashMap; import java.util.Collections; @@ -35,7 +33,6 @@ private ReflectionUtils() { } - private static final Pattern getterOrSetterPattern = Pattern.compile("(get|set|is)(.+)"); private static final Map, Class> primitiveToWrapper; static { @@ -125,11 +122,17 @@ public static boolean isSetter(Method method) public static String getAttributeName(Method method) { - Matcher matcher = getterOrSetterPattern.matcher(method.getName()); - if (!matcher.matches()) { - throw new IllegalArgumentException("method does not represent a getter or setter"); + String name = method.getName(); + + if (name.startsWith("is")) { + return name.substring(2); + } + + if (name.startsWith("get") || name.startsWith("set")) { + return name.substring(3); } - return matcher.group(2); + + throw new IllegalArgumentException("method does not represent a getter or setter"); } public static boolean isValidGetter(Method getter) From 9a95eef0f85d3e9cb524f9473c6a770d86409f8a Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 21:46:22 +0100 Subject: [PATCH 16/17] Get rid of parameternames dependency Since we are on JDK 17 now, this is no longer needed --- pom.xml | 6 ------ src/main/java/org/weakref/jmx/MBeanOperationBuilder.java | 9 +++++++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index a8ab1ef..90a61bd 100644 --- a/pom.xml +++ b/pom.xml @@ -40,12 +40,6 @@ test - - io.airlift - parameternames - 1.5 - - com.google.inject guice diff --git a/src/main/java/org/weakref/jmx/MBeanOperationBuilder.java b/src/main/java/org/weakref/jmx/MBeanOperationBuilder.java index f01bf01..94ef688 100644 --- a/src/main/java/org/weakref/jmx/MBeanOperationBuilder.java +++ b/src/main/java/org/weakref/jmx/MBeanOperationBuilder.java @@ -21,11 +21,13 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.Arrays; import java.util.List; import static com.google.common.base.Preconditions.checkArgument; -import static io.airlift.parameternames.ParameterNames.getParameterNames; import static java.util.Objects.requireNonNull; +import static java.util.stream.Collectors.toUnmodifiableList; public class MBeanOperationBuilder { @@ -71,7 +73,10 @@ public MBeanOperation build() // // Build Parameter Infos - List parameterNames = getParameterNames(concreteMethod); + List parameterNames = Arrays.stream(concreteMethod.getParameters()) + .map(Parameter::getName) + .collect(toUnmodifiableList()); + Class[] types = concreteMethod.getParameterTypes(); // Parameter annotations used form descriptor come from the annotated method, not the public method From aa37804e147c8b0662d2cd1e4e9ed154a731d3f4 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 9 Dec 2025 21:48:47 +0100 Subject: [PATCH 17/17] Add rnn checks to ReflectionMBeanOperation ctor --- .../java/org/weakref/jmx/ReflectionMBeanOperation.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/weakref/jmx/ReflectionMBeanOperation.java b/src/main/java/org/weakref/jmx/ReflectionMBeanOperation.java index 88857b6..442427d 100644 --- a/src/main/java/org/weakref/jmx/ReflectionMBeanOperation.java +++ b/src/main/java/org/weakref/jmx/ReflectionMBeanOperation.java @@ -20,6 +20,8 @@ import javax.management.ReflectionException; import java.lang.reflect.Method; +import static java.util.Objects.requireNonNull; + class ReflectionMBeanOperation implements MBeanOperation { private final MBeanOperationInfo info; @@ -29,9 +31,9 @@ class ReflectionMBeanOperation implements MBeanOperation public ReflectionMBeanOperation(MBeanOperationInfo info, Object target, Method method) { - this.info = info; - this.target = target; - this.method = method; + this.info = requireNonNull(info, "info is null"); + this.target = requireNonNull(target, "target is null"); + this.method = requireNonNull(method, "method is null"); this.signature = new Signature(method); }