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

[Bug]: Environment.report(...) throws NullPointerException #4997

Closed
brandy007 opened this issue Nov 4, 2022 · 0 comments · Fixed by #4998
Closed

[Bug]: Environment.report(...) throws NullPointerException #4997

brandy007 opened this issue Nov 4, 2022 · 0 comments · Fixed by #4998
Labels

Comments

@brandy007
Copy link

Describe the bug

using getEnvironment.report(processor, Level.INFO, element, "Message...") in my processor i get a NullPointerException if the element is implicit.

Source code you are trying to analyze/transform

package fr.inria.gforge.spoon.analysis;

/**
 * The Class Testcase2.
 *
 * testcase to show NullPointerException
 */
public class Testcase2
{

    private class A
    {

    }

    private class B extends A
    {

        /**
         * Instantiates a new b.
         */
        public B()
        {
            super();
        }
    }

    public static void main(String[] args)
    {
        Testcase2 t = new Testcase2();
        t.test();
    }

    public String test()
    {
        var b = new B();
        return b.toString();
    }
}

Source code for your Spoon processing

package fr.inria.gforge.spoon.analysis;

import java.util.List;

import spoon.processing.AbstractProcessor;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.Level;

/**
 * Processor to show {@link NullPointerException} thrown if element is implicit.
 *
 * @author r.b.
 *
 */
public class BugProcessor extends AbstractProcessor<CtConstructor<?>>
{

    /**
     * implementation walks through all invocations and just prints them
     *
     * @see spoon.processing.AbstractProcessor#isToBeProcessed(spoon.reflect.declaration.CtElement)
     *
     * @param candidate element to check
     * @return true if the element should be processed
     */
    public boolean isToBeProcessed(CtConstructor<?> candidate)
    {

        boolean process = false;
        List<CtInvocation<?>> invocations = Query.getElements(candidate, new TypeFilter<>(CtInvocation.class));

        invocations.forEach(i -> getEnvironment().report(this, Level.INFO, i, "Message"));

        return process;
    }

    /**
     * @see spoon.processing.Processor#process(spoon.reflect.declaration.CtElement)
     *
     * @param element
     */
    @Override
    public void process(CtConstructor<?> element)
    {
        // no functionality here ...
    }
}

Actual output

Caused by: java.lang.NullPointerException
    at spoon.support.StandardEnvironment.report (StandardEnvironment.java:265)
    at fr.inria.gforge.spoon.analysis.BugProcessor.lambda$isToBeProcessed$0 (BugProcessor.java:57)
    at java.util.ArrayList.forEach (ArrayList.java:1541)
    at fr.inria.gforge.spoon.analysis.BugProcessor.isToBeProcessed (BugProcessor.java:57)
    at fr.inria.gforge.spoon.analysis.BugProcessor.isToBeProcessed (BugProcessor.java:40)
    at spoon.support.visitor.ProcessingVisitor.scan (ProcessingVisitor.java:71)

Expected output

No Exception

Spoon Version

10.2.0

JVM Version

11

What operating system are you using?

win10

@brandy007 brandy007 added the bug label Nov 4, 2022
@brandy007 brandy007 changed the title Bug: Environment.report(...) throws NullPointerException [Bug]: Environment.report(...) throws NullPointerException Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant