Is it possible, when using custom oval annotation and custom class for check, to access the annotation and retrieve the used annotation attributes ?
Reference for oval: https://sebthom.github.io/oval/USERGUIDE.html#custom-constraint-annotations
Minimal example
Lets assume we have class Foo
.
It has two annotated fields.
Each time, the annotation has a different myValue
– a
and b
.
class Foo {
@CustomAnnotation(myValue = "a")
public String first;
@CustomAnnotation(myValue = "b")
public String second;
}
This is the annotation.
It is noted that a check should be performed using MyCheck.class
, also setting some default value for myValue
.
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Constraint(checkWith = MyCheck.class)
public @interface CustomAnnotation {
String myValue() default "";
}
Now we want to use oval to validate this field.
Most importantly, we want to extract the value a
or b
from the annotation's myValue
and use it inside our validation logic.
public class MyCheck extends AbstractAnnotationCheck<CustomAnnotation> {
@Override
public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context,
Validator validator) throws OValException {
// how to get the value of `myValue`, which is `a` or `b` or empty string as default
}
}
What I have tried and failed:
validatedObject
isFoo.class
. You can easily get its fields and annotations. However, there is no way to differentiate between the two annotations.valueToValidate
is in this caseString
value – whatfirst
orsecond
holds.context
not useful, you can get compile time type from it, which isString
validator
not useful ?