net.sourceforge.pmd.lang.ast.Node类的使用及代码示例

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

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

Node介绍

[英]All AST nodes must implement this interface. It provides basic machinery for constructing the parent and child relationships between nodes.
[中]所有AST节点都必须实现此接口。它为构建节点之间的父子关系提供了基本机制。

代码示例

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

protected Node getFirstChild(Node contextNode) {
  if (contextNode.jjtGetNumChildren() > 0) {
    return contextNode.jjtGetChild(0);
  } else {
    return null;
  }
}

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

private int checkEachChildOnNextLine(Object data, Node parent, int firstLine, int indentation) {
  int currentLine = firstLine;
  for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
    Node child = parent.jjtGetChild(i);
    if (child.getBeginLine() != currentLine) {
      addViolationWithMessage(data, child, child.getImage() + " should be on line " + currentLine);
    } else if (i > 0 && child.getBeginColumn() != indentation) {
      addViolationWithMessage(data, child, child.getImage() + " should begin at column " + indentation);
    }
    // next entry needs to be on the next line
    currentLine++;
  }
  return currentLine;
}

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

public void closeNodeScope(Node n, int num) {
 mk = marks.remove(marks.size()-1);
 while (num-- > 0) {
  Node c = popNode();
  c.jjtSetParent(n);
  n.jjtAddChild(c, num);
 }
 n.jjtClose();
 pushNode(n);
 node_created = true;
}

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

/**
 * This method can be called on a prefix
 */
private ASTArguments getSuffixMethodArgs(Node node) {
  Node prefix = node.jjtGetParent();
  if (prefix instanceof ASTPrimaryPrefix
      && prefix.jjtGetParent().jjtGetNumChildren() >= 2) {
    return prefix.jjtGetParent().jjtGetChild(1).getFirstChildOfType(ASTArguments.class);
  }
  return null;
}

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

protected Node getNextSibling(Node contextNode) {
  Node parentNode = contextNode.jjtGetParent();
  if (parentNode != null) {
    int nextPosition = contextNode.jjtGetChildIndex() + 1;
    if (nextPosition < parentNode.jjtGetNumChildren()) {
      return parentNode.jjtGetChild(nextPosition);
    }
  }
  return null;
}

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

/**
 * Indicate whether this node is allocating a new object.
 *
 * @param n
 *            node that might be allocating a new object
 * @return true if child 0 is an AllocationExpression
 */
private boolean isAllocation(Node n) {
  return n.jjtGetNumChildren() > 0 && n.jjtGetChild(0) instanceof ASTAllocationExpression
      && n.jjtGetParent().jjtGetNumChildren() == 1;
}

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

public ParametricRuleViolation(Rule theRule, RuleContext ctx, T node, String message) {
  rule = theRule;
  description = message;
  filename = ctx.getSourceCodeFilename();
  if (filename == null) {
    filename = "";
  }
  if (node != null) {
    beginLine = node.getBeginLine();
    beginColumn = node.getBeginColumn();
    endLine = node.getEndLine();
    endColumn = node.getEndColumn();
  }
  // Apply Rule specific suppressions
  if (node != null && rule != null) {
    setSuppression(rule, node);
  }
}

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

@Override
public boolean isTargetMethod(JavaNameOccurrence occ) {
  if (occ.getNameForWhichThisIsAQualifier() != null
      && occ.getNameForWhichThisIsAQualifier().getImage().indexOf("trim") != -1) {
    Node pExpression = occ.getLocation().jjtGetParent().jjtGetParent();
    if (pExpression.jjtGetNumChildren() > 2 && "length".equals(pExpression.jjtGetChild(2).getImage())) {
      return true;
    }
  }
  return false;
}

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

private void collectNames(String target, List<String> names, Node node) {
  for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    Node child = node.jjtGetChild(i);
    if (child.jjtGetNumChildren() > 0) {
      collectNames(target, names, child);
    } else {
      if (child instanceof ASTName && isQualifiedName(child)
          && target.equals(getVariableName(child.getImage()))) {
        names.add(child.getImage());
      }
    }
  }
}

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

private static int compareNodes(Node n1, Node n2) {
  int l1 = n1.getBeginLine();
  int l2 = n2.getBeginLine();
  if (l1 == l2) {
    return n1.getBeginColumn() - n2.getBeginColumn();
  }
  return l1 - l2;
}

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

private void setVariableNameIfExists(Node node) {
    if (node instanceof ASTFieldDeclaration) {
      variableName = getVariableNames((ASTFieldDeclaration) node);
    } else if (node instanceof ASTLocalVariableDeclaration) {
      variableName = getVariableNames((ASTLocalVariableDeclaration) node);
    } else if (node instanceof ASTVariableDeclarator) {
      variableName = node.jjtGetChild(0).getImage();
    } else if (node instanceof ASTVariableDeclaratorId) {
      variableName = node.getImage();
    } else if (node instanceof ASTFormalParameter) {
      setVariableNameIfExists(node.getFirstChildOfType(ASTVariableDeclaratorId.class));
    } else {
      variableName = "";
    }
  }
}

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

protected Node getPreviousSibling(Node contextNode) {
  Node parentNode = contextNode.jjtGetParent();
  if (parentNode != null) {
    int prevPosition = contextNode.jjtGetChildIndex() - 1;
    if (prevPosition >= 0) {
      return parentNode.jjtGetChild(prevPosition);
    }
  }
  return null;
}

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

if (argument.jjtGetChild(0).getEndColumn() > longestParameterEndColumn) {
      longestParameterEndColumn = argument.jjtGetChild(0).getEndColumn();
    && arguments.get(0).jjtGetChild(1).getBeginColumn() > expectedBeginColumn) {
  expectedBeginColumn = arguments.get(0).jjtGetChild(1).getBeginColumn();
    checkIndentation(data, expr, expectedBeginColumn, expr.getImage());
if (primaryExpression.getEndLine() != node.getEndLine() + 1) {
  addViolationWithMessage(data, primaryExpression, "Closing paranthesis should be on a new line.");

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

@Override
public Object visit(ASTSubqueryOperation node, Object data) {
  // get previous sibling
  int thisIndex = node.jjtGetChildIndex();
  Node prevSibling = node.jjtGetParent().jjtGetChild(thisIndex - 1);
  checkIndentation(data, node, prevSibling.getBeginColumn(), node.getImage());
  // it should also be on the next line
  if (node.getBeginLine() != prevSibling.getEndLine() + 1) {
    addViolationWithMessage(data, node,
        node.getImage() + " should be on line " + (prevSibling.getEndLine() + 1));
  }
  return super.visit(node, data);
}

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

/**
 * Returns the name of the annotation as it is used,
 * eg {@code java.lang.Override} or {@code Override}.
 */
public String getAnnotationName() {
  return jjtGetChild(0).jjtGetChild(0).getImage();
}

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

private <T extends AstNode> EcmascriptNode<T> buildInternal(T astNode) {
  // Create a Node
  EcmascriptNode<T> node = createNodeAdapter(astNode);
  // Append to parent
  Node parent = nodes.isEmpty() ? null : nodes.peek();
  if (parent != null) {
    parent.jjtAddChild(node, parent.jjtGetNumChildren());
    node.jjtSetParent(parent);
  }
  handleParseProblems(node);
  // Build the children...
  nodes.push(node);
  parents.push(astNode);
  astNode.visit(this);
  nodes.pop();
  parents.pop();
  return node;
}

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

private static boolean isAfter(Node n1, Node n2) {
  return n1.getBeginLine() > n2.getBeginLine()
      || n1.getBeginLine() == n2.getBeginLine() && n1.getBeginColumn() >= n2.getEndColumn();
}

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

private ASTTypeParameter getTypeParameterDeclaration(Node startNode, String image) {
  for (Node parent = startNode.jjtGetParent(); parent != null; parent = parent.jjtGetParent()) {
    ASTTypeParameters typeParameters = null;
    if (parent instanceof ASTTypeParameters) { // if type parameter defined in the same < >
      typeParameters = (ASTTypeParameters) parent;
    } else if (parent instanceof ASTConstructorDeclaration
        || parent instanceof ASTMethodDeclaration
        || parent instanceof ASTClassOrInterfaceDeclaration) {
      typeParameters = parent.getFirstChildOfType(ASTTypeParameters.class);
    }
    if (typeParameters != null) {
      for (int index = 0; index < typeParameters.jjtGetNumChildren(); ++index) {
        String imageToCompareTo = typeParameters.jjtGetChild(index).getImage();
        if (imageToCompareTo != null && imageToCompareTo.equals(image)) {
          return (ASTTypeParameter) typeParameters.jjtGetChild(index);
        }
      }
    }
  }
  return null;
}

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

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;
}

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

private String getLastPartOfName(Node name) {
  String result = "";
  if (name != null) {
    result = name.getImage();
  }
  int dotIndex = result.lastIndexOf('.');
  if (dotIndex > -1 && result.length() > dotIndex + 1) {
    result = result.substring(dotIndex + 1);
  }
  return result;
}

相关文章