Class InjectionTarget
- java.lang.Object
-
- com.github.robtimus.junit.support.extension.InjectionTarget
-
public abstract 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 withtruefor theincludeDeclaringElementsargument, 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.Since version 3.1, injection targets implement
Object.equals(Object)andObject.hashCode()and can therefore be used as keys toExtensionContext.Store. They also implementObject.toString()to return a unique representation for the injection target. This can be used as basis for keys toExtensionContext.Store.- Author:
- Rob Spoor
- Since:
- 2.0
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract JUnitExceptioncreateException(String message)Creates aJUnitExceptionwith a message.abstract JUnitExceptioncreateException(String message, Throwable cause)Creates aJUnitExceptionwith a message and a cause.abstract 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.abstract <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.abstract <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 InjectionTargetforField(Field field)Creates an injection target for a field.static InjectionTargetforParameter(ParameterContext parameterContext)Creates an injection target for a constructor or method parameter.abstract TypegenericType()Returns the generic target type.booleanisAnnotated(Class<? extends Annotation> annotationType)Checks whether or not an annotation of a specific type is either present or meta-present on the injection target.booleanisAnnotated(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.abstract Class<?>type()Returns the target type.
-
-
-
Method Detail
-
declaringClass
public abstract 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 abstract Class<?> type()
Returns the target type.- Returns:
- The target type.
- See Also:
Field.getType(),Parameter.getType()
-
genericType
public abstract 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:
trueif the an annotation of the given type is present or meta-present, orfalseotherwise.
-
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:
trueif the an annotation of the given type is present or meta-present, orfalseotherwise.
-
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
Optionaldescribing the first annotation of the given type, orOptional.empty()if the annotation is not present.
-
findAnnotation
public abstract <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
Optionaldescribing 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 abstract <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 abstract JUnitException createException(String message)
Creates aJUnitExceptionwith a message.- Parameters:
message- The message for the exception.- Returns:
- The created exception.
-
createException
public abstract JUnitException createException(String message, Throwable cause)
Creates aJUnitExceptionwith 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.
-
-