net.sourceforge.pmd.lang.ast.Node.findDescendantsOfType()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(282)

本文整理了Java中net.sourceforge.pmd.lang.ast.Node.findDescendantsOfType()方法的一些代码示例,展示了Node.findDescendantsOfType()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node.findDescendantsOfType()方法的具体详情如下:
包路径:net.sourceforge.pmd.lang.ast.Node
类名称:Node
方法名:findDescendantsOfType

Node.findDescendantsOfType介绍

[英]Traverses down the tree to find all the descendant instances of type descendantType without crossing find boundaries.
[中]沿树向下遍历以查找类型为DegenantType的所有子体实例,而不跨越查找边界。

代码示例

代码示例来源:origin: pmd/pmd

  1. /**
  2. * Adds all methods called on this instance from within this Node.
  3. */
  4. private static void addCalledMethodsOfNode(Node node, List<MethodInvocation> calledMethods, String className) {
  5. List<ASTPrimaryExpression> expressions = new ArrayList<>();
  6. node.findDescendantsOfType(ASTPrimaryExpression.class, expressions, !(node instanceof AccessNode));
  7. addCalledMethodsOfNodeImpl(expressions, calledMethods, className);
  8. }

代码示例来源:origin: pmd/pmd

  1. /**
  2. * Evaluates the number of paths through a boolean expression. This is the total number of {@code &&} and {@code ||}
  3. * operators appearing in the expression. This is used in the calculation of cyclomatic and n-path complexity.
  4. *
  5. * @param expr Expression to analyse
  6. *
  7. * @return The number of paths through the expression
  8. */
  9. public static int booleanExpressionComplexity(Node expr) {
  10. if (expr == null) {
  11. return 0;
  12. }
  13. List<ASTConditionalAndExpression> andNodes = expr.findDescendantsOfType(ASTConditionalAndExpression.class);
  14. List<ASTConditionalOrExpression> orNodes = expr.findDescendantsOfType(ASTConditionalOrExpression.class);
  15. int complexity = 0;
  16. if (expr instanceof ASTConditionalOrExpression || expr instanceof ASTConditionalAndExpression) {
  17. complexity++;
  18. }
  19. for (ASTConditionalOrExpression element : orNodes) {
  20. complexity += element.jjtGetNumChildren() - 1;
  21. }
  22. for (ASTConditionalAndExpression element : andNodes) {
  23. complexity += element.jjtGetNumChildren() - 1;
  24. }
  25. return complexity;
  26. }

代码示例来源:origin: pmd/pmd

  1. /**
  2. * Checks to see if there is string concatenation in the node.
  3. *
  4. * This method checks if it's additive with respect to the append method
  5. * only.
  6. *
  7. * @param n
  8. * Node to check
  9. * @return true if the node has an additive expression (i.e. "Hello " +
  10. * Const.WORLD)
  11. */
  12. private boolean isAdditive(Node n) {
  13. List<ASTAdditiveExpression> lstAdditive = n.findDescendantsOfType(ASTAdditiveExpression.class);
  14. if (lstAdditive.isEmpty()) {
  15. return false;
  16. }
  17. // if there are more than 1 set of arguments above us we're not in the
  18. // append
  19. // but a sub-method call
  20. for (int ix = 0; ix < lstAdditive.size(); ix++) {
  21. ASTAdditiveExpression expr = lstAdditive.get(ix);
  22. if (expr.getParentsOfType(ASTArgumentList.class).size() != 1) {
  23. return false;
  24. }
  25. }
  26. return true;
  27. }

代码示例来源:origin: pmd/pmd

  1. /**
  2. * Checks whether the given target is in the argument list. If this is the
  3. * case, then the target (root exception) is used as the cause.
  4. *
  5. * @param target
  6. * @param baseNode
  7. */
  8. private boolean checkForTargetUsage(String target, Node baseNode) {
  9. boolean match = false;
  10. if (target != null && baseNode != null) {
  11. // TODO : use Node.findDescendantsOfType(ASTName.class, true) on 7.0.0
  12. List<ASTName> nameNodes = new ArrayList<>();
  13. baseNode.findDescendantsOfType(ASTName.class, nameNodes, true);
  14. for (ASTName nameNode : nameNodes) {
  15. if (target.equals(nameNode.getImage())) {
  16. boolean isPartOfStringConcatenation = isStringConcat(nameNode, baseNode);
  17. if (!isPartOfStringConcatenation) {
  18. match = true;
  19. break;
  20. }
  21. }
  22. }
  23. }
  24. return match;
  25. }

代码示例来源:origin: pmd/pmd

  1. private boolean isAnnotatedOverride(ASTMethodDeclaration decl) {
  2. List<ASTMarkerAnnotation> annotations = decl.jjtGetParent().findDescendantsOfType(ASTMarkerAnnotation.class);
  3. for (ASTMarkerAnnotation ann : annotations) { // TODO consider making a method to get the annotations of a method
  4. if (ann.getFirstChildOfType(ASTName.class).getImage().equals("Override")) {
  5. return true;
  6. }
  7. }
  8. return false;
  9. }

代码示例来源:origin: pmd/pmd

  1. /**
  2. * Adds a VariableAccess to a dataflow node.
  3. *
  4. * @param node
  5. * location of the access of a variable
  6. * @param va
  7. * variable access to add
  8. * @param flow
  9. * dataflownodes that can contain the node.
  10. */
  11. private void addVariableAccess(Node node, VariableAccess va, List<DataFlowNode> flow) {
  12. // backwards to find the right inode (not a method declaration)
  13. for (int i = flow.size() - 1; i > 0; i--) {
  14. DataFlowNode inode = flow.get(i);
  15. if (inode.getNode() == null) {
  16. continue;
  17. }
  18. List<? extends Node> children = inode.getNode().findDescendantsOfType(node.getClass());
  19. for (Node n : children) {
  20. if (node.equals(n)) {
  21. List<VariableAccess> v = new ArrayList<>();
  22. v.add(va);
  23. inode.setVariableAccess(v);
  24. return;
  25. }
  26. }
  27. }
  28. }

代码示例来源:origin: pmd/pmd

  1. /**
  2. * Adds a VariableAccess to a dataflow node.
  3. *
  4. * @param node
  5. * location of the access of a variable
  6. * @param va
  7. * variable access to add
  8. * @param flow
  9. * dataflownodes that can contain the node.
  10. */
  11. private void addVariableAccess(Node node, VariableAccess va, List<DataFlowNode> flow) {
  12. // backwards to find the right inode (not a method declaration)
  13. for (int i = flow.size() - 1; i > 0; i--) {
  14. DataFlowNode inode = flow.get(i);
  15. if (inode.getNode() == null) {
  16. continue;
  17. }
  18. List<? extends Node> children = inode.getNode().findDescendantsOfType(node.getClass());
  19. for (Node n : children) {
  20. if (node.equals(n)) {
  21. List<VariableAccess> v = new ArrayList<>();
  22. v.add(va);
  23. inode.setVariableAccess(v);
  24. return;
  25. }
  26. }
  27. }
  28. }

代码示例来源:origin: pmd/pmd

  1. private boolean isEqualsViolation(Node parent) {
  2. // 3. child: equals
  3. if (parent.jjtGetChild(2).hasImageEqualTo("equals")) {
  4. // 4. child: the arguments of equals, there must be exactly one and
  5. // it must be ""
  6. List<ASTArgumentList> argList = parent.jjtGetChild(3).findDescendantsOfType(ASTArgumentList.class);
  7. if (argList.size() == 1) {
  8. List<ASTLiteral> literals = argList.get(0).findDescendantsOfType(ASTLiteral.class);
  9. return literals.size() == 1 && literals.get(0).hasImageEqualTo("\"\"");
  10. }
  11. }
  12. return false;
  13. }

代码示例来源:origin: pmd/pmd

  1. /**
  2. * TODO modify usages to use symbol table Tells if the variable name is a
  3. * local variable declared in the method.
  4. *
  5. * @param vn
  6. * the variable name
  7. * @param node
  8. * the ASTMethodDeclaration where the local variable name will be
  9. * searched
  10. * @return <code>true</code> if the method declaration contains any local
  11. * variable named vn and <code>false</code> in other case
  12. */
  13. protected boolean isLocalVariable(String vn, Node node) {
  14. final List<ASTLocalVariableDeclaration> lvars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
  15. if (lvars != null) {
  16. for (ASTLocalVariableDeclaration lvd : lvars) {
  17. final ASTVariableDeclaratorId vid = lvd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
  18. if (vid != null && vid.hasImageEqualTo(vn)) {
  19. return true;
  20. }
  21. }
  22. }
  23. return false;
  24. }

代码示例来源:origin: pmd/pmd

  1. private static List<String> getMethodDeclaratorParameterTypes(Node methodOrConstructorDeclarator) {
  2. List<ASTFormalParameter> parameters = methodOrConstructorDeclarator
  3. .findDescendantsOfType(ASTFormalParameter.class);
  4. List<String> parameterTypes = new ArrayList<>();
  5. if (parameters != null) {
  6. for (ASTFormalParameter p : parameters) {
  7. ASTType type = p.getFirstChildOfType(ASTType.class);
  8. if (type.jjtGetChild(0) instanceof ASTPrimitiveType) {
  9. parameterTypes.add(type.jjtGetChild(0).getImage());
  10. } else if (type.jjtGetChild(0) instanceof ASTReferenceType) {
  11. parameterTypes.add("ref");
  12. } else {
  13. parameterTypes.add("<unkown>");
  14. }
  15. }
  16. }
  17. return parameterTypes;
  18. }

代码示例来源:origin: pmd/pmd

  1. private int getInitialLength(Node node) {
  2. Node block = node.getFirstParentOfType(ASTBlockStatement.class);
  3. if (block == null) {
  4. block = node.getFirstParentOfType(ASTFieldDeclaration.class);
  5. if (block == null) {
  6. block = node.getFirstParentOfType(ASTFormalParameter.class);
  7. }
  8. }
  9. List<ASTLiteral> literals = block.findDescendantsOfType(ASTLiteral.class);
  10. if (literals.size() == 1) {
  11. ASTLiteral literal = literals.get(0);
  12. String str = literal.getImage();
  13. if (str != null && isStringOrCharLiteral(literal)) {
  14. return str.length() - 2; // take off the quotes
  15. }
  16. }
  17. return 0;
  18. }

代码示例来源:origin: pmd/pmd

  1. /**
  2. * Tells if the node contains a Test annotation with an expected exception.
  3. */
  4. private boolean isExpectAnnotated(Node methodParent) {
  5. List<ASTNormalAnnotation> annotations = methodParent.findDescendantsOfType(ASTNormalAnnotation.class);
  6. for (ASTNormalAnnotation annotation : annotations) {
  7. ASTName name = annotation.getFirstChildOfType(ASTName.class);
  8. if (name != null && TypeHelper.isA(name, JUNIT4_CLASS_NAME)) {
  9. List<ASTMemberValuePair> memberValues = annotation.findDescendantsOfType(ASTMemberValuePair.class);
  10. for (ASTMemberValuePair pair : memberValues) {
  11. if ("expected".equals(pair.getImage())) {
  12. return true;
  13. }
  14. }
  15. }
  16. }
  17. return false;
  18. }

代码示例来源:origin: pmd/pmd

  1. /**
  2. * Returns {@code true} if the node has only one {@link ASTConstructorDeclaration}
  3. * child node and the constructor has empty body or simply invokes the superclass
  4. * constructor with no arguments.
  5. *
  6. * @param node the node to check
  7. */
  8. private boolean isExplicitDefaultConstructor(Node node) {
  9. List<ASTConstructorDeclaration> nodes
  10. = node.findDescendantsOfType(ASTConstructorDeclaration.class);
  11. if (nodes.size() != 1) {
  12. return false;
  13. }
  14. ASTConstructorDeclaration cdnode = nodes.get(0);
  15. return cdnode.getParameterCount() == 0 && !hasIgnoredAnnotation(cdnode)
  16. && !cdnode.hasDescendantOfType(ASTBlockStatement.class) && !cdnode.hasDescendantOfType(ASTNameList.class)
  17. && hasDefaultConstructorInvocation(cdnode);
  18. }

代码示例来源:origin: pmd/pmd

  1. continue;
  2. List<ASTBlockStatement> blocks = trySt.jjtGetChild(0).findDescendantsOfType(ASTBlockStatement.class);
  3. if (blocks.isEmpty()) {
  4. continue;

代码示例来源:origin: pmd/pmd

  1. private void recursivelyEvaluateCRUDMethodCalls(final AbstractApexNode<?> self,
  2. final Set<ASTMethodCallExpression> innerMethodCalls, final ASTBlockStatement blockStatement) {
  3. if (blockStatement != null) {
  4. int numberOfStatements = blockStatement.jjtGetNumChildren();
  5. for (int i = 0; i < numberOfStatements; i++) {
  6. Node n = blockStatement.jjtGetChild(i);
  7. if (n instanceof ASTIfElseBlockStatement) {
  8. List<ASTBlockStatement> innerBlocks = n.findDescendantsOfType(ASTBlockStatement.class);
  9. for (ASTBlockStatement innerBlock : innerBlocks) {
  10. recursivelyEvaluateCRUDMethodCalls(self, innerMethodCalls, innerBlock);
  11. }
  12. }
  13. AbstractApexNode<?> match = n.getFirstDescendantOfType(self.getClass());
  14. if (Objects.equal(match, self)) {
  15. break;
  16. }
  17. ASTMethodCallExpression methodCall = n.getFirstDescendantOfType(ASTMethodCallExpression.class);
  18. if (methodCall != null) {
  19. mapCallToMethodDecl(self, innerMethodCalls, Arrays.asList(methodCall));
  20. }
  21. }
  22. }
  23. }

代码示例来源:origin: pmd/pmd

  1. private boolean hasOverrideAnnotation(ASTMethodDeclaration node) {
  2. int childIndex = node.jjtGetChildIndex();
  3. for (int i = 0; i < childIndex; i++) {
  4. Node previousSibling = node.jjtGetParent().jjtGetChild(i);
  5. List<ASTMarkerAnnotation> annotations = previousSibling.findDescendantsOfType(ASTMarkerAnnotation.class);
  6. for (ASTMarkerAnnotation annotation : annotations) {
  7. ASTName name = annotation.getFirstChildOfType(ASTName.class);
  8. if (name != null && (name.hasImageEqualTo("Override") || name.hasImageEqualTo("java.lang.Override"))) {
  9. return true;
  10. }
  11. }
  12. }
  13. return false;
  14. }
  15. }

代码示例来源:origin: pmd/pmd

  1. private void checkForResources(Node node, Object data) {
  2. List<ASTLocalVariableDeclaration> vars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
  3. List<ASTVariableDeclaratorId> ids = new ArrayList<>();
  4. // find all variable references to Connection objects
  5. for (ASTLocalVariableDeclaration var : vars) {
  6. ASTType type = var.getTypeNode();
  7. if (type != null && type.jjtGetChild(0) instanceof ASTReferenceType) {
  8. ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);
  9. if (ref.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
  10. ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0);
  11. if (clazz.getType() != null && types.contains(clazz.getType().getName())
  12. || clazz.getType() == null && simpleTypes.contains(toSimpleType(clazz.getImage()))
  13. && !clazz.isReferenceToClassSameCompilationUnit()
  14. || types.contains(clazz.getImage()) && !clazz.isReferenceToClassSameCompilationUnit()) {
  15. ASTVariableDeclaratorId id = var.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
  16. ids.add(id);
  17. }
  18. }
  19. }
  20. }
  21. // if there are connections, ensure each is closed.
  22. for (ASTVariableDeclaratorId x : ids) {
  23. ensureClosed((ASTLocalVariableDeclaration) x.jjtGetParent().jjtGetParent(), x, data);
  24. }
  25. }

代码示例来源:origin: pmd/pmd

  1. private boolean doesNodeContainJUnitAnnotation(Node node, String annotationTypeClassName) {
  2. List<ASTAnnotation> annotations = node.findDescendantsOfType(ASTAnnotation.class);
  3. for (ASTAnnotation annotation : annotations) {
  4. Node annotationTypeNode = annotation.jjtGetChild(0);
  5. TypeNode annotationType = (TypeNode) annotationTypeNode;
  6. if (annotationType.getType() == null) {
  7. ASTName name = annotationTypeNode.getFirstChildOfType(ASTName.class);
  8. if (name != null && (name.hasImageEqualTo("Test") || name.hasImageEqualTo(annotationTypeClassName))) {
  9. return true;
  10. }
  11. } else if (TypeHelper.isA(annotationType, annotationTypeClassName)) {
  12. return true;
  13. }
  14. }
  15. return false;
  16. }

代码示例来源:origin: pmd/pmd

  1. return data;
  2. List<ASTLiteral> literals = parent.findDescendantsOfType(ASTLiteral.class);
  3. for (int l = 0; l < literals.size(); l++) {
  4. ASTLiteral literal = literals.get(l);

代码示例来源:origin: pmd/pmd

  1. addViolationWithMessage(data, child, child.getXPathNodeName() + " should be on line " + lineNumber);
  2. List<ASTEqualityExpression> conditions = child.findDescendantsOfType(ASTEqualityExpression.class);

相关文章