Skip to content

Commit

Permalink
Support JSR-305 annotations on endpoint methods
Browse files Browse the repository at this point in the history
Update `OperationMethodParameter` to additionally support JSR-305 based
`@Nullable` annotations.

Closes gh-24647
  • Loading branch information
philwebb committed Jan 5, 2021
1 parent 05890a2 commit 2ad9a47
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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,8 +18,14 @@

import java.lang.reflect.Parameter;

import javax.annotation.Nonnull;
import javax.annotation.meta.When;

import org.springframework.boot.actuate.endpoint.invoke.OperationParameter;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;

/**
Expand All @@ -29,6 +35,8 @@
*/
class OperationMethodParameter implements OperationParameter {

private static final boolean jsr305Present = ClassUtils.isPresent("javax.annotation.Nonnull", null);

private final String name;

private final Parameter parameter;
Expand All @@ -55,12 +63,24 @@ public Class<?> getType() {

@Override
public boolean isMandatory() {
return ObjectUtils.isEmpty(this.parameter.getAnnotationsByType(Nullable.class));
if (!ObjectUtils.isEmpty(this.parameter.getAnnotationsByType(Nullable.class))) {
return false;
}
return (jsr305Present) ? new Jsr305().isMandatory(this.parameter) : true;
}

@Override
public String toString() {
return this.name + " of type " + this.parameter.getType().getName();
}

private static class Jsr305 {

boolean isMandatory(Parameter parameter) {
MergedAnnotation<Nonnull> annotation = MergedAnnotations.from(parameter).get(Nonnull.class);
return !annotation.isPresent() || annotation.getEnum("when", When.class) == When.ALWAYS;
}

}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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 @@ -16,8 +16,14 @@

package org.springframework.boot.actuate.endpoint.invoke.reflect;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifier;
import javax.annotation.meta.When;

import org.junit.jupiter.api.Test;

import org.springframework.lang.Nullable;
Expand All @@ -32,34 +38,68 @@
*/
class OperationMethodParameterTests {

private Method method = ReflectionUtils.findMethod(getClass(), "example", String.class, String.class);
private Method example = ReflectionUtils.findMethod(getClass(), "example", String.class, String.class);

private Method exampleJsr305 = ReflectionUtils.findMethod(getClass(), "exampleJsr305", String.class, String.class);

private Method exampleMetaJsr305 = ReflectionUtils.findMethod(getClass(), "exampleMetaJsr305", String.class,
String.class);

@Test
void getNameShouldReturnName() {
OperationMethodParameter parameter = new OperationMethodParameter("name", this.method.getParameters()[0]);
OperationMethodParameter parameter = new OperationMethodParameter("name", this.example.getParameters()[0]);
assertThat(parameter.getName()).isEqualTo("name");
}

@Test
void getTypeShouldReturnType() {
OperationMethodParameter parameter = new OperationMethodParameter("name", this.method.getParameters()[0]);
OperationMethodParameter parameter = new OperationMethodParameter("name", this.example.getParameters()[0]);
assertThat(parameter.getType()).isEqualTo(String.class);
}

@Test
void isMandatoryWhenNoAnnotationShouldReturnTrue() {
OperationMethodParameter parameter = new OperationMethodParameter("name", this.method.getParameters()[0]);
OperationMethodParameter parameter = new OperationMethodParameter("name", this.example.getParameters()[0]);
assertThat(parameter.isMandatory()).isTrue();
}

@Test
void isMandatoryWhenNullableAnnotationShouldReturnFalse() {
OperationMethodParameter parameter = new OperationMethodParameter("name", this.method.getParameters()[1]);
OperationMethodParameter parameter = new OperationMethodParameter("name", this.example.getParameters()[1]);
assertThat(parameter.isMandatory()).isFalse();
}

@Test
void isMandatoryWhenJsrNullableAnnotationShouldReturnFalse() {
OperationMethodParameter parameter = new OperationMethodParameter("name",
this.exampleJsr305.getParameters()[1]);
assertThat(parameter.isMandatory()).isFalse();
}

@Test
void isMandatoryWhenJsrMetaNullableAnnotationShouldReturnFalse() {
OperationMethodParameter parameter = new OperationMethodParameter("name",
this.exampleMetaJsr305.getParameters()[1]);
assertThat(parameter.isMandatory()).isFalse();
}

void example(String one, @Nullable String two) {

}

void exampleJsr305(String one, @javax.annotation.Nullable String two) {

}

void exampleMetaJsr305(String one, @MetaNullable String two) {

}

@TypeQualifier
@Retention(RetentionPolicy.RUNTIME)
@Nonnull(when = When.MAYBE)
@interface MetaNullable {

}

}

0 comments on commit 2ad9a47

Please sign in to comment.