use of net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression in project pmd by pmd.
the class InefficientStringBufferingRule method isAllocatedStringBuffer.
private boolean isAllocatedStringBuffer(ASTAdditiveExpression node) {
ASTAllocationExpression ao = node.getFirstParentOfType(ASTAllocationExpression.class);
if (ao == null) {
return false;
}
// note that the child can be an ArrayDimsAndInits, for example, from
// java.lang.FloatingDecimal: t = new int[ nWords+wordcount+1 ];
ASTClassOrInterfaceType an = ao.getFirstChildOfType(ASTClassOrInterfaceType.class);
return an != null && TypeHelper.isEither(an, StringBuffer.class, StringBuilder.class);
}
use of net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression in project pmd by pmd.
the class InsufficientStringBufferDeclarationRule method getConstructorLength.
private int getConstructorLength(Node node, int constructorLength) {
int iConstructorLength = constructorLength;
Node block = node.getFirstParentOfType(ASTBlockStatement.class);
if (block == null) {
block = node.getFirstParentOfType(ASTFieldDeclaration.class);
}
if (block == null) {
block = node.getFirstParentOfType(ASTFormalParameter.class);
if (block != null) {
iConstructorLength = -1;
} else {
return DEFAULT_BUFFER_SIZE;
}
}
// if there is any addition/subtraction going on then just use the
// default.
ASTAdditiveExpression exp = block.getFirstDescendantOfType(ASTAdditiveExpression.class);
if (exp != null) {
return DEFAULT_BUFFER_SIZE;
}
ASTMultiplicativeExpression mult = block.getFirstDescendantOfType(ASTMultiplicativeExpression.class);
if (mult != null) {
return DEFAULT_BUFFER_SIZE;
}
List<ASTLiteral> literals;
ASTAllocationExpression constructorCall = block.getFirstDescendantOfType(ASTAllocationExpression.class);
if (constructorCall != null) {
// if this is a constructor call, only consider the literals within
// it.
literals = constructorCall.findDescendantsOfType(ASTLiteral.class);
} else {
// otherwise it might be a setLength call...
literals = block.findDescendantsOfType(ASTLiteral.class);
}
if (literals.isEmpty()) {
List<ASTName> name = block.findDescendantsOfType(ASTName.class);
if (!name.isEmpty()) {
iConstructorLength = -1;
}
} else if (literals.size() == 1) {
ASTLiteral literal = literals.get(0);
String str = literal.getImage();
if (str == null) {
iConstructorLength = 0;
} else if (isStringOrCharLiteral(literal)) {
// since it's not taken into account
// anywhere. only count the extra 16
// characters
// don't add the constructor's length
iConstructorLength = 14 + str.length();
} else if (literal.isIntLiteral()) {
iConstructorLength = literal.getValueAsInt();
}
} else {
iConstructorLength = -1;
}
if (iConstructorLength == 0) {
if (constructorLength == -1) {
iConstructorLength = DEFAULT_BUFFER_SIZE;
} else {
iConstructorLength = constructorLength;
}
}
return iConstructorLength;
}
use of net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression in project pmd by pmd.
the class ConsecutiveLiteralAppendsRule method checkInitializerExpressions.
/**
* Determine if during the variable initializer calls to ".append" are done.
*
* @param node
* @return
*/
private int checkInitializerExpressions(ASTVariableDeclaratorId node) {
ASTVariableInitializer initializer = node.jjtGetParent().getFirstChildOfType(ASTVariableInitializer.class);
ASTPrimaryExpression primary = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
int result = 0;
boolean previousWasAppend = false;
for (int i = 0; i < primary.jjtGetNumChildren(); i++) {
Node child = primary.jjtGetChild(i);
if (child.jjtGetNumChildren() > 0 && child.jjtGetChild(0) instanceof ASTAllocationExpression) {
// skip the constructor call, that has already been checked
continue;
}
if (child instanceof ASTPrimarySuffix) {
ASTPrimarySuffix suffix = (ASTPrimarySuffix) child;
if (suffix.jjtGetNumChildren() == 0 && suffix.hasImageEqualTo("append")) {
previousWasAppend = true;
} else if (suffix.jjtGetNumChildren() > 0 && previousWasAppend) {
previousWasAppend = false;
ASTLiteral literal = suffix.getFirstDescendantOfType(ASTLiteral.class);
if (literal != null && literal.isStringLiteral()) {
result++;
} else {
// checking the remainder of the initializer
break;
}
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression in project pmd by pmd.
the class PreserveStackTraceRule method visit.
@Override
public Object visit(ASTCatchStatement catchStmt, Object data) {
String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage();
// Inspect all the throw stmt inside the catch stmt
List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class);
for (ASTThrowStatement throwStatement : lstThrowStatements) {
Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
if (n instanceof ASTCastExpression) {
ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
RuleContext ctx = (RuleContext) data;
addViolation(ctx, throwStatement);
}
continue;
}
// Retrieve all argument for the throw exception (to see if the
// original exception is preserved)
ASTArgumentList args = throwStatement.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
Node parent = args.jjtGetParent().jjtGetParent();
if (parent instanceof ASTAllocationExpression) {
// maybe it is used inside a anonymous class
ck(data, target, throwStatement, parent);
} else {
// Check all arguments used in the throw statement
ck(data, target, throwStatement, throwStatement);
}
} else {
Node child = throwStatement.jjtGetChild(0);
while (child != null && child.jjtGetNumChildren() > 0 && !(child instanceof ASTName)) {
child = child.jjtGetChild(0);
}
if (child != null) {
if (child instanceof ASTName && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
List<NameOccurrence> occurrences = entry.getValue();
if (decl.getImage().equals(child.getImage())) {
if (!isInitCauseCalled(target, occurrences)) {
// Check how the variable is initialized
ASTVariableInitializer initializer = decl.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableInitializer.class);
if (initializer != null) {
args = initializer.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
// constructor with args?
ck(data, target, throwStatement, args);
} else if (!isFillInStackTraceCalled(target, initializer)) {
addViolation(data, throwStatement);
}
}
}
}
}
} else if (child instanceof ASTClassOrInterfaceType) {
addViolation(data, throwStatement);
}
}
}
}
return super.visit(catchStmt, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression in project pmd by pmd.
the class ClassTypeResolverTest method testNestedAllocationExpressions.
@Test
public void testNestedAllocationExpressions() {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(NestedAllocationExpressions.class);
List<ASTAllocationExpression> allocs = acu.findDescendantsOfType(ASTAllocationExpression.class);
assertFalse(allocs.get(0).isAnonymousClass());
assertEquals(Thread.class, allocs.get(0).getType());
assertTrue(allocs.get(1).isAnonymousClass());
// FUTURE 1.8 use Class.getTypeName() instead of toString
assertTrue(allocs.get(1).getType().toString().endsWith("NestedAllocationExpressions$1"));
}
Aggregations