001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 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: *******************************************************************************/package net.sf.oval.configuration.annotation;
013:
014: import java.lang.annotation.Annotation;
015: import java.lang.reflect.Method;
016:
017: import net.sf.oval.AbstractCheck;
018: import net.sf.oval.internal.Log;
019:
020: /**
021: * Partial implementation of check classes configurable via annotations.
022: *
023: * @author Sebastian Thomschke
024: */
025: public abstract class AbstractAnnotationCheck<ConstraintAnnotation extends Annotation>
026: extends AbstractCheck implements
027: AnnotationCheck<ConstraintAnnotation> {
028: private final static Log LOG = Log
029: .getLog(AbstractAnnotationCheck.class);
030:
031: public void configure(
032: final ConstraintAnnotation constraintAnnotation) {
033: final Class<?> constraintClazz = constraintAnnotation
034: .getClass();
035:
036: /*
037: * Retrieve the message value from the constraint annotation via reflection.
038: * Using reflection is required because annotations do not support inheritance and
039: * therefore cannot implement an interface that could be used for a down cast here.
040: */
041: try {
042: final Method getMessage = constraintClazz
043: .getDeclaredMethod("message", (Class<?>[]) null);
044: message = (String) getMessage.invoke(constraintAnnotation,
045: (Object[]) null);
046: } catch (final Exception e) {
047: LOG
048: .debug(
049: "Cannot determine constraint error message based on annotation {}",
050: constraintClazz.getName(), e);
051: message = constraintClazz.getName() + ".violated";
052: }
053:
054: /*
055: * Retrieve the error code value from the constraint annotation via reflection.
056: */
057: try {
058: final Method getErrorCode = constraintClazz
059: .getDeclaredMethod("errorCode", (Class<?>[]) null);
060: errorCode = (String) getErrorCode.invoke(
061: constraintAnnotation, (Object[]) null);
062: } catch (final Exception e) {
063: LOG
064: .debug(
065: "Cannot determine constraint error code based on annotation {}",
066: constraintClazz.getName(), e);
067: errorCode = constraintClazz.getName();
068: }
069:
070: /*
071: * Retrieve the severity value from the constraint annotation via reflection.
072: */
073: try {
074: final Method getSeverity = constraintClazz
075: .getDeclaredMethod("severity", (Class<?>[]) null);
076: severity = ((Number) getSeverity.invoke(
077: constraintAnnotation, (Object[]) null)).intValue();
078: } catch (final Exception e) {
079: LOG
080: .debug(
081: "Cannot determine constraint severity based on annotation {}",
082: constraintClazz.getName(), e);
083: }
084:
085: /*
086: * Retrieve the profiles value from the constraint annotation via reflection.
087: */
088: try {
089: final Method getProfiles = constraintClazz
090: .getDeclaredMethod("profiles", (Class<?>[]) null);
091: profiles = (String[]) getProfiles.invoke(
092: constraintAnnotation, (Object[]) null);
093: } catch (final Exception e) {
094: LOG
095: .debug(
096: "Cannot determine constraint profiles based on annotation {}",
097: constraintClazz.getName(), e);
098: }
099: }
100: }
|