eu.cqse.check.base.UnwantedExpressionInCommentCheckBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-check-api Show documentation
Show all versions of teamscale-check-api Show documentation
The Teamscale Custom Check API allows users to extend Teamscale by writing custom analyses that create findings.
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.cqse.check.base;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import eu.cqse.check.framework.core.CheckException;
import eu.cqse.check.framework.scanner.IToken;
/**
* Base class for checks which find unwanted expressions in any comment in a resource.
*/
public abstract class UnwantedExpressionInCommentCheckBase extends CommentCheckBase {
/** Stores the unwanted expression regex. */
private Pattern unwantedRegex;
/** {@inheritDoc} */
@Override
public void initialize() throws CheckException {
super.initialize();
unwantedRegex = getUnwantedRegex();
}
/** {@inheritDoc} */
@Override
protected void processComment(IToken token, int startLine, int endLine) throws CheckException {
Matcher commentMatcher = unwantedRegex.matcher(token.getText());
// The pattern might appear anywhere in the token text. Therefore, we
// use find() instead of matches() to search for it.
if (commentMatcher.find()) {
buildFinding(getFindingText(token, commentMatcher.toMatchResult()),
buildLocation().betweenLines(startLine, endLine)).createAndStore();
}
}
/**
* Provides the Regex pattern that specifies which expressions are unwanted in comments. Note that
* this method is only called once at the initialization of the check for performance reasons.
*/
protected abstract Pattern getUnwantedRegex();
/**
* Gets the text for the finding.
*
* @param token
* The token where the unwanted pattern was found.
* @param matchResult
* The {@link MatchResult} which allows providing more details in the findings text.
* @return The finding text.
*/
protected abstract String getFindingText(IToken token, MatchResult matchResult);
}