Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * 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.internal.metadata.aggregated;
 
 import java.util.List;
 import java.util.Set;
 
 
 import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
 import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;

An aggregated view of the constraint related meta data for a given method and all the methods in the inheritance hierarchy which it overrides or implements.

Instances are retrieved by creating a MethodMetaData.Builder and adding all required org.hibernate.validator.internal.metadata.raw.ConstrainedMethod objects to it. Instances are read-only after creation.

Identity is solely based on the method's name and parameter types, hence sets and similar collections of this type may only be created in the scope of one Java type.

Author(s):
Gunnar Morling
 
 public class MethodMetaData extends AbstractConstraintMetaData {
 
 	private static final Log log = LoggerFactory.make();
 
 	private final Class<?>[] parameterTypes;
A declaration exception in case this method contains any illegal method parameter constraints. Such illegal parameter constraints shall not hinder standard bean/property validation as defined by the Bean Validation API. Therefore this exception is created when building up the meta data for validated beans, but it will only be thrown by the validation engine when actually a method validation is performed.
 
 
 	private MethodMetaData(
 			String name,
 			Class<?> returnType,
 			Class<?>[] parameterTypes,
 			Set<MetaConstraint<?>> returnValueConstraints,
 			List<ParameterMetaDataparameterMetaData,
 			ConstraintDeclarationException parameterConstraintDeclarationException,
 			boolean isCascading,
 			boolean isConstrained) {
 
 		super(
 				name,
 				returnType,
 				returnValueConstraints,
 				isCascading,
 				isConstrained
 		);
 
 		this. = parameterTypes;
 		this. = Collections.unmodifiableListparameterMetaData );
 		this. = parameterConstraintDeclarationException;
 	}

Creates new MethodMetaData instances.

Author(s):
Gunnar Morling
Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
	public static class Builder extends MetaDataBuilder {
		private boolean isCascading = false;
		private boolean isConstrained = false;

Creates a new builder based on the given method meta data.

Parameters:
constrainedMethod The base method for this builder. This is the lowest method with a given signature within a type hierarchy.
constraintHelper the constraint helper
		public Builder(ConstrainedMethod constrainedMethodConstraintHelper constraintHelper) {
			superconstraintHelper );
			 = constrainedMethod.getLocation();
			addconstrainedMethod );
		}

		public boolean accepts(ConstrainedElement constrainedElement) {
			return constrainedElement.getKind() == . &&
					ReflectionHelper.haveSameSignature(
							( (ConstrainedMethodconstrainedElement ).getLocation().getMember()
					);
		}

		public void add(ConstrainedElement constrainedElement) {
			ConstrainedMethod constrainedMethod = (ConstrainedMethodconstrainedElement;
			.addconstrainedMethod );
			 =  || constrainedMethod.isCascading();
			 =  || constrainedMethod.isConstrained();
		}

		public MethodMetaData build() {
			Method method = .getMember();
			return new MethodMetaData(
					method.getName(),
					method.getReturnType(),
			);
		}

Finds the one method from the underlying hierarchy with parameter constraints. If no method in the hierarchy is parameter constrained, the parameter meta data from this builder's base method is returned.

Returns:
The parameter meta data for this builder's method.
			List<ParameterMetaData.BuilderparameterBuilders = null;
			for ( ConstrainedMethod oneMethod :  ) {
				if ( parameterBuilders == null ) {
					parameterBuilders = newArrayList();
					for ( ConstrainedParameter oneParameter : oneMethod.getAllParameterMetaData() ) {
						parameterBuilders.add(
										oneParameter,
								)
						);
					}
				}
				else {
					int i = 0;
					for ( ConstrainedParameter oneParameter : oneMethod.getAllParameterMetaData() ) {
						parameterBuilders.geti ).addoneParameter );
						i++;
					}
				}
			}
			List<ParameterMetaDataparameterMetaDatas = newArrayList();
			for ( ParameterMetaData.Builder oneBuilder : parameterBuilders ) {
				parameterMetaDatas.addoneBuilder.build() );
			}
			return parameterMetaDatas;
		}

Checks that there are no invalid parameter constraints defined at this builder's methods.

Returns:
A javax.validation.ConstraintDeclarationException describing the first illegal method parameter constraint found or null, if the methods of this builder have no such illegal constraints.
			Set<ConstrainedMethodmethodsWithParameterConstraints = getMethodsWithParameterConstraints(
			);
			if ( methodsWithParameterConstraints.isEmpty() ) {
				return null;
			}
			Set<Class<?>> definingTypes = newHashSet();
			for ( ConstrainedMethod constrainedMethod : methodsWithParameterConstraints ) {
				definingTypes.addconstrainedMethod.getLocation().getBeanClass() );
			}
			if ( definingTypes.size() > 1 ) {
						"Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints, " +
								"but there are parameter constraints defined at all of the following overridden methods: " +
								methodsWithParameterConstraints
				);
			}
			ConstrainedMethod constrainedMethod = methodsWithParameterConstraints.iterator().next();
			for ( ConstrainedMethod oneMethod :  ) {
				if ( !constrainedMethod.getLocation().getBeanClass()
						.isAssignableFromoneMethod.getLocation().getBeanClass() ) ) {
							"Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints. " +
									"The following method itself has no parameter constraints but it is not defined on a sub-type of " +
									constrainedMethod.getLocation().getBeanClass() + ": " + oneMethod
					);
				}
			}
			return null;
		}

Returns a set with those methods from the given pile of methods that have at least one constrained parameter or at least one parameter annotated with javax.validation.Valid.

Parameters:
methods The methods to search in.
Returns:
A set with constrained methods. May be empty, but never null.
			for ( ConstrainedMethod oneMethod : methods ) {
				if ( oneMethod.hasParameterConstraints() ) {
					theValue.addoneMethod );
				}
			}
			return theValue;
		}
	}

Checks the parameter constraints of this method for correctness.

The following rules apply for this check:

  • Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints in order to avoid the strengthening of a method's preconditions by additional parameter constraints defined at sub-types. If the root method itself has no parameter constraints, also no parameter constraints may be added in sub-types.
  • If there are multiple root methods for an method in an inheritance hierarchy (e.g. by implementing two interfaces defining the same method) no parameter constraints for this method are allowed at all in order to avoid a strengthening of a method's preconditions in parallel types.

Throws:
javax.validation.ConstraintDeclarationException In case the represented method has an illegal parameter constraint.
		}
	}

Returns meta data for the specified parameter of the represented method.

Parameters:
parameterIndex the index of the parameter
Returns:
Meta data for the specified parameter. Will never be null.
	public ParameterMetaData getParameterMetaData(int parameterIndex) {
		if ( parameterIndex < 0 || parameterIndex > .size() - 1 ) {
			throw .getInvalidMethodParameterIndexExceptiongetName(), parameterIndex );
		}
		return .getparameterIndex );
	}

Returns meta data for all parameters of the represented method.

Returns:
A list with parameter meta data. The length corresponds to the number of parameters of the method represented by this meta data object, so an empty list may be returned (in case of a parameterless method), but never null.
	}
	public Class<?>[] getParameterTypes() {
	}
	public MethodDescriptor asDescriptor(boolean defaultGroupSequenceRedefinedList<Class<?>> defaultGroupSequence) {
				parametersAsDescriptorsdefaultGroupSequenceRedefineddefaultGroupSequence ),
				defaultGroupSequenceRedefined,
				defaultGroupSequence
		);
	}
	private List<ParameterDescriptorparametersAsDescriptors(boolean defaultGroupSequenceRedefinedList<Class<?>> defaultGroupSequence) {
		for ( ParameterMetaData oneParameter :  ) {
			theValue.addoneParameter.asDescriptordefaultGroupSequenceRedefineddefaultGroupSequence ) );
		}
		return theValue;
	}
	public String toString() {
		StringBuilder parameterBuilder = new StringBuilder();
		for ( Class<?> oneParameterType : getParameterTypes() ) {
			parameterBuilder.appendoneParameterType.getSimpleName() );
			parameterBuilder.append", " );
		}
		String parameters =
				parameterBuilder.length() > 0 ?
						parameterBuilder.substring( 0, parameterBuilder.length() - 2 ) :
						parameterBuilder.toString();
		return "MethodMetaData [method=" + getType().getSimpleName() + " " + getName() + "(" + parameters + "), isCascading=" + isCascading() + ", isConstrained="
isConstrained() + "]";
	}
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + Arrays.hashCode );
		return result;
	}
	public boolean equals(Object obj) {
		if ( this == obj ) {
			return true;
		}
		if ( !super.equalsobj ) ) {
			return false;
		}
		if ( getClass() != obj.getClass() ) {
			return false;
		}
		MethodMetaData other = (MethodMetaDataobj;
		if ( !Arrays.equalsother.parameterTypes ) ) {
			return false;
		}
		return true;
	}
New to GrepCode? Check out our FAQ X