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

createCodeSnippetExpression/Statement can only be used once. #304

Closed
alcides opened this issue Aug 18, 2015 · 7 comments · Fixed by #443
Closed

createCodeSnippetExpression/Statement can only be used once. #304

alcides opened this issue Aug 18, 2015 · 7 comments · Fixed by #443

Comments

@alcides
Copy link
Contributor

alcides commented Aug 18, 2015

factory.Code().createCodeSnippetExpression(...).compile() and its Statement counterpart can only be used once, even in different Processors.

I want to be able to generate two or more snippets in order to build a new AST to modify the original program. These are small things that are not enough to justify a template, but take 10-20 lines to create manually.

@GerardPaligot
Copy link
Contributor

Can you give us an example or a test failed, because when I wrote this test:

@Test
public void testCompileSnippetSeveralTimes() throws Exception {
    final Factory factory = TestUtils.createFactory();
    final CtCodeSnippetExpression<Object> snippet = factory.Code().createCodeSnippetExpression(
            "1 > 2");
    final CtExpression<Object> compile = snippet.compile();
    final CtCodeSnippetExpression<Object> snippet2 = factory.Code().createCodeSnippetExpression(
            "2 < 1");
    final CtExpression<Object> compile1 = snippet2.compile();
    assertTrue(compile instanceof CtBinaryOperator);
    assertTrue(compile1 instanceof CtBinaryOperator);
}

Everything works or I don't understand your issue.

@GerardPaligot
Copy link
Contributor

It seems that their is no problem. See #362

@alcides
Copy link
Contributor Author

alcides commented Oct 19, 2015

This is still an issue with 4.3 and 4.4-SNAPSHOT of today.

I am using this code:

CtConditional conditional = (CtConditional) factory
.Code()
.createCodeSnippetExpression("( null == null) ? null : null").compile();

I added this expression to the code: It worked.

I copied and pasted it before: It stopped working. Gives:

java.lang.ClassCastException: spoon.support.reflect.code.CtCodeSnippetExpressionImpl cannot be cast to spoon.reflect.code.CtConditional
    at aeminium.jparcompiler.processing.TaskCreationProcessor.futurifyInvocation(TaskCreationProcessor.java:623)

I remove the duplicated instruction and now it does not compile and gives this error. The exact same code as before. This issue has happened to me before, with SnippetExpression and Statements.

I would guess this is some caching being done during the Snippet compilation, but I am not sure how this works exactly.

This is happening in this codebase: alcides/JParCompiler@3561adf, ant run would initiate the process if AeminiumRuntime and AeminiumFutures (both under http://github.com/Aeminium/) are available next to the JParCompiler project.

@GerardPaligot GerardPaligot reopened this Oct 26, 2015
@GerardPaligot
Copy link
Contributor

Do you have a test case which fails? I can't reproduce it.

@alcides
Copy link
Contributor Author

alcides commented Dec 5, 2015

https://dl.dropboxusercontent.com/u/193832/tmp/JParCompiler.zip

Sorry for being such a large project, but "ant compile" on the command line should spill the error.
(That line of code has worked before introducing other CodeSnippets).

This has happened before with other snippets in the project.

Removing other files from the src directory (the files that are going to be compiled with Spoon) makes the example work. That is why I believe it is a cache issue, by reusing the same Wrapper class over and over again.

tdurieux added a commit to tdurieux/spoon that referenced this issue Dec 5, 2015
@tdurieux
Copy link
Collaborator

tdurieux commented Dec 5, 2015

I was able to reproduce and fix the bug (see https://github.com/tdurieux/spoon/tree/fix-snippet).

The bugs were:

  1. when spoon create a snippet it wraps the snippet into a wrapper class, this class was not correctly removed
  2. the signature of a class is the rootPackage was not correctly performed (the signature was "class .Wrapper)

With this two bugs and depending on the state of spoon, spoon returned a snippet not compiled.

@alcides
Copy link
Contributor Author

alcides commented Dec 7, 2015 via email

monperrus added a commit that referenced this issue Dec 8, 2015
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

Successfully merging a pull request may close this issue.

3 participants