use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class LocalScope method addNameOccurrence.
@Override
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occ) {
PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ;
Set<NameDeclaration> declarations = findVariableHere(occurrence);
if (!declarations.isEmpty() && !occurrence.isThisOrSuper()) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
nameOccurrences.add(occurrence);
Node n = occurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
return declarations;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class LocalScope method findVariableHere.
public Set<NameDeclaration> findVariableHere(PLSQLNameOccurrence occurrence) {
Set<NameDeclaration> result = new HashSet<>();
if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
return result;
}
ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
Applier.apply(finder, getVariableDeclarations().keySet().iterator());
if (finder.getDecl() != null) {
result.add(finder.getDecl());
}
return result;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class UselessStringValueOfRule method visit.
@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}
String image = ((ASTName) node.jjtGetChild(0)).getImage();
if ("String.valueOf".equals(image)) {
Node parent = node.jjtGetParent();
if (parent.jjtGetNumChildren() != 2) {
return super.visit(node, data);
}
// skip String.valueOf(anyarraytype[])
ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
ASTName arg = args.getFirstDescendantOfType(ASTName.class);
if (arg != null) {
NameDeclaration declaration = arg.getNameDeclaration();
if (declaration != null) {
ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) {
return super.visit(node, data);
}
}
}
}
Node gp = parent.jjtGetParent();
if (parent instanceof ASTPrimaryExpression && gp instanceof ASTAdditiveExpression && "+".equals(gp.getImage())) {
boolean ok = false;
if (gp.jjtGetChild(0) == parent) {
ok = !isPrimitive(gp.jjtGetChild(1));
} else {
for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
ok = !isPrimitive(gp.jjtGetChild(i));
}
}
if (ok) {
super.addViolation(data, node);
return data;
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class MethodScope method addNameOccurrence.
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
for (NameDeclaration decl : declarations) {
getVariableDeclarations().get(decl).add(javaOccurrence);
Node n = javaOccurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
return declarations;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class JUnitTestsShouldIncludeAssertRule method getRuleAnnotatedExpectedExceptions.
/**
* Gets a list of NameDeclarations for all the fields that have type
* ExpectedException and have a Rule annotation.
*
* @param classScope
* The class scope to search for
* @return See description
*/
private Map<String, List<NameOccurrence>> getRuleAnnotatedExpectedExceptions(Scope classScope) {
Map<String, List<NameOccurrence>> result = new HashMap<>();
Map<NameDeclaration, List<NameOccurrence>> decls = classScope.getDeclarations();
for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
Node parent = entry.getKey().getNode().jjtGetParent().jjtGetParent().jjtGetParent();
if (parent.hasDescendantOfType(ASTMarkerAnnotation.class) && parent.getFirstChildOfType(ASTFieldDeclaration.class) != null) {
String annot = parent.getFirstDescendantOfType(ASTMarkerAnnotation.class).jjtGetChild(0).getImage();
if (!"Rule".equals(annot) && !"org.junit.Rule".equals(annot)) {
continue;
}
Node type = parent.getFirstDescendantOfType(ASTReferenceType.class);
if (!"ExpectedException".equals(type.jjtGetChild(0).getImage())) {
continue;
}
result.put(entry.getKey().getName(), entry.getValue());
}
}
return result;
}
Aggregations