Skip to content

Commit

Permalink
HV-807 Allowing to ignore constraint annotations for methods/construc…
Browse files Browse the repository at this point in the history
…tors via API
  • Loading branch information
gunnarmorling authored and hferentschik committed Sep 24, 2015
1 parent a236fca commit 03faf18
Show file tree
Hide file tree
Showing 18 changed files with 473 additions and 27 deletions.
@@ -0,0 +1,25 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.cfg.context;

/**
* Facet of a constraint mapping creational context which allows to configure how existing annotation should be treated.
*
* @author Gunnar Morling
*/
public interface AnnotationIgnoreOptions<C extends AnnotationIgnoreOptions<C>> {

/**
* Specifies whether annotations at the given element should be ignored or not, overriding any setting given for
* parent elements. E.g. the setting given for a method parameter overrides the setting given for the method
* declaring that parameter.
*
* @param ignoreAnnotations Whether to ignore annotation-based constraints or not.
* @return This context for method chaining.
*/
C ignoreAnnotations(boolean ignoreAnnotations);
}
Expand Up @@ -13,10 +13,13 @@
* @author Hardy Ferentschik
*/
public interface AnnotationProcessingOptions<C extends AnnotationProcessingOptions<C>> {

/**
* Specifies that annotations specified on the configured type or property should be ignored.
*
* @return Returns itself for method chaining.
* @deprecated Use {@link AnnotationIgnoreOptions#ignoreAnnotations(boolean)} instead.
*/
@Deprecated
C ignoreAnnotations();
}
Expand Up @@ -12,6 +12,6 @@
*
* @author Gunnar Morling
*/
public interface ConstructorConstraintMappingContext extends ParameterTarget, CrossParameterTarget, ReturnValueTarget {
public interface ConstructorConstraintMappingContext extends ParameterTarget, CrossParameterTarget, ReturnValueTarget, AnnotationIgnoreOptions<ConstructorConstraintMappingContext> {

}
Expand Up @@ -13,5 +13,5 @@
* @author Gunnar Morling
*/
public interface CrossParameterConstraintMappingContext
extends TypeTarget, ConstructorTarget, MethodTarget, ParameterTarget, ReturnValueTarget, Constrainable<CrossParameterConstraintMappingContext> {
extends TypeTarget, ConstructorTarget, MethodTarget, ParameterTarget, ReturnValueTarget, Constrainable<CrossParameterConstraintMappingContext>, AnnotationIgnoreOptions<CrossParameterConstraintMappingContext> {
}
Expand Up @@ -12,5 +12,5 @@
*
* @author Gunnar Morling
*/
public interface MethodConstraintMappingContext extends ParameterTarget, CrossParameterTarget, ReturnValueTarget {
public interface MethodConstraintMappingContext extends ParameterTarget, CrossParameterTarget, ReturnValueTarget, AnnotationIgnoreOptions<MethodConstraintMappingContext> {
}
Expand Up @@ -16,5 +16,6 @@
*/
public interface ParameterConstraintMappingContext extends TypeTarget, CrossParameterTarget, ParameterTarget,
ReturnValueTarget, ConstructorTarget, MethodTarget, Constrainable<ParameterConstraintMappingContext>,
Cascadable<ParameterConstraintMappingContext>, Unwrapable<ParameterConstraintMappingContext> {
Cascadable<ParameterConstraintMappingContext>, Unwrapable<ParameterConstraintMappingContext>,
AnnotationIgnoreOptions<ParameterConstraintMappingContext> {
}
Expand Up @@ -21,5 +21,6 @@ public interface PropertyConstraintMappingContext extends Constrainable<Property
MethodTarget,
Cascadable<PropertyConstraintMappingContext>,
Unwrapable<PropertyConstraintMappingContext>,
AnnotationProcessingOptions<PropertyConstraintMappingContext> {
AnnotationProcessingOptions<PropertyConstraintMappingContext>,
AnnotationIgnoreOptions<PropertyConstraintMappingContext> {
}
Expand Up @@ -15,6 +15,6 @@
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
*/
public interface ReturnValueConstraintMappingContext
extends TypeTarget, ParameterTarget, CrossParameterTarget, ConstructorTarget, MethodTarget, Constrainable<ReturnValueConstraintMappingContext>, Cascadable<ReturnValueConstraintMappingContext>, Unwrapable<ReturnValueConstraintMappingContext> {
extends TypeTarget, ParameterTarget, CrossParameterTarget, ConstructorTarget, MethodTarget, Constrainable<ReturnValueConstraintMappingContext>, Cascadable<ReturnValueConstraintMappingContext>, Unwrapable<ReturnValueConstraintMappingContext>, AnnotationIgnoreOptions<ReturnValueConstraintMappingContext> {

}
Expand Up @@ -23,7 +23,8 @@ public interface TypeConstraintMappingContext<C> extends Constrainable<TypeConst
PropertyTarget,
MethodTarget,
ConstructorTarget,
AnnotationProcessingOptions<TypeConstraintMappingContext<C>> {
AnnotationProcessingOptions<TypeConstraintMappingContext<C>>,
AnnotationIgnoreOptions<TypeConstraintMappingContext<C>> {

/**
* Defines that all annotations for this type should be ignored.
Expand Down
@@ -0,0 +1,32 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.internal.cfg.context;

import java.lang.reflect.Constructor;

import org.hibernate.validator.cfg.context.ConstructorConstraintMappingContext;

/**
* Constraint mapping creational context representing a constructor.
*
* @author Gunnar Morling
*/
class ConstructorConstraintMappingContextImpl extends ExecutableConstraintMappingContextImpl implements ConstructorConstraintMappingContext {

<T> ConstructorConstraintMappingContextImpl(TypeConstraintMappingContextImpl<T> typeContext, Constructor<T> constructor) {
super( typeContext, constructor );
}

@Override
public ConstructorConstraintMappingContext ignoreAnnotations(boolean ignoreAnnotations) {
typeContext.mapping
.getAnnotationProcessingOptions()
.ignoreConstraintAnnotationsOnMember( executable.getMember(), ignoreAnnotations );

return this;
}
}
Expand Up @@ -36,6 +36,14 @@ public CrossParameterConstraintMappingContext constraint(ConstraintDef<?, ?> def
return this;
}

@Override
public CrossParameterConstraintMappingContext ignoreAnnotations(boolean ignoreAnnotations) {
mapping.getAnnotationProcessingOptions().ignoreConstraintAnnotationsForCrossParameterConstraint(
executableContext.getExecutable().getMember(), ignoreAnnotations
);
return this;
}

@Override
public ParameterConstraintMappingContext parameter(int index) {
return executableContext.parameter( index );
Expand Down
Expand Up @@ -13,9 +13,7 @@

import javax.validation.ParameterNameProvider;

import org.hibernate.validator.cfg.context.ConstructorConstraintMappingContext;
import org.hibernate.validator.cfg.context.CrossParameterConstraintMappingContext;
import org.hibernate.validator.cfg.context.MethodConstraintMappingContext;
import org.hibernate.validator.cfg.context.ParameterConstraintMappingContext;
import org.hibernate.validator.cfg.context.ReturnValueConstraintMappingContext;
import org.hibernate.validator.internal.engine.valuehandling.UnwrapMode;
Expand All @@ -40,18 +38,17 @@
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
* @author Gunnar Morling
*/
class ExecutableConstraintMappingContextImpl
implements ConstructorConstraintMappingContext, MethodConstraintMappingContext {
abstract class ExecutableConstraintMappingContextImpl {

private static final Log log = LoggerFactory.make();

private final TypeConstraintMappingContextImpl<?> typeContext;
private final ExecutableElement executable;
protected final TypeConstraintMappingContextImpl<?> typeContext;
protected final ExecutableElement executable;
private final ParameterConstraintMappingContextImpl[] parameterContexts;
private ReturnValueConstraintMappingContextImpl returnValueContext;
private CrossParameterConstraintMappingContextImpl crossParameterContext;

ExecutableConstraintMappingContextImpl(TypeConstraintMappingContextImpl<?> typeContext, Constructor<?> constructor) {
<T> ExecutableConstraintMappingContextImpl(TypeConstraintMappingContextImpl<T> typeContext, Constructor<T> constructor) {
this( typeContext, ExecutableElement.forConstructor( constructor ) );
}

Expand All @@ -65,7 +62,6 @@ private ExecutableConstraintMappingContextImpl(TypeConstraintMappingContextImpl<
this.parameterContexts = new ParameterConstraintMappingContextImpl[executable.getParameterTypes().length];
}

@Override
public ParameterConstraintMappingContext parameter(int index) {
if ( index < 0 || index >= executable.getParameterTypes().length ) {
throw log.getInvalidExecutableParameterIndexException( executable.getAsString(), index );
Expand All @@ -86,7 +82,6 @@ public ParameterConstraintMappingContext parameter(int index) {
return context;
}

@Override
public CrossParameterConstraintMappingContext crossParameter() {
if ( crossParameterContext != null ) {
throw log.getCrossParameterElementHasAlreadyBeConfiguredViaProgrammaticApiException(
Expand All @@ -99,7 +94,6 @@ public CrossParameterConstraintMappingContext crossParameter() {
return crossParameterContext;
}

@Override
public ReturnValueConstraintMappingContext returnValue() {
if ( returnValueContext != null ) {
throw log.getReturnValueHasAlreadyBeConfiguredViaProgrammaticApiException(
Expand Down
@@ -0,0 +1,32 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.internal.cfg.context;

import java.lang.reflect.Method;

import org.hibernate.validator.cfg.context.MethodConstraintMappingContext;

/**
* Constraint mapping creational context representing a method.
*
* @author Gunnar Morling
*/
class MethodConstraintMappingContextImpl extends ExecutableConstraintMappingContextImpl implements MethodConstraintMappingContext {

MethodConstraintMappingContextImpl(TypeConstraintMappingContextImpl<?> typeContext, Method method) {
super( typeContext, method );
}

@Override
public MethodConstraintMappingContext ignoreAnnotations(boolean ignoreAnnotations) {
typeContext.mapping
.getAnnotationProcessingOptions()
.ignoreConstraintAnnotationsOnMember( executable.getMember(), ignoreAnnotations );

return this;
}
}
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.validator.internal.cfg.context;

import java.util.Collections;

import javax.validation.ParameterNameProvider;

import org.hibernate.validator.cfg.ConstraintDef;
Expand Down Expand Up @@ -61,6 +62,16 @@ public ParameterConstraintMappingContext constraint(ConstraintDef<?, ?> definiti
return this;
}

@Override
public ParameterConstraintMappingContext ignoreAnnotations(boolean ignoreAnnotations) {
mapping.getAnnotationProcessingOptions().ignoreConstraintAnnotationsOnParameter(
executableContext.getExecutable().getMember(),
parameterIndex,
ignoreAnnotations
);
return this;
}

@Override
public ParameterConstraintMappingContext parameter(int index) {
return executableContext.parameter( index );
Expand Down
Expand Up @@ -72,7 +72,12 @@ public PropertyConstraintMappingContext constraint(ConstraintDef<?, ?> definitio

@Override
public PropertyConstraintMappingContext ignoreAnnotations() {
mapping.getAnnotationProcessingOptions().ignoreConstraintAnnotationsOnMember( member, true );
return ignoreAnnotations( true );
}

@Override
public PropertyConstraintMappingContext ignoreAnnotations(boolean ignoreAnnotations) {
mapping.getAnnotationProcessingOptions().ignoreConstraintAnnotationsOnMember( member, ignoreAnnotations );
return this;
}

Expand Down
Expand Up @@ -43,6 +43,14 @@ public ReturnValueConstraintMappingContext constraint(ConstraintDef<?, ?> defini
return this;
}

@Override
public ReturnValueConstraintMappingContext ignoreAnnotations(boolean ignoreAnnotations) {
mapping.getAnnotationProcessingOptions().ignoreConstraintAnnotationsForReturnValue(
executableContext.getExecutable().getMember(), ignoreAnnotations
);
return this;
}

@Override
public ParameterConstraintMappingContext parameter(int index) {
return executableContext.parameter( index );
Expand Down
Expand Up @@ -83,7 +83,12 @@ public TypeConstraintMappingContext<C> constraint(ConstraintDef<?, ?> definition

@Override
public TypeConstraintMappingContext<C> ignoreAnnotations() {
mapping.getAnnotationProcessingOptions().ignoreClassLevelConstraintAnnotations( beanClass, Boolean.TRUE );
return ignoreAnnotations( true );
}

@Override
public TypeConstraintMappingContext<C> ignoreAnnotations(boolean ignoreAnnotations) {
mapping.getAnnotationProcessingOptions().ignoreClassLevelConstraintAnnotations( beanClass, ignoreAnnotations );
return this;
}

Expand Down Expand Up @@ -153,7 +158,7 @@ public MethodConstraintMappingContext method(String name, Class<?>... parameterT
);
}

ExecutableConstraintMappingContextImpl context = new ExecutableConstraintMappingContextImpl( this, method );
MethodConstraintMappingContextImpl context = new MethodConstraintMappingContextImpl( this, method );
configuredMembers.add( method );
executableContexts.add( context );

Expand All @@ -178,7 +183,7 @@ public ConstructorConstraintMappingContext constructor(Class<?>... parameterType
);
}

ExecutableConstraintMappingContextImpl context = new ExecutableConstraintMappingContextImpl(
ConstructorConstraintMappingContextImpl context = new ConstructorConstraintMappingContextImpl(
this,
constructor
);
Expand Down

0 comments on commit 03faf18

Please sign in to comment.