Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2009, Red Hat Middleware LLC, 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.ap.checks;
 
 import java.util.List;
 import java.util.Set;
 
 
 import static javax.lang.model.util.ElementFilter.methodsIn;

Checks, that each constraint annotation type declares the members message(), groups() and payload() as defined by the BV spec.

Author(s):
Gunnar Morling
 
 
 
 	private final Types typeUtils;
 
 	public AnnotationTypeMemberCheck(AnnotationApiHelper annotationApiHelperTypes typeUtils) {
 
 		this. = annotationApiHelper;
 		this. = typeUtils;
 	}
 
 
 		Set<ConstraintCheckErrortheValue = CollectionHelper.newHashSet();
 
 		theValue.addAllcheckMessageAttributeelement ) );
 		theValue.addAllcheckGroupsAttributeelement ) );
 		theValue.addAllcheckPayloadAttributeelement ) );
 
 		return theValue;
 	}

Checks that the given type element

  • has a method with name "message",
  • the return type of this method is java.lang.String.

Parameters:
element The element of interest.
Returns:
A possibly non-empty set of constraint check errors, never null.
 
 
 		ExecutableElement messageMethod = getMethodelement"message" );
 
 		if ( messageMethod == null ) {
 			return CollectionHelper.asSet(
 					new ConstraintCheckErrorelementnull"CONSTRAINT_TYPE_MUST_DECLARE_MESSAGE_MEMBER" )
 			);
 		}
 
 		) ) {
 			return CollectionHelper.asSet(
 					new ConstraintCheckErrormessageMethodnull"RETURN_TYPE_MUST_BE_STRING" )
 			);
 		}
 
		return Collections.emptySet();
	}

Checks that the given type element

  • has a method with name "groups",
  • the return type of this method is Class<?>[],
  • the default value of this method is {}.

Parameters:
element The element of interest.
Returns:
A possibly non-empty set of constraint check errors, never null.
		ExecutableElement groupsMethod = getMethodelement"groups" );
		if ( groupsMethod == null ) {
			return CollectionHelper.asSet(
					new ConstraintCheckErrorelementnull"CONSTRAINT_TYPE_MUST_DECLARE_GROUPS_MEMBER" )
			);
		}
		if ( type == null ) {
			return CollectionHelper.asSet(
					new ConstraintCheckErrorgroupsMethodnull"RETURN_TYPE_MUST_BE_CLASS_ARRAY" )
			);
		}
		boolean typeHasNameClass = type.asElement().getSimpleName().contentEquals"Class" );
		boolean typeHasExactlyOneTypeArgument = type.getTypeArguments().size() == 1;
		boolean typeArgumentIsUnboundWildcard = validateWildcardBoundstype.getTypeArguments().get( 0 ), nullnull );
		if ( !( typeHasNameClass && typeHasExactlyOneTypeArgument && typeArgumentIsUnboundWildcard ) ) {
			return CollectionHelper.asSet(
					new ConstraintCheckErrorgroupsMethodnull"RETURN_TYPE_MUST_BE_CLASS_ARRAY" )
			);
		}
		if ( !isEmptyArraygroupsMethod.getDefaultValue() ) ) {
			return CollectionHelper.asSet(
					new ConstraintCheckErrorgroupsMethodnull"DEFAULT_VALUE_MUST_BE_EMPTY_ARRAY" )
			);
		}
		return Collections.emptySet();
	}

Checks that the given type element

  • has a method with name "payload",
  • the return type of this method is Class<? extends Payload>[],
  • the default value of this method is {}.

Parameters:
element The element of interest.
Returns:
A possibly non-empty set of constraint check errors, never null.
		ExecutableElement payloadMethod = getMethodelement"payload" );
		if ( payloadMethod == null ) {
			return CollectionHelper.asSet(
					new ConstraintCheckErrorelementnull"CONSTRAINT_TYPE_MUST_DECLARE_PAYLOAD_MEMBER" )
			);
		}
		DeclaredType type = getComponentTypeOfArrayReturnTypepayloadMethod );
		if ( type == null ) {
			return CollectionHelper.asSet(
					new ConstraintCheckErrorpayloadMethodnull"PAYLOAD_RETURN_TYPE_MUST_BE_CLASS_ARRAY" )
			);
		}
		boolean typeHasNameClass = type.asElement().getSimpleName().contentEquals"Class" );
		boolean typeHasExactlyOneTypeArgument = type.getTypeArguments().size() == 1;
		boolean typeArgumentIsWildcardWithPayloadExtendsBound = validateWildcardBounds(
				type.getTypeArguments().get( 0 ),
				null
		);
		if ( !( typeHasNameClass && typeHasExactlyOneTypeArgument && typeArgumentIsWildcardWithPayloadExtendsBound ) ) {
			return CollectionHelper.asSet(
					new ConstraintCheckErrorpayloadMethodnull"PAYLOAD_RETURN_TYPE_MUST_BE_CLASS_ARRAY" )
			);
		}
		if ( !isEmptyArraypayloadMethod.getDefaultValue() ) ) {
			return CollectionHelper.asSet(
					new ConstraintCheckErrorpayloadMethodnull"PAYLOAD_DEFAULT_VALUE_MUST_BE_EMPTY_ARRAY" )
			);
		}
		return Collections.emptySet();
	}

Returns the method of the given type with the given name.

Parameters:
element The type of interest.
name The name of the method which should be returned.
Returns:
The method of the given type with the given name or null if no such method exists.
	private ExecutableElement getMethod(TypeElement elementString name) {
		for ( ExecutableElement oneMethod : methodsInelement.getEnclosedElements() ) ) {
			if ( oneMethod.getSimpleName().contentEqualsname ) ) {
				return oneMethod;
			}
		}
		return null;
	}

Returns the component type of the array-typed return value of the given method.

Parameters:
method The method of interest.
Returns:
The component type of the array-typed return value of the given method or null, if the given method has no array-typed return value.
		return method.getReturnType().accept(
										return t;
									}
								}, null
						);
					}
				}, null
		);
	}

Returns true, if the given type mirror is a wildcard type with the given extends and super bounds, false otherwise.

Parameters:
type The type to check.
expectedExtendsBound A mirror representing the expected extends bound.
expectedSuperBound A mirror representing the expected super bound.
Returns:
True, if the given type mirror is a wildcard type with the given extends and super bounds, false otherwise.
	private boolean validateWildcardBounds(TypeMirror typefinal TypeMirror expectedExtendsBoundfinal TypeMirror expectedSuperBound) {
		Boolean theValue = type.accept(
						boolean extendsBoundMatches = ( t.getExtendsBound() == null ? expectedExtendsBound == null : expectedExtendsBound != null && 
								.isSameTypet.getExtendsBound(), expectedExtendsBound ) );
						boolean superBoundMatches = ( t.getSuperBound() == null ? expectedSuperBound == null : expectedSuperBound != null && 
								.isSameTypet.getSuperBound(), expectedSuperBound ) );
						return extendsBoundMatches && superBoundMatches;
					}
				}, null
		);
		return ..equalstheValue );
	}

Checks whether the given annotation value is an empty array or not.

Parameters:
annotationValue The annotation value of interest.
Returns:
True, if the given annotation value is an empty array, false otherwise.
	private boolean isEmptyArray(AnnotationValue annotationValue) {
		return annotationValue != null && ..equals(
				annotationValue.accept(
							public Boolean visitArray(List<? extends AnnotationValuevaluesVoid p) {
								return values.size() == 0;
							}
						}, null
				)
		);
	}
New to GrepCode? Check out our FAQ X