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

Cache ResolvedType in MetaProperty #434

Merged
merged 1 commit into from
Nov 22, 2024
Merged

Cache ResolvedType in MetaProperty #434

merged 1 commit into from
Nov 22, 2024

Conversation

jodastephen
Copy link
Member

@jodastephen jodastephen commented Nov 22, 2024

  • Statically defined meta-properties can cache the resolved type

Summary by CodeRabbit

  • New Features

    • Introduced methods for enhanced type resolution in property management.
    • Added functionality to check if a type is parameterized.
  • Bug Fixes

    • Updated documentation for existing methods to clarify their behaviour.
  • Tests

    • Enhanced test coverage by adding assertions to validate parameterized types.

* Statically defined meta-properties can cache the resolved type
Copy link

coderabbitai bot commented Nov 22, 2024

📝 Walkthrough

Walkthrough

The changes involve modifications to the MetaProperty, ResolvedType, DirectMetaProperty, MinimalMetaProperty, and LightMetaProperty classes within the org.joda.beans package. Key updates include the addition of new methods for resolving property types and enhancements to existing methods to clarify their functionality. New fields have been introduced in several classes to facilitate type resolution based on context. Additionally, a test case has been updated to improve coverage regarding the parameterised state of ResolvedType.

Changes

File Path Change Summary
src/main/java/org/joda/beans/MetaProperty.java - Updated Javadoc for propertyGenericType().
- Added propertyResolvedType(Class<?> contextClass).
src/main/java/org/joda/beans/ResolvedType.java - Added isParameterized() method.
- Updated isRaw() method documentation.
src/main/java/org/joda/beans/impl/direct/DirectMetaProperty.java - Added fields: propertyGenericType, propertyResolvedTypeFn.
- Simplified propertyGenericType() method.
- Added propertyResolvedType(Class<?> contextClass).
src/main/java/org/joda/beans/impl/direct/MinimalMetaProperty.java - Added field: propertyResolvedTypeFn.
- Added createResolvedTypeFunction().
- Updated propertyResolvedType(Class<?> contextClass).
src/main/java/org/joda/beans/impl/light/LightMetaProperty.java - Added field: propertyResolvedTypeFn.
- Updated propertyResolvedType(Class<?> contextClass).
src/test/java/org/joda/beans/TestResolvedType.java - Added assertion to test_queries method to check if ResolvedType is parameterized.

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 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

🧹 Outside diff range and nitpick comments (3)
src/main/java/org/joda/beans/MetaProperty.java (1)

96-106: Implementation looks good and maintains backwards compatibility

The new default method provides a clean way to resolve generic types relative to a context class whilst allowing implementations to override if needed.

Walkthrough of Public API Changes:

  1. Added new public method to MetaProperty interface:
    • ResolvedType propertyResolvedType(Class<?> contextClass)
    • Impact: This is a backwards-compatible change as it's a default method
    • Usage: Enables clients to resolve generic types in the context of a specific class
    • Example: Useful when dealing with generic type parameters in inheritance hierarchies
src/main/java/org/joda/beans/ResolvedType.java (1)

Line range hint 1-600: Changes to Public Java API

The following changes have been made to the public Java API:

  1. Added new public method boolean isParameterized() to ResolvedType class
    • Impact: Additive change that provides new functionality without breaking existing code
    • Usage: Allows clients to check if a type has generic parameters, regardless of whether they are specified

The update to isRaw() refines the implementation without changing its contract, maintaining backwards compatibility.

src/main/java/org/joda/beans/impl/direct/MinimalMetaProperty.java (1)

127-130: Suggestion: Simplify the lambda expressions for readability

Consider extracting the lambda expressions into separate, named methods to improve readability and maintainability, especially for complex conditional logic.

Apply this refactoring:

 return !resolvedType.isParameterized() || Modifier.isFinal(beanType.getModifiers()) ?
-        contextClass -> resolvedType :
-        contextClass -> contextClass == beanType ? resolvedType : ResolvedType.from(propertyGenericType, contextClass);
+        this::resolveTypeForFinalBean :
+        this::resolveTypeForNonFinalBean;

+private ResolvedType resolveTypeForFinalBean(Class<?> contextClass) {
+    return resolvedType;
+}

+private ResolvedType resolveTypeForNonFinalBean(Class<?> contextClass) {
+    return contextClass == beanType ? resolvedType : ResolvedType.from(propertyGenericType, contextClass);
+}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b6a89b0 and a683dc7.

📒 Files selected for processing (6)
  • src/main/java/org/joda/beans/MetaProperty.java (1 hunks)
  • src/main/java/org/joda/beans/ResolvedType.java (1 hunks)
  • src/main/java/org/joda/beans/impl/direct/DirectMetaProperty.java (5 hunks)
  • src/main/java/org/joda/beans/impl/direct/MinimalMetaProperty.java (6 hunks)
  • src/main/java/org/joda/beans/impl/light/LightMetaProperty.java (4 hunks)
  • src/test/java/org/joda/beans/TestResolvedType.java (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
src/main/java/org/joda/beans/MetaProperty.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/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.
src/main/java/org/joda/beans/impl/direct/DirectMetaProperty.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/impl/direct/MinimalMetaProperty.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/impl/light/LightMetaProperty.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
🔇 Additional comments (19)
src/main/java/org/joda/beans/MetaProperty.java (1)

92-92: LGTM - Documentation update looks correct

The removal of "unmodifiable" from the Javadoc is appropriate since java.lang.reflect.Type is inherently immutable.

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

266-266: LGTM! Comprehensive test coverage for isParameterized()

The new assertion correctly validates the parameterised state of ResolvedType by comparing against the raw type's type parameters. The test data in data_resolvedTypes() provides good coverage with various cases including generic types, arrays, and nested generics.

Let's verify the test data covers all edge cases:

✅ Verification successful

Test coverage for isParameterized() is comprehensive and well-structured

The test data in data_resolvedTypes() provides excellent coverage for parameterised types, including:

  • Raw generic types (List, Map)
  • Parameterised types with single type argument (List<String>)
  • Parameterised types with multiple type arguments (Map<String, Number>)
  • Nested generic types (List<List<Address>>)
  • Array types with generics (List<String>[], List<String>[][])
  • Complex nested types (Map<String, List<Number>[]>[])
  • Bounded type parameters with extends (Comparable<Object>)
  • Collection types from different libraries (ImmutableList, ImmutableMap, ImmutableMultiset)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify test coverage for parameterized types
# Look for test cases in data_resolvedTypes() that exercise different scenarios

# Test: Search for various generic type patterns in test data
echo "Checking test coverage for different parameterized type scenarios..."
rg -A 3 'ResolvedType\.(of|from|ofFlat)' src/test/java/org/joda/beans/TestResolvedType.java | \
  grep -E '(List|Map|ImmutableList|ImmutableMap|Comparable|AbstractResult)'

Length of output: 7544

src/main/java/org/joda/beans/ResolvedType.java (2)

475-484: Well-implemented new method with clear documentation

The new isParameterized() method is well-documented with examples and correctly implements the check for generic type parameters. The implementation is efficient and maintains the class's thread-safety.


489-492: Refined implementation maintains backwards compatibility

The isRaw() method has been updated to use isParameterized(), making the code more maintainable whilst preserving backwards compatibility. The change clarifies that a raw type must be both parameterizable and have empty arguments.

src/main/java/org/joda/beans/impl/direct/MinimalMetaProperty.java (6)

21-21: Imports are necessary and correctly added

The import statements for java.lang.reflect.Modifier and org.joda.beans.ResolvedType are appropriately added and are required for the new functionality.

Also applies to: 32-32


48-49: Definition of 'propertyResolvedTypeFn' field is appropriate

The addition of the propertyResolvedTypeFn field correctly encapsulates the logic for resolving property types, enhancing the class's functionality.


89-89: Initialisation of 'propertyResolvedTypeFn' in constructors is correct

The initialisation of propertyResolvedTypeFn in both constructors ensures that the resolved type function is properly set up for different instantiation scenarios.

Also applies to: 121-121


124-130: Implementation of 'createResolvedTypeFunction()' is correct

The createResolvedTypeFunction() method effectively creates a function to determine the resolved type based on whether the property is parameterised and if the bean type is final, ensuring efficient type resolution.


153-156: Overriding 'propertyResolvedType' method is appropriate

The overridden propertyResolvedType method correctly utilises the propertyResolvedTypeFn function to provide dynamic type resolution based on the context class.


Line range hint 1-157: Walkthrough: Review of Public Java API Changes

There are no changes to the public Java API in this PR. All modifications are confined to internal classes and methods, and do not affect the public or protected methods on public classes.

src/main/java/org/joda/beans/impl/light/LightMetaProperty.java (3)

50-51: Addition of private field propertyResolvedTypeFn

The new private field propertyResolvedTypeFn is appropriately declared to cache the resolved type function.


229-233: Correct initialisation of propertyResolvedTypeFn

The logic for initialising propertyResolvedTypeFn effectively handles different scenarios based on whether resolvedType is parameterised and whether beanType is final.


257-260: Changes to the Public Java API

In compliance with the project guidelines, the following change to the public Java API may affect end users:

  1. Overridden Method: The method propertyResolvedType(Class<?> contextClass) is now overridden in LightMetaProperty.

    • Potential Impact: This change may alter the behaviour of LightMetaProperty instances for consumers who rely on the inherited implementation from the superclass BasicMetaProperty. Please verify that this alteration maintains backward compatibility and aligns with the expected functionality.
src/main/java/org/joda/beans/impl/direct/DirectMetaProperty.java (6)

Line range hint 22-36: Review of Added Import Statements and Type Declarations

The additional import statements for java.lang.reflect.Modifier, java.util.function.Function, and org.joda.beans.ResolvedType are appropriate and necessary for the implementation of the new functionality.


52-55: Appropriate Addition of Private Fields

The private fields propertyGenericType and propertyResolvedTypeFn are correctly declared and initialised. They effectively cache the generic type information and the function to resolve the property type, enhancing performance and reducing redundant computations.


203-207: Correct Use of Switch Expression for Initialising propertyGenericType

The utilisation of the switch expression accurately handles the different cases of fieldOrMethod. The inclusion of null, default ensures that all possible values are accounted for, defaulting to propertyType when necessary.


211-215: Efficient Initialisation of propertyResolvedTypeFn

The initialisation logic for propertyResolvedTypeFn optimises type resolution by precomputing or deferring computation based on whether the resolved type is parameterised or if the bean class is final. This approach balances performance with flexibility.


236-237: Optimisation in propertyGenericType() Method

Returning the cached propertyGenericType in the propertyGenericType() method avoids unnecessary recalculations, improving method efficiency.


239-241: Addition of New Public Method propertyResolvedType(Class<?> contextClass)

The new method enhances the API by allowing clients to obtain the resolved type of the property relative to a given context class. Ensure that this addition maintains backward compatibility and adheres to the project's API stability commitments.

@jodastephen jodastephen merged commit ae1ceca into main Nov 22, 2024
5 checks passed
@jodastephen jodastephen deleted the resolved-type3 branch November 22, 2024 15:07
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