All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.cqse.check.base.CommentCheckBase Maven / Gradle / Ivy

Go to download

The Teamscale Custom Check API allows users to extend Teamscale by writing custom analyses that create findings.

There is a newer version: 2024.7.2
Show newest version
/*
 * 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.List;

import eu.cqse.check.framework.core.CheckException;
import eu.cqse.check.framework.core.CheckImplementationBase;
import eu.cqse.check.framework.core.phase.ECodeViewOption;
import eu.cqse.check.framework.scanner.ETokenType.ETokenClass;
import eu.cqse.check.framework.scanner.IToken;
import eu.cqse.check.framework.util.tokens.TokenUtils;

/**
 * Base class for checks which inspects all comments in a resource.
 */
public abstract class CommentCheckBase extends CheckImplementationBase {

	/** {@inheritDoc} */
	@Override
	public void execute() throws CheckException {
		List tokens = context.getTokens(ECodeViewOption.FILTERED);
		for (int i = 0; i < tokens.size(); i++) {
			IToken token = tokens.get(i);

			if (token.getType().getTokenClass() == ETokenClass.COMMENT) {
				int commentEndLine = 0;

				if (i == tokens.size() - 1) {
					commentEndLine = TokenUtils.calculateEndLineByCountingLines(token);
				} else {
					// Estimate the end line for better performance whenever
					// possible.
					commentEndLine = TokenUtils.estimateEndLineByLookahead(token, tokens.get(i + 1));
				}

				processComment(token, token.getLineNumber() + 1, commentEndLine + 1);
			}
		}
	}

	/**
	 * When implemented in a subclass, processes the specified comment token, which runs from the
	 * provided start line to the provided end line.
	 *
	 * @param token
	 *            the comment token to process
	 * @param startLine
	 *            the 1-based start line of the comment token in the analyzed resource (inclusive).
	 * @param endLine
	 *            the 1-based end line of the comment token in the analyzed resource (inclusive).
	 */
	protected abstract void processComment(IToken token, int startLine, int endLine) throws CheckException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy