private boolean isFactory(ASTVariableDeclarator declarator) { boolean factory = false; List<ASTName> names = declarator.findDescendantsOfType(ASTName.class); for (ASTName name : names) { if (name.getImage().toLowerCase(Locale.ROOT).contains("factory")) { factory = true; break; } } return factory; }
private String getMethodOrAttributeName(ASTPrimaryExpression node) { ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class); ASTName name = prefix.getFirstDescendantOfType(ASTName.class); String methodOrAttributeName = null; if (name != null) { int dotIndex = name.getImage().indexOf("."); if (dotIndex > -1) { methodOrAttributeName = name.getImage().substring(dotIndex + 1); } } return methodOrAttributeName; }
private boolean throwsOneException(ASTMethodDeclaration node, Class<? extends Throwable> exception) { ASTNameList throwsList = node.getThrows(); if (throwsList != null && throwsList.jjtGetNumChildren() == 1) { ASTName n = (ASTName) throwsList.jjtGetChild(0); if (n.getType() == exception || exception.getSimpleName().equals(n.getImage()) || exception.getName().equals(n.getImage())) { return true; } } return false; }
private boolean variableIsPassedToMethod(ASTPrimaryExpression expr, String variable) { List<ASTName> methodParams = new ArrayList<>(); expr.findDescendantsOfType(ASTName.class, methodParams, true); for (ASTName pName : methodParams) { String paramName = pName.getImage(); // also check if we've got the a parameter (i.e if it's an argument // !) ASTArgumentList parentParam = pName.getFirstParentOfType(ASTArgumentList.class); if (paramName.equals(variable) && parentParam != null) { return true; } } return false; }
private String getNameImage(ASTPrimaryExpression node) { ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class); ASTName name = prefix.getFirstDescendantOfType(ASTName.class); String image = null; if (name != null) { image = name.getImage(); } return image; }
private boolean isMethodThrowingType(ASTMethodDeclaration node, List<String> exceptedExceptions) { boolean result = false; ASTNameList thrownsExceptions = node.getFirstChildOfType(ASTNameList.class); if (thrownsExceptions != null) { List<ASTName> names = thrownsExceptions.findChildrenOfType(ASTName.class); for (ASTName name : names) { for (String exceptedException : exceptedExceptions) { if (exceptedException.equals(name.getImage())) { result = true; } } } } return result; }
/** * Returns whether the variable is mentioned within the statement or not. */ private static boolean hasReferencesIn(ASTBlockStatement block, String varName) { // allow for closures on the var for (ASTName name : block.findDescendantsOfType(ASTName.class, true)) { if (isReference(varName, name.getImage())) { return true; } } return false; }
public String getPackageNameImage() { return ((ASTName) jjtGetChild(this.jjtGetNumChildren() - 1)).getImage(); } }
private boolean isAnnotatedOverride(ASTMethodDeclaration decl) { List<ASTMarkerAnnotation> annotations = decl.jjtGetParent().findDescendantsOfType(ASTMarkerAnnotation.class); for (ASTMarkerAnnotation ann : annotations) { // TODO consider making a method to get the annotations of a method if (ann.getFirstChildOfType(ASTName.class).getImage().equals("Override")) { return true; } } return false; }
@Override public Object visit(ASTName node, Object data) { if (!(node.jjtGetParent() instanceof ASTImportDeclaration) && !(node.jjtGetParent() instanceof ASTPackageDeclaration)) { // This name has no qualification, it can't be unnecessarily qualified if (node.getImage().indexOf('.') < 0) { return data; } checkImports(node, data); } return data; }
/** * ASTPrimaryPrefix has name in child node of ASTName */ private static String getNameFromPrefix(ASTPrimaryPrefix node) { String name = null; // should only be 1 child, if more I need more knowledge if (node.jjtGetNumChildren() == 1) { // safety check Node nnode = node.jjtGetChild(0); if (nnode instanceof ASTName) { // just as easy as null check and it // should be an ASTName anyway name = ((ASTName) nnode).getImage(); } } return name; }
private boolean thirdChildHasDottedName(Node primaryExpression) { Node thirdChild = primaryExpression.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0); return thirdChild instanceof ASTName && ((ASTName) thirdChild).getImage().indexOf('.') == -1; }
private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) { if (pp.jjtGetNumChildren() == 1 && pp.jjtGetChild(0) instanceof ASTName) { return ((ASTName) pp.jjtGetChild(0)).getImage(); } return null; } }
private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) { if (pp.jjtGetNumChildren() == 1 && pp.jjtGetChild(0) instanceof ASTName) { return ((ASTName) pp.jjtGetChild(0)).getImage(); } return null; } }
@Override public Object visit(ASTName node, Object data) { String image = node.getImage(); String var = getResultSetVariableName(image); if (var != null && resultSetVariables.containsKey(var) && node.getFirstParentOfType(ASTIfStatement.class) == null && node.getFirstParentOfType(ASTWhileStatement.class) == null && node.getFirstParentOfType(ASTReturnStatement.class) == null) { addViolation(data, resultSetVariables.get(var)); } return super.visit(node, data); }
private String getName(Node node) { if (node.jjtGetNumChildren() > 0) { if (node.jjtGetChild(0) instanceof ASTName) { return ((ASTName) node.jjtGetChild(0)).getImage(); } else { return getName(node.jjtGetChild(0)); } } throw new IllegalArgumentException("Check with hasNameAsChild() first!"); } }
private Entry<VariableNameDeclaration, List<NameOccurrence>> getIterableDeclOfIteratorLoop(VariableNameDeclaration indexDecl, Scope scope) { Node initializer = indexDecl.getNode().getFirstParentOfType(ASTVariableDeclarator.class) .getFirstChildOfType(ASTVariableInitializer.class); if (initializer == null) { return null; } ASTName nameNode = initializer.getFirstDescendantOfType(ASTName.class); if (nameNode == null) { // TODO : This can happen if we are calling a local / statically imported method that returns the iterable - currently unhandled return null; } String name = nameNode.getImage(); int dotIndex = name.indexOf('.'); if (dotIndex > 0) { name = name.substring(0, dotIndex); } return findDeclaration(name, scope); }
@Override public Object visit(ASTImportDeclaration node, Object data) { ASTName importedType = (ASTName) node.jjtGetChild(0); if (importedType.getType() != null) { node.setType(importedType.getType()); } else { populateType(node, importedType.getImage()); } if (node.getType() != null) { node.setPackage(node.getType().getPackage()); } // no need to visit children, the only child, ASTName, will have no type return data; }