use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class ArrayIsStoredDirectlyRule method checkForDirectAssignment.
/**
* Checks if the variable designed in parameter is written to a field (not
* local variable) in the statements.
*/
private boolean checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter, final List<ASTBlockStatement> bs) {
final ASTVariableDeclaratorId vid = parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
final String varName = vid.getImage();
for (ASTBlockStatement b : bs) {
if (b.hasDescendantOfType(ASTAssignmentOperator.class)) {
final ASTStatementExpression se = b.getFirstDescendantOfType(ASTStatementExpression.class);
if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) {
continue;
}
String assignedVar = getExpressionVarName(se);
if (assignedVar == null) {
continue;
}
ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
Node n = pe.getFirstParentOfType(ASTMethodDeclaration.class);
if (n == null) {
n = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
if (n == null) {
continue;
}
}
if (!isLocalVariable(assignedVar, n)) {
// something
if (se.jjtGetNumChildren() < 3) {
continue;
}
ASTExpression e = (ASTExpression) se.jjtGetChild(2);
if (e.hasDescendantOfType(ASTEqualityExpression.class)) {
continue;
}
String val = getExpressionVarName(e);
if (val == null) {
continue;
}
ASTPrimarySuffix foo = e.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (foo != null && foo.isArrayDereference()) {
continue;
}
if (val.equals(varName)) {
Node md = parameter.getFirstParentOfType(ASTMethodDeclaration.class);
if (md == null) {
md = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
}
if (!isLocalVariable(varName, md)) {
addViolation(ctx, parameter, varName);
}
}
}
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class JUnitTestsShouldIncludeAssertRule method isExpectStatement.
private boolean isExpectStatement(ASTStatementExpression expression, Map<String, List<NameOccurrence>> expectables) {
if (expression != null) {
ASTPrimaryExpression pe = expression.getFirstChildOfType(ASTPrimaryExpression.class);
if (pe != null) {
Node name = pe.getFirstDescendantOfType(ASTName.class);
// case of an AllocationExpression
if (name == null) {
return false;
}
String img = name.getImage();
if (img.indexOf(".") == -1) {
return false;
}
String varname = img.split("\\.")[0];
if (!expectables.containsKey(varname)) {
return false;
}
for (NameOccurrence occ : expectables.get(varname)) {
if (occ.getLocation() == name && img.startsWith(varname + ".expect")) {
return true;
}
}
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class UselessStringValueOfRule method isPrimitive.
private static boolean isPrimitive(Node parent) {
boolean result = false;
if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
Node child = parent.jjtGetChild(0);
if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
Node gc = child.jjtGetChild(0);
if (gc instanceof ASTName) {
ASTName name = (ASTName) gc;
NameDeclaration nd = name.getNameDeclaration();
if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
result = true;
}
} else if (gc instanceof ASTLiteral) {
result = !((ASTLiteral) gc).isStringLiteral();
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class JavaNameOccurrence method isSelfAssignment.
/**
* Assert it the occurrence is a self assignment such as:
* <code>i += 3;</code>
*
* @return true, if the occurrence is self-assignment, false, otherwise.
*/
@SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop")
public boolean isSelfAssignment() {
Node l = location;
while (true) {
Node p = l.jjtGetParent();
Node gp = p.jjtGetParent();
Node node = gp.jjtGetParent();
if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) {
return true;
}
if (hasAssignmentOperator(gp)) {
return isCompoundAssignment(gp);
}
if (hasAssignmentOperator(node)) {
return isCompoundAssignment(node);
}
// deal with extra parenthesis: "(i)++"
if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 && gp instanceof ASTPrimaryExpression && gp.jjtGetNumChildren() == 1 && node instanceof ASTExpression && node.jjtGetNumChildren() == 1 && node.jjtGetParent() instanceof ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) {
l = node;
continue;
}
// catch this.i++ or ++this.i
return gp instanceof ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression || gp instanceof ASTPostfixExpression;
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class JavaNameOccurrence method useThisOrSuper.
/**
* Simply return if the image start with keyword 'this' or 'super'.
*
* @return true, if keyword is used, false otherwise.
*/
public boolean useThisOrSuper() {
Node node = location.jjtGetParent();
if (node instanceof ASTPrimaryExpression) {
ASTPrimaryExpression primaryExpression = (ASTPrimaryExpression) node;
ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) primaryExpression.jjtGetChild(0);
if (prefix != null) {
return prefix.usesSuperModifier() || prefix.usesThisModifier();
}
}
return image.startsWith(THIS_DOT) || image.startsWith(SUPER_DOT);
}
Aggregations