Search in sources :

Example 1 with ImageFinderFunction

use of net.sourceforge.pmd.lang.symboltable.ImageFinderFunction in project pmd by pmd.

the class ImageFinderFunctionTest method testSeveralImages.

@Test
public void testSeveralImages() {
    List<String> imgs = new ArrayList<>();
    imgs.add("Foo.foo");
    imgs.add("foo");
    ImageFinderFunction f = new ImageFinderFunction(imgs);
    ASTVariableDeclaratorId node = new ASTVariableDeclaratorId(1);
    node.setImage("foo");
    NameDeclaration decl = new VariableNameDeclaration(node);
    f.applyTo(decl);
    assertEquals(decl, f.getDecl());
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ArrayList(java.util.ArrayList) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 2 with ImageFinderFunction

use of net.sourceforge.pmd.lang.symboltable.ImageFinderFunction in project pmd by pmd.

the class ImageFinderFunctionTest method testSingleImage.

@Test
public void testSingleImage() {
    ImageFinderFunction f = new ImageFinderFunction("foo");
    ASTVariableDeclaratorId node = new ASTVariableDeclaratorId(1);
    node.setImage("foo");
    NameDeclaration decl = new VariableNameDeclaration(node);
    f.applyTo(decl);
    assertEquals(decl, f.getDecl());
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 3 with ImageFinderFunction

use of net.sourceforge.pmd.lang.symboltable.ImageFinderFunction in project pmd by pmd.

the class ClassScope method findVariableHere.

protected Set<NameDeclaration> findVariableHere(PLSQLNameOccurrence occurrence) {
    Set<NameDeclaration> result = new HashSet<>();
    Map<VariableNameDeclaration, List<NameOccurrence>> variableDeclarations = getVariableDeclarations();
    Map<MethodNameDeclaration, List<NameOccurrence>> methodDeclarations = getMethodDeclarations();
    if (occurrence.isThisOrSuper() || occurrence.getImage().equals(className)) {
        if (variableDeclarations.isEmpty() && methodDeclarations.isEmpty()) {
            // }
            return result;
        }
        // and then we'll look up X.
        if (!variableDeclarations.isEmpty()) {
            result.add(variableDeclarations.keySet().iterator().next());
            return result;
        }
        result.add(methodDeclarations.keySet().iterator().next());
        return result;
    }
    if (occurrence.isMethodOrConstructorInvocation()) {
        for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
            if (mnd.getImage().equals(occurrence.getImage())) {
                int args = occurrence.getArgumentCount();
                if (args == mnd.getParameterCount() || mnd.isVarargs() && args >= mnd.getParameterCount() - 1) {
                    // FIXME if several methods have the same name
                    // and parameter count, only one will get caught here
                    // we need to make some attempt at type lookup and
                    // discrimination
                    // or, failing that, mark this as a usage of all those
                    // methods
                    result.add(mnd);
                }
            }
        }
        return result;
    }
    List<String> images = new ArrayList<>();
    images.add(occurrence.getImage());
    if (null == occurrence.getImage()) {
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("occurrence==" + occurrence.toString() + "with Argumanet Count == " + occurrence.getArgumentCount() + " for className=" + className);
        }
    }
    if (occurrence.getImage().startsWith(className)) {
        images.add(clipClassName(occurrence.getImage()));
    }
    ImageFinderFunction finder = new ImageFinderFunction(images);
    Applier.apply(finder, getVariableDeclarations().keySet().iterator());
    if (finder.getDecl() != null) {
        result.add(finder.getDecl());
    }
    return result;
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) ArrayList(java.util.ArrayList) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 4 with ImageFinderFunction

use of net.sourceforge.pmd.lang.symboltable.ImageFinderFunction in project pmd by pmd.

the class LocalScope method findVariableHere.

public Set<NameDeclaration> findVariableHere(PLSQLNameOccurrence occurrence) {
    Set<NameDeclaration> result = new HashSet<>();
    if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
        return result;
    }
    ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
    Applier.apply(finder, getVariableDeclarations().keySet().iterator());
    if (finder.getDecl() != null) {
        result.add(finder.getDecl());
    }
    return result;
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) HashSet(java.util.HashSet)

Example 5 with ImageFinderFunction

use of net.sourceforge.pmd.lang.symboltable.ImageFinderFunction in project pmd by pmd.

the class ClassScope method findVariableHere.

protected Set<NameDeclaration> findVariableHere(JavaNameOccurrence occurrence) {
    if (occurrence.isThisOrSuper() || className.equals(occurrence.getImage())) {
        // Reference to ourselves!
        return Collections.<NameDeclaration>singleton(classDeclaration);
    }
    Map<MethodNameDeclaration, List<NameOccurrence>> methodDeclarations = getMethodDeclarations();
    Set<NameDeclaration> result = new HashSet<>();
    if (occurrence.isMethodOrConstructorInvocation()) {
        final boolean hasAuxclasspath = getEnclosingScope(SourceFileScope.class).hasAuxclasspath();
        matchMethodDeclaration(occurrence, methodDeclarations.keySet(), hasAuxclasspath, result);
        if (isEnum && "valueOf".equals(occurrence.getImage())) {
            result.add(createBuiltInMethodDeclaration("valueOf", "String"));
        }
        if (result.isEmpty()) {
            for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
                matchMethodDeclaration(occurrence, innerClass.getScope().getDeclarations(MethodNameDeclaration.class).keySet(), hasAuxclasspath, result);
            }
        }
        return result;
    }
    if (occurrence.isMethodReference()) {
        for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
            if (mnd.getImage().equals(occurrence.getImage())) {
                result.add(mnd);
            }
        }
        return result;
    }
    List<String> images = new ArrayList<>();
    if (occurrence.getImage() != null) {
        images.add(occurrence.getImage());
        if (occurrence.getImage().startsWith(className)) {
            images.add(clipClassName(occurrence.getImage()));
        }
    }
    Map<VariableNameDeclaration, List<NameOccurrence>> variableDeclarations = getVariableDeclarations();
    ImageFinderFunction finder = new ImageFinderFunction(images);
    Applier.apply(finder, variableDeclarations.keySet().iterator());
    if (finder.getDecl() != null) {
        result.add(finder.getDecl());
    }
    // search inner classes
    Map<ClassNameDeclaration, List<NameOccurrence>> classDeclarations = getClassDeclarations();
    if (result.isEmpty() && !classDeclarations.isEmpty()) {
        for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
            Applier.apply(finder, innerClass.getScope().getDeclarations(VariableNameDeclaration.class).keySet().iterator());
            if (finder.getDecl() != null) {
                result.add(finder.getDecl());
            }
        }
    }
    return result;
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) ArrayList(java.util.ArrayList) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ArrayList(java.util.ArrayList) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

ImageFinderFunction (net.sourceforge.pmd.lang.symboltable.ImageFinderFunction)10 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 HashSet (java.util.HashSet)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 Test (org.junit.Test)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)1 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)1