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

org.hibernate.validation.metadata.AnnotationIgnores Maven / Gradle / Ivy

Go to download

Module repackaging of the Hibernate validator library and Validation API (JSR 303)

There is a newer version: 3.0-JBoss-4.0.2_03
Show newest version
// $Id: AnnotationIgnores.java 16798 2009-06-16 16:16:48Z hardy.ferentschik $
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.validation.metadata;

import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;

import org.hibernate.validation.util.LoggerFactory;

/**
 * This class instantiated during the parsing of the XML configuration data and keeps
 * track of the annotations which should be ignored.
 *
 * @author Hardy Ferentschik
 */
public class AnnotationIgnores {

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

	/**
	 * Keeps track whether the 'ignore-annotations' flag is set on bean level in the xml configuration. If 'ignore-annotations'
	 * is not specified false is the default.
	 */
	private final Map, Boolean> ignoreAnnotationDefaults = new HashMap, Boolean>();

	/**
	 * Keeps track of explicitly excluded members (fields and properties) for a given class. If a member appears in
	 * the list mapped to a given class 'ignore-annotations' was explicitly set to true in the configuration
	 * for this class.
	 */
	private final Map, List> ignoreAnnotationOnMember = new HashMap, List>();

	private final List> ignoreAnnotationOnClass = new ArrayList>();

	public void setDefaultIgnoreAnnotation(Class clazz, Boolean b) {
		if ( b == null ) {
			ignoreAnnotationDefaults.put( clazz, Boolean.FALSE );
		}
		else {
			ignoreAnnotationDefaults.put( clazz, b );
		}
	}

	public boolean getDefaultIgnoreAnnotation(Class clazz) {
		return ignoreAnnotationDefaults.containsKey( clazz ) && ignoreAnnotationDefaults.get( clazz );
	}

	public void setIgnoreAnnotationsOnMember(Member member) {
		Class beanClass = member.getDeclaringClass();
		if ( ignoreAnnotationOnMember.get( beanClass ) == null ) {
			List tmpList = new ArrayList();
			tmpList.add( member );
			ignoreAnnotationOnMember.put( beanClass, tmpList );
		}
		else {
			ignoreAnnotationOnMember.get( beanClass ).add( member );
		}
	}

	public boolean isIgnoreAnnotations(Member member) {
		boolean ignoreAnnotation;
		Class clazz = member.getDeclaringClass();
		List ignoreAnnotationForMembers = ignoreAnnotationOnMember.get( clazz );
		if ( ignoreAnnotationForMembers == null || !ignoreAnnotationForMembers.contains( member ) ) {
			ignoreAnnotation = getDefaultIgnoreAnnotation( clazz );
		}
		else {
			ignoreAnnotation = ignoreAnnotationForMembers.contains( member );
		}
		if ( ignoreAnnotation ) {
			logMessage( member, clazz );
		}
		return ignoreAnnotation;
	}

	private void logMessage(Member member, Class clazz) {
		String type;
		if ( member instanceof Field ) {
			type = "Field";
		}
		else {
			type = "Property";
		}
		log.debug( type + " level annotations are getting ignored for " + clazz.getName() + "." + member.getName() );
	}

	public void setIgnoreAnnotationsOnClass(Class clazz) {
		ignoreAnnotationOnClass.add( clazz );
	}

	public boolean isIgnoreAnnotations(Class clazz) {
		boolean ignoreAnnotation = ignoreAnnotationOnClass.contains( clazz ) || getDefaultIgnoreAnnotation( clazz );
		if ( log.isDebugEnabled() && ignoreAnnotation ) {
			log.debug( "Class level annotation are getting ignored for " + clazz.getName() );
		}
		return ignoreAnnotation;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy