View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.configuration.annotation;
14  
15  import java.lang.annotation.Annotation;
16  import java.lang.reflect.Method;
17  
18  import net.sf.oval.AbstractCheckExclusion;
19  import net.sf.oval.internal.Log;
20  
21  /**
22   * Partial implementation of check exclusion classes configurable via annotations.
23   * 
24   * @author Sebastian Thomschke
25   */
26  public abstract class AbstractAnnotationCheckExclusion<ExclusionAnnotation extends Annotation>
27  		extends
28  			AbstractCheckExclusion implements AnnotationCheckExclusion<ExclusionAnnotation>
29  {
30  	private static final long serialVersionUID = 1L;
31  
32  	private static final Log LOG = Log.getLog(AbstractAnnotationCheckExclusion.class);
33  
34  	/**
35  	 * {@inheritDoc}
36  	 */
37  	public void configure(final ExclusionAnnotation exclusionAnnotation)
38  	{
39  		final Class< ? > exclusionClazz = exclusionAnnotation.getClass();
40  
41  		/*
42  		 * Retrieve the profiles value from the constraint exclusion annotation via reflection.
43  		 */
44  		try
45  		{
46  			final Method getProfiles = exclusionClazz.getDeclaredMethod("profiles", (Class< ? >[]) null);
47  			setProfiles((String[]) getProfiles.invoke(exclusionAnnotation, (Object[]) null));
48  		}
49  		catch (final Exception e)
50  		{
51  			LOG.debug("Cannot determine constraint profiles based on annotation {1}", exclusionClazz.getName(), e);
52  		}
53  
54  		/*
55  		 * Retrieve the when formula from the constraint exclusion annotation via reflection.
56  		 */
57  		try
58  		{
59  			final Method getWhen = exclusionClazz.getDeclaredMethod("when", (Class< ? >[]) null);
60  			setWhen((String) getWhen.invoke(exclusionClazz, (Object[]) null));
61  		}
62  		catch (final Exception e)
63  		{
64  			LOG.debug("Cannot determine constraint when formula based on annotation {1}", exclusionClazz.getName(), e);
65  		}
66  	}
67  }