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] Issue with CtClass::compileAndReplaceSnippets compiling Block after Comment #3688

Closed
lapplislazuli opened this issue Nov 11, 2020 · 2 comments
Labels

Comments

@lapplislazuli
Copy link

Hi there,

similar to my other issue #3687, I found another problem appearing in compileAndReplaceSnippets after a comment.
This time, it gives an actual pointer into the code.

To reproduce

    @Tag("Regression")
    @Test
    void testSnippetReplacement_BlockHasIssuesWithNextAfterComment(){
        String initialClass = "package some; class A { public int mult(int a, int b){return a * b;}";

        CtClass<?> containingClass = Launcher.parseClass(initialClass);
        String methodName = "someMethod";

        var methodToAlter= containingClass.getMethodsByName("mult").get(0);

        // Build an empty method, return void
        CtMethod emptyMethod = containingClass.getFactory().createMethod();
        emptyMethod.setSimpleName(methodName);
        emptyMethod.setParent(containingClass);
        emptyMethod.setType(emptyMethod.getFactory().Type().VOID_PRIMITIVE);
        emptyMethod.addModifier(ModifierKind.PRIVATE);
        emptyMethod.setBody(emptyMethod.getFactory().createBlock());

        containingClass.addMethod(emptyMethod);

        methodToAlter.getBody().addStatement(0,
                containingClass.getFactory().createBlock().addStatement( containingClass.getFactory().createCodeSnippetStatement(methodName+"()"))
               );

        methodToAlter.getBody().addStatement(0,containingClass.getFactory().createComment("I seem to break the test", CtComment.CommentType.BLOCK));

        // This fails the test
        containingClass.compileAndReplaceSnippets();

        return;
    }

Expected Output

The program should compile and the AST should be fully working without elements vanishing.

Actual Output

java.util.NoSuchElementException
	at java.base/java.util.LinkedList$ListItr.next(LinkedList.java:894)
	at spoon.support.compiler.SnippetCompilationHelper.compileAndReplaceSnippetsIn(SnippetCompilationHelper.java:85)
	at spoon.support.reflect.declaration.CtTypeImpl.compileAndReplaceSnippets(CtTypeImpl.java:448)

Further Info / Considerations

This only occurs when there is a comment before the method. If you remove the line where the InlineComment is added, the test succeeds.

Other

I am using Spoon-Core 8.3.0, in a maven project. Spoon is only used as a library.

@MartinWitt MartinWitt added the bug label Nov 12, 2020
@monperrus
Copy link
Collaborator

Thanks for the bug report. Would you be able to a propose a pull-request with a failing test case?

@monperrus
Copy link
Collaborator

Closed per #3888

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.

3 participants