001package eu.cqse.check.framework.core;
002
003import java.util.Arrays;
004import java.util.EnumSet;
005import java.util.LinkedHashMap;
006import java.util.Set;
007
008import org.conqat.lib.commons.assertion.CCSMAssert;
009import org.conqat.lib.commons.collections.CollectionUtils;
010import org.conqat.lib.commons.collections.UnmodifiableMap;
011
012import eu.cqse.check.framework.core.option.CheckOptionWrapper;
013import eu.cqse.check.framework.core.phase.IGlobalExtractionPhase;
014import eu.cqse.check.framework.core.util.CheckUtils;
015import eu.cqse.check.framework.scanner.ELanguage;
016
017/**
018 * A check info contains information about a custom check.
019 */
020public class CheckInfo {
021
022        /** The check's class. */
023        private final Class<?> checkClass;
024
025        /** The name. */
026        private final String name;
027
028        /** The description. */
029        private final String description;
030
031        /** The analysis group name. */
032        private final String groupName;
033
034        /** The analysis category name. */
035        private final String categoryName;
036
037        /** The default enablement. */
038        private final EFindingEnablement defaultEnablement;
039
040        /** The supported languages. */
041        private final Set<ELanguage> languages;
042
043        /** The parameters that must be provided to this check. */
044        private final Set<ECheckParameter> parameters;
045
046        /**
047         * The options that can be configured.
048         *
049         * This is explicitly a {@link LinkedHashMap} since that preserves the order of
050         * options (the order in which they are defined in the custom check). A generic
051         * {@link java.util.Map} does not ensure this property.
052         */
053        private final LinkedHashMap<String, CheckOptionWrapper<?>> options;
054
055        /** The phases needed for the check. */
056        private final Set<Class<? extends IGlobalExtractionPhase<?, ?>>> phases;
057
058        /**
059         * Creates a new check info with the given parameters. None of them must be
060         * null.
061         */
062        public CheckInfo(Class<?> checkClass, String name, String description, String groupName, String categoryName,
063                        EFindingEnablement defaultEnablement, ELanguage[] languages, ECheckParameter[] parameters,
064                        Class<? extends IGlobalExtractionPhase<?, ?>>[] phases,
065                        LinkedHashMap<String, CheckOptionWrapper<?>> options) {
066                CCSMAssert.isNotNull(checkClass);
067                CCSMAssert.isNotNull(name);
068                CCSMAssert.isNotNull(description);
069                CCSMAssert.isNotNull(groupName);
070                CCSMAssert.isNotNull(categoryName);
071                CCSMAssert.isNotNull(defaultEnablement);
072                CCSMAssert.isNotNull(languages);
073                CCSMAssert.isNotNull(phases);
074                CCSMAssert.isNotNull(parameters);
075                CCSMAssert.isNotNull(options);
076
077                this.checkClass = checkClass;
078
079                this.name = name;
080                this.description = description;
081                this.groupName = groupName;
082                this.categoryName = categoryName;
083                this.defaultEnablement = defaultEnablement;
084                this.languages = CollectionUtils.asHashSet(languages);
085                this.phases = CollectionUtils.asHashSet(phases);
086                this.parameters = EnumSet.noneOf(ECheckParameter.class);
087                this.parameters.addAll(Arrays.asList(parameters));
088                this.options = new LinkedHashMap<>(options);
089        }
090
091        /** Creates a new instance of the underlying check implementation. */
092        public CheckImplementationBase instantiateCheckImplementation() throws CheckException {
093                return CheckImplementationBase.createInstance(checkClass);
094        }
095
096        /** Returns the name. */
097        public String getName() {
098                return name;
099        }
100
101        /** Returns the description. */
102        public String getDescription() {
103                return description;
104        }
105
106        /** Returns the analysis group name. */
107        public String getGroupName() {
108                return groupName;
109        }
110
111        /** @see #categoryName */
112        public String getCategoryName() {
113                return categoryName;
114        }
115
116        /** Returns the default enablement. */
117        public EFindingEnablement getDefaultEnablement() {
118                return defaultEnablement;
119        }
120
121        /** Returns the supported languages. */
122        public Set<ELanguage> getSupportedLanguages() {
123                return languages;
124        }
125
126        /** @see #phases */
127        public Set<Class<? extends IGlobalExtractionPhase<?, ?>>> getRequiredPhases() {
128                return phases;
129        }
130
131        /** Returns the parameters that must be provided to this check. */
132        public Set<ECheckParameter> getParameters() {
133                return parameters;
134        }
135
136        /**
137         * Returns all options that can be provided to this check.
138         *
139         * This is an {@link UnmodifiableMap} backed by a {@link LinkedHashMap} since
140         * that preserves the order of options (the order in which they are defined in
141         * the custom check).
142         */
143        public UnmodifiableMap<String, CheckOptionWrapper<?>> getOptions() {
144                return CollectionUtils.asUnmodifiable(options);
145        }
146
147        /** Returns a unique identifier for this check. */
148        public String getIdentifier() {
149                return CheckUtils.buildIdentifier(categoryName, groupName, name);
150        }
151
152        /** Returns the simple class name of the check. */
153        public String getSimpleClassName() {
154                return checkClass.getSimpleName();
155        }
156
157        /** Returns the full class name of the check. */
158        public String getCheckClassName() {
159                return checkClass.getName();
160        }
161}