Repository navigation
feat: report unused Java private members #165
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
Merged
devill
merged 1 commit into
habit-hooks:main
from
renanfranca:164-java-unused-private-members
Sep 17, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
plugins/generic/src/habit_hooks_generic/guides/unused-class-member.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| An unused field, method, or property is dead surface that makes a type appear to | ||
| have responsibilities and collaborations it does not exercise. It asks readers | ||
| to understand and maintain behavior that may not exist. | ||
|
|
||
| Investigate every occurrence before changing it. Static analysis may not see | ||
| reflection, annotations or decorators, serialization, framework lifecycle | ||
| hooks, dependency injection, generated callers, or other dynamic uses. When one | ||
| of those mechanisms owns the call, preserve the member and make that use legible | ||
| to the detector or future readers. | ||
|
|
||
| Once those hidden uses are ruled out, choose the design that makes the type | ||
| honest: connect an intended caller, extract a cohesive group of members into a | ||
| real unit, or remove a member confirmed to be dead together with code that | ||
| exists only to support it. Do not remove a member automatically from this | ||
| report alone. | ||
|
|
||
| The unused members: | ||
|
|
||
| {% include "includes/line_level_issues.md" %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1798
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
plugins/java/tests/test_unused_private_members_follow_pmd.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| """Unused private-member detection remains PMD's decision.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| SENSOR = ( | ||
| Path(__file__).resolve().parents[1] / "src/habit_hooks_java/sensors/pmd_sensor.py" | ||
| ) | ||
| RULESET_HEADER = """<?xml version="1.0"?> | ||
| <ruleset name="custom" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd"> | ||
| <description>project policy</description> | ||
| """ | ||
|
|
||
|
|
||
| def _run(project: Path, pmd: str, *arguments: str) -> list[dict]: | ||
| result = subprocess.run( | ||
| [sys.executable, str(SENSOR), pmd, *arguments, "--", "Members.java"], | ||
| cwd=project, | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| errors="replace", | ||
| ) | ||
| assert result.returncode == 0, result.stderr | ||
| return json.loads(result.stdout) | ||
|
|
||
|
|
||
| def test_the_bundled_rules_report_only_unreferenced_private_members( | ||
| tmp_path: Path, pmd: str | ||
| ) -> None: | ||
| (tmp_path / "Members.java").write_text( | ||
| "class Members {\n" | ||
| " private int staleField = 1;\n" | ||
| " private int liveField = 2;\n" | ||
| " private void staleMethod() {}\n" | ||
| " private int liveMethod() { return liveField; }\n" | ||
| " int value() { return liveMethod(); }\n" | ||
| "}\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| findings = _run(tmp_path, pmd) | ||
|
|
||
| assert [finding["smell"] for finding in findings] == ["unused-class-member"] | ||
| issues = findings[0]["issues"] | ||
| assert [issue["details"]["line"] for issue in issues] == [2, 4] | ||
| assert [issue["details"]["message"] for issue in issues] == [ | ||
| "Avoid unused private fields such as 'staleField'.", | ||
| "Avoid unused private methods such as 'staleMethod()'.", | ||
| ] | ||
| assert [issue["details"]["source"] for issue in issues] == [ | ||
| "pmd:UnusedPrivateField", | ||
| "pmd:UnusedPrivateMethod", | ||
| ] | ||
| assert [issue["details"]["content"] for issue in issues] == [ | ||
| "private int staleField = 1;", | ||
| "private void staleMethod() {}", | ||
| ] | ||
| assert all(Path(issue["key"]).name == "Members.java" for issue in issues) | ||
| assert all("name" not in issue["details"] for issue in issues) | ||
|
|
||
|
|
||
| def test_the_bundled_rules_leave_framework_and_serialization_exclusions_to_pmd( | ||
| tmp_path: Path, pmd: str | ||
| ) -> None: | ||
| (tmp_path / "Members.java").write_text( | ||
| "@interface Managed {}\n" | ||
| "class Members implements java.io.Serializable {\n" | ||
| " private static final long serialVersionUID = 1L;\n" | ||
| " @Managed private Object injected;\n" | ||
| " private Object staleField;\n" | ||
| " private void readObject(java.io.ObjectInputStream stream)\n" | ||
| " throws java.io.IOException, ClassNotFoundException {\n" | ||
| " stream.defaultReadObject();\n" | ||
| " }\n" | ||
| " private void staleMethod() {}\n" | ||
| "}\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| findings = _run(tmp_path, pmd) | ||
|
|
||
| assert [finding["smell"] for finding in findings] == ["unused-class-member"] | ||
| issues = findings[0]["issues"] | ||
| assert [issue["details"]["line"] for issue in issues] == [5, 10] | ||
| assert [issue["details"]["source"] for issue in issues] == [ | ||
| "pmd:UnusedPrivateField", | ||
| "pmd:UnusedPrivateMethod", | ||
| ] | ||
| messages = [issue["details"]["message"] for issue in issues] | ||
| assert all("serialVersionUID" not in message for message in messages) | ||
| assert all("injected" not in message for message in messages) | ||
| assert all("readObject" not in message for message in messages) | ||
|
|
||
|
|
||
| def test_a_project_ruleset_decides_whether_unused_members_are_enabled( | ||
| tmp_path: Path, pmd: str | ||
| ) -> None: | ||
| (tmp_path / "Members.java").write_text( | ||
| "class Members {\n" | ||
| " private int staleField;\n" | ||
| " private void staleMethod() {}\n" | ||
| "}\n", | ||
| encoding="utf-8", | ||
| ) | ||
| ruleset = tmp_path / "ruleset.xml" | ||
| ruleset.write_text( | ||
| RULESET_HEADER | ||
| + ' <rule ref="category/java/design.xml/AvoidDeeplyNestedIfStmts"/>\n' | ||
| + "</ruleset>\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| omitted = _run(tmp_path, pmd) | ||
|
|
||
| assert omitted == [] | ||
|
|
||
| ruleset.write_text( | ||
| RULESET_HEADER | ||
| + ' <rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>\n' | ||
| + ' <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod"/>\n' | ||
| + "</ruleset>\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| enabled = _run(tmp_path, pmd) | ||
|
|
||
| assert [issue["details"]["source"] for issue in enabled[0]["issues"]] == [ | ||
| "pmd:UnusedPrivateField", | ||
| "pmd:UnusedPrivateMethod", | ||
| ] | ||
|
|
||
|
|
||
| def test_a_project_ruleset_can_customize_pmds_ignored_field_names( | ||
| tmp_path: Path, pmd: str | ||
| ) -> None: | ||
| (tmp_path / "Members.java").write_text( | ||
| "class Members {\n" | ||
| " private int retainedByPolicy;\n" | ||
| " private int staleField;\n" | ||
| "}\n", | ||
| encoding="utf-8", | ||
| ) | ||
| (tmp_path / "ruleset.xml").write_text( | ||
| RULESET_HEADER | ||
| + ' <rule ref="category/java/bestpractices.xml/UnusedPrivateField">\n' | ||
| + " <properties>\n" | ||
| + ' <property name="ignoredFieldNames" value="retainedByPolicy"/>\n' | ||
| + " </properties>\n" | ||
| + " </rule>\n" | ||
| + "</ruleset>\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| findings = _run(tmp_path, pmd) | ||
|
|
||
| issues = findings[0]["issues"] | ||
| assert [issue["details"]["line"] for issue in issues] == [3] | ||
| assert issues[0]["details"]["message"] == ( | ||
| "Avoid unused private fields such as 'staleField'." | ||
| ) |
11 changes: 0 additions & 11 deletions
11
plugins/typescript/src/habit_hooks_typescript/guides/unused-class-member.md
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.