use of net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration in project pmd by pmd.
the class JavaRuleViolationTest method testPackageAndClassName.
/**
* Tests that the class name is taken correctly, even if the node is outside
* of a class scope, e.g. a import declaration.
*
* @see <a href="https://sourceforge.net/p/pmd/bugs/1529/">#1529</a>
*/
@Test
public void testPackageAndClassName() {
ASTCompilationUnit ast = parse("package pkg; import java.util.List; public class Foo { }");
ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
assertEquals("pkg", violation.getPackageName());
assertEquals("Foo", violation.getClassName());
}
use of net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration in project pmd by pmd.
the class ClassTypeResolver method populateImports.
/**
* If the outer class wasn't found then we'll get in here
*
* @param node
*/
private void populateImports(ASTCompilationUnit node) {
List<ASTImportDeclaration> theImportDeclarations = node.findChildrenOfType(ASTImportDeclaration.class);
importedClasses.putAll(JAVA_LANG);
// go through the imports
for (ASTImportDeclaration anImportDeclaration : theImportDeclarations) {
String strPackage = anImportDeclaration.getPackageName();
if (anImportDeclaration.isStatic()) {
if (anImportDeclaration.isImportOnDemand()) {
importOnDemandStaticClasses.add(JavaTypeDefinition.forClass(loadClass(strPackage)));
} else {
// not import on-demand
String strName = anImportDeclaration.getImportedName();
String fieldName = strName.substring(strName.lastIndexOf('.') + 1);
Class<?> staticClassWithField = loadClass(strPackage);
if (staticClassWithField != null) {
JavaTypeDefinition typeDef = getFieldType(JavaTypeDefinition.forClass(staticClassWithField), fieldName, currentAcu.getType());
staticFieldImageToTypeDef.put(fieldName, typeDef);
}
List<JavaTypeDefinition> typeList = staticNamesToClasses.get(fieldName);
if (typeList == null) {
typeList = new ArrayList<>();
}
typeList.add(JavaTypeDefinition.forClass(staticClassWithField));
staticNamesToClasses.put(fieldName, typeList);
}
} else {
// non-static
if (anImportDeclaration.isImportOnDemand()) {
importedOnDemand.add(strPackage);
} else {
// not import on-demand
String strName = anImportDeclaration.getImportedName();
importedClasses.put(strName, strName);
importedClasses.put(strName.substring(strPackage.length() + 1), strName);
}
}
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration in project pmd by pmd.
the class NcssCountImportsDecorator method visit.
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
ASTCompilationUnit acu = node.getFirstParentOfType(ASTCompilationUnit.class);
List<ASTImportDeclaration> imports = acu.findChildrenOfType(ASTImportDeclaration.class);
int increment = imports.size();
if (!acu.findChildrenOfType(ASTPackageDeclaration.class).isEmpty()) {
increment++;
}
((MutableInt) data).add(increment);
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration in project pmd by pmd.
the class UnnecessaryFullyQualifiedNameRule method isAvoidingConflict.
private boolean isAvoidingConflict(final JavaNode node, final String name, final ASTImportDeclaration firstMatch) {
// is it a conflict between different imports?
if (firstMatch.isImportOnDemand() && firstMatch.isStatic() && name.indexOf('.') != -1) {
final String methodCalled = name.substring(name.indexOf('.') + 1);
// Is there any other static import conflictive?
for (final ASTImportDeclaration importDeclaration : imports) {
if (!Objects.equals(importDeclaration, firstMatch) && importDeclaration.isStatic()) {
if (importDeclaration.getImportedName().startsWith(firstMatch.getImportedName()) && importDeclaration.getImportedName().lastIndexOf('.') == firstMatch.getImportedName().length()) {
// import static java.util.Arrays.asList;
continue;
}
if (importDeclaration.isImportOnDemand()) {
// conflicting method
if (importDeclaration.getType() != null) {
for (final Method m : importDeclaration.getType().getMethods()) {
if (m.getName().equals(methodCalled)) {
return true;
}
}
}
} else if (importDeclaration.getImportedName().endsWith(methodCalled)) {
return true;
}
}
}
}
// Is it a conflict with a class in the same file?
final String unqualifiedName = name.substring(name.lastIndexOf('.') + 1);
final int unqualifiedNameLength = unqualifiedName.length();
final Set<String> qualifiedTypes = node.getScope().getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames().keySet();
for (final String qualified : qualifiedTypes) {
int fullLength = qualified.length();
if (qualified.endsWith(unqualifiedName) && (fullLength == unqualifiedNameLength || qualified.charAt(fullLength - unqualifiedNameLength - 1) == '.')) {
return true;
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration in project pmd by pmd.
the class UnnecessaryFullyQualifiedNameRule method checkImports.
private void checkImports(JavaNode node, Object data) {
String name = node.getImage();
matches.clear();
// Find all "matching" import declarations
for (ASTImportDeclaration importDeclaration : imports) {
if (importDeclaration.isImportOnDemand()) {
// On demand import exactly matches the package of the type
if (name.startsWith(importDeclaration.getImportedName())) {
if (name.lastIndexOf('.') == importDeclaration.getImportedName().length()) {
matches.add(importDeclaration);
continue;
}
}
} else {
// Exact match of imported class
if (name.equals(importDeclaration.getImportedName())) {
matches.add(importDeclaration);
continue;
}
// Match of static method call on imported class
if (name.startsWith(importDeclaration.getImportedName())) {
if (name.lastIndexOf('.') == importDeclaration.getImportedName().length()) {
matches.add(importDeclaration);
continue;
}
}
}
}
// }
if (matches.isEmpty() && name.indexOf('.') >= 0) {
for (ASTImportDeclaration importDeclaration : imports) {
if (importDeclaration.isStatic()) {
String[] importParts = importDeclaration.getImportedName().split("\\.");
String[] nameParts = name.split("\\.");
if (importDeclaration.isImportOnDemand()) {
// Name class part matches class part of static import?
if (nameParts[nameParts.length - 2].equals(importParts[importParts.length - 1])) {
matches.add(importDeclaration);
}
} else {
// Last 2 parts match?
if (nameParts[nameParts.length - 1].equals(importParts[importParts.length - 1]) && nameParts[nameParts.length - 2].equals(importParts[importParts.length - 2])) {
matches.add(importDeclaration);
}
}
}
}
}
if (!matches.isEmpty()) {
ASTImportDeclaration firstMatch = matches.get(0);
// Could this done to avoid a conflict?
if (!isAvoidingConflict(node, name, firstMatch)) {
String importStr = firstMatch.getImportedName() + (firstMatch.isImportOnDemand() ? ".*" : "");
String type = firstMatch.isStatic() ? "static " : "";
addViolation(data, node, new Object[] { node.getImage(), importStr, type });
}
}
matches.clear();
}
Aggregations