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

Modify Types.instanceOf to take a ClassInfo as LHS #410

Conversation

zhangwen0411
Copy link
Contributor

This PR is a port of #388 to the java-10-gradle branch.

I omitted a test case (f575504) that depends on the ASM framework, a dependency added to the master branch in #203 but not present in the java-10-gradle branch. I'd be happy to add the dependency and the test case if that's preferred!

This method's declaration currently reads `instanceOf(String, String)`,
taking both the LHS and RHS types as type signature strings.  As a
result, to use a `ClassInfo` object `ci` as the LHS, a caller must
convert it into a string via `ci.getType()` (see
`ElementInfo.instanceOf`), before the string eventually gets turned back
into a `ClassInfo` object (if it's a reference type) in the method body.

This patch changes the method declaration to read `instanceOf(ClassInfo,
String)`.  This change not only saves the back-and-forth, but also
addresses the case where the LHS class has been loaded by a non-default
class loader, in which case the original implementation fails to resolve
its type string back into a `ClassInfo`.

One downside is that if LHS is a nested array, the new implementation
will resolve a `ClassInfo` object for every "layer" of the array (via
`ClassInfo.getComponentClassInfo`), while the original implementation
"peels" the array via (cheaper) calls to `String.substring`.  I'm
guessing this performance degradation will not be noticeable in common
scenarios.

This patch also simplifies the implementation of `instanceOf`, ensures
it recognizes arrays as subtypes of Cloneable and Serializable (JLS
4.10.3), changes the names of a few variables to clarify they're
expected to contain type signatures, and adds unit tests (to
`ClassTest.java`, where the existing `testInstanceOf` unit test
resides).
Copy link
Member

@cyrille-artho cyrille-artho left a comment

Choose a reason for hiding this comment

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

Thanks, I think that if it's possible to avoid ASM, then it is better to omit a test. Perhaps we can later have an equivalent test without ASM.
@pparizek , WDYT?

@pparizek
Copy link
Contributor

Maybe the relevant classes (example.MyClass1, example.MyClass2) can be added in the form of real class files as resources for tests, instead of generating them at runtime with ASM.

@cyrille-artho
Copy link
Member

cyrille-artho commented Aug 16, 2023 via email

@cyrille-artho
Copy link
Member

@zhangwen0411 , can you please add two empty classes with suitable package names to src/tests and check if the additional test works like that (without the code that manually builds the corresponding bytecode)? See issue #411.

Copy link
Member

@cyrille-artho cyrille-artho left a comment

Choose a reason for hiding this comment

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

If possible, let's add the two classes the normal way, as I've described in issue #411.

@cyrille-artho
Copy link
Member

I see. In that case, I think it's best to add the extra test as is now, and we can see later if the test can be rewritten later.

@cyrille-artho
Copy link
Member

Subsumed by PR #415 (which we wanted to merge now as some other patches depend on it). Thank you for providing this PR, which served us as a template.

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