Skip to content

Issue #583: drop reflection from XChartPanelSaveTargetTest#1010

Merged
timmolter merged 1 commit into
developfrom
issue-583-drop-test-reflection
Jul 20, 2026
Merged

Issue #583: drop reflection from XChartPanelSaveTargetTest#1010
timmolter merged 1 commit into
developfrom
issue-583-drop-test-reflection

Conversation

@timmolter

Copy link
Copy Markdown
Member

Follow-up to the #583 Save As work. No behavior change — test cleanup only.

Problem

XChartPanelSaveTargetTest reached XChartPanel.SuffixSaveFilter reflectively:

Class<?> clazz = Class.forName("org.knowm.xchart.XChartPanel$SuffixSaveFilter");
Constructor<?> ctor = clazz.getDeclaredConstructor(String.class);
ctor.setAccessible(true);

Two things wrong with that:

  • The class name is a string literal, so renaming or moving the class fails at runtime with ClassNotFoundException instead of at compile time.
  • It's inconsistent within the same file. resolveSaveTarget and suffixOf are already package-private statics and the test calls them directly; only the filter was reached reflectively.

The constructor was already public — only the nested class was private, so setAccessible wasn't even the thing unblocking it.

Change

Relax SuffixSaveFilter to package-private and construct it directly:

private static FileFilter filter(String suffix) {
  return new XChartPanel.SuffixSaveFilter(suffix);
}

This matches the established idiom in the codebase — internal/chartpart has ~86 package-private members serving as test seams, and tests there read them directly (e.g. RegressionTestIssue577 asserts on legend.xOffset). SuffixSaveFilter stays invisible to library users; it was already nested inside XChartPanel with no public exposure.

Also drops the now-unneeded throws Exception from the five test methods and the java.lang.reflect.Constructor import.

Verification

Full xchart suite green (146 tests); XChartPanelSaveTargetTest itself runs 5/5. With this and #1009, no test in the tree uses reflection.

🤖 Generated with Claude Code

The test reflected on XChartPanel$SuffixSaveFilter via Class.forName with
a string literal, so a rename would surface as a runtime
ClassNotFoundException rather than a compile error. This was also
inconsistent within the file: resolveSaveTarget and suffixOf are already
package-private and called directly by the same test.

The constructor was already public; only the nested class was private.
Relaxing it to package-private lets the test construct the filter
directly, matching the idiom used elsewhere for test seams.

No behavior change. Full xchart suite green (146 tests).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@timmolter
timmolter merged commit 05bd2f3 into develop Jul 20, 2026
1 check passed
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.

1 participant