Skip to content

Commit

Permalink
HV-371: First draft for method-level meta data API
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Mar 4, 2011
1 parent 290e7a7 commit 3458757
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 1 deletion.
Expand Up @@ -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;

Expand Down Expand Up @@ -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> T unwrap(Class<T> type) {
if ( type.isAssignableFrom( getClass() ) ) {
return type.cast( this );
Expand Down
Expand Up @@ -179,5 +179,16 @@ <T> Set<MethodConstraintViolation<T>> validateParameter(T object,
* @return A set with the constraint violations caused by this validation.
* Will be empty, of no error occurs, but never null.
*/
<T> Set<MethodConstraintViolation<T>> validateReturnValue(T object, Method method, Object returnValue, Class<?>... groups);
<T> Set<MethodConstraintViolation<T>> 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);
}
@@ -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();

/**
* <p>
* Returns a list with descriptors for this method's parameters.
* </p>
* <p>
* 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 <code>null</code>
* at the position representing that parameter.
* </p>
*
* @return A list with descriptors for this method's parameters. An empty
* list will be returned if this method has no parameters.
*/
List<ParameterDescriptor> 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 <code>True</code>, if this method's return value shall be
* validated recursively, <code>false</code> otherwise.
*/
boolean isCascaded();
}
@@ -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 <code>True</code>, if this parameter shall be
* validated recursively, <code>false</code> otherwise.
*/
boolean isCascaded();

/**
* Returns this parameter's index within the parameter array of the
* method holding it.
*
* @return This parameter's index.
*/
int getIndex();

}
@@ -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 <code>true</code>, if
* {@link BeanDescriptor#isBeanConstrained()} would return true for the same
* type. Furthermore this will be true, if
* <ul>
* <li>a constraint is hosted on any of this type's method's parameters</li>
* <li>any of this type's method's parameters is marked for cascaded
* validation with the {@link Valid} annotation</li>
* <li>a constraint is hosted on the return value of any of this type's
* methods</li>
* <li>the return value of any of this type's methods is marked for cascaded
* validation with the {@link Valid} annotation</li>
* </ul>
*
* @return <code>True</code>, if this type has any constraints,
* <code>false</code>
*/
boolean isTypeConstrained();

/**
* @see BeanDescriptor#getConstrainedProperties()
*/
Set<PropertyDescriptor> 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<MethodDescriptor> 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);

}
@@ -0,0 +1,29 @@
<!--
~
~ 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.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
</head>
<body>
<p>This package provides a meta model related to method-level
constraints and as such is an addition to the package <code>javax.validation.metadata</code>
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.</p>
</body>
</html>

0 comments on commit 3458757

Please sign in to comment.