Class InjectionTarget
- java.lang.Object
-
- com.github.robtimus.junit.support.extension.InjectionTarget
-
public final class InjectionTarget extends Object
A representation of an injection target. This can be a field or a parameter.When method
isAnnotated(Class, boolean)
,findAnnotation(Class, boolean)
orfindRepeatableAnnotations(Class, boolean)
is called withtrue
for theincludeDeclaringElements
argument, the declaring elements are checked if the target itself is not annotated. The declaring elements are the constructor or method (for parameters only) and the class the field, constructor or method is declared in. If the class is a nested class, its declaring class is also checked; this continues until a top-level class is found.- Author:
- Rob Spoor
- Since:
- 2.0
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JUnitException
createException(String message)
Creates aJUnitException
with a message.JUnitException
createException(String message, Throwable cause)
Creates aJUnitException
with a message and a cause.Class<?>
declaringClass()
Returns the declaring class.<A extends Annotation>
Optional<A>findAnnotation(Class<A> annotationType)
Finds the first annotation of a specific type that is either present or meta-present on the injection target.<A extends Annotation>
Optional<A>findAnnotation(Class<A> annotationType, boolean includeDeclaringElements)
Finds the first annotation of a specific type that is either present or meta-present on the injection target.<A extends Annotation>
List<A>findRepeatableAnnotations(Class<A> annotationType)
Finds all repeatable annotations of a specific type that are either present or meta-present on the injection target.<A extends Annotation>
List<A>findRepeatableAnnotations(Class<A> annotationType, boolean includeDeclaringElements)
Finds all repeatable annotations of a specific type that are either present or meta-present on the injection target.static InjectionTarget
forField(Field field)
Creates an injection target for a field.static InjectionTarget
forParameter(ParameterContext parameterContext)
Creates an injection target for a constructor or method parameter.Type
genericType()
Returns the generic target type.boolean
isAnnotated(Class<? extends Annotation> annotationType)
Checks whether or not an annotation of a specific type is either present or meta-present on the injection target.boolean
isAnnotated(Class<? extends Annotation> annotationType, boolean includeDeclaringElements)
Checks whether or not an annotation of a specific type is either present or meta-present on the injection target.Class<?>
type()
Returns the target type.
-
-
-
Method Detail
-
declaringClass
public Class<?> declaringClass()
Returns the declaring class. For parameters, this is the declaring class of the constructor or method.- Returns:
- The declaring class.
- See Also:
Field.getDeclaringClass()
,Parameter.getDeclaringExecutable()
,Executable.getDeclaringClass()
-
type
public Class<?> type()
Returns the target type.- Returns:
- The target type.
- See Also:
Field.getType()
,Parameter.getType()
-
genericType
public Type genericType()
Returns the generic target type.- Returns:
- The generic target type.
- See Also:
Field.getGenericType()
,Parameter.getParameterizedType()
-
isAnnotated
public boolean isAnnotated(Class<? extends Annotation> annotationType)
Checks whether or not an annotation of a specific type is either present or meta-present on the injection target.- Parameters:
annotationType
- The type to check.- Returns:
true
if the an annotation of the given type is present or meta-present, orfalse
otherwise.
-
isAnnotated
public boolean isAnnotated(Class<? extends Annotation> annotationType, boolean includeDeclaringElements)
Checks whether or not an annotation of a specific type is either present or meta-present on the injection target.- Parameters:
annotationType
- The type to check.includeDeclaringElements
- Iftrue
, the injection targets declaring elements are checked if the target itself is not annotated.- Returns:
true
if the an annotation of the given type is present or meta-present, orfalse
otherwise.
-
findAnnotation
public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType)
Finds the first annotation of a specific type that is either present or meta-present on the injection target.- Type Parameters:
A
- The type of annotation.- Parameters:
annotationType
- The type to find an annotation for.- Returns:
- An
Optional
describing the first annotation of the given type, orOptional.empty()
if the annotation is not present.
-
findAnnotation
public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType, boolean includeDeclaringElements)
Finds the first annotation of a specific type that is either present or meta-present on the injection target.- Type Parameters:
A
- The type of annotation.- Parameters:
annotationType
- The type to find an annotation for.includeDeclaringElements
- Iftrue
, the injection targets declaring elements are checked if the target itself is not annotated.- Returns:
- An
Optional
describing the first annotation of the given type, orOptional.empty()
if the annotation is not present.
-
findRepeatableAnnotations
public <A extends Annotation> List<A> findRepeatableAnnotations(Class<A> annotationType)
Finds all repeatable annotations of a specific type that are either present or meta-present on the injection target.- Type Parameters:
A
- The type of annotation.- Parameters:
annotationType
- The type to find annotations for.- Returns:
- A list with all annotations of the given type; possibly empty but never
null
.
-
findRepeatableAnnotations
public <A extends Annotation> List<A> findRepeatableAnnotations(Class<A> annotationType, boolean includeDeclaringElements)
Finds all repeatable annotations of a specific type that are either present or meta-present on the injection target.- Type Parameters:
A
- The type of annotation.- Parameters:
annotationType
- The type to find annotations for.includeDeclaringElements
- Iftrue
, the injection targets declaring elements are checked if the target itself is not annotated.- Returns:
- A list with all annotations of the given type; possibly empty but never
null
.
-
createException
public JUnitException createException(String message)
Creates aJUnitException
with a message.- Parameters:
message
- The message for the exception.- Returns:
- The created exception.
-
createException
public JUnitException createException(String message, Throwable cause)
Creates aJUnitException
with a message and a cause.- Parameters:
message
- The message for the exception.cause
- The cause of the exception.- Returns:
- The created exception.
-
forParameter
public static InjectionTarget forParameter(ParameterContext parameterContext)
Creates an injection target for a constructor or method parameter.- Parameters:
parameterContext
- The parameter context describing the parameter.- Returns:
- The created injection target.
- Throws:
NullPointerException
- If the given parameter context isnull
.
-
forField
public static InjectionTarget forField(Field field)
Creates an injection target for a field.- Parameters:
field
- The field.- Returns:
- The created injection target.
- Throws:
NullPointerException
- If the given parameter context isnull
.
-
-