001package eu.cqse.check.framework.core.option;
002
003import java.lang.reflect.Field;
004
005import org.conqat.lib.commons.assertion.CCSMAssert;
006import org.conqat.lib.commons.reflect.ReflectionUtils;
007
008import eu.cqse.check.framework.core.CheckImplementationBase;
009
010/**
011 * An option of custom check. Points to an attribute of the check, that
012 * represents the option. Contains an optional default value, but no concrete
013 * set in an analysis profile.
014 *
015 * Concrete values are loaded from a CheckOptionStore in
016 * CheckProcessor.initializeChecks().
017 */
018public class CheckOptionWrapper<T> {
019
020        /** The annotation that marks the option's field. */
021        private final CheckOption annotation;
022
023        /** The option field. */
024        private final Field field;
025
026        /** The option's default value. */
027        private final T defaultValue;
028
029        /** The option's type. */
030        private final Class<T> type;
031
032        /** Constructor. */
033        public CheckOptionWrapper(CheckOption annotation, Field field, T defaultValue, Class<T> type) {
034                CCSMAssert.isNotNull(annotation);
035                CCSMAssert.isNotNull(field);
036                CCSMAssert.isTrue(ReflectionUtils.isAssignable(type, field.getType()),
037                                "The given field must be assignable from the given type.");
038                this.annotation = annotation;
039                this.field = field;
040                this.defaultValue = defaultValue;
041                this.type = type;
042        }
043
044        /** Returns the option's name. */
045        public String getName() {
046                return annotation.name();
047        }
048
049        /** Returns the option's description. */
050        public String getDescription() {
051                return annotation.description();
052        }
053
054        /** Returns whether this option is multiline text. */
055        public boolean isMultilineText() {
056                return annotation.multilineText();
057        }
058
059        /** Returns the option's field. */
060        public Field getField() {
061                return field;
062        }
063
064        /** Returns the option's type. */
065        public Class<T> getType() {
066                return type;
067        }
068
069        /** Returns the option's default value. */
070        public T getDefaultValue() {
071                return defaultValue;
072        }
073
074        /**
075         * Set the option to the given value in the given check implementation instance.
076         */
077        public void setOption(CheckImplementationBase implementation, T value) {
078                try {
079                        field.set(implementation, value);
080                } catch (IllegalArgumentException | IllegalAccessException e) {
081                        CCSMAssert.fail("Failed to set option: " + e.getMessage());
082                }
083        }
084}