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

com.liferay.source.formatter.check.JavaDuplicateVariableCheck Maven / Gradle / Ivy

There is a newer version: 1.0.1437
Show newest version
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.source.formatter.check;

import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.source.formatter.check.util.BNDSourceUtil;
import com.liferay.source.formatter.check.util.JavaSourceUtil;
import com.liferay.source.formatter.check.util.SourceUtil;
import com.liferay.source.formatter.parser.JavaClass;
import com.liferay.source.formatter.parser.JavaClassParser;
import com.liferay.source.formatter.parser.JavaTerm;
import com.liferay.source.formatter.parser.JavaVariable;
import com.liferay.source.formatter.util.FileUtil;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Hugo Huijser
 */
public class JavaDuplicateVariableCheck extends BaseJavaTermCheck {

	@Override
	protected String doProcess(
			String fileName, String absolutePath, JavaTerm javaTerm,
			String fileContent)
		throws IOException {

		JavaClass javaClass = (JavaClass)javaTerm;

		if (javaClass.isAnonymous() ||
			(javaClass.getParentJavaClass() != null)) {

			return javaTerm.getContent();
		}

		List extendedClassNames = javaClass.getExtendedClassNames(true);

		for (JavaTerm childJavaTerm : javaClass.getChildJavaTerms()) {
			if ((childJavaTerm instanceof JavaVariable) &&
				!childJavaTerm.isFinal() &&
				!childJavaTerm.hasAnnotation("Deprecated") &&
				(childJavaTerm.isPublic() || childJavaTerm.isProtected())) {

				_checkDuplicateVariable(
					fileName, absolutePath, extendedClassNames,
					(JavaVariable)childJavaTerm, javaClass.getPackageName());
			}
		}

		return javaTerm.getContent();
	}

	@Override
	protected String[] getCheckableJavaTermNames() {
		return new String[] {JAVA_CLASS};
	}

	private void _checkDuplicateVariable(
			String fileName, String absolutePath,
			List extendedClassNames, JavaVariable javaVariable,
			String packageName)
		throws IOException {

		String variableName = javaVariable.getName();

		for (String extendedClassName : extendedClassNames) {
			Map> extendedJavaVariablesMap =
				_getJavaVariablesMap(absolutePath, extendedClassName);

			for (Map.Entry> entry :
					extendedJavaVariablesMap.entrySet()) {

				List javaVariables = entry.getValue();

				for (JavaVariable curJavaVariable : javaVariables) {
					if (!variableName.equals(curJavaVariable.getName())) {
						continue;
					}

					String fullyQualifiedClassName = entry.getKey();

					int pos = fullyQualifiedClassName.lastIndexOf(
						CharPool.PERIOD);

					String variablePackageName =
						fullyQualifiedClassName.substring(0, pos);

					if (curJavaVariable.isPublic() ||
						(curJavaVariable.isProtected() &&
						 packageName.equals(variablePackageName))) {

						String variableClassName =
							fullyQualifiedClassName.substring(pos + 1);

						addMessage(
							fileName,
							StringBundler.concat(
								javaVariable.getAccessModifier(), " variable '",
								variableName,
								"' already exists in extended class '",
								variableClassName, "'"),
							javaVariable.getLineNumber());
					}
				}
			}
		}
	}

	private synchronized Map _getBundleSymbolicNamesMap(
		String absolutePath) {

		if (_bundleSymbolicNamesMap != null) {
			return _bundleSymbolicNamesMap;
		}

		_bundleSymbolicNamesMap = BNDSourceUtil.getBundleSymbolicNamesMap(
			_getRootDirName(absolutePath));

		return _bundleSymbolicNamesMap;
	}

	private Map> _getJavaVariablesMap(
			String absolutePath, String fullyQualifiedClassName)
		throws IOException {

		if (_javaVariablesMap.containsKey(fullyQualifiedClassName)) {
			return _javaVariablesMap.get(fullyQualifiedClassName);
		}

		Map> javaVariablesMap = new HashMap<>();

		File file = JavaSourceUtil.getJavaFile(
			fullyQualifiedClassName, _getRootDirName(absolutePath),
			_getBundleSymbolicNamesMap(absolutePath));

		if (file != null) {
			try {
				JavaClass javaClass = JavaClassParser.parseJavaClass(
					SourceUtil.getAbsolutePath(file), FileUtil.read(file));

				javaVariablesMap.putAll(
					_getJavaVariablesMap(
						absolutePath, fullyQualifiedClassName, javaClass));
			}
			catch (Exception exception) {
				if (_log.isDebugEnabled()) {
					_log.debug(exception);
				}
			}
		}

		_javaVariablesMap.put(fullyQualifiedClassName, javaVariablesMap);

		return javaVariablesMap;
	}

	private Map> _getJavaVariablesMap(
			String absolutePath, String fullyQualifiedName, JavaClass javaClass)
		throws IOException {

		Map> javaVariablesMap =
			HashMapBuilder.>put(
				fullyQualifiedName,
				() -> {
					List javaVariables = new ArrayList<>();

					for (JavaTerm javaTerm : javaClass.getChildJavaTerms()) {
						if ((javaTerm instanceof JavaVariable) &&
							(javaTerm.isProtected() || javaTerm.isPublic())) {

							javaVariables.add((JavaVariable)javaTerm);
						}
					}

					return javaVariables;
				}
			).build();

		List extendedClassNames = javaClass.getExtendedClassNames(true);

		for (String extendedClassName : extendedClassNames) {
			javaVariablesMap.putAll(
				_getJavaVariablesMap(absolutePath, extendedClassName));
		}

		return javaVariablesMap;
	}

	private synchronized String _getRootDirName(String absolutePath) {
		if (_rootDirName != null) {
			return _rootDirName;
		}

		_rootDirName = SourceUtil.getRootDirName(absolutePath);

		return _rootDirName;
	}

	private static final Log _log = LogFactoryUtil.getLog(
		JavaDuplicateVariableCheck.class);

	private Map _bundleSymbolicNamesMap;
	private final Map>>
		_javaVariablesMap = new HashMap<>();
	private String _rootDirName;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy