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

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

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

Node.getFirstParentOfType介绍

[英]Traverses up the tree to find the first parent instance of type parentType or one of its subclasses.
[中]向上遍历树以查找parentType类型或其子类之一的第一个父实例。

代码示例

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

  1. private boolean inLoopOrTry(Node node) {
  2. return node.getFirstParentOfType(ASTTryStatement.class) != null
  3. || node.getFirstParentOfType(ASTForStatement.class) != null
  4. || node.getFirstParentOfType(ASTWhileStatement.class) != null
  5. || node.getFirstParentOfType(ASTDoStatement.class) != null;
  6. }

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

  1. private ASTIfStatement findIfStatement(ASTBlock enclosingBlock, Node node) {
  2. ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class);
  3. List<ASTIfStatement> allIfStatements = enclosingBlock.findDescendantsOfType(ASTIfStatement.class);
  4. if (ifStatement != null && allIfStatements.contains(ifStatement)) {
  5. return ifStatement;
  6. }
  7. return null;
  8. }

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

  1. private boolean inAnonymousInnerClass(Node node) {
  2. ASTClassOrInterfaceBodyDeclaration parent = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
  3. return parent != null && parent.isAnonymousInnerClass();
  4. }

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

  1. /**
  2. * Gets the Image of the first parent node of type
  3. * ASTClassOrInterfaceDeclaration or <code>null</code>
  4. *
  5. * @param node
  6. * the node which will be searched
  7. */
  8. protected final String getDeclaringType(Node node) {
  9. ASTClassOrInterfaceDeclaration c = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
  10. if (c != null) {
  11. return c.getImage();
  12. }
  13. return null;
  14. }

代码示例来源: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. private boolean calledFromOutsideItself(List<NameOccurrence> occs, NameDeclaration mnd) {
  2. int callsFromOutsideMethod = 0;
  3. for (NameOccurrence occ : occs) {
  4. Node occNode = occ.getLocation();
  5. ASTConstructorDeclaration enclosingConstructor = occNode
  6. .getFirstParentOfType(ASTConstructorDeclaration.class);
  7. if (enclosingConstructor != null) {
  8. callsFromOutsideMethod++;
  9. break; // Do we miss unused private constructors here?
  10. }
  11. ASTInitializer enclosingInitializer = occNode.getFirstParentOfType(ASTInitializer.class);
  12. if (enclosingInitializer != null) {
  13. callsFromOutsideMethod++;
  14. break;
  15. }
  16. ASTMethodDeclaration enclosingMethod = occNode.getFirstParentOfType(ASTMethodDeclaration.class);
  17. if (enclosingMethod == null || !mnd.getNode().jjtGetParent().equals(enclosingMethod)) {
  18. callsFromOutsideMethod++;
  19. }
  20. }
  21. return callsFromOutsideMethod == 0;
  22. }

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

  1. c = node.getFirstParentOfType(ASTPackageSpecification.class);
  2. if (c != null) {
  3. return c.getImage();
  4. c = node.getFirstParentOfType(ASTTypeSpecification.class);
  5. if (c != null) {
  6. return c.getImage();
  7. c = node.getFirstParentOfType(ASTPackageBody.class);
  8. if (c != null) {
  9. return c.getImage();
  10. c = node.getFirstParentOfType(ASTTriggerUnit.class);
  11. if (c != null) {
  12. return c.getImage();
  13. c = node.getFirstParentOfType(ASTProgramUnit.class);
  14. if (c != null) {
  15. return c.getImage();

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

  1. Node possibleStatement = statement.getFirstParentOfType(ASTIfStatement.class);
  2. while (possibleStatement instanceof ASTIfStatement) {
  3. statement = possibleStatement;
  4. possibleStatement = possibleStatement.getFirstParentOfType(ASTIfStatement.class);

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

  1. private boolean initializedInConstructor(List<NameOccurrence> usages) {
  2. boolean initInConstructor = false;
  3. for (NameOccurrence occ : usages) {
  4. // specifically omitting prefix and postfix operators as there are
  5. // legitimate usages of these with static fields, e.g. typesafe enum pattern.
  6. if (((JavaNameOccurrence) occ).isOnLeftHandSide()) {
  7. Node node = occ.getLocation();
  8. Node constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
  9. if (constructor != null) {
  10. initInConstructor = true;
  11. }
  12. }
  13. }
  14. return initInConstructor;
  15. }

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

  1. if (jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) {
  2. Node node = jocc.getLocation();
  3. ASTConstructorDeclaration constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
  4. if (constructor != null) {
  5. if (inLoopOrTry(node)) {
  6. if (node.getFirstParentOfType(ASTIfStatement.class) != null) {
  7. methodInitCount++;
  8. } else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
  9. lambdaUsage++;
  10. } else {
  11. if (node.getFirstParentOfType(ASTMethodDeclaration.class) != null) {
  12. methodInitCount++;
  13. } else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
  14. lambdaUsage++;

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

  1. /**
  2. * Check if the given node is the first statement in the block.
  3. */
  4. private boolean isFirstStatementInBlock(Node node, ASTStatement loopBody) {
  5. // find the statement of the operation and the loop body block statement
  6. final ASTBlockStatement statement = node.getFirstParentOfType(ASTBlockStatement.class);
  7. final ASTBlock block = loopBody.getFirstDescendantOfType(ASTBlock.class);
  8. if (statement == null || block == null) {
  9. return false;
  10. }
  11. // is the first statement in the loop body?
  12. return block.equals(statement.jjtGetParent()) && statement.jjtGetChildIndex() == 0;
  13. }

代码示例来源: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. private int getConstructorLength(Node node, int constructorLength) {
  2. int iConstructorLength = constructorLength;
  3. Node block = node.getFirstParentOfType(ASTBlockStatement.class);
  4. block = node.getFirstParentOfType(ASTFieldDeclaration.class);
  5. block = node.getFirstParentOfType(ASTFormalParameter.class);
  6. if (block != null) {
  7. iConstructorLength = -1;

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

  1. private boolean isCommentNotWithin(FormalComment n1, Node n2, Node node) {
  2. if (n1 == null || n2 == null || node == null) {
  3. return true;
  4. }
  5. boolean isNotWithinNode2 = !(n1.getEndLine() < n2.getEndLine()
  6. || n1.getEndLine() == n2.getEndLine() && n1.getEndColumn() < n2.getEndColumn());
  7. boolean isNotSameClass = node.getFirstParentOfType(ASTClassOrInterfaceBody.class) != n2
  8. .getFirstParentOfType(ASTClassOrInterfaceBody.class);
  9. boolean isNodeWithinNode2 = node.getEndLine() < n2.getEndLine()
  10. || node.getEndLine() == n2.getEndLine() && node.getEndColumn() < n2.getEndColumn();
  11. return isNotWithinNode2 || isNotSameClass || isNodeWithinNode2;
  12. }

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

  1. private boolean methodHasOverride(Node node) {
  2. ASTClassOrInterfaceBodyDeclaration method = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
  3. if (method != null && method.jjtGetNumChildren() > 0 && method.jjtGetChild(0) instanceof ASTAnnotation) {
  4. ASTMarkerAnnotation marker = method.getFirstDescendantOfType(ASTMarkerAnnotation.class);
  5. if (marker != null && marker.getFirstChildOfType(ASTName.class) != null) {
  6. ASTName name = marker.getFirstChildOfType(ASTName.class);
  7. if (name.getType() == Override.class) {
  8. return true;
  9. }
  10. }
  11. }
  12. return false;
  13. }
  14. }

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

  1. if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {
  2. continue;
  3. ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class);
  4. if (method != null && !method.isSynchronized()) {
  5. addViolation(data, n);

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

  1. protected static boolean isInStringBufferOperation(Node node, int length, String methodName) {
  2. if (!(node.getNthParent(length) instanceof ASTStatementExpression)) {
  3. return false;
  4. }
  5. ASTStatementExpression s = node.getFirstParentOfType(ASTStatementExpression.class);
  6. if (s == null) {
  7. return false;
  8. }
  9. ASTName n = s.getFirstDescendantOfType(ASTName.class);
  10. if (n == null || n.getImage().indexOf(methodName) == -1
  11. || !(n.getNameDeclaration() instanceof TypedNameDeclaration)) {
  12. return false;
  13. }
  14. // TODO having to hand-code this kind of dredging around is ridiculous
  15. // we need something to support this in the framework
  16. // but, "for now" (tm):
  17. // if more than one arg to append(), skip it
  18. ASTArgumentList argList = s.getFirstDescendantOfType(ASTArgumentList.class);
  19. if (argList == null || argList.jjtGetNumChildren() > 1) {
  20. return false;
  21. }
  22. return TypeHelper.isExactlyAny((TypedNameDeclaration) n.getNameDeclaration(), StringBuffer.class,
  23. StringBuilder.class);
  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. Node parentNode = n.getNode().getFirstParentOfType(dataFlowHandler.getLabelStatementNodeClass());
  2. if (parentNode == null) {
  3. break;

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

  1. Node loc = jocc.getLocation();
  2. if (loc != null) {
  3. ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
  4. while (exp != null) {
  5. if (exp.jjtGetParent() instanceof ASTStatementExpression) {

相关文章