diff --git a/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorImpl.java b/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorImpl.java index 6a3bb225c5..aba1568f92 100644 --- a/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorImpl.java +++ b/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorImpl.java @@ -58,6 +58,7 @@ import org.hibernate.validator.metadata.ParameterMetaData; import org.hibernate.validator.method.MethodConstraintViolation; import org.hibernate.validator.method.MethodValidator; +import org.hibernate.validator.method.metadata.TypeDescriptor; import org.hibernate.validator.util.Contracts; import org.hibernate.validator.util.ReflectionHelper; @@ -252,6 +253,10 @@ public final BeanDescriptor getConstraintsForClass(Class clazz) { return getBeanMetaData( clazz ).getBeanDescriptor(); } + public final TypeDescriptor getConstraintsForType(Class clazz) { + throw new UnsupportedOperationException( "Not yet implemented." ); + } + public final T unwrap(Class type) { if ( type.isAssignableFrom( getClass() ) ) { return type.cast( this ); diff --git a/hibernate-validator/src/main/java/org/hibernate/validator/method/MethodValidator.java b/hibernate-validator/src/main/java/org/hibernate/validator/method/MethodValidator.java index 3cb141010e..c589a16b41 100644 --- a/hibernate-validator/src/main/java/org/hibernate/validator/method/MethodValidator.java +++ b/hibernate-validator/src/main/java/org/hibernate/validator/method/MethodValidator.java @@ -179,5 +179,16 @@ Set> validateParameter(T object, * @return A set with the constraint violations caused by this validation. * Will be empty, of no error occurs, but never null. */ - Set> validateReturnValue(T object, Method method, Object returnValue, Class... groups); + Set> validateReturnValue(T object, + Method method, Object returnValue, Class... groups); + + /** + * Returns a descriptor providing access to constraint-related meta data for + * the given type. + * + * @param clazz The type of interest. + * + * @return A descriptor for the given type, will never be null. + */ + TypeDescriptor getConstraintsForType(Class clazz); } diff --git a/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/MethodDescriptor.java b/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/MethodDescriptor.java new file mode 100644 index 0000000000..1b943d0d4d --- /dev/null +++ b/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/MethodDescriptor.java @@ -0,0 +1,65 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.hibernate.validator.method.metadata; + +import java.lang.reflect.Method; +import java.util.List; +import javax.validation.Valid; +import javax.validation.metadata.ElementDescriptor; + +/** + * Describes a constrained method and the constraints associated with it. + * + * @author Gunnar Morling + */ +public interface MethodDescriptor extends ElementDescriptor { + + /** + * Returns the method represented by this descriptor. + * + * @return The method represented by this descriptor. + */ + Method getMethod(); + + /** + *

+ * Returns a list with descriptors for this method's parameters. + *

+ *

+ * The size of this list corresponds with the number of this method's + * parameters. If there are no constraints defined for a given parameter + * (neither locally nor in the inheritance hierarchy) and this parameter is + * also not annotated with {@link Valid} (neither locally nor in the + * inheritance hierarchy), the returned list will contain null + * at the position representing that parameter. + *

+ * + * @return A list with descriptors for this method's parameters. An empty + * list will be returned if this method has no parameters. + */ + List getParameterConstraints(); + + /** + * Whether a cascaded validation for this method's return value shall be + * performed or not. This is the case if this method is annotated with the + * {@link Valid} annotation either locally or in the inheritance hierarchy. + * + * @return True, if this method's return value shall be + * validated recursively, false otherwise. + */ + boolean isCascaded(); +} diff --git a/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/ParameterDescriptor.java b/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/ParameterDescriptor.java new file mode 100644 index 0000000000..cd5aa24c72 --- /dev/null +++ b/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/ParameterDescriptor.java @@ -0,0 +1,47 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual contributors +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package org.hibernate.validator.method.metadata; + +import javax.validation.Valid; +import javax.validation.metadata.ElementDescriptor; + +/** + * Describes a constrained parameter and the constraints associated with it. + * + * @author Gunnar Morling + */ +public interface ParameterDescriptor extends ElementDescriptor { + + /** + * Whether cascaded validation for this parameter shall be + * performed or not. This is the case if this parameter is annotated with the + * {@link Valid} annotation either locally or in the inheritance hierarchy. + * + * @return True, if this parameter shall be + * validated recursively, false otherwise. + */ + boolean isCascaded(); + + /** + * Returns this parameter's index within the parameter array of the + * method holding it. + * + * @return This parameter's index. + */ + int getIndex(); + +} diff --git a/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/TypeDescriptor.java b/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/TypeDescriptor.java new file mode 100644 index 0000000000..4de80714c3 --- /dev/null +++ b/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/TypeDescriptor.java @@ -0,0 +1,83 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.hibernate.validator.method.metadata; + +import java.lang.reflect.Method; +import java.util.Set; +import javax.validation.Valid; +import javax.validation.metadata.BeanDescriptor; +import javax.validation.metadata.ElementDescriptor; +import javax.validation.metadata.PropertyDescriptor; + +/** + * Describes a constrained Java type and the constraints associated to it. + * + * @author Gunnar Morling + */ +public interface TypeDescriptor extends ElementDescriptor { + + /** + * Whether this type has any class-, property- or method-level constraints + * or not. This is true, if + * {@link BeanDescriptor#isBeanConstrained()} would return true for the same + * type. Furthermore this will be true, if + *
    + *
  • a constraint is hosted on any of this type's method's parameters
  • + *
  • any of this type's method's parameters is marked for cascaded + * validation with the {@link Valid} annotation
  • + *
  • a constraint is hosted on the return value of any of this type's + * methods
  • + *
  • the return value of any of this type's methods is marked for cascaded + * validation with the {@link Valid} annotation
  • + *
+ * + * @return True, if this type has any constraints, + * false + */ + boolean isTypeConstrained(); + + /** + * @see BeanDescriptor#getConstrainedProperties() + */ + Set getConstrainedProperties(); + + /** + * @see BeanDescriptor#getConstraintsForProperty(String) + */ + PropertyDescriptor getConstraintsForProperty(String propertyName); + + /** + * Returns a set with the constrained methods of this type. + * + * @return A set with the constrained methods of this type, will be empty if + * none of this type's methods are constrained. + */ + Set getConstrainedMethods(); + + /** + * Returns a descriptor for the specified method. + * + * @param method The method of interest. + * + * @return A descriptor for the specified method. Will never be null. + * + * @throws IllegalArgumentException if method is null or is not a method of the type represented + * by this descriptor. + */ + MethodDescriptor getConstraintsForMethod(Method method); + +} diff --git a/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/package.html b/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/package.html new file mode 100644 index 0000000000..575d443388 --- /dev/null +++ b/hibernate-validator/src/main/java/org/hibernate/validator/method/metadata/package.html @@ -0,0 +1,29 @@ + + + + + + +

This package provides a meta model related to method-level +constraints and as such is an addition to the package javax.validation.metadata +provided by the Bean Validation API. This meta model is read-only, all +of its types are immutable. This package is part of the public Hibernate +Validator API.

+ +