Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
codeql/java/ql/src/DeadCode/DeadClass.qhelp at codeql-cli/v2.26.2 · github/codeql · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
github
/
codeql
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Issues
998
Pull requests
460
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Files
Expand file tree
codeql-cli/v2.26.2
Breadcrumbs
codeql
/
java
/
ql
/
src
/
DeadCode
/
DeadClass.qhelp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
99 lines (98 loc) · 4.57 KB
codeql-cli/v2.26.2
Breadcrumbs
codeql
/
java
/
ql
/
src
/
DeadCode
/
DeadClass.qhelp
Copy path
Top
File metadata and controls
Code
Blame
99 lines (98 loc) · 4.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!
DOCTYPE
qhelp
PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<
qhelp
>
<
overview
>
<
p
>
Classes that are never used at runtime are redundant and should be removed.
</
p
>
<
include
src
=
"
DeadCodeSummary.inc.qhelp
"
/>
<
p
>
Classes are considered dead if at runtime:
</
p
>
<
ul
>
<
li
>No methods declared in the class, or a sub-type, are called.</
li
>
<
li
>No fields declared in the class, or a sub-type, are read.</
li
>
<
li
>The class is never constructed.</
li
>
</
ul
>
<
p
>Any class which is not dead is considered to be "live". Nested classes are considered and listed
separately, as a live nested class within a dead outer class can be moved to a separate file,
allowing the outer class to be deleted.
</
p
>
<
p
>
A special exception is made for "namespace classes". A namespace class is used only to group static
fields, methods and nested classes - it is never instantiated, has no public constructor and has no
instance methods. If a class is considered to be a namespace class, then it is live if at least one
of the static members of that class is live - including static nested classes.
</
p
>
<
include
src
=
"
DeadCodeDetails.inc.qhelp
"
/>
</
overview
>
<
recommendation
>
<
p
>
Before making any changes, confirm that the class is not required by verifying that the only
dependencies on the class are from other dead classes and methods. This confirmation is necessary
because there may be project-specific frameworks or techniques which can introduce hidden
dependencies. If this project is for a library, then consider whether the class is part of the
external API, and may be used in external projects that are not included in the snapshot.
</
p
>
<
p
>
After confirming that the class is not required, remove the class. You will also need to remove any
references to this class, which may, in turn, require removing other unused classes, methods
and fields (see Example 1).
</
p
>
<
p
>
Nested classes within this type should be moved, either to a new top-level type, or to another
type, unless they are also marked as dead, in which case they can also be removed. Alternatively,
if there are some live nested classes within the dead class, the class can be retained by
converting all live nested classes to static members, and removing all instance methods and fields,
and all dead static members (see Example 2).
</
p
>
<
include
src
=
"
DeadCodeExtraEntryPoints.inc.qhelp
"
/>
</
recommendation
>
<
section
title
=
"
Example 1
"
>
<
p
>
In the following example, we have a number of classes, and an "entry point" in the form of a main
method.
</
p
>
<
sample
src
=
"
DeadClass.java
"
/>
<
p
>
The class <
code
>Customer</
code
> is constructed in the main method, and is therefore live. The
class <
code
>Address</
code
> is constructed in <
code
>setAddress</
code
>, so we might think that it
would also be live. However, <
code
>setAddress</
code
> is never called by the main method, so,
assuming that this is the entire program, an <
code
>Address</
code
> is never constructed at runtime.
Therefore, the <
code
>Address</
code
> class is dead and can be removed without changing the meaning
of this program. To delete the <
code
>Address</
code
> class we will also need to delete the
<
code
>setAddress</
code
> and <
code
>getAddress</
code
> methods, and the <
code
>address</
code
> field,
otherwise the program will not compile.
</
p
>
</
section
>
<
section
title
=
"
Example 2
"
>
<
p
>
In the next example, we have a <
code
>CustomerActions</
code
> class containing <
code
>Action</
code
>s
that affect customers. For example, this could be a Java Swing application, and the
<
code
>Action</
code
>s could be actions that are available in the user interface.
</
p
>
<
sample
src
=
"
NamespaceClass.java
"
/>
<
p
>
The <
code
>CustomerActions</
code
> class has a constructor and an instance method, which are never
called. Instead, actions are instantiated directly. Although this makes the nested
<
code
>Action</
code
> classes live, live nested classes do not make the outer class live. Therefore,
the <
code
>CustomerActions</
code
> class is marked as dead.
</
p
>
<
p
>
There are two ways to resolve the dead <
code
>CustomerActions</
code
> class:
</
p
>
<
ul
>
<
li
>Move each nested static action that is used by the program to a new file, or nest it within
a different class, then delete the dead <
code
>CustomerActions</
code
> class.</
li
>
<
li
>Convert the <
code
>CustomerActions</
code
> class to a <
em
>namespace class</
em
>. First convert the
constructor to a <
em
>suppressed constructor</
em
> by making it private, preventing the class from
being instantiated, then remove the instance method <
code
>createAddCustomerAction</
code
>.</
li
>
</
ul
>
<
p
>
Taking the second approach, this is the final result.
</
p
>
<
sample
src
=
"
NamespaceClass2.java
"
/>
</
section
>
<
include
src
=
"
DeadCodeReferences.inc.qhelp
"
/>
</
qhelp
>
You can’t perform that action at this time.