RFR: 8344942: Template-Based Testing Framework [v61]
Emanuel Peter
epeter at openjdk.org
Sun Jun 1 16:03:08 UTC 2025
On Fri, 30 May 2025 08:39:14 GMT, Christian Hagedorn <chagedorn at openjdk.org> wrote:
>> test/hotspot/jtreg/testlibrary_tests/template_framework/examples/TestTutorial.java line 358:
>>
>>> 356:
>>> 357: // We saw the use of custom hooks above, but now we look at the use of CLASS_HOOK and METHOD_HOOK
>>> 358: // from the Template Library.
>>
>> Can you expand here on why it's better to use them instead of creating your own? Is it just readability/convenience?
>
> Another question which is not evidently clear by following the examples: Can and should (not) you use the same hook inside the hook itself, i.e.:
>
> Hooks.CLASS_HOOK.anchor(
> Hooks.CLASS_HOOK.anchor(
> // ...
>
> This is probably not done on purpose but such a situation could arise when nesting more templates and suddenly one anchors the same hook again?
I extended the explanations:
~ 397 // We saw the use of custom hooks above, but now we look at the use of CLASS_HOOK and METHOD_HOOK.
~ 398 // By convention, we use the CLASS_HOOK for class scopes, and METHOD_HOOK for method scopes.
+ 399 // Whenever we open a class scope, we should anchor a CLASS_HOOK for that scope, and whenever we
+ 400 // open a method, we should anchor a METHOD_HOOK. Conversely, this allows us to check if we are
+ 401 // inside a class or method scope by querying "isAnchored". This convention helps us when building
+ 402 // a large library of Templates. But if you are writing your own self-contained set of Templates,
+ 403 // you do not have to follow this convention.
+ 404 //
+ 405 // Hooks are "re-entrant", that is we can anchor the same hook inside a scope that we already
+ 406 // anchored it previously. The "Hook.insert" always goes to the innermost anchoring of that
+ 407 // hook. There are cases where "re-entrant" Hooks are helpful such as nested classes, where
+ 408 // there is a class scope inside another class scope. Similarly, we can nest lambda bodies
+ 409 // inside method bodies, so also METHOD_HOOK can be used in such a "re-entrant" way.
We could consider having both "re-entrant" and "non-re-entrant" Hooks. But I'm not yet convinced it is a very useful feature. Sure, there could be some confusion with nested hooks. But I think that confusion to code generation, because we can also nest class and method/lambda scopes.
What do you think?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/24217#discussion_r2119274873
More information about the hotspot-compiler-dev
mailing list