001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.configuration.annotation;
014    
015    import java.lang.annotation.Annotation;
016    import java.lang.reflect.Method;
017    
018    import net.sf.oval.AbstractCheckExclusion;
019    import net.sf.oval.internal.Log;
020    
021    /**
022     * Partial implementation of check exclusion classes configurable via annotations.
023     * 
024     * @author Sebastian Thomschke
025     */
026    public abstract class AbstractAnnotationCheckExclusion<ExclusionAnnotation extends Annotation>
027                    extends
028                            AbstractCheckExclusion implements AnnotationCheckExclusion<ExclusionAnnotation>
029    {
030            private static final long serialVersionUID = 1L;
031    
032            private static final Log LOG = Log.getLog(AbstractAnnotationCheckExclusion.class);
033    
034            /**
035             * {@inheritDoc}
036             */
037            public void configure(final ExclusionAnnotation exclusionAnnotation)
038            {
039                    final Class< ? > exclusionClazz = exclusionAnnotation.getClass();
040    
041                    /*
042                     * Retrieve the profiles value from the constraint exclusion annotation via reflection.
043                     */
044                    try
045                    {
046                            final Method getProfiles = exclusionClazz.getDeclaredMethod("profiles", (Class< ? >[]) null);
047                            setProfiles((String[]) getProfiles.invoke(exclusionAnnotation, (Object[]) null));
048                    }
049                    catch (final Exception e)
050                    {
051                            LOG.debug("Cannot determine constraint profiles based on annotation {1}", exclusionClazz.getName(), e);
052                    }
053    
054                    /*
055                     * Retrieve the when formula from the constraint exclusion annotation via reflection.
056                     */
057                    try
058                    {
059                            final Method getWhen = exclusionClazz.getDeclaredMethod("when", (Class< ? >[]) null);
060                            setWhen((String) getWhen.invoke(exclusionClazz, (Object[]) null));
061                    }
062                    catch (final Exception e)
063                    {
064                            LOG.debug("Cannot determine constraint when formula based on annotation {1}", exclusionClazz.getName(), e);
065                    }
066            }
067    }