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

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

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

Node.getParentsOfType介绍

[英]Traverses up the tree to find all of the parent instances of type parentType or one of its subclasses. The nodes are ordered deepest-first.
[中]向上遍历树以查找parentType类型或其子类之一的所有父实例。首先对节点进行排序。

代码示例

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

  1. /**
  2. * performs a check on the variable and updates the counter. Counter is
  3. * instance for a class and is reset upon new class scan.
  4. *
  5. * @param variableType
  6. * The variable type.
  7. */
  8. private void checkVariableType(Node nameNode, String variableType) {
  9. // TODO - move this into the symbol table somehow?
  10. if (nameNode.getParentsOfType(ASTClassOrInterfaceDeclaration.class).isEmpty()) {
  11. return;
  12. }
  13. // if the field is of any type other than the class type
  14. // increment the count
  15. ClassScope clzScope = ((JavaNode) nameNode).getScope().getEnclosingScope(ClassScope.class);
  16. if (!clzScope.getClassName().equals(variableType) && !this.filterTypes(variableType)
  17. && !this.typesFoundSoFar.contains(variableType)) {
  18. couplingCount++;
  19. typesFoundSoFar.add(variableType);
  20. }
  21. }

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

  1. /**
  2. * Tries to resolve a given typeImage as a generic Type. If the Generic Type
  3. * is found, any defined ClassOrInterfaceType below this type declaration is
  4. * used (this is typically a type bound, e.g. {@code <T extends List>}.
  5. *
  6. * @param argument
  7. * the node, from where to start searching.
  8. * @param typeImage
  9. * the type as string
  10. * @return the resolved class or <code>null</code> if nothing was found.
  11. */
  12. private Class<?> resolveGenericType(Node argument, String typeImage) {
  13. List<ASTTypeParameter> types = new ArrayList<>();
  14. // first search only within the same method
  15. ASTClassOrInterfaceBodyDeclaration firstParentOfType = argument
  16. .getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
  17. if (firstParentOfType != null) {
  18. types.addAll(firstParentOfType.findDescendantsOfType(ASTTypeParameter.class));
  19. }
  20. // then search class level types, from inner-most to outer-most
  21. List<ASTClassOrInterfaceDeclaration> enclosingClasses = argument
  22. .getParentsOfType(ASTClassOrInterfaceDeclaration.class);
  23. for (ASTClassOrInterfaceDeclaration enclosing : enclosingClasses) {
  24. ASTTypeParameters classLevelTypeParameters = enclosing.getFirstChildOfType(ASTTypeParameters.class);
  25. if (classLevelTypeParameters != null) {
  26. types.addAll(classLevelTypeParameters.findDescendantsOfType(ASTTypeParameter.class));
  27. }
  28. }
  29. return resolveGenericType(typeImage, types);
  30. }

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

  1. .getParentsOfType(ASTStatementExpression.class);
  2. for (ASTStatementExpression statementExpression : parentStatements) {
  3. if (statementExpression != null && statementExpression.equals(potentialStatement)) {

代码示例来源:origin: com.alibaba.p3c/p3c-pmd

  1. /**
  2. * Check if reference is inside macro.
  3. *
  4. * @param node node
  5. * @return true/false
  6. */
  7. private boolean checkMacro(Node node) {
  8. List<ASTDirective> directiveParents = node.getParentsOfType(ASTDirective.class);
  9. for (ASTDirective directiveParent : directiveParents) {
  10. if (MACRO_NAME.equals(directiveParent.getDirectiveName())) {
  11. return true;
  12. }
  13. }
  14. return false;
  15. }

代码示例来源:origin: net.sourceforge.pmd/pmd-java

  1. /**
  2. * performs a check on the variable and updates the counter. Counter is
  3. * instance for a class and is reset upon new class scan.
  4. *
  5. * @param variableType
  6. * The variable type.
  7. */
  8. private void checkVariableType(Node nameNode, String variableType) {
  9. // TODO - move this into the symbol table somehow?
  10. if (nameNode.getParentsOfType(ASTClassOrInterfaceDeclaration.class).isEmpty()) {
  11. return;
  12. }
  13. // if the field is of any type other than the class type
  14. // increment the count
  15. ClassScope clzScope = ((JavaNode) nameNode).getScope().getEnclosingScope(ClassScope.class);
  16. if (!clzScope.getClassName().equals(variableType) && !this.filterTypes(variableType)
  17. && !this.typesFoundSoFar.contains(variableType)) {
  18. couplingCount++;
  19. typesFoundSoFar.add(variableType);
  20. }
  21. }

代码示例来源:origin: net.sourceforge.pmd/pmd-java

  1. /**
  2. * Tries to resolve a given typeImage as a generic Type. If the Generic Type
  3. * is found, any defined ClassOrInterfaceType below this type declaration is
  4. * used (this is typically a type bound, e.g. {@code <T extends List>}.
  5. *
  6. * @param argument
  7. * the node, from where to start searching.
  8. * @param typeImage
  9. * the type as string
  10. * @return the resolved class or <code>null</code> if nothing was found.
  11. */
  12. private Class<?> resolveGenericType(Node argument, String typeImage) {
  13. List<ASTTypeParameter> types = new ArrayList<>();
  14. // first search only within the same method
  15. ASTClassOrInterfaceBodyDeclaration firstParentOfType = argument
  16. .getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
  17. if (firstParentOfType != null) {
  18. types.addAll(firstParentOfType.findDescendantsOfType(ASTTypeParameter.class));
  19. }
  20. // then search class level types, from inner-most to outer-most
  21. List<ASTClassOrInterfaceDeclaration> enclosingClasses = argument
  22. .getParentsOfType(ASTClassOrInterfaceDeclaration.class);
  23. for (ASTClassOrInterfaceDeclaration enclosing : enclosingClasses) {
  24. ASTTypeParameters classLevelTypeParameters = enclosing.getFirstChildOfType(ASTTypeParameters.class);
  25. if (classLevelTypeParameters != null) {
  26. types.addAll(classLevelTypeParameters.findDescendantsOfType(ASTTypeParameter.class));
  27. }
  28. }
  29. return resolveGenericType(typeImage, types);
  30. }

代码示例来源:origin: com.alibaba.p3c/p3c-pmd

  1. item.getParentsOfType(ASTVariableDeclarator.class);
  2. if (parents == null || parents.size() == 0 || parents.size() > 1) {
  3. continue;

代码示例来源:origin: net.sourceforge.pmd/pmd-java

  1. .getParentsOfType(ASTStatementExpression.class);
  2. for (ASTStatementExpression statementExpression : parentStatements) {
  3. if (statementExpression != null && statementExpression.equals(potentialStatement)) {

相关文章