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

Convert ResolvedType to Type #461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Convert ResolvedType to Type #461

wants to merge 1 commit into from

Conversation

jodastephen
Copy link
Member

@jodastephen jodastephen commented Jan 2, 2025

  • Allow a ResolvedType to be converted to a Type
  • Rename methods and add Javadoc for clarity
  • Fix uses of resolved type in Binary/JSON serialization

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced type resolution capabilities in the Joda Beans library.
    • Improved handling of generic types and arrays.
    • Added support for serializable type resolution.
  • Improvements

    • Refined parsing logic for type arguments.
    • Updated method signatures to support more flexible type handling.
    • Introduced new methods for converting between resolved types and Java types.
    • Standardised handling of raw types for collections and optional types during serialization.
  • Testing

    • Expanded test coverage for complex type scenarios.
    • Added new test cases for dynamic and array type resolution.

These updates improve the library's type handling and provide more robust type resolution mechanisms.

Copy link

coderabbitai bot commented Jan 2, 2025

📝 Walkthrough

Walkthrough

The pull request introduces comprehensive modifications to the ResolvedType class in the Joda Beans library, focusing on enhancing type resolution capabilities. The changes include updates to method signatures to incorporate an allowRaw parameter, the addition of new methods for handling raw type resolution, and the introduction of private classes for managing parameterized types and generic arrays. Corresponding adjustments have been made in serialization and test classes to support the new type resolution mechanisms, ensuring consistent handling of generics and collections.

Changes

File Change Summary
src/main/java/org/joda/beans/ResolvedType.java - Added methods: fromAllowRaw(), toJavaType(), toBaseType()
- Updated method signatures to include allowRaw parameter
- Replaced from() with a private method accepting allowRaw
- Added DynamicParameterizedType and DynamicGenericArrayType classes
src/main/java/org/joda/beans/ser/bin/JodaBeanStandardBinWriter.java - Replaced toRawType() with toRaw() in optional handlers
- Updated toWeakenedType method to use base.toRaw()
src/main/java/org/joda/beans/ser/json/JodaBeanJsonWriter.java - Replaced propertyGenericType() with propertyResolvedType()
- Updated toWeakenedType to use base.toRaw() for collections
- Modified optional handlers to use declaredType.getArgumentOrDefault(0).toRaw()
src/test/java/org/joda/beans/TestResolvedType.java - Added new test methods: test_javaType(), test_dynamicTypes()
- Extended data_resolvedTypes() with new test cases
- Introduced new test data generation method data_dynamicTypes()

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/main/java/org/joda/beans/ResolvedType.java (2)

402-406: Avoid over-reliance on Object.class in default type parameters
resolveClass(..., allowRaw) uses Object.class for missing type arguments unless allowRaw is true. This is sensible, but ensure no implicit casting or ClassCastException can occur downstream.

Do you want me to open a follow-up issue to propose additional validation logic for partially resolved generic types?

Also applies to: 411-411


762-824: Check serialisation logic in DynamicParameterizedType
The new DynamicParameterizedType provides a robust means to reify parameterised type info at runtime. Just ensure all relevant properties (e.g. ownerType) are consistently handled, especially in edge cases like local or anonymous classes.

src/test/java/org/joda/beans/TestResolvedType.java (1)

285-298: test_javaType
Verifying that toJavaType() yields the expected raw or parameterised type is key. Confirming round-trip equivalence with ResolvedType.fromAllowRaw(...) is an excellent strategy to detect regressions.

src/main/java/org/joda/beans/ser/bin/JodaBeanStandardBinWriter.java (1)

684-684: Handling potential null entries
Lines 684, 708, and 722 carefully write null or skip writing properties when Optionals are empty. This logic matches the project’s existing approach to minimal representation. Just ensure the skipping logic is documented for users.

Also applies to: 708-708, 722-722

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5c6e611 and 498679a.

📒 Files selected for processing (4)
  • src/main/java/org/joda/beans/ResolvedType.java (13 hunks)
  • src/main/java/org/joda/beans/ser/bin/JodaBeanStandardBinWriter.java (5 hunks)
  • src/main/java/org/joda/beans/ser/json/JodaBeanJsonWriter.java (6 hunks)
  • src/test/java/org/joda/beans/TestResolvedType.java (6 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
src/main/java/org/joda/beans/ser/bin/JodaBeanStandardBinWriter.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
src/main/java/org/joda/beans/ser/json/JodaBeanJsonWriter.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
src/test/java/org/joda/beans/TestResolvedType.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/test/java/**/*.java: For test code, focus on:

  • Correctness of test assertions
  • Test coverage of edge cases
  • Clear test naming and documentation
  • Encourage test methods to be package-scoped where possible
  • Be more lenient with code style and minor optimisations
src/main/java/org/joda/beans/ResolvedType.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
🔇 Additional comments (17)
src/main/java/org/joda/beans/ResolvedType.java (8)

Line range hint 333-346: Use of from(Class<?> rawType) ensures safe defaults
The new from(Class<?> rawType) method’s approach of defaulting missing type arguments to Object is clear and intuitive. It avoids null type arguments while preserving backward compatibility.


350-364: Ensure consistent usage of new from(Type javaType, Class<?> contextClass, boolean allowRaw)
The refactored code introduces an additional boolean parameter allowRaw. Verify that all callers in the codebase understand the difference between allowing raw types and enforcing default type arguments, to avoid unexpected behaviour.


365-382: Check for potential side effects of allowRaw
When allowRaw is set to true, type variables might be left unresolved instead of defaulting to Object. Ensure that all downstream code that processes these ResolvedType objects can handle incomplete type arguments.


417-421: Preferred usage of var
In the loop that populates typeArguments, the code uses for (var i = 0; ...). This aligns well with Java 21 guidelines to streamline type declarations. Nicely done.


610-623: Implementation of toJavaType()
The new logic for arrays and parameterised types is well-structured. Wrapping parameterised types with DynamicParameterizedType or arrays with DynamicGenericArrayType ensures correct Type representations.


Line range hint 625-647: Methods for array handling
toComponentType() and toComponentTypeOrDefault() gracefully handle arrays, providing error prevention for non-array types. This clarity improves maintainability.


671-683: toBaseType() for dropping array dimensions
This method is useful for obtaining the non-array class while preserving generic arguments. Make sure that upstream code is tested with multi-dimensional arrays to confirm correctness.


827-859: DynamicGenericArrayType
Similarly, this class helps represent generic arrays. Confirm no reflection-based logic incorrectly identifies multi-dimensional arrays as single-dimensional, and ensure test coverage of complex generic array scenarios.

src/test/java/org/joda/beans/TestResolvedType.java (4)

71-72: Raw vs. parameterised usage
The calls to ResolvedType.of(List.class) with no arguments vs. ResolvedType.from(List.class) highlight the subtle distinction between raw types and defaulted arguments. These tests effectively reveal any pitfalls in raw-type handling.


94-96: Testing arrays of collections
Testing List[].class helps ensure array-of-generic classes remain accurate through the new logic in ResolvedType, including correct string representations and argument forwarding.


245-254: test_queries
Lines 245-254 introduce checks for isArray and isRaw. The approach of peeling off each array dimension to test the underlying raw type is correct. This ensures robust coverage of multi-dimensional arrays.


375-386: Refined dynamic type tests
These lines introduce fields (field0 to field3b) as test fixtures for raw and parameterised array types, plus Optional. The checks in test_dynamicTypes confirm that no unexpected resolution behaviour creeps in. This is thorough and good practice.

Also applies to: 390-401, 403-415

src/main/java/org/joda/beans/ser/bin/JodaBeanStandardBinWriter.java (2)

272-272: toWeakenedType shift from toRawType() to toRaw()
Returning base.toRaw() now ensures the raw type is used for collections. This helps unify logic with other changes in ResolvedType. Confirm that older code references have been fully updated.


670-670: OptionalBinHandler respects raw argument
Using declaredType.getArgumentOrDefault(0).toRaw() for Optionals ensures consistent serialisation of the underlying type. This is a positive move for uniformity.

src/main/java/org/joda/beans/ser/json/JodaBeanJsonWriter.java (3)

276-276: Fetching resolved type
Replacing metaProperty.propertyGenericType(...) with metaProperty.propertyResolvedType(...) ensures that the new advanced type resolution is used. This helps unify JSON serialisation with the same logic as binary serialisation.


481-481: Use of toRaw() with nested collections
Switching from toRawType() to toRaw() keeps the code aligned with the updated ResolvedType. This ensures no partial type arguments remain inadvertently.


898-898: OptionalJsonHandler and GuavaOptionalJsonHandler
Lines 898, 912, 931, and 945 consistently retrieve toRaw() for the contained type and skip writing empty optionals. This matches best practices for consistency and simpler JSON.

Also applies to: 912-912, 931-931, 945-945

* Allow a `ResolvedType` to be converted to a `Type`
* Rename methods and add Javadoc for clarity
* Fix uses of resolved type in Binary/JSON serialization
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/test/java/org/joda/beans/TestResolvedType.java (1)

376-414: Dynamic type tests are comprehensive, consider adding more edge cases.

The test coverage is good, but consider adding test cases for:

  1. Nested generic types (e.g., List<Map<String, Integer>>)
  2. Wildcard types with bounds
src/main/java/org/joda/beans/ResolvedType.java (2)

761-822: DynamicParameterizedType implementation is thorough!

Consider adding @OverRide annotation to toString() method for consistency.

-        public String toString() {
+        @Override
+        public String toString() {

824-858: DynamicGenericArrayType implementation is solid!

Consider adding @OverRide annotation to toString() method for consistency.

-        public String toString() {
+        @Override
+        public String toString() {
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 498679a and 326c78e.

📒 Files selected for processing (4)
  • src/main/java/org/joda/beans/ResolvedType.java (13 hunks)
  • src/main/java/org/joda/beans/ser/bin/JodaBeanStandardBinWriter.java (5 hunks)
  • src/main/java/org/joda/beans/ser/json/JodaBeanJsonWriter.java (6 hunks)
  • src/test/java/org/joda/beans/TestResolvedType.java (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/java/org/joda/beans/ser/bin/JodaBeanStandardBinWriter.java
  • src/main/java/org/joda/beans/ser/json/JodaBeanJsonWriter.java
🧰 Additional context used
📓 Path-based instructions (2)
src/test/java/org/joda/beans/TestResolvedType.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/test/java/**/*.java: For test code, focus on:

  • Correctness of test assertions
  • Test coverage of edge cases
  • Clear test naming and documentation
  • Encourage test methods to be package-scoped where possible
  • Be more lenient with code style and minor optimisations
src/main/java/org/joda/beans/ResolvedType.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
🔇 Additional comments (9)
src/test/java/org/joda/beans/TestResolvedType.java (4)

71-72: Test case for raw List type looks good!

The test case properly verifies the handling of raw List type without type arguments.


94-96: Test case for List array type looks good!

The test case properly verifies the handling of List array type without type arguments.


245-254: Array type handling logic looks good!

The code correctly extracts the base component type from array types, handling both single and double array dimensions. The isRaw() check is properly updated to use the base component type.


285-297: Test method for toJavaType() is well-structured!

The test method effectively verifies:

  1. Raw type conversion for types without arguments
  2. Bidirectional conversion between ResolvedType and Java Type
src/main/java/org/joda/beans/ResolvedType.java (5)

Line range hint 332-346: Method update aligns with PR objectives!

The method now correctly handles raw type resolution with clear documentation and examples.


348-380: API design for type resolution is well-structured!

The separation between from() and fromAllowRaw() provides clear choices for type resolution with excellent documentation.


401-413: Type resolution logic is robust!

The method properly handles raw types and type resolution based on the allowRaw flag.


605-622: Type conversion implementation is elegant!

The method effectively handles conversion to Java Type for both simple types and complex generic/array types.


670-682: Base type extraction is well-implemented!

The method effectively extracts base types from array types with clear documentation and examples.

@jodastephen jodastephen changed the title Convert ResolvedTyoe to Type Convert ResolvedType to Type Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant