All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.validator.internal.cfg.context.ConfiguredConstraint Maven / Gradle / Ivy

/*
* 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.cfg.context;

import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Map;

import org.hibernate.validator.cfg.ConstraintDef;
import org.hibernate.validator.internal.metadata.location.BeanConstraintLocation;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.metadata.location.ExecutableConstraintLocation;
import org.hibernate.validator.internal.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validator.internal.util.annotationfactory.AnnotationFactory;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

/**
 * Represents a programmatically configured constraint and meta-data
 * related to its location (bean type etc.).
 *
 * @author Gunnar Morling
 */
public class ConfiguredConstraint {

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

	private final ConstraintDefAccessor constraint;
	private final L location;

	private ConfiguredConstraint(ConstraintDef constraint, L location) {

		this.constraint = new ConstraintDefAccessor( constraint );
		this.location = location;
	}

	public static  ConfiguredConstraint forType(ConstraintDef constraint, Class beanType) {
		return new ConfiguredConstraint(
				constraint, new BeanConstraintLocation( beanType )
		);
	}

	public static  ConfiguredConstraint forProperty(ConstraintDef constraint, Member member) {

		return new ConfiguredConstraint(
				constraint, new BeanConstraintLocation( member )
		);
	}

	public static  ConfiguredConstraint forParameter(ConstraintDef constraint, Method method, int parameterIndex) {
		return new ConfiguredConstraint(
				constraint, new ExecutableConstraintLocation( method, parameterIndex )
		);
	}

	public static  ConfiguredConstraint forReturnValue(ConstraintDef constraint, Method method) {
		return new ConfiguredConstraint(
				constraint, new ExecutableConstraintLocation( method )
		);
	}

	public ConstraintDef getConstraint() {
		return constraint;
	}

	public L getLocation() {
		return location;
	}

	public Class getConstraintType() {
		return constraint.getConstraintType();
	}

	public Map getParameters() {
		return constraint.getParameters();
	}

	public A createAnnotationProxy() {

		AnnotationDescriptor annotationDescriptor = new AnnotationDescriptor( getConstraintType() );
		for ( Map.Entry parameter : getParameters().entrySet() ) {
			annotationDescriptor.setValue( parameter.getKey(), parameter.getValue() );
		}

		try {
			return AnnotationFactory.create( annotationDescriptor );
		}
		catch ( RuntimeException e ) {
			throw log.getUnableToCreateAnnotationForConfiguredConstraintException( e );
		}
	}

	/**
	 * Provides access to the members of a {@link ConstraintDef}.
	 */
	private static class ConstraintDefAccessor
			extends ConstraintDef, A> {

		private ConstraintDefAccessor(ConstraintDef original) {
			super( original );
		}

		private Class getConstraintType() {
			return constraintType;
		}

		private Map getParameters() {
			return parameters;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy