Skip to content

Conversation

@bhaskar-allam
Copy link

refactor: Modernize ChaincodeBase class

  • Utilize Java 8+ features like lambda expressions and Optional
  • Implement try-with-resources for better resource management
  • Improve logging with concise lambda expressions
  • Use switch expressions for cleaner level mapping
  • Enhance environment variable processing
  • Implement text blocks for multi-line string formatting
  • Streamline StreamObserver implementations
  • General code cleanup and organization improvements

Signed-off-by: Bhaskar Allam bhaskar.ram@linux.com

Signed-off-by: Bhaskar Ram <bhaskar.ram@linux.com>
@bhaskar-allam bhaskar-allam requested a review from a team as a code owner October 21, 2024 13:18
Copy link
Member

@bestbeforetoday bestbeforetoday left a comment

Choose a reason for hiding this comment

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

Several of your changes use language features not available in Java 8. Did you try to compile this code using the Gradle configuration, or to run unit tests?

Comment on lines 297 to 301
Optional.ofNullable(this.getClass().getPackage())
.ifPresentOrElse(
pkg -> Logger.getLogger(pkg.getName()).setLevel(chaincodeLogLevel),
() -> Logger.getLogger("").setLevel(chaincodeLogLevel)
);
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any real benefit to using an optional to perform an if/else, and I don't think this is available in Java 8

Copy link
Author

@bhaskar-allam bhaskar-allam Oct 22, 2024

Choose a reason for hiding this comment

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

Replaced the Optional usage with a simple null check.
Uses a standard if/else statement, which is more readable and familiar to most Java developers. Now in the latest commit It Is compatible with Java 8 and earlier versions.

Comment on lines 310 to 317
return switch (level.toUpperCase().trim()) {
case "CRITICAL", "ERROR" -> Level.SEVERE;
case "WARNING", "WARN" -> Level.WARNING;
case "INFO" -> Level.INFO;
case "NOTICE" -> Level.CONFIG;
case "DEBUG" -> Level.FINEST;
default -> Level.INFO;
};
Copy link
Member

Choose a reason for hiding this comment

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

This looks reasonable but the project uses the Java 8 language level, which does not support switch expressions

Copy link
Author

Choose a reason for hiding this comment

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

Updated in latest commit.

Comment on lines 418 to 427
Optional.ofNullable(System.getenv(CORE_PEER_ADDRESS))
.ifPresent(address -> {
String[] hostArr = address.split(":");
if (hostArr.length == 2) {
this.port = Integer.parseInt(hostArr[1].trim());
this.host = hostArr[0].trim();
} else {
LOGGER.severe(() -> String.format("peer address argument should be in host:port format, ignoring current %s", address));
}
});
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any real benefit to using an optional only to perform a null check

Copy link
Author

Choose a reason for hiding this comment

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

Replaced the Optional.ofNullable().ifPresent() with a simple null check using an if statement. Improved readability by using a more straightforward and familiar Java construct.

Comment on lines 441 to 454
LOGGER.info(() -> String.format("""
<<<<<<<<<<<<<Environment options>>>>>>>>>>>>
CORE_CHAINCODE_ID_NAME: %s
CORE_PEER_ADDRESS: %s
CORE_PEER_TLS_ENABLED: %s
CORE_PEER_TLS_ROOTCERT_FILE: %s
CORE_TLS_CLIENT_KEY_PATH: %s
CORE_TLS_CLIENT_CERT_PATH: %s
CORE_TLS_CLIENT_KEY_FILE: %s
CORE_TLS_CLIENT_CERT_FILE: %s
CORE_PEER_LOCALMSPID: %s
CHAINCODE_SERVER_ADDRESS: %s
LOGLEVEL: %s
""",
Copy link
Member

Choose a reason for hiding this comment

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

Text blocks are not available in Java 8

Copy link
Author

Choose a reason for hiding this comment

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

Latest Commit is now fully compatible with Java 8.

Signed-off-by: Bhaskar Ram <bhaskar.ram@linux.com>
@sonarqubecloud
Copy link

@bhaskar-allam
Copy link
Author

@bestbeforetoday I've added recent changes for compatibility for Java 8, please review it.

@bestbeforetoday
Copy link
Member

The build is failing since the code does not even compile.

Looking at the current set of changes (after the use of language features not available in Java 8 are reverted), the vast majority of the changes are reformatting of the code. A couple of variables are renamed and, in a couple of cases, the logic flow of the code is slightly reordered. What issue is this change addressing?

@bestbeforetoday
Copy link
Member

No response so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants