Search in sources :

Example 51 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.

the class MethodNamingConventionsRule method isOverriddenMethod.

private boolean isOverriddenMethod(ASTMethodDeclarator node) {
    ASTClassOrInterfaceBodyDeclaration declaration = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
    List<ASTMarkerAnnotation> annotations = declaration.findDescendantsOfType(ASTMarkerAnnotation.class);
    for (ASTMarkerAnnotation ann : annotations) {
        ASTName name = ann.getFirstChildOfType(ASTName.class);
        if (name != null && name.hasImageEqualTo("Override")) {
            return true;
        }
    }
    return false;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) ASTMarkerAnnotation(net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation)

Example 52 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.

the class ClassTypeResolverTest method testFullyQualifiedType.

/**
 * The type should be filled also on the ASTVariableDeclaratorId node, not
 * only on the variable name declaration.
 */
@Test
public void testFullyQualifiedType() {
    String source = "public class Foo {\n" + "    public void bar() {\n" + "        java.util.StringTokenizer st = new StringTokenizer(\"a.b.c.d\", \".\");\n" + "        while (st.hasMoreTokens()) {\n" + "            System.out.println(st.nextToken());\n" + "        }\n" + "    }\n" + "}";
    ASTCompilationUnit acu = parseAndTypeResolveForString(source, "1.5");
    List<ASTName> names = acu.findDescendantsOfType(ASTName.class);
    ASTName theStringTokenizer = null;
    for (ASTName name : names) {
        if (name.hasImageEqualTo("st.hasMoreTokens")) {
            theStringTokenizer = name;
            break;
        }
    }
    Assert.assertNotNull(theStringTokenizer);
    VariableNameDeclaration declaration = (VariableNameDeclaration) theStringTokenizer.getNameDeclaration();
    Assert.assertNotNull(declaration);
    Assert.assertEquals("java.util.StringTokenizer", declaration.getTypeImage());
    Assert.assertNotNull(declaration.getType());
    Assert.assertSame(StringTokenizer.class, declaration.getType());
    ASTVariableDeclaratorId id = (ASTVariableDeclaratorId) declaration.getNode();
    Assert.assertNotNull(id.getType());
    Assert.assertSame(StringTokenizer.class, id.getType());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Test(org.junit.Test)

Example 53 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.

the class LocalScopeTest method testNameWithSuperIsNotFlaggedAsUnused.

@Test
public void testNameWithSuperIsNotFlaggedAsUnused() {
    LocalScope scope = new LocalScope();
    ASTName name = new ASTName(1);
    name.setImage("foo");
    ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
    prefix.setUsesSuperModifier();
    name.jjtAddChild(prefix, 1);
    JavaNameOccurrence occ = new JavaNameOccurrence(name, "foo");
    scope.addNameOccurrence(occ);
    assertFalse(scope.getDeclarations().keySet().iterator().hasNext());
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Test(org.junit.Test)

Aggregations

ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)53 Node (net.sourceforge.pmd.lang.ast.Node)25 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)17 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)14 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)11 ArrayList (java.util.ArrayList)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)10 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)7 List (java.util.List)6 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)6 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)6 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 Map (java.util.Map)4 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)4 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)3