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

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

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

Node.getFirstDescendantOfType介绍

[英]Traverses down the tree to find the first descendant instance of type descendantType without crossing find boundaries.
[中]沿树向下遍历,以查找类型为genderantType的第一个子实例,而不跨越查找边界。

代码示例

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

  1. private boolean isAdditive(Node n) {
  2. ASTAdditiveExpression add = n.getFirstDescendantOfType(ASTAdditiveExpression.class);
  3. // if the first descendant additive expression is deeper than 4 levels,
  4. // it belongs to a nested method call and not anymore to the append
  5. // argument.
  6. return add != null && add.getNthParent(4) == n;
  7. }

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

  1. private int processAdditive(Node sn) {
  2. ASTAdditiveExpression additive = sn.getFirstDescendantOfType(ASTAdditiveExpression.class);
  3. if (additive == null) {
  4. return 0;
  5. }
  6. int anticipatedLength = 0;
  7. for (int ix = 0; ix < additive.jjtGetNumChildren(); ix++) {
  8. Node childNode = additive.jjtGetChild(ix);
  9. ASTLiteral literal = childNode.getFirstDescendantOfType(ASTLiteral.class);
  10. if (literal != null && literal.getImage() != null) {
  11. anticipatedLength += literal.getImage().length() - 2;
  12. }
  13. }
  14. return anticipatedLength;
  15. }

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

  1. private String getExpressionVarName(Node e) {
  2. String assignedVar = getFirstNameImage(e);
  3. if (assignedVar == null) {
  4. ASTPrimarySuffix suffix = e.getFirstDescendantOfType(ASTPrimarySuffix.class);
  5. if (suffix != null) {
  6. assignedVar = suffix.getImage();
  7. ASTPrimaryPrefix prefix = e.getFirstDescendantOfType(ASTPrimaryPrefix.class);
  8. if (prefix != null) {
  9. if (prefix.usesThisModifier()) {
  10. assignedVar = "this." + assignedVar;
  11. } else if (prefix.usesSuperModifier()) {
  12. assignedVar = "super." + assignedVar;
  13. }
  14. }
  15. }
  16. }
  17. return assignedVar;
  18. }

代码示例来源: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. /**
  2. * Gets the image of the ASTName node found by
  3. * {@link Node#getFirstDescendantOfType(Class)} if it is the greatgrandchild
  4. * of the given node.
  5. *
  6. * E.g.
  7. *
  8. * <pre>
  9. * n = Expression || StatementExpression
  10. * PrimaryExpression
  11. * PrimaryPrefix
  12. * Name
  13. * </pre>
  14. *
  15. * @param n
  16. * the node to search
  17. * @return the image of the first ASTName or <code>null</code>
  18. */
  19. protected String getFirstNameImage(Node n) {
  20. ASTName name = n.getFirstDescendantOfType(ASTName.class);
  21. if (name != null && name.getNthParent(3) == n) {
  22. return name.getImage();
  23. }
  24. return null;
  25. }

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

  1. /**
  2. * Determines the log level, that is used. It is either the called method name
  3. * itself or - in case java util logging is used, then it is the first argument of
  4. * the method call (if it exists).
  5. *
  6. * @param node the method call
  7. * @param methodCallName the called method name previously determined
  8. * @return the log level or <code>null</code> if it could not be determined
  9. */
  10. private String getLogLevelName(Node node, String methodCallName) {
  11. if (!JAVA_UTIL_LOG_METHOD.equals(methodCallName) && !JAVA_UTIL_LOG_GUARD_METHOD.equals(methodCallName)) {
  12. return methodCallName;
  13. }
  14. String logLevel = null;
  15. ASTPrimarySuffix suffix = node.getFirstDescendantOfType(ASTPrimarySuffix.class);
  16. if (suffix != null) {
  17. ASTArgumentList argumentList = suffix.getFirstDescendantOfType(ASTArgumentList.class);
  18. if (argumentList != null && argumentList.jjtGetNumChildren() > 0) {
  19. // at least one argument - the log level. If the method call is "log", then a message might follow
  20. ASTName name = GuardLogStatementRule.<ASTName>getFirstChild(argumentList.jjtGetChild(0),
  21. ASTPrimaryExpression.class, ASTPrimaryPrefix.class, ASTName.class);
  22. String lastPart = getLastPartOfName(name);
  23. lastPart = lastPart.toLowerCase(Locale.ROOT);
  24. if (!lastPart.isEmpty()) {
  25. logLevel = lastPart;
  26. }
  27. }
  28. }
  29. return logLevel;
  30. }

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

  1. private Entry<VariableNameDeclaration, List<NameOccurrence>> getIterableDeclOfIteratorLoop(VariableNameDeclaration indexDecl, Scope scope) {
  2. Node initializer = indexDecl.getNode().getFirstParentOfType(ASTVariableDeclarator.class)
  3. .getFirstChildOfType(ASTVariableInitializer.class);
  4. if (initializer == null) {
  5. return null;
  6. }
  7. ASTName nameNode = initializer.getFirstDescendantOfType(ASTName.class);
  8. if (nameNode == null) {
  9. // TODO : This can happen if we are calling a local / statically imported method that returns the iterable - currently unhandled
  10. return null;
  11. }
  12. String name = nameNode.getImage();
  13. int dotIndex = name.indexOf('.');
  14. if (dotIndex > 0) {
  15. name = name.substring(0, dotIndex);
  16. }
  17. return findDeclaration(name, scope);
  18. }

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

  1. /**
  2. * Gets a list of NameDeclarations for all the fields that have type
  3. * ExpectedException and have a Rule annotation.
  4. *
  5. * @param classScope
  6. * The class scope to search for
  7. * @return See description
  8. */
  9. private Map<String, List<NameOccurrence>> getRuleAnnotatedExpectedExceptions(Scope classScope) {
  10. Map<String, List<NameOccurrence>> result = new HashMap<>();
  11. Map<NameDeclaration, List<NameOccurrence>> decls = classScope.getDeclarations();
  12. for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
  13. Node parent = entry.getKey().getNode().jjtGetParent().jjtGetParent().jjtGetParent();
  14. if (parent.hasDescendantOfType(ASTMarkerAnnotation.class)
  15. && parent.getFirstChildOfType(ASTFieldDeclaration.class) != null) {
  16. String annot = parent.getFirstDescendantOfType(ASTMarkerAnnotation.class).jjtGetChild(0).getImage();
  17. if (!"Rule".equals(annot) && !"org.junit.Rule".equals(annot)) {
  18. continue;
  19. }
  20. Node type = parent.getFirstDescendantOfType(ASTReferenceType.class);
  21. if (!"ExpectedException".equals(type.jjtGetChild(0).getImage())) {
  22. continue;
  23. }
  24. result.put(entry.getKey().getName(), entry.getValue());
  25. }
  26. }
  27. return result;
  28. }

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

  1. private Object process(Node node, Object data) {
  2. ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
  3. if (cit == null || !implClassNames.contains(cit.getImage())) {
  4. return data;
  5. }
  6. cit = cit.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
  7. if (cit == null) {
  8. return data;
  9. }
  10. ASTVariableDeclaratorId decl = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
  11. List<NameOccurrence> usages = decl.getUsages();
  12. for (NameOccurrence no : usages) {
  13. ASTName name = (ASTName) no.getLocation();
  14. Node n = name.jjtGetParent().jjtGetParent().jjtGetParent();
  15. if (n instanceof ASTCastExpression) {
  16. addViolation(data, n);
  17. }
  18. }
  19. return null;
  20. }
  21. }

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

  1. @Override
  2. public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) {
  3. boolean inAnnotation = false;
  4. for (int i = 0; i < node.jjtGetNumChildren(); i++) {
  5. Node child = node.jjtGetChild(i);
  6. if (child instanceof ASTAnnotation) {
  7. ASTName annotationName = child.getFirstDescendantOfType(ASTName.class);
  8. if ("Test".equals(annotationName.getImage())) {
  9. inAnnotation = true;
  10. continue;
  11. }
  12. }
  13. if (child instanceof ASTMethodDeclaration) {
  14. boolean isJUnitMethod = isJUnitMethod((ASTMethodDeclaration) child, data);
  15. if (inAnnotation || isJUnitMethod) {
  16. List<Node> found = new ArrayList<>();
  17. found.addAll((List<Node>) visit((ASTMethodDeclaration) child, data));
  18. for (Node name : found) {
  19. addViolation(data, name);
  20. }
  21. }
  22. }
  23. inAnnotation = false;
  24. }
  25. return super.visit(node, data);
  26. }

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

  1. private List<VariableAccess> markUsages(DataFlowNode inode) {
  2. // undefinitions was once a field... seems like it works fine as a local
  3. List<VariableAccess> undefinitions = new ArrayList<>();
  4. Set<Map<NameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
  5. for (Map<NameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
  6. for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
  7. NameDeclaration vnd = entry.getKey();
  8. if (vnd.getNode().jjtGetParent() instanceof ASTFormalParameter) {
  9. // no definition/undefinition/references for parameters
  10. continue;
  11. } else if (vnd.getNode().jjtGetParent()
  12. .getFirstDescendantOfType(ASTVariableOrConstantInitializer.class) != null) {
  13. // add definition for initialized variables
  14. addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()),
  15. inode.getFlow());
  16. }
  17. undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
  18. for (NameOccurrence occurrence : entry.getValue()) {
  19. addAccess(occurrence, inode);
  20. }
  21. }
  22. }
  23. return undefinitions;
  24. }

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

  1. private int getConstructorAppendsLength(final Node node) {
  2. final Node parent = node.getFirstParentOfType(ASTVariableDeclarator.class);
  3. int size = 0;
  4. if (parent != null) {
  5. final Node initializer = parent.getFirstChildOfType(ASTVariableInitializer.class);
  6. if (initializer != null) {
  7. final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
  8. if (primExp != null) {
  9. for (int i = 0; i < primExp.jjtGetNumChildren(); i++) {
  10. final Node sn = primExp.jjtGetChild(i);
  11. if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) {
  12. continue;
  13. }
  14. size += processNode(sn);
  15. }
  16. }
  17. }
  18. }
  19. return size;
  20. }

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

  1. private int processNode(Node sn) {
  2. int anticipatedLength = 0;
  3. if (sn != null) {
  4. ASTPrimaryPrefix xn = sn.getFirstDescendantOfType(ASTPrimaryPrefix.class);
  5. if (xn != null) {
  6. if (xn.jjtGetNumChildren() != 0 && xn.jjtGetChild(0) instanceof ASTLiteral) {

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

  1. @Override
  2. public Object visit(ASTAllocationExpression node, Object data) {
  3. ASTClassOrInterfaceType declClassName = node.getFirstChildOfType(ASTClassOrInterfaceType.class);
  4. if (declClassName != null && TypeHelper.isA(declClassName, javax.crypto.spec.IvParameterSpec.class)) {
  5. Node firstArgument = null;
  6. ASTArguments arguments = node.getFirstChildOfType(ASTArguments.class);
  7. if (arguments.getArgumentCount() > 0) {
  8. firstArgument = arguments.getFirstChildOfType(ASTArgumentList.class).jjtGetChild(0);
  9. }
  10. if (firstArgument != null) {
  11. ASTPrimaryPrefix prefix = firstArgument.getFirstDescendantOfType(ASTPrimaryPrefix.class);
  12. validateProperIv(data, prefix);
  13. }
  14. }
  15. return data;
  16. }

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

  1. @Override
  2. public Object visit(ASTAllocationExpression node, Object data) {
  3. ASTClassOrInterfaceType declClassName = node.getFirstChildOfType(ASTClassOrInterfaceType.class);
  4. if (declClassName != null && TypeHelper.isA(declClassName, SECRET_KEY_SPEC)) {
  5. Node firstArgument = null;
  6. ASTArguments arguments = node.getFirstChildOfType(ASTArguments.class);
  7. if (arguments.getArgumentCount() > 0) {
  8. firstArgument = arguments.getFirstChildOfType(ASTArgumentList.class).jjtGetChild(0);
  9. }
  10. if (firstArgument != null) {
  11. ASTPrimaryPrefix prefix = firstArgument.getFirstDescendantOfType(ASTPrimaryPrefix.class);
  12. validateProperKeyArgument(data, prefix);
  13. }
  14. }
  15. return data;
  16. }

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

  1. /**
  2. * Determine if the constructor contains (or ends with) a String Literal
  3. *
  4. * @param node
  5. * @return 1 if the constructor contains string argument, else 0
  6. */
  7. private int checkConstructor(ASTVariableDeclaratorId node, Object data) {
  8. Node parent = node.jjtGetParent();
  9. if (parent.jjtGetNumChildren() >= 2) {
  10. ASTAllocationExpression allocationExpression = parent.jjtGetChild(1)
  11. .getFirstDescendantOfType(ASTAllocationExpression.class);
  12. ASTArgumentList list = null;
  13. if (allocationExpression != null) {
  14. list = allocationExpression.getFirstDescendantOfType(ASTArgumentList.class);
  15. }
  16. if (list != null) {
  17. ASTLiteral literal = list.getFirstDescendantOfType(ASTLiteral.class);
  18. if (!isAdditive(list) && literal != null && literal.isStringLiteral()) {
  19. return 1;
  20. }
  21. return processAdditive(data, 0, list, node);
  22. }
  23. }
  24. return 0;
  25. }

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

  1. /**
  2. * Gets the name of the variable returned. Some examples: <br>
  3. * for this.foo returns foo <br>
  4. * for foo returns foo <br>
  5. * for foo.bar returns foo.bar
  6. *
  7. * @param ret
  8. * a return statement to evaluate
  9. * @return the name of the variable associated or <code>null</code> if it
  10. * cannot be detected
  11. */
  12. protected final String getReturnedVariableName(ASTReturnStatement ret) {
  13. if (hasTernaryCondition(ret) && hasTernaryNullCheck(ret)) {
  14. return ret.getFirstDescendantOfType(ASTConditionalExpression.class).jjtGetChild(0)
  15. .getFirstDescendantOfType(ASTName.class).getImage();
  16. }
  17. final ASTName n = ret.getFirstDescendantOfType(ASTName.class);
  18. if (n != null) {
  19. return n.getImage();
  20. }
  21. final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
  22. if (ps != null) {
  23. return ps.getImage();
  24. }
  25. return null;
  26. }

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

  1. for (int a = 0; a < argumentList.jjtGetNumChildren(); a++) {
  2. Node expression = argumentList.jjtGetChild(a);
  3. ASTPrimaryPrefix arg = expression.getFirstDescendantOfType(ASTPrimaryPrefix.class);
  4. String type = "<unknown>";
  5. if (arg != null && arg.jjtGetNumChildren() > 0) {

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

  1. private int processAdditive(Object data, int concurrentCount, Node sn, Node rootNode) {
  2. ASTAdditiveExpression additive = sn.getFirstDescendantOfType(ASTAdditiveExpression.class);

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

  1. ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
  2. if (args != null) {
  3. ASTName arg = args.getFirstDescendantOfType(ASTName.class);
  4. if (declaration != null) {
  5. ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent()
  6. .getFirstDescendantOfType(ASTType.class);
  7. if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType
  8. && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) {

相关文章