-
Notifications
You must be signed in to change notification settings - Fork 90
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
Render final keyword for @for loops in java source or source may not compile #41
Comments
Three comments.
|
Thanks for your reply.
I'll get back to you soon. |
Ok, I reproduced it. I had a look through the code and tests, and I will add a relevant test file to rocker-test-java6/src/test/java/rocker Since I expect it to fail for java 8 as well, shall I add them to rocker-test-java8/src/test/java/rocker too? If so, I will modify them to use the java 8 syntactic sugar. |
If the fixes for both are different then yes you'd want to add tests to rocker-test-java6 and rocker-test-java8. Looking fwd to the fix! If you can get the PR over to me in the next 4 hours I'll be happy to push it to maven central. |
Thanks, that would be awesome! I'll get the fixes in and create the pull request. There is one specific loop (normal one) I cannot fix this quickly right now as I need to have a better look at the code for that, but you will see that in the test file (I will add the failing test, but disable that specific one). I don't mind looking into that one later, but that will have to wait for the weekend as I have more time then. If that's ok with you, I'll have a look on that one then. The current fixes should cover all for loop related issues except that one, as far I can see now. |
Ok, pull request created. Hope it's ok. :) See my comment above, so you can decide how to deal with the current disabled failing test. |
Hey Joe, I had a look at the remaining issue. This one is a lot harder to tackle, and we should consider not even trying to fix it (as it's actually how normal java works here). Since we're dealing with ordinary loops like:
For one thing 'i' can never be final, since it's part of a loop after all and does change. In theory you could workaround this, by doing the loop in a different fashion and generating it differently, but then you need to fully parse the expression first before you can do that (atm this is stored in ForStatement as 3 parts). Don't think it's really worth the effort. What I think would make more sense (and that's how you would deal with it in normal java anyway) is to have a separate final assignent variable. E.g. like the @with but in a non-scoped way. Samples of what I was thinking about:
These assigns will be made final in generation then and are available in the template. This would be a very useful addition, I'd love to have them: specifically to assign things used repeatedly in a template, as well as for expensive methods calls, I rather execute only once than many times. The above will also work for the for loop:
What are your thoughts? |
A couple thoughts. I agree its too hard to support the standard syntax. Parsing the inner expression is a lot of work for this one case. Not sure i'm sold on an |
I see, what were the issues you used to have with the scoping-less @with? That aside, now that we're talking about the @with. What would be nice as well, to have the with accept multiple arguments for assignment. So you can define multiple scoped arguments. Which would make the code really clean, without having to nest multiple times if you need several assignments. I don't have the code here when writing this comment, so I do not know if that would be doable or incredibly hard to support that, as it is now. If it's doable, I may be willing to put time in that. What do you think? Could you put the current version as is - in maven btw, then I can use an official build at work again. :) |
@mreuvers v0.14.0 is released. Will take a couple hours to hit maven central I'd guess. I think setting multiple arguments in a |
Hi there,
I have been using Rocker for our new development project as I accidentally found it and like the concepts. Unfortunately I ran into the following issue while trying it out, see the sample code below.
@for (ModelClass modelClass : modelClasses) { @with (List<Method> methodsFound = modelClass.getMethods()) { @if (!methodsFound.isEmpty()) { // Class: @modelClass.getFullyQualifiedName() } } }
The line Class: @modelClass.getFullyQualifiedName() will not compile, because modelClass is rendered as non-final variable in the template java source (java compiler message: "Cannot refer to the non-final local variable modelClass defined in an enclosing scope").
Afaict the easiest way to deal with this is render these assignments with the final keyword always, that would apply to anything where things are assigned like in the different @for loops (and possibly more?).
The above or e.g. class ForStatement needs to deal with an optional 'final' keyword (and probably more different statements).
I am happy to help you fix this, if you tell me what solution you prefer (I know antlr4 quite well too). But only if you want me to do so. If so, I'll make a pull request then.
I hope you can help out as it's blocking our progress.
The text was updated successfully, but these errors were encountered: