All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.liferay.source.formatter.checks.JavaDuplicateVariableCheck Maven / Gradle / Ivy
/**
* 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;
import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.source.formatter.checks.util.BNDSourceUtil;
import com.liferay.source.formatter.checks.util.JavaSourceUtil;
import com.liferay.source.formatter.checks.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) {
}
}
_javaVariablesMap.put(fullyQualifiedClassName, javaVariablesMap);
return javaVariablesMap;
}
private Map> _getJavaVariablesMap(
String absolutePath, String fullyQualifiedName, JavaClass javaClass)
throws IOException {
List javaVariables = new ArrayList<>();
for (JavaTerm javaTerm : javaClass.getChildJavaTerms()) {
if ((javaTerm instanceof JavaVariable) &&
(javaTerm.isProtected() || javaTerm.isPublic())) {
javaVariables.add((JavaVariable)javaTerm);
}
}
Map> javaVariablesMap = HashMapBuilder.put(
fullyQualifiedName, 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 Map _bundleSymbolicNamesMap;
private final Map>>
_javaVariablesMap = new HashMap<>();
private String _rootDirName;
}