Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException on CtParameterReferenceImpl.getDeclaration(CtParameterReferenceImpl.java:44) #729

Closed
arnobl opened this issue Jun 28, 2016 · 5 comments

Comments

@arnobl
Copy link
Contributor

arnobl commented Jun 28, 2016

Using the latest 5.2-SNAPSHOT (20160626) version I have a NPE (it was not the case some days ago).

java.lang.NullPointerException
    at spoon.support.reflect.reference.CtParameterReferenceImpl.getDeclaration(CtParameterReferenceImpl.java:44)
    at spoon.support.reflect.reference.CtParameterReferenceImpl.getDeclaration(CtParameterReferenceImpl.java:27)
    at fr.inria.diverse.torgen.inspectorguidget.filter.LocalVariableAccessFilter.matches(LocalVariableAccessFilter.java:15)
    at fr.inria.diverse.torgen.inspectorguidget.filter.LocalVariableAccessFilter.matches(LocalVariableAccessFilter.java:1)

My code is:

    @Override
    public boolean matches(final CtVariableReference<?> element) {
        return element.getDeclaration() instanceof CtLocalVariable;
    }

I think that the problem is the clone method of CtParameterReferenceImpl does not consider the executable parameter, so that it can become null in some cases.

The code I analyse is (it concerns the local var e):

class Foo {
    public void foo() {
        JButton b = new JButton();
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                switch(e.getActionCommand()) {
                }
            }
        });
    }
}
@arnobl
Copy link
Contributor Author

arnobl commented Jun 28, 2016

There may be a problem in the class CtParameterReferenceImpl: the method setDeclaringExecutable can set the executable to null but the method getDeclaration does not check the nullity of executable.

@arnobl
Copy link
Contributor Author

arnobl commented Jun 28, 2016

I checked that the problem does not appear using 5.2.0-20160616.235102-85

@GerardPaligot
Copy link
Contributor

Do you have any test case who highlight your bug? Because I can't reproduce it.

I create a file with your snippet code (Foo class), I apply Spoon on it in no classpath mode and I execute a Query on each CtVariableReference:

@Test
public void testDeclarationOfVariableReference() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/Foo2.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();

    launcher.getModel().getElements(new TypeFilter<CtVariableReference>(CtVariableReference.class) {
        @Override
        public boolean matches(CtVariableReference element) {
            try {
                if (element.getDeclaration() instanceof CtLocalVariable) {

                }
            } catch (NullPointerException e) {
                fail("Fail with " + element.getSimpleName() + " declared in " + element.getParent().getSignature());
            }
            return super.matches(element);
        }
    });
}

But everything works.

@arnobl
Copy link
Contributor Author

arnobl commented Jun 28, 2016

I tried to isolate the problem in a test case but it also works. However, my tool does not work anymore. I will try to find the reason ASAP.

@GerardPaligot
Copy link
Contributor

Ok, the test doesn't work if I change it to:

if (element.clone().getDeclaration() instanceof CtLocalVariable) {
}

There is a bug in the clone. PR soon to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants