/** * 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.checks.util; import com.liferay.portal.kernel.util.CharPool; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portal.tools.ToolsUtil; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author Hugo Huijser */ public class JavaSourceUtil extends SourceUtil { public static String getAbsolutePath(String fileName) { Path filePath = Paths.get(fileName); filePath = filePath.toAbsolutePath(); filePath = filePath.normalize(); return StringUtil.replace( filePath.toString(), CharPool.BACK_SLASH, CharPool.SLASH); } public static String getClassName(String fileName) { int x = fileName.lastIndexOf(CharPool.SLASH); int y = fileName.lastIndexOf(CharPool.PERIOD); return fileName.substring(x + 1, y); } public static String getPackagePath(String content) { Matcher matcher = _packagePattern.matcher(content); if (matcher.find()) { return matcher.group(2); } return StringPool.BLANK; } public static List<String> getParameterList(String methodCall) { String parameters = null; int x = -1; while (true) { x = methodCall.indexOf(StringPool.CLOSE_PARENTHESIS, x + 1); parameters = methodCall.substring(0, x + 1); if ((getLevel(parameters, "(", ")") == 0) && (getLevel(parameters, "{", "}") == 0)) { break; } } x = parameters.indexOf(StringPool.OPEN_PARENTHESIS); parameters = parameters.substring(x + 1, parameters.length() - 1); return splitParameters(parameters); } public static boolean isValidJavaParameter(String javaParameter) { if (javaParameter.contains(" implements ") || javaParameter.contains(" throws ")) { return false; } if ((getLevel(javaParameter, "(", ")") == 0) && (getLevel(javaParameter, "<", ">") == 0) && (getLevel(javaParameter, "{", "}") == 0)) { return true; } return false; } public static List<String> splitParameters(String parameters) { List<String> parametersList = new ArrayList<>(); int x = -1; while (true) { x = parameters.indexOf(StringPool.COMMA, x + 1); if (x == -1) { parametersList.add(StringUtil.trim(parameters)); return parametersList; } if (ToolsUtil.isInsideQuotes(parameters, x)) { continue; } String linePart = parameters.substring(0, x); if ((getLevel(linePart, "(", ")") == 0) && (getLevel(linePart, "{", "}") == 0)) { parametersList.add(StringUtil.trim(linePart)); parameters = parameters.substring(x + 1); x = -1; } } } private static final Pattern _packagePattern = Pattern.compile( "(\n|^)\\s*package (.*);\n"); }