Skip to content

small improvements #353

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

Merged
merged 7 commits into from
Apr 17, 2025
Merged

small improvements #353

merged 7 commits into from
Apr 17, 2025

Conversation

Ladicek
Copy link
Contributor

@Ladicek Ladicek commented Apr 16, 2025

  • add MethodTyped.parameterCount()
  • add BlockCreator.newEmptyArray() overloads that take an int size
  • add BlockCreator.throw_() overloads that take an Expr message
  • add ConstructorDesc.of() overloads that accept vararg parameter types
  • validate the return type on creation of ConstructorDesc
  • add TestClassMaker.loadClass()
  • improve the exception message when an Item is used on an unexpected place

@Ladicek Ladicek added the 2.x Issue applies to Gizmo 2.x label Apr 16, 2025
@Ladicek Ladicek moved this to In Progress in WG - Gizmo 2 Apr 16, 2025
@Ladicek
Copy link
Contributor Author

Ladicek commented Apr 16, 2025

Commits are best reviewed in isolation, although they are all pretty small. The last commit adds certain (nontrivial?) overhead, so I'd be willing to gate this behind a system property, but I found it tremendously helpful when figuring out what Expr exactly needs to be stored in a LocalVar.

@@ -2539,7 +2571,7 @@ default void throw_(Class<? extends Throwable> type) {
/**
* Throw a new exception of the given type with a message.
*
* @param type the exception type (must not be {@code null})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this edit hit the wrong line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, you're right, my bad. Will fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/**
* Throw a new exception of the given type with a message.
*
* @param type the exception type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here; type is not nullable but message is nullable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 45 to 48
ClassDesc[] params = new ClassDesc[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
params[i] = Util.classDesc(paramTypes[i]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Stream.of(paramTypes).map(Util::classDesc).toArray(ClassDesc[]::new)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -18,6 +18,7 @@
import io.quarkus.gizmo2.impl.constant.ConstantImpl;

public abstract non-sealed class Item implements Expr {
private final String creationSite = Util.callerOutsideGizmo();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this might have a significant performance impact. We might want to disable it by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I was expecting this comment :-) I have no idea how I would quantify the impact, but the number of Items we allocate is nontrivial, so the impact here is likely to be nontrivial as well. I'll add a system property.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Ladicek added 6 commits April 17, 2025 11:04
The existing overloads of `newEmptyArray()` take an `Expr` as a size,
but it's often necessary to create a new array of statically known size.
The new overloads are easier to use in that case.
It is often necessary to build the exception message dynamically, even
if the exception class is known statically. The new overloads are easier
to use in such situation.
It is often needed to invoke a constructor of a dynamically created class
which has a statically known parameter list. The new overloads are easier
to use in such situation.

It is unfortunately not possible to add an overload that would accept
a `List<Class<?>>`, because that would have the same erasure as an already
existing method that takes `List<ClassDesc>`.
A `ConstructorDesc` must have a return type of `void`, which most factory
methods on `ConstructorDesc` guarantee, but not all of them. In this commit,
we validate the return type in the constructor of `ConstructorDescImpl`,
which is what all factory methods end up calling.
This method returns a helper object of class `TestClassOps` that contains
the same helper methods as the `TestClassMaker` class, except they operate
on the given class instead of the last accepted class. In fact, the helper
methods of `TestClassMaker` now delegate to `TestClassOps` created for the
last accepted class.
When the system property `gizmo.trackCreations` is set (even to an empty
value, the only exception is the `false` value), all created `Item`s
will carry a textual description of their creation site (which consists
of the class name, method name and line number). When the Item is used
on an unexpected place, that creation site description is used as part
of the exception message, greatly improving the debugging experience.
@Ladicek Ladicek force-pushed the small-improvements branch from fd22082 to 131610d Compare April 17, 2025 09:07
@Ladicek
Copy link
Contributor Author

Ladicek commented Apr 17, 2025

In addition to addressing the comments, I reworked the commit that updates TestClassMaker. The class has gained a method forClass() that returns a helper object of class TestClassOps with the same methods as TestClassMaker, except for the given class (and not for the last accepted class). Actually, the helper methods directly on TestClassMaker now delegate to TestClassOps created for the last accepted class. That feels more natural and more universal.

@dmlloyd dmlloyd merged commit 20fa855 into quarkusio:main Apr 17, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in WG - Gizmo 2 Apr 17, 2025
@Ladicek Ladicek deleted the small-improvements branch April 17, 2025 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x Issue applies to Gizmo 2.x
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants