Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>parameternames</artifactId>
<version>1.5</version>
</dependency>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/weakref/jmx/AnnotationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ private static void processAnnotation(Annotation annotation, Map<String, Object>

// 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();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -212,7 +212,7 @@ public static String getDescription(Annotation... annotations)
.stream()
)
.findFirst()
.orElse("");
.orElse(null);
}

public static String getName(Method annotatedMethod)
Expand All @@ -231,7 +231,7 @@ public static String getName(Annotation... annotations)
.stream()
)
.findFirst()
.orElse("");
.orElse(null);
}

/**
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/org/weakref/jmx/MBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -92,7 +94,7 @@ public Collection<MBeanOperation> 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) {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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"));
}
}
}
98 changes: 42 additions & 56 deletions src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -181,51 +168,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)
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/org/weakref/jmx/MBeanBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, MBeanAttributeBuilder> attributeBuilders = new TreeMap<>();

Expand Down Expand Up @@ -121,11 +120,11 @@ public MBean build()
List<MBeanOperation> 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);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/weakref/jmx/MBeanExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ public Map<String, Exception> unexportAllAndReportMissing()
toRemove.add(objectName);
}
catch (MBeanRegistrationException e) {
//noinspection ThrowableResultOfMethodCallIgnored
errors.put(objectName.toString(), e);
}
}
Expand Down
Loading