use of net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration in project pmd by pmd.
the class UnusedPrivateMethodRule method findUnique.
private Set<MethodNameDeclaration> findUnique(Map<MethodNameDeclaration, List<NameOccurrence>> methods) {
// some rather hideous hackery here
// to work around the fact that PMD does not yet do full type analysis
// when it does, delete this
Set<MethodNameDeclaration> unique = new HashSet<>();
Set<String> sigs = new HashSet<>();
for (MethodNameDeclaration mnd : methods.keySet()) {
String sig = mnd.getImage() + mnd.getParameterCount() + mnd.isVarargs();
if (!sigs.contains(sig)) {
unique.add(mnd);
}
sigs.add(sig);
}
return unique;
}
use of net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration in project pmd by pmd.
the class UseCollectionIsEmptyRule method getTypeOfMethodCall.
private ASTClassOrInterfaceType getTypeOfMethodCall(ASTPrimarySuffix node) {
ASTClassOrInterfaceType type = null;
ASTName methodName = node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class);
if (methodName != null) {
ClassScope classScope = node.getScope().getEnclosingScope(ClassScope.class);
Map<MethodNameDeclaration, List<NameOccurrence>> methods = classScope.getMethodDeclarations();
for (Map.Entry<MethodNameDeclaration, List<NameOccurrence>> e : methods.entrySet()) {
if (e.getKey().getName().equals(methodName.getImage())) {
type = e.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class).getFirstChildOfType(ASTResultType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
break;
}
}
}
return type;
}
use of net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration in project pmd by pmd.
the class AccessorMethodGenerationRule method analyzeScope.
private void analyzeScope(final AbstractJavaScope file, final Object data) {
for (final ClassNameDeclaration classDecl : file.getDeclarations(ClassNameDeclaration.class).keySet()) {
final ClassScope classScope = (ClassScope) classDecl.getScope();
// Check fields
for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> varDecl : classScope.getVariableDeclarations().entrySet()) {
final ASTFieldDeclaration field = varDecl.getKey().getNode().getFirstParentOfType(ASTFieldDeclaration.class);
analyzeMember(field, varDecl.getValue(), classScope, data);
}
// Check methods
for (final Map.Entry<MethodNameDeclaration, List<NameOccurrence>> methodDecl : classScope.getMethodDeclarations().entrySet()) {
final ASTMethodDeclaration method = methodDecl.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class);
analyzeMember(method, methodDecl.getValue(), classScope, data);
}
// Check inner classes
analyzeScope(classScope, data);
}
}
use of net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration in project pmd by pmd.
the class BeanMembersShouldSerializeRule method visit.
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
if (node.isInterface()) {
return data;
}
Map<MethodNameDeclaration, List<NameOccurrence>> methods = node.getScope().getEnclosingScope(ClassScope.class).getMethodDeclarations();
List<ASTMethodDeclarator> getSetMethList = new ArrayList<>(methods.size());
for (MethodNameDeclaration d : methods.keySet()) {
ASTMethodDeclarator mnd = d.getMethodNameDeclaratorNode();
if (isBeanAccessor(mnd)) {
getSetMethList.add(mnd);
}
}
String[] methNameArray = imagesOf(getSetMethList);
Arrays.sort(methNameArray);
Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
AccessNode accessNodeParent = decl.getAccessNodeParent();
if (entry.getValue().isEmpty() || accessNodeParent.isTransient() || accessNodeParent.isStatic()) {
continue;
}
String varName = trimIfPrefix(decl.getImage());
varName = varName.substring(0, 1).toUpperCase(Locale.ROOT) + varName.substring(1, varName.length());
boolean hasGetMethod = Arrays.binarySearch(methNameArray, "get" + varName) >= 0 || Arrays.binarySearch(methNameArray, "is" + varName) >= 0;
boolean hasSetMethod = Arrays.binarySearch(methNameArray, "set" + varName) >= 0;
// variable...
if (!hasGetMethod || !accessNodeParent.isFinal() && !hasSetMethod) {
addViolation(data, decl.getNode(), decl.getImage());
}
}
return super.visit(node, data);
}
Aggregations