org.sonar.java.JavaFilesCache Maven / Gradle / Ivy
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program 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 Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.sonar.java.model.JavaTree;
import org.sonar.java.model.JavaTree.PackageDeclarationTreeImpl;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;
public class JavaFilesCache extends BaseTreeVisitor implements JavaFileScanner {
private Set classNames = new HashSet<>();
private Deque currentClassKey = new LinkedList<>();
private Deque parent = new LinkedList<>();
private Deque anonymousInnerClassCounter = new LinkedList<>();
private String currentPackage;
public Set getClassNames() {
return classNames;
}
@Override
public void scanFile(JavaFileScannerContext context) {
JavaTree.CompilationUnitTreeImpl tree = (JavaTree.CompilationUnitTreeImpl) context.getTree();
currentPackage = PackageDeclarationTreeImpl.packageNameAsString(tree.packageDeclaration()).replace('.', '/');
currentClassKey.clear();
parent.clear();
anonymousInnerClassCounter.clear();
scan(tree);
}
@Override
public void visitClass(ClassTree tree) {
String className = "";
IdentifierTree simpleName = tree.simpleName();
if (simpleName != null) {
className = simpleName.name();
}
String key = getClassKey(className);
currentClassKey.push(key);
parent.push(tree);
anonymousInnerClassCounter.push(0);
classNames.add(key);
super.visitClass(tree);
currentClassKey.pop();
parent.pop();
anonymousInnerClassCounter.pop();
}
private String getClassKey(String className) {
String key = className;
if (StringUtils.isNotEmpty(currentPackage)) {
key = currentPackage + "/" + className;
}
if ("".equals(className) || (parent.peek() != null && parent.peek().is(Tree.Kind.METHOD))) {
// inner class declared within method
int count = anonymousInnerClassCounter.pop() + 1;
key = currentClassKey.peek() + "$" + count + className;
anonymousInnerClassCounter.push(count);
} else if (currentClassKey.peek() != null) {
key = currentClassKey.peek() + "$" + className;
}
return key;
}
@Override
public void visitMethod(MethodTree tree) {
parent.push(tree);
super.visitMethod(tree);
parent.pop();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy