Task/bugfix 2578#3270
Conversation
Adding a failing test to demonstrate the problem
Closes testng-team#2578 Fixing the constructor invocation to ensure that instantiation problems bubble up.
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
testng-core-api/src/main/java/org/testng/internal/objects/InstanceCreator.java (1)
28-37: Redundant catch clauses can be merged.The
IllegalAccessException | NoSuchMethodException | SecurityExceptionbranch (lines 32–33) and theThrowablebranch (lines 34–36) build the exact sameTestNGExceptionwith the same message and cause, so the first clause is fully subsumed by the second. Collapsing them removes duplication without changing behavior.♻️ Proposed simplification
public static <T> T newInstance(Class<T> clazz) { try { MethodHandle constructor = MethodHandles.lookup().findConstructor(clazz, MethodType.methodType(void.class)); return (T) constructor.invoke(); - } catch (IllegalAccessException | NoSuchMethodException | SecurityException e) { - throw new TestNGException(CANNOT_INSTANTIATE_CLASS + clazz.getName(), e); } catch (Throwable e) { throw new TestNGException(CANNOT_INSTANTIATE_CLASS + clazz.getName(), e); } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@testng-core-api/src/main/java/org/testng/internal/objects/InstanceCreator.java` around lines 28 - 37, The two catch blocks that both throw new TestNGException(CANNOT_INSTANTIATE_CLASS + clazz.getName(), e) are redundant; merge them into a single catch that catches Throwable (or the narrower common supertype you prefer) and rethrows the TestNGException, keeping the existing message and cause so behavior is unchanged—update the try/catch around MethodHandles.lookup().findConstructor(clazz, MethodType.methodType(void.class)) / constructor.invoke() to use a single catch referencing the same TestNGException and CANNOT_INSTANTIATE_CLASS + clazz.getName().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@testng-core-api/src/main/java/org/testng/internal/objects/InstanceCreator.java`:
- Around line 28-37: The two catch blocks that both throw new
TestNGException(CANNOT_INSTANTIATE_CLASS + clazz.getName(), e) are redundant;
merge them into a single catch that catches Throwable (or the narrower common
supertype you prefer) and rethrows the TestNGException, keeping the existing
message and cause so behavior is unchanged—update the try/catch around
MethodHandles.lookup().findConstructor(clazz, MethodType.methodType(void.class))
/ constructor.invoke() to use a single catch referencing the same
TestNGException and CANNOT_INSTANTIATE_CLASS + clazz.getName().
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 16096982-4580-422a-b89a-34a861fb15ed
📒 Files selected for processing (6)
testng-core-api/src/main/java/org/testng/internal/objects/InstanceCreator.javatestng-core/src/test/java/test/listeners/ListenersTest.javatestng-core/src/test/java/test/listeners/issue2578/ListenerWithMissingConstructorDependency.javatestng-core/src/test/java/test/listeners/issue2578/MissingConstructorDependency.javatestng-core/src/test/java/test/listeners/issue2578/MissingDependencyClassLoader.javatestng-core/src/test/java/test/listeners/issue2578/TestClassSample.java
Fixes #2578.
Did you remember to?
CHANGES.txt./gradlew autostyleApplyWe encourage pull requests that:
If your pull request involves fixing SonarQube issues then we would suggest that you please discuss this with the
TestNG-dev before you spend time working on it.
Note: For more information on contribution guidelines please make sure you refer our Contributing section for detailed set of steps.
Summary by CodeRabbit
Bug Fixes
Tests