use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class LocalScopeTest method testNameWithThisOrSuperIsNotFlaggedAsUnused.
@Test
public void testNameWithThisOrSuperIsNotFlaggedAsUnused() {
LocalScope scope = new LocalScope();
ASTName name = new ASTName(1);
name.setImage("foo");
ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
prefix.setUsesThisModifier();
name.jjtAddChild(prefix, 1);
JavaNameOccurrence occ = new JavaNameOccurrence(name, "foo");
scope.addNameOccurrence(occ);
assertFalse(scope.getDeclarations().keySet().iterator().hasNext());
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class InefficientStringBufferingRule method isInStringBufferOperation.
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.isEither((TypedNameDeclaration) n.getNameDeclaration(), StringBuffer.class, StringBuilder.class);
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class StringInstantiationRule method visit.
@Override
public Object visit(ASTAllocationExpression node, Object data) {
if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
return data;
}
if (!TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), String.class)) {
return data;
}
List<ASTExpression> exp = node.findDescendantsOfType(ASTExpression.class);
if (exp.size() >= 2) {
return data;
}
if (node.hasDescendantOfAnyType(ASTArrayDimsAndInits.class, ASTAdditiveExpression.class)) {
return data;
}
ASTName name = node.getFirstDescendantOfType(ASTName.class);
// Literal, i.e., new String("foo")
if (name == null) {
addViolation(data, node);
return data;
}
NameDeclaration nd = name.getNameDeclaration();
if (nd == null) {
return data;
}
if (nd instanceof TypedNameDeclaration && TypeHelper.isA((TypedNameDeclaration) nd, String.class)) {
addViolation(data, node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class UnnecessaryWrapperObjectCreationRule method visit.
public Object visit(ASTPrimaryPrefix node, Object data) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}
String image = ((ASTName) node.jjtGetChild(0)).getImage();
if (image.startsWith("java.lang.")) {
image = image.substring(10);
}
boolean checkBoolean = ((RuleContext) data).getLanguageVersion().compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0;
if (PREFIX_SET.contains(image) || checkBoolean && "Boolean.valueOf".equals(image)) {
ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
if (parent.jjtGetNumChildren() >= 3) {
Node n = parent.jjtGetChild(2);
if (n instanceof ASTPrimarySuffix) {
ASTPrimarySuffix suffix = (ASTPrimarySuffix) n;
image = suffix.getImage();
if (SUFFIX_SET.contains(image) || checkBoolean && "booleanValue".equals(image)) {
super.addViolation(data, node);
return data;
}
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class CompareObjectsWithEqualsRule method visit.
public Object visit(ASTEqualityExpression node, Object data) {
Node c0 = node.jjtGetChild(0).jjtGetChild(0);
Node c1 = node.jjtGetChild(1).jjtGetChild(0);
// equals expression is correct
if (isAllocation(c0) || isAllocation(c1)) {
addViolation(data, node);
return data;
}
// skip if either child is not a simple name
if (!hasName(c0) || !hasName(c1)) {
return data;
}
// skip if either is a qualified name
if (isQualifiedName(c0.jjtGetChild(0)) || isQualifiedName(c1.jjtGetChild(0))) {
return data;
}
// skip if either is part of a qualified name
if (isPartOfQualifiedName(node.jjtGetChild(0)) || isPartOfQualifiedName(node.jjtGetChild(1))) {
return data;
}
// skip static initializers... missing some cases here
if (!node.getParentsOfType(ASTInitializer.class).isEmpty()) {
return data;
}
ASTName n0 = (ASTName) c0.jjtGetChild(0);
ASTName n1 = (ASTName) c1.jjtGetChild(0);
if (n0.getNameDeclaration() instanceof VariableNameDeclaration && n1.getNameDeclaration() instanceof VariableNameDeclaration) {
VariableNameDeclaration nd0 = (VariableNameDeclaration) n0.getNameDeclaration();
VariableNameDeclaration nd1 = (VariableNameDeclaration) n1.getNameDeclaration();
// FIXME catch comparisons btwn array elements of reference types
if (nd0.isArray() || nd1.isArray()) {
return data;
}
if (nd0.isReferenceType() && nd1.isReferenceType()) {
ASTReferenceType type0 = ((Node) nd0.getAccessNodeParent()).getFirstDescendantOfType(ASTReferenceType.class);
ASTReferenceType type1 = ((Node) nd1.getAccessNodeParent()).getFirstDescendantOfType(ASTReferenceType.class);
// skip, if it is an enum
if (type0.getType() != null && type0.getType().equals(type1.getType()) && // It may be a custom enum class or an explicit Enum class usage
(type0.getType().isEnum() || type0.getType() == java.lang.Enum.class)) {
return data;
}
addViolation(data, node);
}
}
return data;
}
Aggregations