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

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

本文整理了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

/**
 * performs a check on the variable and updates the counter. Counter is
 * instance for a class and is reset upon new class scan.
 *
 * @param variableType
 *            The variable type.
 */
private void checkVariableType(Node nameNode, String variableType) {
  // TODO - move this into the symbol table somehow?
  if (nameNode.getParentsOfType(ASTClassOrInterfaceDeclaration.class).isEmpty()) {
    return;
  }
  // if the field is of any type other than the class type
  // increment the count
  ClassScope clzScope = ((JavaNode) nameNode).getScope().getEnclosingScope(ClassScope.class);
  if (!clzScope.getClassName().equals(variableType) && !this.filterTypes(variableType)
      && !this.typesFoundSoFar.contains(variableType)) {
    couplingCount++;
    typesFoundSoFar.add(variableType);
  }
}

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

/**
 * 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);
}

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

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

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

/**
 * Check if reference is inside macro.
 *
 * @param node node
 * @return true/false
 */
private boolean checkMacro(Node node) {
  List<ASTDirective> directiveParents = node.getParentsOfType(ASTDirective.class);
  for (ASTDirective directiveParent : directiveParents) {
    if (MACRO_NAME.equals(directiveParent.getDirectiveName())) {
      return true;
    }
  }
  return false;
}

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

/**
 * performs a check on the variable and updates the counter. Counter is
 * instance for a class and is reset upon new class scan.
 *
 * @param variableType
 *            The variable type.
 */
private void checkVariableType(Node nameNode, String variableType) {
  // TODO - move this into the symbol table somehow?
  if (nameNode.getParentsOfType(ASTClassOrInterfaceDeclaration.class).isEmpty()) {
    return;
  }
  // if the field is of any type other than the class type
  // increment the count
  ClassScope clzScope = ((JavaNode) nameNode).getScope().getEnclosingScope(ClassScope.class);
  if (!clzScope.getClassName().equals(variableType) && !this.filterTypes(variableType)
      && !this.typesFoundSoFar.contains(variableType)) {
    couplingCount++;
    typesFoundSoFar.add(variableType);
  }
}

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

/**
 * 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);
}

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

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

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

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

相关文章