private boolean inLoopOrTry(Node node) { return node.getFirstParentOfType(ASTTryStatement.class) != null || node.getFirstParentOfType(ASTForStatement.class) != null || node.getFirstParentOfType(ASTWhileStatement.class) != null || node.getFirstParentOfType(ASTDoStatement.class) != null; }
private ASTIfStatement findIfStatement(ASTBlock enclosingBlock, Node node) { ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class); List<ASTIfStatement> allIfStatements = enclosingBlock.findDescendantsOfType(ASTIfStatement.class); if (ifStatement != null && allIfStatements.contains(ifStatement)) { return ifStatement; } return null; }
private boolean inAnonymousInnerClass(Node node) { ASTClassOrInterfaceBodyDeclaration parent = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); return parent != null && parent.isAnonymousInnerClass(); }
/** * Gets the Image of the first parent node of type * ASTClassOrInterfaceDeclaration or <code>null</code> * * @param node * the node which will be searched */ protected final String getDeclaringType(Node node) { ASTClassOrInterfaceDeclaration c = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class); if (c != null) { return c.getImage(); } return null; }
private int getInitialLength(Node node) { Node block = node.getFirstParentOfType(ASTBlockStatement.class); if (block == null) { block = node.getFirstParentOfType(ASTFieldDeclaration.class); if (block == null) { block = node.getFirstParentOfType(ASTFormalParameter.class); } } List<ASTLiteral> literals = block.findDescendantsOfType(ASTLiteral.class); if (literals.size() == 1) { ASTLiteral literal = literals.get(0); String str = literal.getImage(); if (str != null && isStringOrCharLiteral(literal)) { return str.length() - 2; // take off the quotes } } return 0; }
private boolean calledFromOutsideItself(List<NameOccurrence> occs, NameDeclaration mnd) { int callsFromOutsideMethod = 0; for (NameOccurrence occ : occs) { Node occNode = occ.getLocation(); ASTConstructorDeclaration enclosingConstructor = occNode .getFirstParentOfType(ASTConstructorDeclaration.class); if (enclosingConstructor != null) { callsFromOutsideMethod++; break; // Do we miss unused private constructors here? } ASTInitializer enclosingInitializer = occNode.getFirstParentOfType(ASTInitializer.class); if (enclosingInitializer != null) { callsFromOutsideMethod++; break; } ASTMethodDeclaration enclosingMethod = occNode.getFirstParentOfType(ASTMethodDeclaration.class); if (enclosingMethod == null || !mnd.getNode().jjtGetParent().equals(enclosingMethod)) { callsFromOutsideMethod++; } } return callsFromOutsideMethod == 0; }
c = node.getFirstParentOfType(ASTPackageSpecification.class); if (c != null) { return c.getImage(); c = node.getFirstParentOfType(ASTTypeSpecification.class); if (c != null) { return c.getImage(); c = node.getFirstParentOfType(ASTPackageBody.class); if (c != null) { return c.getImage(); c = node.getFirstParentOfType(ASTTriggerUnit.class); if (c != null) { return c.getImage(); c = node.getFirstParentOfType(ASTProgramUnit.class); if (c != null) { return c.getImage();
Node possibleStatement = statement.getFirstParentOfType(ASTIfStatement.class); while (possibleStatement instanceof ASTIfStatement) { statement = possibleStatement; possibleStatement = possibleStatement.getFirstParentOfType(ASTIfStatement.class);
private boolean initializedInConstructor(List<NameOccurrence> usages) { boolean initInConstructor = false; for (NameOccurrence occ : usages) { // specifically omitting prefix and postfix operators as there are // legitimate usages of these with static fields, e.g. typesafe enum pattern. if (((JavaNameOccurrence) occ).isOnLeftHandSide()) { Node node = occ.getLocation(); Node constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class); if (constructor != null) { initInConstructor = true; } } } return initInConstructor; }
if (jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) { Node node = jocc.getLocation(); ASTConstructorDeclaration constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class); if (constructor != null) { if (inLoopOrTry(node)) { if (node.getFirstParentOfType(ASTIfStatement.class) != null) { methodInitCount++; } else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) { lambdaUsage++; } else { if (node.getFirstParentOfType(ASTMethodDeclaration.class) != null) { methodInitCount++; } else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) { lambdaUsage++;
/** * Check if the given node is the first statement in the block. */ private boolean isFirstStatementInBlock(Node node, ASTStatement loopBody) { // find the statement of the operation and the loop body block statement final ASTBlockStatement statement = node.getFirstParentOfType(ASTBlockStatement.class); final ASTBlock block = loopBody.getFirstDescendantOfType(ASTBlock.class); if (statement == null || block == null) { return false; } // is the first statement in the loop body? return block.equals(statement.jjtGetParent()) && statement.jjtGetChildIndex() == 0; }
/** * Tries to resolve a given typeImage as a generic Type. If the Generic Type * is found, any defined ClassOrInterfaceType below this type declaration is * used (this is typically a type bound, e.g. {@code <T extends List>}. * * @param argument * the node, from where to start searching. * @param typeImage * the type as string * @return the resolved class or <code>null</code> if nothing was found. */ private Class<?> resolveGenericType(Node argument, String typeImage) { List<ASTTypeParameter> types = new ArrayList<>(); // first search only within the same method ASTClassOrInterfaceBodyDeclaration firstParentOfType = argument .getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); if (firstParentOfType != null) { types.addAll(firstParentOfType.findDescendantsOfType(ASTTypeParameter.class)); } // then search class level types, from inner-most to outer-most List<ASTClassOrInterfaceDeclaration> enclosingClasses = argument .getParentsOfType(ASTClassOrInterfaceDeclaration.class); for (ASTClassOrInterfaceDeclaration enclosing : enclosingClasses) { ASTTypeParameters classLevelTypeParameters = enclosing.getFirstChildOfType(ASTTypeParameters.class); if (classLevelTypeParameters != null) { types.addAll(classLevelTypeParameters.findDescendantsOfType(ASTTypeParameter.class)); } } return resolveGenericType(typeImage, types); }
private int getConstructorLength(Node node, int constructorLength) { int iConstructorLength = constructorLength; Node block = node.getFirstParentOfType(ASTBlockStatement.class); block = node.getFirstParentOfType(ASTFieldDeclaration.class); block = node.getFirstParentOfType(ASTFormalParameter.class); if (block != null) { iConstructorLength = -1;
private boolean isCommentNotWithin(FormalComment n1, Node n2, Node node) { if (n1 == null || n2 == null || node == null) { return true; } boolean isNotWithinNode2 = !(n1.getEndLine() < n2.getEndLine() || n1.getEndLine() == n2.getEndLine() && n1.getEndColumn() < n2.getEndColumn()); boolean isNotSameClass = node.getFirstParentOfType(ASTClassOrInterfaceBody.class) != n2 .getFirstParentOfType(ASTClassOrInterfaceBody.class); boolean isNodeWithinNode2 = node.getEndLine() < n2.getEndLine() || node.getEndLine() == n2.getEndLine() && node.getEndColumn() < n2.getEndColumn(); return isNotWithinNode2 || isNotSameClass || isNodeWithinNode2; }
private boolean methodHasOverride(Node node) { ASTClassOrInterfaceBodyDeclaration method = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); if (method != null && method.jjtGetNumChildren() > 0 && method.jjtGetChild(0) instanceof ASTAnnotation) { ASTMarkerAnnotation marker = method.getFirstDescendantOfType(ASTMarkerAnnotation.class); if (marker != null && marker.getFirstChildOfType(ASTName.class) != null) { ASTName name = marker.getFirstChildOfType(ASTName.class); if (name.getType() == Override.class) { return true; } } } return false; } }
if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) { continue; ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class); if (method != null && !method.isSynchronized()) { addViolation(data, n);
protected static boolean isInStringBufferOperation(Node node, int length, String methodName) { if (!(node.getNthParent(length) instanceof ASTStatementExpression)) { return false; } ASTStatementExpression s = node.getFirstParentOfType(ASTStatementExpression.class); if (s == null) { return false; } ASTName n = s.getFirstDescendantOfType(ASTName.class); if (n == null || n.getImage().indexOf(methodName) == -1 || !(n.getNameDeclaration() instanceof TypedNameDeclaration)) { return false; } // TODO having to hand-code this kind of dredging around is ridiculous // we need something to support this in the framework // but, "for now" (tm): // if more than one arg to append(), skip it ASTArgumentList argList = s.getFirstDescendantOfType(ASTArgumentList.class); if (argList == null || argList.jjtGetNumChildren() > 1) { return false; } return TypeHelper.isExactlyAny((TypedNameDeclaration) n.getNameDeclaration(), StringBuffer.class, StringBuilder.class); }
private int getConstructorAppendsLength(final Node node) { final Node parent = node.getFirstParentOfType(ASTVariableDeclarator.class); int size = 0; if (parent != null) { final Node initializer = parent.getFirstChildOfType(ASTVariableInitializer.class); if (initializer != null) { final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class); if (primExp != null) { for (int i = 0; i < primExp.jjtGetNumChildren(); i++) { final Node sn = primExp.jjtGetChild(i); if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) { continue; } size += processNode(sn); } } } } return size; }
Node parentNode = n.getNode().getFirstParentOfType(dataFlowHandler.getLabelStatementNodeClass()); if (parentNode == null) { break;
Node loc = jocc.getLocation(); if (loc != null) { ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class); while (exp != null) { if (exp.jjtGetParent() instanceof ASTStatementExpression) {