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

Add native hints #648

Merged
merged 5 commits into from
Apr 28, 2023
Merged

Conversation

noblehelm
Copy link
Contributor

In order to let any Java project including PebbleTemplates as a dependency to be able to build and run a native image using the AOT-compiler GraalVM, it is necessary to add native hints to the GraalVM so that it can maintain class and variables information during compile time (and consequently runtime) to be used as information for Reflection, Proxies and other JVM mechanisms.

This will add the necessary TypeHints for the SpringBoot starter only, in order to enable the building of a project using Spring Boot as framework, and to promote further experiments with other frameworks.

In order to let any Java project including PebbleTemplates as a
dependency to be able to build and run a native image using the
AOT-compiler GraalVM, it is necessary to add native hints to the GraalVM
so that it can maintain class and variables information during
compile time (and consequently runtime) to be used as information for
Reflection, Proxies and other JVM mechanisms.

This will add the necessary TypeHints for the SpringBoot starter only,
in order to enable the building of a project using Spring Boot as
framework, and to promote further experiments with other frameworks.
@noblehelm
Copy link
Contributor Author

noblehelm commented Mar 18, 2023

Proposed fix for #647

@noblehelm
Copy link
Contributor Author

noblehelm commented Mar 18, 2023

Unfortunately, even with the added hints for the ForNode class, for the inner LoopVariable class, it is not possible to let the native image reach its fields due to the fact that it is a private class. So, even with the hints, we end up with a similar error to this one:

2023-03-18 00:29:24.390 -ERROR 1 --- [nio-7300-exec-3] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/api/planrepository] threw exception [Request processing failed: io.pebbletemplates.pebble.error.PebbleException: Could not perform addition (**template**:6)] with root cause

io.pebbletemplates.pebble.error.AttributeNotFoundException: Attribute [index] of [io.pebbletemplates.pebble.node.ForNode$LoopVariables] does not exist or can not be accessed and strict variables is set to true. (**template**:6)
	at io.pebbletemplates.pebble.node.expression.GetAttributeExpression.evaluate(GetAttributeExpression.java:91) ~[native:na]
	at io.pebbletemplates.pebble.node.expression.AddExpression.evaluate(AddExpression.java:22) ~[native:na]
	at io.pebbletemplates.pebble.node.expression.FilterExpression.evaluate(FilterExpression.java:66) ~[native:na]
	at io.pebbletemplates.pebble.node.PrintNode.render(PrintNode.java:37) ~[na:na]
	at io.pebbletemplates.pebble.node.BodyNode.render(BodyNode.java:44) ~[native:na]
	at io.pebbletemplates.pebble.node.ForNode.render(ForNode.java:125) ~[native:na]
	at io.pebbletemplates.pebble.node.BodyNode.render(BodyNode.java:44) ~[native:na]
	at io.pebbletemplates.pebble.node.RootNode.render(RootNode.java:31) ~[na:na]
	at io.pebbletemplates.pebble.template.PebbleTemplateImpl.evaluate(PebbleTemplateImpl.java:157) ~[native:na]
	at io.pebbletemplates.pebble.template.PebbleTemplateImpl.evaluate(PebbleTemplateImpl.java:96) ~[native:na]

Is there any way to solve this?

@ebussieres
Copy link
Member

Never had the chance to try GraalVM. Can't help you on that one

@fabriceci
Copy link
Contributor

fabriceci commented Mar 26, 2023

I haven't tested it but I wanted to one day now that it works out of the box on Spring boot. @noblehelm Were you able to work around the problem?

If you override the class by creating in your project ,the same class file in the same package (ugly but works), and make it public, does that solve the problem?

@noblehelm
Copy link
Contributor Author

I've managed to make it work by transforming the LoopVariable class into an immutable class/object, with private fields and its respective Getters, and making it public instead of private. Then, it was just a jump over into adding the apropriate type hint for the LoopVariable and everything worked.

I've pushed a new commit to the PR, will do a squash if required.
I'll try to give some time this week to test all of the different expressions to see if these hints already do cover what is needed (because I don't know if my project does use all the different expressions).

@ebussieres
Copy link
Member

Ok let me know when you are done testing

Upon tests made based on the native-image.peb resource file, only the
inner LoopVariable class and the UnaryMinusExpression actually required
the type hint for the native image generation to work. The rest worked
out of the box for the spring-boot module.
@noblehelm
Copy link
Contributor Author

I've finished the tests and included the resource file native-image.peb I have crafted with all of the examples from each different functionality listed on the documentation page (except for the inherit functionality, which required three diferentes pebs. I've done the tests with those and it worked, but I decided to not include here to not pollute the resource tests). I included the resource file, even though there is no unit test that actually uses it, for "historical" purposes.

Upon tests, the only expression that actually required the type hint was the UnaryMinus. All of the rest worked out-of-the-box with the Spring integration. So, I removed the code in order to reflect only what is actually needed. If sometime in the future another expression is to be added, maybe a type hint can be added there IF it does require the hint. Otherwise, there is no necessity to do it.

@noblehelm
Copy link
Contributor Author

If you wish, I can squash the commits before merge.

@noblehelm
Copy link
Contributor Author

Any updates?

@ebussieres ebussieres added this to the 3.2.1 milestone Apr 28, 2023
@ebussieres ebussieres merged commit 034c818 into PebbleTemplates:master Apr 28, 2023
@ebussieres
Copy link
Member

Thanks ! Will try to do a release soon !

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 this pull request may close these issues.

3 participants