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 inject for non singleton class #1617

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

jorg3lopez
Copy link
Contributor

Description

Under construction

Issue

Add a link to the issue here. Consider using
closing keywords
if the this PR isn't for a story (stories will be closed through different means).

Checklist

  • I have added tests to cover my changes
  • I have added logging where useful (with appropriate log level)
  • I have added JavaDocs where required
  • I have updated the documentation accordingly

Note: You may remove items that are not applicable

Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Exception Handling
The method 'injectIntoField' throws a generic IllegalArgumentException for any exception during field injection. This could mask different issues like security or logical errors which might need different handling.

Dependency Injection
The use of 'ApplicationContext.injectIntoNonSingleton(this)' within the constructor might lead to issues with incomplete construction if the injected dependencies themselves depend on the partially constructed object.

try {
field.set(instance, fieldImplementation);
} catch (IllegalAccessException | IllegalArgumentException exception) {
throw new IllegalArgumentException(

Choose a reason for hiding this comment

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

Consider adding specific exception handling for different types of injection failures. This could improve the robustness and security of the application by providing clearer error messages and handling specific cases differently. [important]

private final IBaseResource innerResource;

public HapiFhirResource(IBaseResource innerResource) {
ApplicationContext.injectIntoNonSingleton(this);

Choose a reason for hiding this comment

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

To avoid potential issues with circular dependencies and incomplete construction, consider using a factory or builder pattern to manage object creation and dependency injection, rather than injecting dependencies directly in the constructor. [important]

Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Score
Possible issue
Prevent potential NullPointerException by checking if instance is null before setting a field

Ensure that injectIntoField checks for null instance before attempting to set the
field, to prevent NullPointerException.

shared/src/main/java/gov/hhs/cdc/trustedintermediary/context/ApplicationContext.java [82]

-field.set(instance, fieldImplementation);
+if (instance != null) {
+    field.set(instance, fieldImplementation);
+}
Suggestion importance[1-10]: 8

Why: This suggestion correctly identifies a potential NullPointerException risk in the code and provides a solution to prevent it, enhancing the robustness of the method.

8
Enhance robustness by ensuring the annotation parameter is a valid subclass of Annotation

Modify getFieldsAnnotatedWithInstance to handle the case where the annotation
parameter is not a subclass of Annotation, to avoid a potential ClassCastException.

shared/src/main/java/gov/hhs/cdc/trustedintermediary/context/Reflection.java [36]

-.filter(field -> field.isAnnotationPresent(annotation.asSubclass(Annotation.class)))
+.filter(field -> Annotation.class.isAssignableFrom(annotation) && field.isAnnotationPresent(annotation.asSubclass(Annotation.class)))
Suggestion importance[1-10]: 7

Why: The suggestion accurately addresses a potential ClassCastException by checking if the annotation parameter is a valid subclass of Annotation before casting, thus improving the method's safety.

7
General
Improve stability by handling potential injection failures gracefully

Add error handling for the injectIntoNonSingleton method in the constructor to
manage potential injection failures gracefully.

shared/src/main/java/gov/hhs/cdc/trustedintermediary/external/hapi/HapiFhirResource.java [16]

-ApplicationContext.injectIntoNonSingleton(this);
+try {
+    ApplicationContext.injectIntoNonSingleton(this);
+} catch (Exception e) {
+    logger.error("Injection failed", e);
+}
Suggestion importance[1-10]: 6

Why: This suggestion proposes adding error handling around the injection process to manage failures more gracefully, which is a good practice for improving the reliability of the application.

6
Provide clearer diagnostics by handling null implementations more informatively

Add logging or throw a more specific exception when fieldImplementation is null, to
provide clearer diagnostics when injection fails.

shared/src/main/java/gov/hhs/cdc/trustedintermediary/context/ApplicationContext.java [76-77]

 if (fieldImplementation == null) {
-    return;
+    throw new IllegalStateException("No implementation found for " + fieldType);
 }
Suggestion importance[1-10]: 5

Why: The suggestion to throw a more specific exception when an implementation is not found is beneficial for debugging and understanding injection failures, although it could be improved by also including logging before throwing the exception.

5

Copy link

sonarcloud bot commented Nov 26, 2024

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.

2 participants