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

Use modern Java language features #140

Merged
merged 1 commit into from
Aug 19, 2024
Merged

Use modern Java language features #140

merged 1 commit into from
Aug 19, 2024

Conversation

jodastephen
Copy link
Member

@jodastephen jodastephen commented Aug 19, 2024

Summary by CodeRabbit

  • New Features

    • Improved code readability across several classes by adopting the var keyword for type inference in variable declarations.
    • Enhanced documentation consistency through formatting adjustments in Javadoc comments.
  • Bug Fixes

    • No functional changes; all modifications focus on code style and documentation.
  • Style

    • Refactored variable declarations to use var for clearer and more concise code.
    • Standardised Javadoc formatting by removing unnecessary whitespace.
  • Tests

    • Updated test classes to use var for local variable declarations, maintaining existing logic while improving readability.

Copy link

coderabbitai bot commented Aug 19, 2024

Walkthrough

Walkthrough

The recent changes across the Joda Money codebase primarily focus on modernising the code with the introduction of the var keyword for type inference, enhancing readability and reducing verbosity. Additionally, minor formatting adjustments in documentation have been made to improve consistency without impacting the functionality. The modifications uphold existing logic while aligning the code with contemporary Java standards.

Changes

Files Change Summary
src/main/java/org/joda/money/BigMoney.java, BigMoneyProvider.java, CurrencyUnit.java, Money.java, MoneyUtils.java, Ser.java, AmountPrinterParser.java, MoneyFormatter.java, MoneyFormatterBuilder.java, SignedPrinterParser.java, TestMoney.java, TestMoneyUtils_BigMoney.java, TestStringConvert.java Replaced explicit type declarations with var for local variables, enhancing readability and code conciseness.
src/main/java/org/joda/money/CurrencyMismatchException.java, CurrencyUnitDataProvider.java, DefaultCurrencyUnitDataProvider.java, IllegalCurrencyException.java, MoneyFormatException.java, MoneyParseContext.java Minor whitespace adjustments in Javadoc comments for improved formatting without altering functionality.
src/main/java/org/joda/money/BigMoneyProvider.java, MoneyParser.java, MoneyPrinter.java Explicitly defined method visibility modifiers as public abstract in interface methods for clarity, without changing functionality.
src/main/java/org/joda/money/CurrencyUnit.java Updated generic type parameters in ConcurrentMap declarations to use the diamond operator (<>) for cleaner syntax.
src/test/java/org/joda/money/TestCurrencyMismatchException.java, TestCurrencyUnit.java, TestCurrencyUnitExtension.java, TestIllegalCurrencyException.java, src/test/java/org/joda/money/TestMoneyFormatterException.java Used var for local variable declarations in test cases, maintaining existing assertions and logic.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MoneyUtils
    participant BigMoney
    User ->> MoneyUtils: Calls method with Money
    MoneyUtils ->> BigMoney: Processes money calculations
    MoneyUtils -->> User: Returns the result
Loading

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@jodastephen
Copy link
Member Author

@coderabbitai review

Copy link

coderabbitai bot commented Aug 19, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0f79872 and 99ec0dd.

Files selected for processing (34)
  • src/main/java/org/joda/money/BigMoney.java (96 hunks)
  • src/main/java/org/joda/money/BigMoneyProvider.java (1 hunks)
  • src/main/java/org/joda/money/CurrencyMismatchException.java (3 hunks)
  • src/main/java/org/joda/money/CurrencyUnit.java (24 hunks)
  • src/main/java/org/joda/money/CurrencyUnitDataProvider.java (1 hunks)
  • src/main/java/org/joda/money/DefaultCurrencyUnitDataProvider.java (6 hunks)
  • src/main/java/org/joda/money/IllegalCurrencyException.java (1 hunks)
  • src/main/java/org/joda/money/Money.java (70 hunks)
  • src/main/java/org/joda/money/MoneyUtils.java (13 hunks)
  • src/main/java/org/joda/money/Ser.java (4 hunks)
  • src/main/java/org/joda/money/format/AmountPrinterParser.java (7 hunks)
  • src/main/java/org/joda/money/format/LiteralPrinterParser.java (1 hunks)
  • src/main/java/org/joda/money/format/MoneyAmountStyle.java (28 hunks)
  • src/main/java/org/joda/money/format/MoneyFormatException.java (2 hunks)
  • src/main/java/org/joda/money/format/MoneyFormatter.java (14 hunks)
  • src/main/java/org/joda/money/format/MoneyFormatterBuilder.java (19 hunks)
  • src/main/java/org/joda/money/format/MoneyParseContext.java (22 hunks)
  • src/main/java/org/joda/money/format/MoneyParser.java (1 hunks)
  • src/main/java/org/joda/money/format/MoneyPrintContext.java (3 hunks)
  • src/main/java/org/joda/money/format/MoneyPrinter.java (1 hunks)
  • src/main/java/org/joda/money/format/MultiPrinterParser.java (2 hunks)
  • src/main/java/org/joda/money/format/SignedPrinterParser.java (1 hunks)
  • src/test/java/org/joda/money/TestCurrencyMismatchException.java (4 hunks)
  • src/test/java/org/joda/money/TestCurrencyUnit.java (36 hunks)
  • src/test/java/org/joda/money/TestCurrencyUnitExtension.java (5 hunks)
  • src/test/java/org/joda/money/TestIllegalCurrencyException.java (1 hunks)
  • src/test/java/org/joda/money/TestMoney.java (81 hunks)
  • src/test/java/org/joda/money/TestMoneyUtils_BigMoney.java (2 hunks)
  • src/test/java/org/joda/money/TestStringConvert.java (2 hunks)
  • src/test/java/org/joda/money/format/TestMoneyAmountStyle.java (23 hunks)
  • src/test/java/org/joda/money/format/TestMoneyFormatter.java (15 hunks)
  • src/test/java/org/joda/money/format/TestMoneyFormatterBuilder.java (34 hunks)
  • src/test/java/org/joda/money/format/TestMoneyFormatterException.java (1 hunks)
  • src/test/java/org/joda/money/format/TestMoneyParseContext.java (5 hunks)
Files skipped from review due to trivial changes (29)
  • src/main/java/org/joda/money/BigMoney.java
  • src/main/java/org/joda/money/BigMoneyProvider.java
  • src/main/java/org/joda/money/CurrencyMismatchException.java
  • src/main/java/org/joda/money/CurrencyUnit.java
  • src/main/java/org/joda/money/CurrencyUnitDataProvider.java
  • src/main/java/org/joda/money/DefaultCurrencyUnitDataProvider.java
  • src/main/java/org/joda/money/IllegalCurrencyException.java
  • src/main/java/org/joda/money/Money.java
  • src/main/java/org/joda/money/MoneyUtils.java
  • src/main/java/org/joda/money/format/AmountPrinterParser.java
  • src/main/java/org/joda/money/format/LiteralPrinterParser.java
  • src/main/java/org/joda/money/format/MoneyAmountStyle.java
  • src/main/java/org/joda/money/format/MoneyFormatException.java
  • src/main/java/org/joda/money/format/MoneyFormatter.java
  • src/main/java/org/joda/money/format/MoneyFormatterBuilder.java
  • src/main/java/org/joda/money/format/MoneyParseContext.java
  • src/main/java/org/joda/money/format/MoneyPrintContext.java
  • src/main/java/org/joda/money/format/MoneyPrinter.java
  • src/main/java/org/joda/money/format/MultiPrinterParser.java
  • src/main/java/org/joda/money/format/SignedPrinterParser.java
  • src/test/java/org/joda/money/TestCurrencyMismatchException.java
  • src/test/java/org/joda/money/TestCurrencyUnitExtension.java
  • src/test/java/org/joda/money/TestIllegalCurrencyException.java
  • src/test/java/org/joda/money/TestMoneyUtils_BigMoney.java
  • src/test/java/org/joda/money/TestStringConvert.java
  • src/test/java/org/joda/money/format/TestMoneyAmountStyle.java
  • src/test/java/org/joda/money/format/TestMoneyFormatterBuilder.java
  • src/test/java/org/joda/money/format/TestMoneyFormatterException.java
  • src/test/java/org/joda/money/format/TestMoneyParseContext.java
Additional context used
ast-grep
src/test/java/org/joda/money/format/TestMoneyFormatter.java

[warning] 89-89: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8

src/test/java/org/joda/money/TestCurrencyUnit.java

[warning] 453-453: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8


[warning] 466-466: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8


[warning] 481-481: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8

src/test/java/org/joda/money/TestMoney.java

[warning] 826-826: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8


[warning] 840-840: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8


[warning] 854-854: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8

Additional comments not posted (5)
src/main/java/org/joda/money/format/MoneyParser.java (1)

48-48: Visibility Modifier Clarification.

The change to explicitly declare the parse method as public abstract enhances clarity and aligns with interface design best practices.

src/main/java/org/joda/money/Ser.java (1)

76-88: Modern Java Features Utilised.

The use of switch expressions and var enhances code readability and conciseness. The changes are well-implemented and maintain the original logic.

src/test/java/org/joda/money/format/TestMoneyFormatter.java (1)

62-70: Lambda Expressions and Type Inference.

The use of lambda expressions and var for type inference improves code readability and reduces boilerplate. The changes are appropriate and maintain the test logic.

src/test/java/org/joda/money/TestCurrencyUnit.java (1)

Line range hint 47-61: Type Inference with var.

The use of var for type inference simplifies the code and enhances readability. The changes maintain the original functionality and logic of the tests.

src/test/java/org/joda/money/TestMoney.java (1)

86-94: Type Inference with var.

The use of var for type inference enhances readability and reduces verbosity. The changes preserve the logic and functionality of the tests.

}

private void writeBigMoney(ObjectOutput out, BigMoney obj) throws IOException {
writeCurrency(out, obj.getCurrencyUnit());
byte[] bytes = obj.getAmount().unscaledValue().toByteArray();
var bytes = obj.getAmount().unscaledValue().toByteArray();
Copy link

@coderabbitai coderabbitai bot Aug 19, 2024

Choose a reason for hiding this comment

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

Security Concern: ObjectInputStream Usage.

Using ObjectInputStream can lead to security vulnerabilities such as remote code execution if untrusted data is deserialized. Consider alternatives or ensure that data is trusted and validated.

Copy link
Member Author

Choose a reason for hiding this comment

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

Using ObjectInputStream is the only way to test code that implements Serializable, removing Serializable would be backwards incompatible. "Security concern" is a bit of an over-reaction to this code.

Copy link

Choose a reason for hiding this comment

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

@jodastephen, thank you for your insight. You're correct that ObjectInputStream is essential for testing Serializable code and maintaining backward compatibility. The security concern typically applies to scenarios involving untrusted data. In a controlled environment, where the data is trusted, the risk is indeed minimal. Your approach makes sense given the context.

Comment on lines +85 to +92
var a = iPrintTest;
var baos = new ByteArrayOutputStream();
try (var oos = new ObjectOutputStream(baos)) {
oos.writeObject(a);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
MoneyFormatter input = (MoneyFormatter) ois.readObject();
Money value = MONEY_GBP_12_34;
var ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
var input = (MoneyFormatter) ois.readObject();
var value = MONEY_GBP_12_34;
Copy link

Choose a reason for hiding this comment

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

Security Concern: ObjectInputStream Usage.

Using ObjectInputStream can lead to security vulnerabilities such as remote code execution if untrusted data is deserialized. Consider alternatives or ensure that data is trusted and validated.

Tools
ast-grep

[warning] 89-89: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8

Comment on lines +449 to +455
var cu = CurrencyUnit.of("GBP");
var baos = new ByteArrayOutputStream();
try (var oos = new ObjectOutputStream(baos)) {
oos.writeObject(cu);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
CurrencyUnit input = (CurrencyUnit) ois.readObject();
var ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
var input = (CurrencyUnit) ois.readObject();
Copy link

Choose a reason for hiding this comment

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

Security Concern: ObjectInputStream Usage.

Using ObjectInputStream can lead to security vulnerabilities such as remote code execution if untrusted data is deserialized. Consider alternatives or ensure that data is trusted and validated.

Tools
ast-grep

[warning] 453-453: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8

Comment on lines +822 to +828
var a = GBP_2_34;
var baos = new ByteArrayOutputStream();
try (var oos = new ObjectOutputStream(baos)) {
oos.writeObject(a);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
Money input = (Money) ois.readObject();
var ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
var input = (Money) ois.readObject();
Copy link

Choose a reason for hiding this comment

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

Security Concern: ObjectInputStream Usage.

Using ObjectInputStream can lead to security vulnerabilities such as remote code execution if untrusted data is deserialized. Consider alternatives or ensure that data is trusted and validated.

Tools
ast-grep

[warning] 826-826: Avoid using ObjectInputStream, it is insecure and can lead to remote code execution
Context: new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
Note: [CWE-502]: Deserialization of Untrusted Data [OWASP A08:2017]: Insecure Deserialization [OWASP A08:2021]: Software and Data Integrity Failures [REFERENCES]
- https://www.owasp.org/index.php/Deserialization_of_untrusted_data
- https://www.oracle.com/java/technologies/javase/seccodeguide.html#8

@jodastephen jodastephen merged commit c7ba6ef into v2.x Aug 19, 2024
3 checks passed
@jodastephen jodastephen deleted the modernize branch August 19, 2024 21:52
jodastephen added a commit that referenced this pull request Aug 25, 2024
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