We're updating the issue view to help you get more done. 

Custom constraint causes "target cannot be inferred" exception when used for return value

Description

I have created a CustomMin constraint, which is an exact copy of javax.validation.constraints.Min as follows:

CustomMin.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented @Constraint(validatedBy = { }) public @interface CustomMin { String message() default "{javax.validation.constraints.Min.message}"; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; /** * @return value the element must be higher or equal to */ long value(); /** * Defines several {@link CustomMin} annotations on the same element. * * @see CustomMin */ @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented @interface List { CustomMin[] value(); } }

I am trying to validate parameters using the ExecutableValidator as follows:

TestValidator.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.test.validation.constraints; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.ValidatorFactory; import javax.validation.constraints.Min; import javax.validation.executable.ExecutableValidator; import java.lang.reflect.Method; import java.util.Set; public class TestValidator { @CustomMin(1) public Integer test(@Min(1) Integer i) { return i; } public static void main(String[] args) throws Exception { TestValidator customerFacade = new TestValidator(); Method method = TestValidator.class.getMethod("test", Integer.class); Object[] parameterValues = {0}; ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); ExecutableValidator executableValidator = factory.getValidator().forExecutables(); Set<ConstraintViolation<TestValidator>> parameterViolations = executableValidator.validateParameters( customerFacade, method, parameterValues ); System.out.println("parameterViolations = " + parameterViolations); } }

The above is causing the following exception:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Exception in thread "main" javax.validation.ConstraintDeclarationException: HV000141: The constraint com.test.validation.constraints.CustomMin used ConstraintTarget#IMPLICIT where the target cannot be inferred. at org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.determineConstraintType(ConstraintDescriptorImpl.java:404) at org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.<init>(ConstraintDescriptorImpl.java:186) at org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.<init>(ConstraintDescriptorImpl.java:208) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.buildConstraintDescriptor(AnnotationMetaDataProvider.java:588) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findConstraintAnnotations(AnnotationMetaDataProvider.java:515) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findConstraints(AnnotationMetaDataProvider.java:469) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findExecutableMetaData(AnnotationMetaDataProvider.java:313) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMetaData(AnnotationMetaDataProvider.java:294) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMethodMetaData(AnnotationMetaDataProvider.java:280) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:143) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:130) at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfigurationForHierarchy(AnnotationMetaDataProvider.java:113) at org.hibernate.validator.internal.metadata.BeanMetaDataManager.createBeanMetaData(BeanMetaDataManager.java:187) at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanMetaData(BeanMetaDataManager.java:152) at org.hibernate.validator.internal.engine.ValidatorImpl.validateParametersInContext(ValidatorImpl.java:878) at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:240) at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:198) at com.test.validation.constraints.TestValidator.main(TestValidator.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Environment

I am using hibernate validator 5.0.2 with Java 1.6.0_45-b06-451 on OSX 10.9.1.

Status

Assignee

Gunnar Morling

Reporter

George Fountopoulos

Labels

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Affects versions

5.0.2.Final

Priority

Major