-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Avoid naming clashes in code generation #415
Conversation
Important Review skippedMore than 25% of the files skipped due to max files limit. The review is being skipped to prevent a low-quality review. 37 files out of 119 files are above the max files limit of 75. Please upgrade to Pro plan to get higher limits. You can disable this status message by setting the 📝 Walkthrough📝 WalkthroughWalkthroughThe pull request introduces modifications to the Changes
Assessment against linked issues
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 6
🧹 Outside diff range and nitpick comments (10)
src/test/java/org/joda/beans/sample/FieldNamesLight.java (3)
103-118
: Consider consistent use ofthis.
prefix for all field referencesWhile the equals method correctly handles field comparisons, some field references use the
this.
prefix (for 'obj' and 'other') while others don't. For consistency and to align with the PR's objective of avoiding naming clashes, consider using thethis.
prefix for all field references.@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj != null && obj.getClass() == this.getClass()) { FieldNamesLight other = (FieldNamesLight) obj; return JodaBeanUtils.equal(this.obj, other.obj) && JodaBeanUtils.equal(this.other, other.other) && - JodaBeanUtils.equal(propertyName, other.propertyName) && - JodaBeanUtils.equal(newValue, other.newValue) && - JodaBeanUtils.equal(bean, other.bean) && - JodaBeanUtils.equal(beanToCopy, other.beanToCopy); + JodaBeanUtils.equal(this.propertyName, other.propertyName) && + JodaBeanUtils.equal(this.newValue, other.newValue) && + JodaBeanUtils.equal(this.bean, other.bean) && + JodaBeanUtils.equal(this.beanToCopy, other.beanToCopy); } return false; }
120-130
: Consider consistent use ofthis.
prefix in hashCode methodFor consistency with the equals method and to prevent potential naming clashes, consider using the
this.
prefix for all field references.@Override public int hashCode() { int hash = getClass().hashCode(); - hash = hash * 31 + JodaBeanUtils.hashCode(obj); - hash = hash * 31 + JodaBeanUtils.hashCode(other); - hash = hash * 31 + JodaBeanUtils.hashCode(propertyName); - hash = hash * 31 + JodaBeanUtils.hashCode(newValue); - hash = hash * 31 + JodaBeanUtils.hashCode(bean); - hash = hash * 31 + JodaBeanUtils.hashCode(beanToCopy); + hash = hash * 31 + JodaBeanUtils.hashCode(this.obj); + hash = hash * 31 + JodaBeanUtils.hashCode(this.other); + hash = hash * 31 + JodaBeanUtils.hashCode(this.propertyName); + hash = hash * 31 + JodaBeanUtils.hashCode(this.newValue); + hash = hash * 31 + JodaBeanUtils.hashCode(this.bean); + hash = hash * 31 + JodaBeanUtils.hashCode(this.beanToCopy); return hash; }
132-144
: Consider consistent use ofthis.
prefix in toString methodFor consistency with other methods, consider using the
this.
prefix for all field references.@Override public String toString() { StringBuilder buf = new StringBuilder(224); buf.append("FieldNamesLight{"); - buf.append("obj").append('=').append(JodaBeanUtils.toString(obj)).append(',').append(' '); - buf.append("other").append('=').append(JodaBeanUtils.toString(other)).append(',').append(' '); - buf.append("propertyName").append('=').append(JodaBeanUtils.toString(propertyName)).append(',').append(' '); - buf.append("newValue").append('=').append(JodaBeanUtils.toString(newValue)).append(',').append(' '); - buf.append("bean").append('=').append(JodaBeanUtils.toString(bean)).append(',').append(' '); - buf.append("beanToCopy").append('=').append(JodaBeanUtils.toString(beanToCopy)); + buf.append("obj").append('=').append(JodaBeanUtils.toString(this.obj)).append(',').append(' '); + buf.append("other").append('=').append(JodaBeanUtils.toString(this.other)).append(',').append(' '); + buf.append("propertyName").append('=').append(JodaBeanUtils.toString(this.propertyName)).append(',').append(' '); + buf.append("newValue").append('=').append(JodaBeanUtils.toString(this.newValue)).append(',').append(' '); + buf.append("bean").append('=').append(JodaBeanUtils.toString(this.bean)).append(',').append(' '); + buf.append("beanToCopy").append('=').append(JodaBeanUtils.toString(this.beanToCopy)); buf.append('}'); return buf.toString(); }src/test/java/org/joda/beans/sample/FieldNamesMutableMinimal.java (1)
71-77
: Consider using consistent field access patterns.The getter functions use direct field access (e.g.,
b -> b.obj
), whilst the setters usethis
prefix. For consistency with the PR's objective of avoiding naming clashes, consider usingthis
prefix in the getter functions as well.Arrays.<Function<FieldNamesMutableMinimal, Object>>asList( - b -> b.obj, - b -> b.other, - b -> b.propertyName, - b -> b.newValue, - b -> b.bean, - b -> b.beanToCopy), + b -> b.this.obj, + b -> b.this.other, + b -> b.this.propertyName, + b -> b.this.newValue, + b -> b.this.bean, + b -> b.this.beanToCopy),src/test/java/org/joda/beans/sample/FieldNamesImmutableMinimal.java (2)
97-110
: Consider using 'this' prefix in constructorGiven the PR's focus on avoiding name clashes, consider explicitly using the 'this' prefix in the constructor assignments for consistency and clarity:
- this.obj = obj; - this.other = other; - this.propertyName = propertyName; - this.newValue = newValue; - this.bean = bean; - this.beanToCopy = beanToCopy; + this.obj = this.obj; + this.other = this.other; + this.propertyName = this.propertyName; + this.newValue = this.newValue; + this.bean = this.bean; + this.beanToCopy = this.beanToCopy;
256-264
: Consider adding parameter validation in build methodThe
build()
method could benefit from parameter validation to ensure robust testing:@Override public FieldNamesImmutableMinimal build() { + // Validate parameters to ensure robust testing + if (obj == null || other == null || propertyName == null || + newValue == null || bean == null || beanToCopy == null) { + throw new IllegalStateException("All parameters must be non-null"); + } return new FieldNamesImmutableMinimal( obj, other, propertyName, newValue, bean, beanToCopy); }src/main/java/org/joda/beans/gen/PropertyGen.java (2)
83-85
: LGTM! Consider using a constant for the property name.The change correctly addresses the naming clash issue by prefixing the field reference with 'this.' when the field name is 'propertyName'. This fix aligns with the PR objectives and resolves the shadowing problem described in issue #405.
Consider extracting 'propertyName' as a private static final constant to improve maintainability:
+ private static final String PROPERTY_NAME = "propertyName"; - fieldName = fieldName.equals("propertyName") ? "this.propertyName" : fieldName; + fieldName = fieldName.equals(PROPERTY_NAME) ? "this." + PROPERTY_NAME : fieldName;
209-211
: LGTM! Maintain consistency with the meta-property implementation.The change correctly implements the same fix in the builder context, ensuring consistent behaviour across the codebase.
For consistency with the meta-property implementation, consider using the same constant:
+ private static final String PROPERTY_NAME = "propertyName"; - fieldName = fieldName.equals("propertyName") ? "this.propertyName" : fieldName; + fieldName = fieldName.equals(PROPERTY_NAME) ? "this." + PROPERTY_NAME : fieldName;src/test/java/org/joda/beans/sample/FieldNamesMutable.java (1)
314-331
: Consider renaming parameter to avoid field shadowingIn the
metaPropertyGet
method, the parameter namepropertyName
could shadow the field of the same name. Consider renaming the parameter to something likepropertyKey
to avoid potential confusion.@Override - protected MetaProperty<?> metaPropertyGet(String propertyName) { - switch (propertyName.hashCode()) { + protected MetaProperty<?> metaPropertyGet(String propertyKey) { + switch (propertyKey.hashCode()) { case 109815: // obj return obj; case 106069776: // other return other; case -864691712: // propertyName return this.propertyName; case 1368456113: // newValue return newValue; case 3019696: // bean return bean; case -1343227808: // beanToCopy return beanToCopy; } - return super.metaPropertyGet(propertyName); + return super.metaPropertyGet(propertyKey); }src/main/java/org/joda/beans/gen/BeanGen.java (1)
815-820
: LGTM! Consider extracting the prefix logic to a helper method.The changes correctly address the naming clash issue by prefixing field references with "this." when the field name is "obj" or "other". The implementation maintains proper handling of primitive types.
A minor suggestion to improve readability: Consider extracting the prefix determination logic into a helper method like
getFieldAccessPrefix(String fieldName)
. This would make the code more maintainable and reusable if similar prefixing is needed elsewhere.+ private String getFieldAccessPrefix(String fieldName) { + return fieldName.equals("obj") || fieldName.equals("other") ? "this." : ""; + }
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (7)
src/main/java/org/joda/beans/gen/BeanGen.java
(1 hunks)src/main/java/org/joda/beans/gen/PropertyGen.java
(2 hunks)src/test/java/org/joda/beans/sample/FieldNamesImmutable.java
(1 hunks)src/test/java/org/joda/beans/sample/FieldNamesImmutableMinimal.java
(1 hunks)src/test/java/org/joda/beans/sample/FieldNamesLight.java
(1 hunks)src/test/java/org/joda/beans/sample/FieldNamesMutable.java
(1 hunks)src/test/java/org/joda/beans/sample/FieldNamesMutableMinimal.java
(1 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
src/main/java/org/joda/beans/gen/BeanGen.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. - Allow use of
var
keyword when value is a castnull
. - 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 handlesnull
, returningfalse
.
Pattern **/main/java/**/*.java
: - This project is mature and must provide a stable backwards-compatible public Java API.
- In the 'Summary by CodeRabbit' 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 filemodule-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/gen/PropertyGen.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. - Allow use of
var
keyword when value is a castnull
. - 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 handlesnull
, returningfalse
.
Pattern **/main/java/**/*.java
: - This project is mature and must provide a stable backwards-compatible public Java API.
- In the 'Summary by CodeRabbit' 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 filemodule-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/sample/FieldNamesImmutable.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. - Allow use of
var
keyword when value is a castnull
. - 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 handlesnull
, returningfalse
.
Pattern **/test/java/**/*.java
: For test code, focus on:
- Correctness of test assertions
- Test coverage of edge cases
- Clear test naming and documentation
- Be more lenient with code style and minor optimisations
src/test/java/org/joda/beans/sample/FieldNamesImmutableMinimal.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. - Allow use of
var
keyword when value is a castnull
. - 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 handlesnull
, returningfalse
.
Pattern **/test/java/**/*.java
: For test code, focus on:
- Correctness of test assertions
- Test coverage of edge cases
- Clear test naming and documentation
- Be more lenient with code style and minor optimisations
src/test/java/org/joda/beans/sample/FieldNamesLight.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. - Allow use of
var
keyword when value is a castnull
. - 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 handlesnull
, returningfalse
.
Pattern **/test/java/**/*.java
: For test code, focus on:
- Correctness of test assertions
- Test coverage of edge cases
- Clear test naming and documentation
- Be more lenient with code style and minor optimisations
src/test/java/org/joda/beans/sample/FieldNamesMutable.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. - Allow use of
var
keyword when value is a castnull
. - 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 handlesnull
, returningfalse
.
Pattern **/test/java/**/*.java
: For test code, focus on:
- Correctness of test assertions
- Test coverage of edge cases
- Clear test naming and documentation
- Be more lenient with code style and minor optimisations
src/test/java/org/joda/beans/sample/FieldNamesMutableMinimal.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. - Allow use of
var
keyword when value is a castnull
. - 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 handlesnull
, returningfalse
.
Pattern **/test/java/**/*.java
: For test code, focus on:
- Correctness of test assertions
- Test coverage of edge cases
- Clear test naming and documentation
- Be more lenient with code style and minor optimisations
🔇 Additional comments (13)
src/test/java/org/joda/beans/sample/FieldNamesLight.java (4)
28-51
: Well-structured test class for field name clash scenarios!
The class is properly designed to test field name clashes with common variable names. The fields are well-documented and correctly annotated.
53-80
: Meta-bean setup looks good!
The meta-bean is properly initialised and registered, with all field names correctly specified.
82-95
: Constructor implementation is correct!
The private constructor properly initialises all fields, maintaining immutability.
97-100
: Meta-bean accessor is properly implemented!
src/test/java/org/joda/beans/sample/FieldNamesMutableMinimal.java (2)
34-54
: Well-structured test class for field name clash scenarios.
The class effectively sets up test cases for common variable names that could cause shadowing issues. The use of @PropertyDefinition(get = "field")
ensures direct field access, which is appropriate for testing field name clash scenarios.
108-155
: Excellent implementation of setters with consistent field access.
The setter methods consistently use the this
prefix for field access, which effectively prevents variable shadowing. This implementation aligns perfectly with the PR's objective of avoiding naming clashes.
src/test/java/org/joda/beans/sample/FieldNamesImmutableMinimal.java (3)
33-53
: Well-structured test class for field name clash scenarios
The class effectively sets up test scenarios for field name clashes with obj
, other
, propertyName
, etc. The use of @PropertyDefinition(get = "field")
ensures direct field access, which is appropriate for testing the fix.
59-75
: Verify meta-bean field access implementation
The meta-bean setup uses lambda expressions for field access. Consider adding test cases to verify that these lambdas correctly handle the field name clashes, particularly for the obj
and other
fields.
30-32
: Verify test coverage for name clash scenarios
This class is marked as a mock JavaBean for testing, but we should verify that comprehensive test cases exist to exercise the name clash scenarios.
src/main/java/org/joda/beans/gen/PropertyGen.java (1)
83-85
: Verify handling of 'obj' and 'other' property names.
The current changes address the 'propertyName' case, but the PR objectives also mention issues with properties named 'obj' and 'other'. Please verify if these cases need similar treatment.
Let's check if there are any fields named 'obj' or 'other' in the codebase that might need the same fix:
Also applies to: 209-211
src/test/java/org/joda/beans/sample/FieldNamesImmutable.java (1)
36-56
: Well-structured test class for field name clash scenarios!
The class definition effectively sets up test cases for potential naming conflicts, with fields specifically chosen to test common variable names that could clash in generated code.
src/test/java/org/joda/beans/sample/FieldNamesMutable.java (2)
36-56
: Well-structured test class for field name clash scenarios!
The class effectively tests field names that could potentially clash with common variable names in generated code. The consistent use of @PropertyDefinition(get = "field")
for all fields is appropriate.
444-618
: Builder implementation looks good!
The builder implementation follows best practices with:
- Proper field initialisation
- Correct copy constructor implementation
- Fluent interface for method chaining
src/test/java/org/joda/beans/sample/FieldNamesMutableMinimal.java
Outdated
Show resolved
Hide resolved
src/test/java/org/joda/beans/sample/FieldNamesImmutableMinimal.java
Outdated
Show resolved
Hide resolved
* Prefix properties with `this.` when not currently prefixed * Fixes #405
c36412a
to
1d8d668
Compare
obj
,other
orpropertyName
caused problemsthis
to avoid a clashSummary by CodeRabbit
equals
andhashCode
methods to enhance accuracy in property comparisons.