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

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

There is a newer version: 1.0.1464
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.FileContents;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

/**
 * @author Hugo Huijser
 */
public class JavadocCheck extends BaseCheck {

	@Override
	public int[] getDefaultTokens() {
		return new int[] {
			TokenTypes.ANNOTATION_DEF, TokenTypes.ANNOTATION_FIELD_DEF,
			TokenTypes.CLASS_DEF, TokenTypes.CTOR_DEF,
			TokenTypes.ENUM_CONSTANT_DEF, TokenTypes.ENUM_DEF,
			TokenTypes.INTERFACE_DEF, TokenTypes.METHOD_DEF,
			TokenTypes.PACKAGE_DEF, TokenTypes.VARIABLE_DEF
		};
	}

	@Override
	protected void doVisitToken(DetailAST detailAST) {
		FileContents fileContents = getFileContents();

		TextBlock javadoc = fileContents.getJavadocBefore(
			detailAST.getLineNo());

		if (javadoc == null) {
			return;
		}

		_checkJavadoc(javadoc);

		javadoc = fileContents.getJavadocBefore(javadoc.getStartLineNo());

		if (javadoc != null) {
			DetailAST nameAST = detailAST.findFirstToken(TokenTypes.IDENT);

			log(
				detailAST.getLineNo(), _MSG_MULTIPLE_JAVADOC,
				nameAST.getText());
		}
	}

	private void _checkJavadoc(TextBlock javadoc) {
		String[] text = javadoc.getText();

		_checkLine(javadoc, text, 1, "/**", _MSG_INCORRECT_FIRST_LINE, true);
		_checkLine(javadoc, text, 2, StringPool.STAR, _MSG_EMPTY_LINE, false);
		_checkLine(
			javadoc, text, text.length - 1, StringPool.STAR, _MSG_EMPTY_LINE,
			false);
		_checkLine(
			javadoc, text, text.length, "*/", _MSG_INCORRECT_LAST_LINE, true);
	}

	private void _checkLine(
		TextBlock javadoc, String[] text, int lineNumber, String expectedValue,
		String message, boolean match) {

		String line = StringUtil.trim(text[lineNumber - 1]);

		if ((match && !line.equals(expectedValue)) ||
			(!match && line.equals(expectedValue))) {

			log(javadoc.getStartLineNo() + lineNumber - 1, message);
		}
	}

	private static final String _MSG_EMPTY_LINE = "javadoc.empty.line";

	private static final String _MSG_INCORRECT_FIRST_LINE =
		"javadoc.incorrect.first.line";

	private static final String _MSG_INCORRECT_LAST_LINE =
		"javadoc.incorrect.last.line";

	private static final String _MSG_MULTIPLE_JAVADOC = "javadoc.multiple";

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy