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

com.liferay.source.formatter.checkstyle.checks.PlusStatementCheck Maven / Gradle / Ivy

There is a newer version: 1.0.1437
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.source.formatter.checkstyle.checks;

import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.util.StringUtil;

import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

import java.util.List;

/**
 * @author Hugo Huijser
 */
public class PlusStatementCheck extends BaseStringConcatenationCheck {

	@Override
	public int[] getDefaultTokens() {
		return new int[] {TokenTypes.PLUS};
	}

	@Override
	protected void doVisitToken(DetailAST detailAST) {
		_checkPlusOperator(detailAST);
	}

	private void _checkLiteralStrings(
		DetailAST detailAST, DetailAST leftHandOperandDetailAST,
		DetailAST rightHandOperandDetailAST) {

		String value1 = getStringValue(leftHandOperandDetailAST);
		String value2 = getStringValue(rightHandOperandDetailAST);

		if (rightHandOperandDetailAST.getLineNo() ==
				leftHandOperandDetailAST.getLineNo()) {

			log(
				leftHandOperandDetailAST, MSG_COMBINE_LITERAL_STRINGS, value1,
				value2);

			return;
		}

		if (_isRegexPattern(detailAST)) {
			return;
		}

		checkLiteralStringStartAndEndCharacter(
			value1, value2, detailAST.getLineNo());

		String line1 = getLine(rightHandOperandDetailAST.getLineNo() - 2);
		String line2 = getLine(rightHandOperandDetailAST.getLineNo() - 1);

		if (_getLeadingTabCount(line1) == _getLeadingTabCount(line2)) {
			return;
		}

		int lineLength1 = CommonUtil.lengthExpandedTabs(
			line1, line1.length(), getTabWidth());

		String trimmedLine2 = StringUtil.trim(line2);

		if ((lineLength1 + trimmedLine2.length() - 4) <= getMaxLineLength()) {
			log(
				rightHandOperandDetailAST, MSG_COMBINE_LITERAL_STRINGS, value1,
				value2);

			return;
		}

		DetailAST parentDetailAST = detailAST.getParent();

		if ((parentDetailAST.getType() == TokenTypes.PLUS) &&
			((lineLength1 + value2.length()) <= getMaxLineLength())) {

			log(detailAST, MSG_COMBINE_LITERAL_STRINGS, value1, value2);

			return;
		}

		int pos = getStringBreakPos(
			value1, value2, getMaxLineLength() - lineLength1);

		if (pos != -1) {
			log(
				rightHandOperandDetailAST, MSG_MOVE_LITERAL_STRING,
				value2.substring(0, pos + 1));
		}
	}

	private void _checkPlusOperator(DetailAST detailAST) {
		if (detailAST.getChildCount() != 2) {
			return;
		}

		DetailAST leftHandOperandDetailAST = _getOperandDetailAST(
			detailAST.getFirstChild());
		DetailAST rightHandOperandDetailAST = _getOperandDetailAST(
			detailAST.getLastChild());

		if ((leftHandOperandDetailAST.getType() == TokenTypes.STRING_LITERAL) &&
			(rightHandOperandDetailAST.getType() ==
				TokenTypes.STRING_LITERAL)) {

			_checkLiteralStrings(
				detailAST, leftHandOperandDetailAST, rightHandOperandDetailAST);

			return;
		}

		if (leftHandOperandDetailAST.getType() == TokenTypes.STRING_LITERAL) {
			checkCombineOperand(
				leftHandOperandDetailAST, rightHandOperandDetailAST);
		}
		else if (rightHandOperandDetailAST.getType() ==
					TokenTypes.STRING_LITERAL) {

			checkCombineOperand(
				rightHandOperandDetailAST, leftHandOperandDetailAST);
		}
	}

	private int _getLeadingTabCount(String line) {
		int leadingTabCount = 0;

		while (line.startsWith(StringPool.TAB)) {
			line = line.substring(1);

			leadingTabCount++;
		}

		return leadingTabCount;
	}

	private DetailAST _getOperandDetailAST(DetailAST detailAST) {
		if (detailAST.getType() == TokenTypes.PLUS) {
			return detailAST.getLastChild();
		}

		return detailAST;
	}

	private boolean _isRegexPattern(DetailAST detailAST) {
		DetailAST parentDetailAST = detailAST.getParent();

		while (parentDetailAST != null) {
			if (parentDetailAST.getType() != TokenTypes.METHOD_CALL) {
				parentDetailAST = parentDetailAST.getParent();

				continue;
			}

			DetailAST firstChildDetailAST = parentDetailAST.getFirstChild();

			if (firstChildDetailAST.getType() != TokenTypes.DOT) {
				return false;
			}

			List nameDetailASTList = getAllChildTokens(
				firstChildDetailAST, false, TokenTypes.IDENT);

			if (nameDetailASTList.size() != 2) {
				return false;
			}

			DetailAST classNameDetailAST = nameDetailASTList.get(0);
			DetailAST methodNameDetailAST = nameDetailASTList.get(1);

			String methodCallClassName = classNameDetailAST.getText();
			String methodCallMethodName = methodNameDetailAST.getText();

			if (methodCallMethodName.equals("matches") ||
				(methodCallClassName.equals("Pattern") &&
				 methodCallMethodName.equals("compile"))) {

				return true;
			}

			return false;
		}

		return false;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy