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

eu.cqse.check.framework.util.clike.ConditionalBlockUtils 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.framework.util.clike;

import java.util.EnumSet;
import java.util.List;
import java.util.stream.Collectors;

import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.UnmodifiableSet;

import eu.cqse.check.framework.scanner.ETokenType;
import eu.cqse.check.framework.scanner.IToken;
import eu.cqse.check.framework.shallowparser.SubTypeNames;
import eu.cqse.check.framework.shallowparser.framework.ShallowEntity;

/**
 * This utility class provides methods that are used when working with blocks.
 */
public class ConditionalBlockUtils {

	/** The blocks that have a condition. */
	public static final UnmodifiableSet BLOCKS_WITH_CONDITION = CollectionUtils
			.asUnmodifiable(EnumSet.of(EConditionalType.IF, EConditionalType.ELSE_IF, EConditionalType.CASE));

	/** The blocks that do not have a condition. */
	public static final UnmodifiableSet BLOCKS_WITHOUT_CONDITION = CollectionUtils
			.asUnmodifiable(EnumSet.of(EConditionalType.SWITCH, EConditionalType.ELSE, EConditionalType.DEFAULT));

	/** The blocks that correspond to an if block. */
	public static final UnmodifiableSet BLOCKS_CORRESPONDING_TO_IF = CollectionUtils
			.asUnmodifiable(EnumSet.of(EConditionalType.ELSE_IF, EConditionalType.ELSE));

	/** The blocks that correspond to a switch block. */
	public static final UnmodifiableSet BLOCKS_CORRESPONDING_TO_SWITCH = CollectionUtils
			.asUnmodifiable(EnumSet.of(EConditionalType.CASE, EConditionalType.DEFAULT));

	/** The token types that get sorted out when extracting the condition */
	public static final UnmodifiableSet UNWANTED_TOKEN_TYPES_IN_CONDITION = CollectionUtils
			.asUnmodifiable(EnumSet.of(ETokenType.SWITCH, ETokenType.LBRACE, ETokenType.IF, ETokenType.ELSEIF,
					ETokenType.CASE, ETokenType.DOUBLE_DOT, ETokenType.COLON));

	/**
	 * Extracts the {@link EConditionalType} from the given {@link ShallowEntity}.
	 */
	public static EConditionalType getBlockTypeFromEntity(ShallowEntity entity) throws IllegalArgumentException {
		return getBlockTypeFromEntitySubtype(entity.getSubtype());
	}

	/**
	 * Converts the Subtype of an entity represented by a string to the corresponding
	 * {@link EConditionalType}.
	 */
	public static EConditionalType getBlockTypeFromEntitySubtype(String subtype) throws IllegalArgumentException {
		/*
		 * The fall-through in this switch construct is intended to clarify which {@link #SubTypeNames}
		 * corresponds to which {@link #EConditionalType}.
		 */
		switch (subtype) {
		case SubTypeNames.IF:
		case SubTypeNames.IF_LET:
		case SubTypeNames.IF_VAR:
			return EConditionalType.IF;
		case SubTypeNames.ELIF:
		case SubTypeNames.ELSIF:
		case SubTypeNames.ELSE_IF:
		case SubTypeNames.ELSE_IF_NOSPACE:
			return EConditionalType.ELSE_IF;
		case SubTypeNames.ELSE:
			return EConditionalType.ELSE;
		case SubTypeNames.SWITCH:
			return EConditionalType.SWITCH;
		case SubTypeNames.CASE:
			return EConditionalType.CASE;
		case SubTypeNames.DEFAULT:
			return EConditionalType.DEFAULT;
		default:
			return null;
		}
	}

	/**
	 * Extracts the tokens describing the condition out of the given list of tokens.
	 */
	public static List extractCondition(ShallowEntity entity) {
		List tokens = getHeaderTokens(entity);
		return CollectionUtils.filter(tokens, token -> !UNWANTED_TOKEN_TYPES_IN_CONDITION.contains(token.getType())
				&& token.getLineNumber() == tokens.get(0).getLineNumber());
	}

	/** Extracts the header tokens from the entity. */
	private static List getHeaderTokens(ShallowEntity entity) {
		int startTokenIndex = entity.getStartOffset();
		int endTokenIndex;
		if (entity.getChildren().isEmpty()) {
			List includedTokens = entity.includedTokens();
			endTokenIndex = includedTokens.get(includedTokens.size() - 1).getOffset();
		} else {
			endTokenIndex = entity.getChildren().get(0).getStartOffset();
		}
		return entity.includedTokens().stream()
				.filter(token -> token.getOffset() > startTokenIndex && token.getEndOffset() < endTokenIndex)
				.collect(Collectors.toList());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy