本文整理了Java中net.sourceforge.pmd.lang.ast.Node.getFirstParentOfType()
方法的一些代码示例,展示了Node.getFirstParentOfType()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node.getFirstParentOfType()
方法的具体详情如下:
包路径:net.sourceforge.pmd.lang.ast.Node
类名称:Node
方法名:getFirstParentOfType
[英]Traverses up the tree to find the first parent instance of type parentType or one of its subclasses.
[中]向上遍历树以查找parentType类型或其子类之一的第一个父实例。
代码示例来源:origin: pmd/pmd
private boolean inLoopOrTry(Node node) {
return node.getFirstParentOfType(ASTTryStatement.class) != null
|| node.getFirstParentOfType(ASTForStatement.class) != null
|| node.getFirstParentOfType(ASTWhileStatement.class) != null
|| node.getFirstParentOfType(ASTDoStatement.class) != null;
}
代码示例来源:origin: pmd/pmd
private ASTIfStatement findIfStatement(ASTBlock enclosingBlock, Node node) {
ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class);
List<ASTIfStatement> allIfStatements = enclosingBlock.findDescendantsOfType(ASTIfStatement.class);
if (ifStatement != null && allIfStatements.contains(ifStatement)) {
return ifStatement;
}
return null;
}
代码示例来源:origin: pmd/pmd
private boolean inAnonymousInnerClass(Node node) {
ASTClassOrInterfaceBodyDeclaration parent = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
return parent != null && parent.isAnonymousInnerClass();
}
代码示例来源:origin: pmd/pmd
/**
* Gets the Image of the first parent node of type
* ASTClassOrInterfaceDeclaration or <code>null</code>
*
* @param node
* the node which will be searched
*/
protected final String getDeclaringType(Node node) {
ASTClassOrInterfaceDeclaration c = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
if (c != null) {
return c.getImage();
}
return null;
}
代码示例来源:origin: pmd/pmd
private int getInitialLength(Node node) {
Node block = node.getFirstParentOfType(ASTBlockStatement.class);
if (block == null) {
block = node.getFirstParentOfType(ASTFieldDeclaration.class);
if (block == null) {
block = node.getFirstParentOfType(ASTFormalParameter.class);
}
}
List<ASTLiteral> literals = block.findDescendantsOfType(ASTLiteral.class);
if (literals.size() == 1) {
ASTLiteral literal = literals.get(0);
String str = literal.getImage();
if (str != null && isStringOrCharLiteral(literal)) {
return str.length() - 2; // take off the quotes
}
}
return 0;
}
代码示例来源:origin: pmd/pmd
private boolean calledFromOutsideItself(List<NameOccurrence> occs, NameDeclaration mnd) {
int callsFromOutsideMethod = 0;
for (NameOccurrence occ : occs) {
Node occNode = occ.getLocation();
ASTConstructorDeclaration enclosingConstructor = occNode
.getFirstParentOfType(ASTConstructorDeclaration.class);
if (enclosingConstructor != null) {
callsFromOutsideMethod++;
break; // Do we miss unused private constructors here?
}
ASTInitializer enclosingInitializer = occNode.getFirstParentOfType(ASTInitializer.class);
if (enclosingInitializer != null) {
callsFromOutsideMethod++;
break;
}
ASTMethodDeclaration enclosingMethod = occNode.getFirstParentOfType(ASTMethodDeclaration.class);
if (enclosingMethod == null || !mnd.getNode().jjtGetParent().equals(enclosingMethod)) {
callsFromOutsideMethod++;
}
}
return callsFromOutsideMethod == 0;
}
代码示例来源:origin: pmd/pmd
c = node.getFirstParentOfType(ASTPackageSpecification.class);
if (c != null) {
return c.getImage();
c = node.getFirstParentOfType(ASTTypeSpecification.class);
if (c != null) {
return c.getImage();
c = node.getFirstParentOfType(ASTPackageBody.class);
if (c != null) {
return c.getImage();
c = node.getFirstParentOfType(ASTTriggerUnit.class);
if (c != null) {
return c.getImage();
c = node.getFirstParentOfType(ASTProgramUnit.class);
if (c != null) {
return c.getImage();
代码示例来源:origin: pmd/pmd
Node possibleStatement = statement.getFirstParentOfType(ASTIfStatement.class);
while (possibleStatement instanceof ASTIfStatement) {
statement = possibleStatement;
possibleStatement = possibleStatement.getFirstParentOfType(ASTIfStatement.class);
代码示例来源:origin: pmd/pmd
private boolean initializedInConstructor(List<NameOccurrence> usages) {
boolean initInConstructor = false;
for (NameOccurrence occ : usages) {
// specifically omitting prefix and postfix operators as there are
// legitimate usages of these with static fields, e.g. typesafe enum pattern.
if (((JavaNameOccurrence) occ).isOnLeftHandSide()) {
Node node = occ.getLocation();
Node constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
if (constructor != null) {
initInConstructor = true;
}
}
}
return initInConstructor;
}
代码示例来源:origin: pmd/pmd
if (jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) {
Node node = jocc.getLocation();
ASTConstructorDeclaration constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
if (constructor != null) {
if (inLoopOrTry(node)) {
if (node.getFirstParentOfType(ASTIfStatement.class) != null) {
methodInitCount++;
} else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
lambdaUsage++;
} else {
if (node.getFirstParentOfType(ASTMethodDeclaration.class) != null) {
methodInitCount++;
} else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
lambdaUsage++;
代码示例来源:origin: pmd/pmd
/**
* Check if the given node is the first statement in the block.
*/
private boolean isFirstStatementInBlock(Node node, ASTStatement loopBody) {
// find the statement of the operation and the loop body block statement
final ASTBlockStatement statement = node.getFirstParentOfType(ASTBlockStatement.class);
final ASTBlock block = loopBody.getFirstDescendantOfType(ASTBlock.class);
if (statement == null || block == null) {
return false;
}
// is the first statement in the loop body?
return block.equals(statement.jjtGetParent()) && statement.jjtGetChildIndex() == 0;
}
代码示例来源: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
private int getConstructorLength(Node node, int constructorLength) {
int iConstructorLength = constructorLength;
Node block = node.getFirstParentOfType(ASTBlockStatement.class);
block = node.getFirstParentOfType(ASTFieldDeclaration.class);
block = node.getFirstParentOfType(ASTFormalParameter.class);
if (block != null) {
iConstructorLength = -1;
代码示例来源: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 boolean methodHasOverride(Node node) {
ASTClassOrInterfaceBodyDeclaration method = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
if (method != null && method.jjtGetNumChildren() > 0 && method.jjtGetChild(0) instanceof ASTAnnotation) {
ASTMarkerAnnotation marker = method.getFirstDescendantOfType(ASTMarkerAnnotation.class);
if (marker != null && marker.getFirstChildOfType(ASTName.class) != null) {
ASTName name = marker.getFirstChildOfType(ASTName.class);
if (name.getType() == Override.class) {
return true;
}
}
}
return false;
}
}
代码示例来源:origin: pmd/pmd
if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {
continue;
ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class);
if (method != null && !method.isSynchronized()) {
addViolation(data, n);
代码示例来源:origin: pmd/pmd
protected static boolean isInStringBufferOperation(Node node, int length, String methodName) {
if (!(node.getNthParent(length) instanceof ASTStatementExpression)) {
return false;
}
ASTStatementExpression s = node.getFirstParentOfType(ASTStatementExpression.class);
if (s == null) {
return false;
}
ASTName n = s.getFirstDescendantOfType(ASTName.class);
if (n == null || n.getImage().indexOf(methodName) == -1
|| !(n.getNameDeclaration() instanceof TypedNameDeclaration)) {
return false;
}
// TODO having to hand-code this kind of dredging around is ridiculous
// we need something to support this in the framework
// but, "for now" (tm):
// if more than one arg to append(), skip it
ASTArgumentList argList = s.getFirstDescendantOfType(ASTArgumentList.class);
if (argList == null || argList.jjtGetNumChildren() > 1) {
return false;
}
return TypeHelper.isExactlyAny((TypedNameDeclaration) n.getNameDeclaration(), StringBuffer.class,
StringBuilder.class);
}
代码示例来源:origin: pmd/pmd
private int getConstructorAppendsLength(final Node node) {
final Node parent = node.getFirstParentOfType(ASTVariableDeclarator.class);
int size = 0;
if (parent != null) {
final Node initializer = parent.getFirstChildOfType(ASTVariableInitializer.class);
if (initializer != null) {
final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
if (primExp != null) {
for (int i = 0; i < primExp.jjtGetNumChildren(); i++) {
final Node sn = primExp.jjtGetChild(i);
if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) {
continue;
}
size += processNode(sn);
}
}
}
}
return size;
}
代码示例来源:origin: pmd/pmd
Node parentNode = n.getNode().getFirstParentOfType(dataFlowHandler.getLabelStatementNodeClass());
if (parentNode == null) {
break;
代码示例来源:origin: pmd/pmd
Node loc = jocc.getLocation();
if (loc != null) {
ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
while (exp != null) {
if (exp.jjtGetParent() instanceof ASTStatementExpression) {
内容来源于网络,如有侵权,请联系作者删除!