-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Java: Add new quality query to detect empty methods #18947
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
tamasvajk
merged 24 commits into
github:main
from
tamasvajk:tamasvajk/java_empty_method
Mar 26, 2025
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f7f8b47
Java: Add initial version of empty method query
tamasvajk a8063e1
Adjust query name
tamasvajk 614bee9
Use inline test expectations
tamasvajk 4bf26af
Add more test cases
tamasvajk 349f489
Make query more accepting
tamasvajk 7476f19
Adjust query help
tamasvajk 6512ed9
Adjust alert message
tamasvajk 3d2a723
Improve ql code quality
tamasvajk 7740077
Add change note
tamasvajk 3d4fcef
Do not accept empty `default` methods
tamasvajk 3be7044
Fix references in query help file
tamasvajk dea081b
Add quality and cwe tag
tamasvajk 17aa3fc
Add compliant/non-compliant comments back to the test file
tamasvajk 24f129c
Fix typo in QL help
tamasvajk 050ef40
Improve query help
tamasvajk 2538ba8
Revert message
tamasvajk 30ff68d
Update java/ql/src/Language Abuse/EmptyMethod.md
tamasvajk 05502bc
Change severity and precision
tamasvajk 9662b47
Move likely test method logic to library
tamasvajk d4955a0
Fix failing test and add new test case
tamasvajk 246c827
Update java/ql/lib/semmle/code/java/UnitTests.qll
tamasvajk 9bdec21
Clean test files and add new test cases
tamasvajk 1f76793
Merge branch 'main' into tamasvajk/java_empty_method
mchammer01 a5fd2e9
Improve query documentation
tamasvajk 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| ## Overview | ||
|
|
||
| An empty method may indicate that an implementation was intended to be provided but was accidentally omitted. When using the method, it will not be clear that it does not provide an implementation and with dynamic dispatch, resolving to a blank method may result in unexpected program behavior. | ||
|
|
||
| ## Recommendation | ||
|
|
||
| If a method is intended to be left empty, do one of the following to indicate that it is intentionally empty: | ||
| 1. Mark it abstract in an abstract class | ||
| 2. Place it in an interface (then it can be implicitly abstract) | ||
| 3. Place a comment in that method that lets others know that the implementation was intentionally omitted | ||
| 4. Add `UnsupportedOperationException` to the method (as in `java.util.Collection.add`). | ||
|
|
||
| ## Example | ||
|
|
||
| ```java | ||
| public class Test { | ||
| public void f1() { // COMPLIANT | ||
| // intentionally empty | ||
| } | ||
|
|
||
| public void f2() {} // NON_COMPLIANT | ||
|
|
||
| public void f3(){ throw new UnsupportedOperationException(); } // COMPLIANT | ||
|
|
||
| public abstract class TestInner { | ||
|
|
||
| public abstract void f(); // COMPLIANT - intentionally empty | ||
| } | ||
|
|
||
| } | ||
| ``` | ||
|
|
||
| ## Implementation Notes | ||
|
|
||
| The rule excludes reporting methods that are annotated. | ||
|
|
||
| ## References | ||
| - Java SE Documentation: [java.util.Collection.add](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/Collection.html#add(E)). | ||
| - Wikipedia: [Template method pattern](https://en.wikipedia.org/wiki/Template_method_pattern). | ||
| - Common Weakness Enumeration: [CWE-1071](https://cwe.mitre.org/data/definitions/1071.html). |
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,41 @@ | ||
| /** | ||
| * @id java/empty-method | ||
| * @name Empty method | ||
| * @description An empty method serves no purpose and makes code less readable. An empty method may | ||
| * indicate an error on the part of the developer. | ||
| * @kind problem | ||
| * @precision medium | ||
| * @problem.severity recommendation | ||
| * @tags correctness | ||
| * maintainability | ||
| * readability | ||
| * quality | ||
| * external/cwe/cwe-1071 | ||
| */ | ||
|
|
||
| import java | ||
|
|
||
| /** | ||
| * A `Method` from source that is not abstract, and likely not a test method | ||
| */ | ||
| class NonAbstractSource extends Method { | ||
| NonAbstractSource() { | ||
| this.fromSource() and | ||
| not this.isAbstract() and | ||
| not this instanceof LikelyTestMethod | ||
| } | ||
| } | ||
|
|
||
| from NonAbstractSource m | ||
| where | ||
| //empty | ||
| not exists(m.getBody().getAChild()) and | ||
| //permit comment lines explaining why this is empty | ||
| m.getNumberOfCommentLines() = 0 and | ||
| //permit a javadoc above as well as sufficient reason to leave empty | ||
| not exists(m.getDoc().getJavadoc()) and | ||
| //annotated methods are considered compliant | ||
| not exists(m.getAnAnnotation()) and | ||
tamasvajk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| //native methods have no body | ||
| not m.isNative() | ||
| select m, "Empty method found." | ||
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,4 @@ | ||
| --- | ||
| category: newQuery | ||
| --- | ||
| * Added a new quality query, `java/empty-method`, to detect empty methods. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import org.aspectj.lang.annotation.Pointcut; | ||
|
|
||
| public class Class1 { | ||
|
|
||
| // COMPLIANT | ||
| public void f() { | ||
| int i = 0; | ||
| } | ||
|
|
||
| // COMPLIANT | ||
| public void f1() { | ||
| // intentionally empty | ||
| } | ||
|
|
||
| // NON_COMPLIANT | ||
| public void f2() { } // $ Alert | ||
|
|
||
| // COMPLIANT - exception | ||
| @Pointcut() | ||
| public void f4() { | ||
| } | ||
|
|
||
| /** | ||
| * COMPLIANT - empty method with javadoc | ||
| */ | ||
| public void f5() { | ||
| } | ||
|
|
||
| public abstract class TestInner { | ||
|
|
||
| public abstract void f(); // COMPLIANT - intentionally empty | ||
|
|
||
| } | ||
|
|
||
| public class Derived extends TestInner { | ||
|
|
||
| // COMPLIANT: with annotation | ||
| @Override | ||
| public void f() { | ||
| } | ||
|
|
||
| // COMPLIANT: native | ||
| public native int nativeMethod(); | ||
| } | ||
|
|
||
| public interface TestInterface { | ||
|
|
||
| // NON_COMPLIANT | ||
| default void method() { } // $ Alert | ||
|
|
||
| void method2(); // COMPLIANT | ||
| } | ||
|
|
||
| } | ||
tamasvajk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,2 @@ | ||
| | Class1.java:16:15:16:16 | f2 | Empty method found. | | ||
| | Class1.java:49:18:49:23 | method | Empty method found. | |
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,2 @@ | ||
| query: Language Abuse/EmptyMethod.ql | ||
| postprocess: utils/test/InlineExpectationsTestQuery.ql |
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,5 @@ | ||
| public class Test { | ||
| // COMPLIANT: allow empty method in test class | ||
| public void f() { | ||
tamasvajk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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 @@ | ||
| //semmle-extractor-options: --javac-args -cp ${testdir}/../../stubs/aspectj |
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.