use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class CloneMethodMustImplementCloneableRule method determineTopLevelCloneableClasses.
/**
* Determines all the class/interface declarations inside this compilation
* unit, which implement Cloneable
*
* @param currentClass
* the node of the class, that is currently analyzed (inside this
* compilation unit)
* @return a Set of class/interface names
*/
private Set<String> determineTopLevelCloneableClasses(final ASTClassOrInterfaceDeclaration currentClass) {
final List<ASTClassOrInterfaceDeclaration> classes = currentClass.getFirstParentOfType(ASTCompilationUnit.class).findDescendantsOfType(ASTClassOrInterfaceDeclaration.class);
final Set<String> classesNames = new HashSet<String>();
for (final ASTClassOrInterfaceDeclaration c : classes) {
if (!Objects.equals(c, currentClass) && extendsOrImplementsCloneable(c)) {
classesNames.add(c.getImage());
}
}
return classesNames;
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class CloneMethodMustImplementCloneableRule method visit.
@Override
public Object visit(final ASTMethodDeclaration node, final Object data) {
// Is this a clone method?
final ASTMethodDeclarator methodDeclarator = node.getFirstChildOfType(ASTMethodDeclarator.class);
if (!isCloneMethod(methodDeclarator)) {
return data;
}
// Is the clone method just throwing CloneNotSupportedException?
final ASTClassOrInterfaceDeclaration classOrInterface = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
if (// Don't analyze enums, which cannot subclass clone()
classOrInterface != null && (node.isFinal() || classOrInterface.isFinal())) {
if (node.findDescendantsOfType(ASTBlock.class).size() == 1) {
final List<ASTBlockStatement> blocks = node.findDescendantsOfType(ASTBlockStatement.class);
if (blocks.size() == 1) {
final ASTBlockStatement block = blocks.get(0);
final ASTClassOrInterfaceType type = block.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (type != null && type.getType() != null && type.getNthParent(9).equals(node) && type.getType().equals(CloneNotSupportedException.class)) {
return data;
} else if (type != null && type.getType() == null && "CloneNotSupportedException".equals(type.getImage())) {
return data;
}
}
}
}
// TODO : Should we really care about this? It can only happen with an incomplete auxclasspath
if (classOrInterface != null && classOrInterface.getType() == null) {
// Now check other whether implemented or extended classes are defined inside the same file
final Set<String> classesNames = determineTopLevelCloneableClasses(classOrInterface);
final ASTImplementsList implementsList = classOrInterface.getFirstChildOfType(ASTImplementsList.class);
if (implementsList != null) {
final List<ASTClassOrInterfaceType> types = implementsList.findChildrenOfType(ASTClassOrInterfaceType.class);
for (final ASTClassOrInterfaceType t : types) {
if (classesNames.contains(t.getImage())) {
return data;
}
}
}
final ASTExtendsList extendsList = classOrInterface.getFirstChildOfType(ASTExtendsList.class);
if (extendsList != null) {
final ASTClassOrInterfaceType type = extendsList.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (classesNames.contains(type.getImage())) {
return data;
}
}
}
// Nothing can save us now
addViolation(data, node);
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class UnusedFormalParameterRule method check.
private void check(Node node, Object data) {
Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((JavaNode) node).getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration nameDecl = entry.getKey();
if (actuallyUsed(nameDecl, entry.getValue())) {
continue;
}
addViolation(data, nameDecl.getNode(), new Object[] { node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage() });
}
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class UnnecessaryModifierRule method visit.
public Object visit(ASTAnnotationTypeDeclaration node, Object data) {
if (node.isAbstract()) {
// an abstract annotation
addViolation(data, node, getMessage());
}
if (!node.isNested()) {
return data;
}
Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
boolean isParentInterfaceOrAnnotation = parent instanceof ASTAnnotationTypeDeclaration || parent instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) parent).isInterface();
// a public annotation within an interface or annotation
if (node.isPublic() && isParentInterfaceOrAnnotation) {
addViolation(data, node, getMessage());
}
if (node.isStatic()) {
// a static annotation
addViolation(data, node, getMessage());
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class VariableNamingConventionsRule method visit.
public Object visit(ASTFieldDeclaration node, Object data) {
if (!checkMembers) {
return data;
}
boolean isStatic = node.isStatic();
boolean isFinal = node.isFinal();
Node type = node.jjtGetParent().jjtGetParent().jjtGetParent();
// Anything inside an annotation type is also static and final
if (type instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) type).isInterface() || type instanceof ASTAnnotationTypeDeclaration) {
isStatic = true;
isFinal = true;
}
return checkVariableDeclarators(node.isStatic() ? staticPrefixes : memberPrefixes, isStatic ? staticSuffixes : memberSuffixes, node, isStatic, isFinal, data);
}
Aggregations