/**
* 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;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.source.formatter.checkstyle.util.CheckStyleUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @author Hugo Huijser
*/
public class JavaSourceProcessor extends BaseSourceProcessor {
@Override
protected List<String> doGetFileNames() throws Exception {
String[] includes = getIncludes();
if (ArrayUtil.isEmpty(includes)) {
return new ArrayList<>();
}
Collection<String> fileNames = null;
if (portalSource || subrepository) {
fileNames = _getPortalJavaFiles(includes);
}
else {
fileNames = _getPluginJavaFiles(includes);
}
return new ArrayList<>(fileNames);
}
@Override
protected String[] doGetIncludes() {
return _INCLUDES;
}
@Override
protected void postFormat() throws Exception {
_processCheckStyle();
}
@Override
protected String processSourceChecks(
File file, String fileName, String absolutePath, String content)
throws Exception {
if (_hasGeneratedTag(content)) {
return content;
}
_ungeneratedFiles.add(file);
return super.processSourceChecks(file, fileName, absolutePath, content);
}
private String[] _getPluginExcludes(String pluginDirectoryName) {
return new String[] {
pluginDirectoryName + "**/model/*Clp.java",
pluginDirectoryName + "**/model/impl/*BaseImpl.java",
pluginDirectoryName + "**/model/impl/*Model.java",
pluginDirectoryName + "**/model/impl/*ModelImpl.java",
pluginDirectoryName + "**/service/**/service/*Service.java",
pluginDirectoryName + "**/service/**/service/*ServiceClp.java",
pluginDirectoryName + "**/service/**/service/*ServiceFactory.java",
pluginDirectoryName + "**/service/**/service/*ServiceUtil.java",
pluginDirectoryName + "**/service/**/service/*ServiceWrapper.java",
pluginDirectoryName + "**/service/**/service/ClpSerializer.java",
pluginDirectoryName +
"**/service/**/service/messaging/*ClpMessageListener.java",
pluginDirectoryName +
"**/service/**/service/persistence/*Finder.java",
pluginDirectoryName +
"**/service/**/service/persistence/*Util.java",
pluginDirectoryName + "**/service/base/*ServiceBaseImpl.java",
pluginDirectoryName + "**/service/base/*ServiceClpInvoker.java",
pluginDirectoryName + "**/service/http/*JSONSerializer.java",
pluginDirectoryName + "**/service/http/*ServiceHttp.java",
pluginDirectoryName + "**/service/http/*ServiceJSON.java",
pluginDirectoryName + "**/service/http/*ServiceSoap.java",
pluginDirectoryName + "**/tools/templates/**"
};
}
private Collection<String> _getPluginJavaFiles(String[] includes)
throws Exception {
Collection<String> fileNames = new TreeSet<>();
String[] excludes = _getPluginExcludes(StringPool.BLANK);
fileNames.addAll(getFileNames(excludes, includes));
return fileNames;
}
private Collection<String> _getPortalJavaFiles(String[] includes)
throws Exception {
Collection<String> fileNames = new TreeSet<>();
String[] excludes = new String[] {
"**/*_IW.java", "**/counter/service/**", "**/jsp/*",
"**/model/impl/*Model.java", "**/model/impl/*ModelImpl.java",
"**/portal/service/**", "**/portal-client/**",
"**/portal-web/test/**/*Test.java", "**/test/*-generated/**"
};
for (String directoryName : getPluginsInsideModulesDirectoryNames()) {
excludes = ArrayUtil.append(
excludes, _getPluginExcludes("**" + directoryName));
}
fileNames.addAll(getFileNames(excludes, includes));
excludes = new String[] {
"**/portal-client/**", "**/tools/ext_tmpl/**", "**/*_IW.java",
"**/test/**/*PersistenceTest.java"
};
includes = new String[] {
"**/com/liferay/portal/kernel/service/ServiceContext*.java",
"**/model/BaseModel.java", "**/model/impl/BaseModelImpl.java",
"**/portal-test/**/portal/service/**/*.java",
"**/portal-test-integration/**/portal/service/**/*.java",
"**/service/Base*.java",
"**/service/PersistedModelLocalService*.java",
"**/service/configuration/**/*.java",
"**/service/http/*HttpTest.java", "**/service/http/*SoapTest.java",
"**/service/http/TunnelUtil.java", "**/service/impl/*.java",
"**/service/jms/*.java", "**/service/permission/*.java",
"**/service/persistence/BasePersistence.java",
"**/service/persistence/BatchSession*.java",
"**/service/persistence/*FinderImpl.java",
"**/service/persistence/*Query.java",
"**/service/persistence/impl/*.java",
"**/portal-impl/test/**/*.java", "**/util-bridges/**/*.java"
};
fileNames.addAll(getFileNames(excludes, includes));
return fileNames;
}
private List<File> _getSuppressionsFiles() throws Exception {
String fileName = "checkstyle-suppressions.xml";
List<File> suppressionsFiles = new ArrayList<>();
// Find suppressions file in portal-impl/src/
if (portalSource) {
File suppressionsFile = getFile(
"portal-impl/src/" + fileName, PORTAL_MAX_DIR_LEVEL);
if (suppressionsFile != null) {
suppressionsFiles.add(suppressionsFile);
}
}
// Find suppressions files in any parent directory
int maxDirLevel = PLUGINS_MAX_DIR_LEVEL;
String parentDirName = sourceFormatterArgs.getBaseDirName();
if (portalSource || subrepository) {
maxDirLevel = PORTAL_MAX_DIR_LEVEL - 1;
parentDirName += "../";
}
for (int i = 0; i < maxDirLevel; i++) {
File suppressionsFile = new File(parentDirName + fileName);
if (suppressionsFile.exists()) {
suppressionsFiles.add(suppressionsFile);
}
parentDirName += "../";
}
if (!portalSource && !subrepository) {
return suppressionsFiles;
}
// Find suppressions files in any child directory
List<String> moduleSuppressionsFileNames = getFileNames(
new String[0], new String[] {"**/modules/**/" + fileName}, true);
for (String moduleSuppressionsFileName : moduleSuppressionsFileNames) {
moduleSuppressionsFileName = StringUtil.replace(
moduleSuppressionsFileName, CharPool.BACK_SLASH,
CharPool.SLASH);
suppressionsFiles.add(new File(moduleSuppressionsFileName));
}
return suppressionsFiles;
}
private boolean _hasGeneratedTag(String content) {
if ((content.contains("* @generated") || content.contains("$ANTLR")) &&
!content.contains("hasGeneratedTag")) {
return true;
}
else {
return false;
}
}
private void _processCheckStyle() throws Exception {
if (_ungeneratedFiles.isEmpty()) {
return;
}
Set<SourceFormatterMessage> sourceFormatterMessages =
CheckStyleUtil.process(
_ungeneratedFiles, _getSuppressionsFiles(),
sourceFormatterArgs.getBaseDirName());
for (SourceFormatterMessage sourceFormatterMessage :
sourceFormatterMessages) {
processMessage(
sourceFormatterMessage.getFileName(), sourceFormatterMessage);
printError(
sourceFormatterMessage.getFileName(),
sourceFormatterMessage.toString());
}
}
private static final String[] _INCLUDES = new String[] {"**/*.java"};
private final Set<File> _ungeneratedFiles = new CopyOnWriteArraySet<>();
}