Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
ab808035
Commit
ab808035
authored
Jan 05, 2021
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.3.x' into 2.4.x
Closes gh-24654
parents
50534090
2ad9a47d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
8 deletions
+68
-8
OperationMethodParameter.java
...ate/endpoint/invoke/reflect/OperationMethodParameter.java
+22
-2
OperationMethodParameterTests.java
...ndpoint/invoke/reflect/OperationMethodParameterTests.java
+46
-6
No files found.
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/invoke/reflect/OperationMethodParameter.java
View file @
ab808035
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
21
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.
...
...
@@ -18,8 +18,14 @@ package org.springframework.boot.actuate.endpoint.invoke.reflect;
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
;
/**
...
...
@@ -29,6 +35,8 @@ import org.springframework.util.ObjectUtils;
*/
class
OperationMethodParameter
implements
OperationParameter
{
private
static
final
boolean
jsr305Present
=
ClassUtils
.
isPresent
(
"javax.annotation.Nonnull"
,
null
);
private
final
String
name
;
private
final
Parameter
parameter
;
...
...
@@ -55,7 +63,10 @@ class OperationMethodParameter implements OperationParameter {
@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
...
...
@@ -63,4 +74,13 @@ class OperationMethodParameter implements OperationParameter {
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
;
}
}
}
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/invoke/reflect/OperationMethodParameterTests.java
View file @
ab808035
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
21
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.
...
...
@@ -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
;
...
...
@@ -32,29 +38,48 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
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
();
}
...
...
@@ -62,4 +87,19 @@ class OperationMethodParameterTests {
}
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
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment