@Override public Object visit(ASTFieldDeclaration node, Object data) { for (ASTVariableDeclaratorId declaration : node.findDescendantsOfType(ASTVariableDeclaratorId.class)) { for (NameOccurrence no : declaration.getUsages()) { Node location = no.getLocation(); System.out.println(declaration.getImage() + " is used here: " + location.getImage()); } } return data; } }
@Override public Object visit(ASTLocalVariableDeclaration node, Object data) { ASTVariableDeclaratorId var = getDeclaration(node); if (var == null) { return super.visit(node, data); } String variableName = var.getImage(); for (NameOccurrence no : var.getUsages()) { // FIXME - getUsages will return everything with the same name as // the variable, // see JUnit test, case 6. Changing to Node below, revisit when // getUsages is fixed Node sn = no.getLocation(); Node primaryExpression = sn.jjtGetParent().jjtGetParent(); Class<? extends Node> parentClass = primaryExpression.jjtGetParent().getClass(); if (parentClass.equals(ASTStatementExpression.class)) { String methodCall = sn.getImage().substring(variableName.length()); ASTType nodeType = node.getTypeNode(); if (nodeType != null) { if (MAP_CLASSES.get(nodeType.getTypeImage()).contains(methodCall)) { addViolation(data, sn); } } } } return super.visit(node, data); }
private Object process(Node node, Object data) { ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null || !implClassNames.contains(cit.getImage())) { return data; } cit = cit.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null) { return data; } ASTVariableDeclaratorId decl = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); List<NameOccurrence> usages = decl.getUsages(); for (NameOccurrence no : usages) { ASTName name = (ASTName) no.getLocation(); Node n = name.jjtGetParent().jjtGetParent().jjtGetParent(); if (n instanceof ASTCastExpression) { addViolation(data, n); } } return null; } }
for (NameOccurrence occ : node.getUsages()) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; if (isNotedMethod(jocc.getNameForWhichThisIsAQualifier())) {
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { if (!TypeHelper.isExactlyAny(node.getNameDeclaration(), String.class) && !TypeHelper.isExactlyAny(node.getNameDeclaration(), String[].class)) { return data; } boolean isArray = node.isArray(); for (NameOccurrence occ : node.getUsages()) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier(); if (qualifier != null) { if (!isArray && isNotAMethodReference(qualifier) && qualifier.getImage().indexOf("toString") != -1) { addViolation(data, jocc.getLocation()); } else if (isArray && isNotAName(qualifier) && qualifier.getImage().equals("toString")) { addViolation(data, jocc.getLocation()); } } } return data; }
@Override public Object visit(ASTLocalVariableDeclaration decl, Object data) { for (int i = 0; i < decl.jjtGetNumChildren(); i++) { if (!(decl.jjtGetChild(i) instanceof ASTVariableDeclarator)) { continue; } ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) decl.jjtGetChild(i).jjtGetChild(0); // TODO this isArray() check misses some cases // need to add DFAish code to determine if an array // is initialized locally or gotten from somewhere else if (!node.getNameDeclaration().isArray() && !actuallyUsed(node.getUsages())) { addViolation(data, node, node.getNameDeclaration().getImage()); } } return data; }
for (NameOccurrence occ : var.getUsages()) { Node n = occ.getLocation(); if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { Node nameNode = node.getTypeNameNode(); if (nameNode == null || nameNode instanceof ASTPrimitiveType || !appliesToClassName(node.getNameDeclaration().getTypeImage())) { return data; } List<NameOccurrence> declars = node.getUsages(); for (NameOccurrence occ : declars) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; if (!isTargetMethod(jocc)) { continue; } Node expr = jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetParent(); checkNodeAndReport(data, jocc.getLocation(), expr); } return data; }
List<NameOccurrence> usage = node.getUsages(); Map<Node, Map<Node, Integer>> blocks = new HashMap<>(); for (NameOccurrence no : usage) {
return data; for (NameOccurrence occ : node.getUsages()) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
for (ASTVariableDeclarator declarator : node.findChildrenOfType(ASTVariableDeclarator.class)) { ASTVariableDeclaratorId declaration = (ASTVariableDeclaratorId) declarator.jjtGetChild(0); List<NameOccurrence> usages = declaration.getUsages(); Node decl = null; boolean violation = true;
return data; for (NameOccurrence no : node.getUsages()) { Node name = no.getLocation(); ASTStatementExpression statement = name.getFirstParentOfType(ASTStatementExpression.class);
@Override public Object visit(ASTFieldDeclaration node, Object data) { for (ASTVariableDeclaratorId declaration : node.findDescendantsOfType(ASTVariableDeclaratorId.class)) { for (NameOccurrence no : declaration.getUsages()) { Node location = no.getLocation(); System.out.println(declaration.getImage() + " is used here: " + location.getImage()); } } return data; } }
@Override public Object visit(ASTLocalVariableDeclaration node, Object data) { ASTVariableDeclaratorId var = getDeclaration(node); if (var == null) { return super.visit(node, data); } String variableName = var.getImage(); for (NameOccurrence no : var.getUsages()) { // FIXME - getUsages will return everything with the same name as // the variable, // see JUnit test, case 6. Changing to Node below, revisit when // getUsages is fixed Node sn = no.getLocation(); Node primaryExpression = sn.jjtGetParent().jjtGetParent(); Class<? extends Node> parentClass = primaryExpression.jjtGetParent().getClass(); if (parentClass.equals(ASTStatementExpression.class)) { String methodCall = sn.getImage().substring(variableName.length()); ASTType nodeType = node.getTypeNode(); if (nodeType != null) { if (MAP_CLASSES.get(nodeType.getTypeImage()).contains(methodCall)) { addViolation(data, sn); } } } } return super.visit(node, data); }
private Object process(Node node, Object data) { ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null || !implClassNames.contains(cit.getImage())) { return data; } cit = cit.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null) { return data; } ASTVariableDeclaratorId decl = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); List<NameOccurrence> usages = decl.getUsages(); for (NameOccurrence no : usages) { ASTName name = (ASTName) no.getLocation(); Node n = name.jjtGetParent().jjtGetParent().jjtGetParent(); if (n instanceof ASTCastExpression) { addViolation(data, n); } } return null; } }
for (NameOccurrence occ : node.getUsages()) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; if (isNotedMethod(jocc.getNameForWhichThisIsAQualifier())) {
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { if (!TypeHelper.isExactlyAny(node.getNameDeclaration(), String.class) && !TypeHelper.isExactlyAny(node.getNameDeclaration(), String[].class)) { return data; } boolean isArray = node.isArray(); for (NameOccurrence occ : node.getUsages()) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier(); if (qualifier != null) { if (!isArray && isNotAMethodReference(qualifier) && qualifier.getImage().indexOf("toString") != -1) { addViolation(data, jocc.getLocation()); } else if (isArray && isNotAName(qualifier) && qualifier.getImage().equals("toString")) { addViolation(data, jocc.getLocation()); } } } return data; }
@Override public Object visit(ASTFieldDeclaration node, Object data) { if (!node.isStatic()) { return data; } ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null || !TypeHelper.isA(cit, formatterClassToCheck)) { return data; } ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); for (NameOccurrence occ : var.getUsages()) { Node n = occ.getLocation(); if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) { continue; } // ignore usages, that don't call a method. if (!n.getImage().contains(".")) { continue; } ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class); if (method != null && !method.isSynchronized()) { addViolation(data, n); } } return data; } }
@Override public Object visit(ASTLocalVariableDeclaration decl, Object data) { for (int i = 0; i < decl.jjtGetNumChildren(); i++) { if (!(decl.jjtGetChild(i) instanceof ASTVariableDeclarator)) { continue; } ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) decl.jjtGetChild(i).jjtGetChild(0); // TODO this isArray() check misses some cases // need to add DFAish code to determine if an array // is initialized locally or gotten from somewhere else if (!node.getNameDeclaration().isArray() && !actuallyUsed(node.getUsages())) { addViolation(data, node, node.getNameDeclaration().getImage()); } } return data; }
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { Node nameNode = node.getTypeNameNode(); if (nameNode == null || nameNode instanceof ASTPrimitiveType || !appliesToClassName(node.getNameDeclaration().getTypeImage())) { return data; } List<NameOccurrence> declars = node.getUsages(); for (NameOccurrence occ : declars) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; if (!isTargetMethod(jocc)) { continue; } Node expr = jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetParent(); checkNodeAndReport(data, jocc.getLocation(), expr); } return data; }