Skip to content

Commit 62ef811

Browse files
committed
Java: Autoformat.
1 parent 766b07b commit 62ef811

File tree

8 files changed

+165
-156
lines changed

8 files changed

+165
-156
lines changed

java/ql/src/Architecture/Dependencies/UnusedMavenDependencyBinary.ql

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,24 @@ predicate whitelist(Dependency d) {
2020

2121
from PomDependency d, Pom source
2222
where
23-
source.getADependency() = d and
24-
// There is not a Pom file for the target of this dependency, so we assume that it was resolved by
25-
// a binary file in the local maven repository.
26-
not exists(Pom target | target = d.getPom()) and
27-
// In order to accurately identify whether this binary dependency is required, we must have identified
28-
// a Maven repository. If we have not found a repository, it's likely that it has a custom path of
29-
// which we are unaware, so do not report any problems.
30-
exists(MavenRepo mr) and
31-
// We either haven't indexed a relevant jar file, which suggests that nothing statically depended upon
32-
// it, or we have indexed the relevant jar file, but no source code in the project defined by the pom
33-
// depends on any code within the detected jar.
34-
not pomDependsOnContainer(source, d.getJar()) and
35-
// If something that depends on us depends on the jar represented by this dependency, and it doesn't
36-
// depend directly on the jar itself, we don't consider it to be "unused".
37-
not exists(Pom pomThatDependsOnSource |
38-
pomThatDependsOnSource.getAnExportedPom+() = source
39-
|
40-
pomDependsOnContainer(pomThatDependsOnSource, d.getJar()) and
41-
not exists(File f | f = pomThatDependsOnSource.getADependency().getJar() and f = d.getJar())) and
42-
// Filter out those dependencies on the whitelist
43-
not whitelist(d)
23+
source.getADependency() = d and
24+
// There is not a Pom file for the target of this dependency, so we assume that it was resolved by
25+
// a binary file in the local maven repository.
26+
not exists(Pom target | target = d.getPom()) and
27+
// In order to accurately identify whether this binary dependency is required, we must have identified
28+
// a Maven repository. If we have not found a repository, it's likely that it has a custom path of
29+
// which we are unaware, so do not report any problems.
30+
exists(MavenRepo mr) and
31+
// We either haven't indexed a relevant jar file, which suggests that nothing statically depended upon
32+
// it, or we have indexed the relevant jar file, but no source code in the project defined by the pom
33+
// depends on any code within the detected jar.
34+
not pomDependsOnContainer(source, d.getJar()) and
35+
// If something that depends on us depends on the jar represented by this dependency, and it doesn't
36+
// depend directly on the jar itself, we don't consider it to be "unused".
37+
not exists(Pom pomThatDependsOnSource | pomThatDependsOnSource.getAnExportedPom+() = source |
38+
pomDependsOnContainer(pomThatDependsOnSource, d.getJar()) and
39+
not exists(File f | f = pomThatDependsOnSource.getADependency().getJar() and f = d.getJar())
40+
) and
41+
// Filter out those dependencies on the whitelist
42+
not whitelist(d)
4443
select d, "Maven dependency on the binary package " + d.getShortCoordinate() + " is unused."
45-

java/ql/src/Architecture/Dependencies/UnusedMavenDependencySource.ql

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ import UnusedMavenDependencies
1313

1414
from PomDependency d, Pom source, Pom target
1515
where
16-
source.getADependency() = d and
17-
// We have a targetPom file, so this is a "source" dependency, rather than a binary dependency
18-
// from the Maven repository. Note, although .pom files exist in the local maven repository, they
19-
// are usually not indexed because they are outside the source directory. We assume that they have
20-
// not been indexed.
21-
target = d.getPom() and
22-
// If we have a pom for the target of this dependency, then it is unused iff neither it, nor any
23-
// of its transitive dependencies are required.
24-
not exists(Pom exported |
25-
exported = target.getAnExportedPom*()
26-
|
27-
pomDependsOnContainer(source, exported.getAnExportedDependency().getJar()) or
28-
pomDependsOnPom(source, exported)
29-
)
16+
source.getADependency() = d and
17+
// We have a targetPom file, so this is a "source" dependency, rather than a binary dependency
18+
// from the Maven repository. Note, although .pom files exist in the local maven repository, they
19+
// are usually not indexed because they are outside the source directory. We assume that they have
20+
// not been indexed.
21+
target = d.getPom() and
22+
// If we have a pom for the target of this dependency, then it is unused iff neither it, nor any
23+
// of its transitive dependencies are required.
24+
not exists(Pom exported | exported = target.getAnExportedPom*() |
25+
pomDependsOnContainer(source, exported.getAnExportedDependency().getJar()) or
26+
pomDependsOnPom(source, exported)
27+
)
3028
select d, "Maven dependency onto " + d.getShortCoordinate() + " is unused."

java/ql/src/DeadCode/DeadClass.ql

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@
99
* useless-code
1010
* external/cwe/cwe-561
1111
*/
12+
1213
import semmle.code.java.deadcode.DeadCode
1314

1415
from DeadClass c, Element origin, string reason
1516
where
16-
if exists(DeadRoot root | root = c.getADeadRoot() | not root = c.getACallable()) then (
17-
// Report a list of the dead roots.
18-
origin = c.getADeadRoot() and
19-
not origin = c.getACallable() and
20-
// There are uses of this class from outside the class.
21-
reason = " is only used from dead code originating at $@."
17+
if exists(DeadRoot root | root = c.getADeadRoot() | not root = c.getACallable())
18+
then (
19+
// Report a list of the dead roots.
20+
origin = c.getADeadRoot() and
21+
not origin = c.getACallable() and
22+
// There are uses of this class from outside the class.
23+
reason = " is only used from dead code originating at $@."
2224
) else (
2325
// There are no dead roots outside this class.
2426
origin = c and
25-
if c.isUnusedOutsideClass() then
27+
if c.isUnusedOutsideClass()
28+
then
2629
// Never accessed outside this class, so it's entirely unused.
2730
reason = " is entirely unused."
2831
else

java/ql/src/DeadCode/FLinesOfDeadCode.ql

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,50 @@
99
* @tags maintainability
1010
* external/cwe/cwe-561
1111
*/
12+
1213
import java
1314
import semmle.code.java.deadcode.DeadCode
1415

1516
from File f, int n
1617
where
1718
n =
1819
// Lines of code contributed by dead classes.
19-
sum(DeadClass deadClass | deadClass.getFile() = f |
20-
deadClass.getNumberOfLinesOfCode() -
21-
// Remove inner and local classes, as they are reported as separate dead classes. Do not
22-
// remove anonymous classes, because they aren't reported separately.
23-
sum(NestedClass innerClass | innerClass.getEnclosingType() = deadClass and not innerClass.isAnonymous() |
24-
innerClass.getNumberOfLinesOfCode()
25-
)
26-
) +
27-
// Lines of code contributed by dead methods, not in dead classes.
28-
sum(DeadMethod deadMethod | deadMethod.getFile() = f and not deadMethod.isInDeadScope() |
29-
deadMethod.getNumberOfLinesOfCode() -
30-
// Remove local classes defined in the dead method - they are reported separately as a dead
31-
// class. We keep anonymous class counts, because anonymous classes are not reported
32-
// separately.
33-
sum(LocalClass localClass | localClass.getLocalClassDeclStmt().getEnclosingCallable() = deadMethod |
34-
localClass.getNumberOfLinesOfCode()
20+
sum(DeadClass deadClass |
21+
deadClass.getFile() = f
22+
|
23+
deadClass.getNumberOfLinesOfCode() -
24+
// Remove inner and local classes, as they are reported as separate dead classes. Do not
25+
// remove anonymous classes, because they aren't reported separately.
26+
sum(NestedClass innerClass |
27+
innerClass.getEnclosingType() = deadClass and not innerClass.isAnonymous()
28+
|
29+
innerClass.getNumberOfLinesOfCode()
30+
)
31+
) +
32+
// Lines of code contributed by dead methods, not in dead classes.
33+
sum(DeadMethod deadMethod |
34+
deadMethod.getFile() = f and not deadMethod.isInDeadScope()
35+
|
36+
deadMethod.getNumberOfLinesOfCode() -
37+
// Remove local classes defined in the dead method - they are reported separately as a dead
38+
// class. We keep anonymous class counts, because anonymous classes are not reported
39+
// separately.
40+
sum(LocalClass localClass |
41+
localClass.getLocalClassDeclStmt().getEnclosingCallable() = deadMethod
42+
|
43+
localClass.getNumberOfLinesOfCode()
44+
)
45+
) +
46+
// Lines of code contributed by dead fields, not in dead classes.
47+
sum(DeadField deadField |
48+
deadField.getFile() = f and not deadField.isInDeadScope()
49+
|
50+
deadField.getNumberOfLinesOfCode()
51+
) +
52+
// Lines of code contributed by unused enum constants.
53+
sum(UnusedEnumConstant deadEnumConstant |
54+
deadEnumConstant.getFile() = f
55+
|
56+
deadEnumConstant.getNumberOfLinesOfCode()
3557
)
36-
) +
37-
// Lines of code contributed by dead fields, not in dead classes.
38-
sum(DeadField deadField | deadField.getFile() = f and not deadField.isInDeadScope() |
39-
deadField.getNumberOfLinesOfCode()
40-
) +
41-
// Lines of code contributed by unused enum constants.
42-
sum(UnusedEnumConstant deadEnumConstant | deadEnumConstant.getFile() = f |
43-
deadEnumConstant.getNumberOfLinesOfCode()
44-
)
45-
select f, n
46-
order by n desc
58+
select f, n order by n desc

java/ql/src/Frameworks/Spring/Architecture/Refactoring Opportunities/UnusedBean.ql

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ class InstanceFieldWrite extends FieldWrite {
3737
*/
3838
class ImpureStmt extends Stmt {
3939
ImpureStmt() {
40-
exists(Expr e |
41-
e.getEnclosingStmt() = this
42-
|
40+
exists(Expr e | e.getEnclosingStmt() = this |
4341
// Only permit calls to set of whitelisted targets.
4442
(
4543
e instanceof Call and
@@ -57,12 +55,12 @@ class ImpureStmt extends Stmt {
5755
*/
5856
private Stmt getANestedStmt(Block block) {
5957
// Any non-block statement
60-
not result instanceof Block and result = block.getAStmt() or
58+
not result instanceof Block and result = block.getAStmt()
59+
or
6160
// Or any statement nested in a block
6261
result = getANestedStmt(block.getAStmt())
6362
}
6463

65-
6664
/**
6765
* A class whose loading and construction by Spring does not have any side-effects outside the class.
6866
*
@@ -73,12 +71,8 @@ class SpringPureClass extends Class {
7371
(
7472
// The only permitted statement in static initializers is the initialization of a static
7573
// final or effectively final logger fields, or effectively immutable types.
76-
forall(Stmt s |
77-
s = getANestedStmt(getAMember().(StaticInitializer).getBody())
78-
|
79-
exists(Field f |
80-
f = s.(ExprStmt).getExpr().(AssignExpr).getDest().(FieldWrite).getField()
81-
|
74+
forall(Stmt s | s = getANestedStmt(getAMember().(StaticInitializer).getBody()) |
75+
exists(Field f | f = s.(ExprStmt).getExpr().(AssignExpr).getDest().(FieldWrite).getField() |
8276
(
8377
// A logger field
8478
f.getName().toLowerCase() = "logger" or
@@ -88,11 +82,7 @@ class SpringPureClass extends Class {
8882
) and
8983
f.isStatic() and
9084
// Only written to in this statement e.g. final or effectively final
91-
forall(FieldWrite fw |
92-
fw = f.getAnAccess()
93-
|
94-
fw.getEnclosingStmt() = s
95-
)
85+
forall(FieldWrite fw | fw = f.getAnAccess() | fw.getEnclosingStmt() = s)
9686
)
9787
)
9888
) and
@@ -103,20 +93,23 @@ class SpringPureClass extends Class {
10393
c = getAMember()
10494
) and
10595
impureStmt.getEnclosingCallable() = c
106-
|
107-
c instanceof InstanceInitializer or
108-
c instanceof Constructor or
96+
|
97+
c instanceof InstanceInitializer
98+
or
99+
c instanceof Constructor
100+
or
109101
// afterPropertiesSet() method called after bean initialization
110-
c = this.(InitializingBeanClass).getAfterPropertiesSet() or
102+
c = this.(InitializingBeanClass).getAfterPropertiesSet()
103+
or
111104
// Init and setter methods must be pure, because they are called when the bean is initialized
112-
exists(SpringBean bean |
113-
this = bean.getClass()
114-
|
105+
exists(SpringBean bean | this = bean.getClass() |
115106
c = bean.getInitMethod() or
116107
c = bean.getAProperty().getSetterMethod()
117-
) or
108+
)
109+
or
118110
// Setter method by autowiring, either in the XML or by annotation
119-
c = this.getAMethod().(SpringBeanAutowiredCallable) or
111+
c = this.getAMethod().(SpringBeanAutowiredCallable)
112+
or
120113
c = this.getAMethod().(SpringBeanXMLAutowiredSetterMethod)
121114
)
122115
}
@@ -130,7 +123,6 @@ class SpringBeanFactory extends ClassOrInterface {
130123
getAnAncestor().hasQualifiedName("org.springframework.beans.factory", "BeanFactory")
131124
}
132125

133-
134126
/**
135127
* Get a bean constructed by a call to this bean factory.
136128
*/
@@ -139,7 +131,7 @@ class SpringBeanFactory extends ClassOrInterface {
139131
getBean.hasName("getBean") and
140132
call.getMethod() = getBean and
141133
getBean.getDeclaringType() = this
142-
|
134+
|
143135
result.getBeanIdentifier() = call.getArgument(0).(CompileTimeConstantExpr).getStringValue()
144136
)
145137
}
@@ -158,7 +150,8 @@ class LiveSpringBean extends SpringBean {
158150
not isLazyInit() and
159151
// or has no side-effects when constructed
160152
not getClass() instanceof SpringPureClass
161-
) or
153+
)
154+
or
162155
(
163156
// If the class does not exist for this bean, or the class is not a source bean, then this is
164157
// likely to be a definition using a library class, in which case we should consider it to be
@@ -170,43 +163,45 @@ class LiveSpringBean extends SpringBean {
170163
// A live child bean implies this bean is live
171164
exists(LiveSpringBean child | this = child.getBeanParent()) or
172165
// Beans constructed by a bean factory are considered live
173-
exists(SpringBeanFactory beanFactory |
174-
this = beanFactory.getAConstructedBean()
175-
)
176-
) or
166+
exists(SpringBeanFactory beanFactory | this = beanFactory.getAConstructedBean())
167+
)
168+
or
177169
(
178170
// Referenced by a live bean, either as a property or argument in the XML
179171
exists(LiveSpringBean other |
180172
this = other.getAConstructorArg().getArgRefBean() or
181173
this = other.getAProperty().getPropertyRefBean()
182-
) or
174+
)
175+
or
183176
// Referenced as a factory bean
184-
exists(LiveSpringBean springBean |
185-
this = springBean.getFactoryBean()
186-
) or
177+
exists(LiveSpringBean springBean | this = springBean.getFactoryBean())
178+
or
187179
// Injected by @Autowired annotation
188180
exists(SpringBeanAutowiredCallable autowiredCallable |
189181
// The callable must be in a live class
190182
autowiredCallable.getEnclosingSpringBean() instanceof LiveSpringBean or
191183
autowiredCallable.getEnclosingSpringComponent().isLive()
192-
|
184+
|
193185
// This bean is injected into it
194186
this = autowiredCallable.getAnInjectedBean()
195-
) or
187+
)
188+
or
196189
// Injected by @Autowired annotation on field
197190
exists(SpringBeanAutowiredField autowiredField |
198191
// The field must be in a live class
199192
autowiredField.getEnclosingSpringBean() instanceof LiveSpringBean or
200193
autowiredField.getEnclosingSpringComponent().isLive()
201-
|
194+
|
202195
// This bean is injected into it
203196
this = autowiredField.getInjectedBean()
204-
) or
197+
)
198+
or
205199
// Injected by autowired specified in XML
206200
exists(SpringBeanXMLAutowiredSetterMethod setterMethod |
207201
// The config method must be on a live bean
208-
setterMethod.getDeclaringType().(SpringBeanRefType).getSpringBean() instanceof LiveSpringBean
209-
|
202+
setterMethod.getDeclaringType().(SpringBeanRefType).getSpringBean() instanceof
203+
LiveSpringBean
204+
|
210205
// This bean is injected into it
211206
this = setterMethod.getInjectedBean()
212207
)
@@ -218,9 +213,7 @@ class LiveSpringBean extends SpringBean {
218213
* A `SpringBean` that can be safely removed from the program without changing overall behavior.
219214
*/
220215
class UnusedSpringBean extends SpringBean {
221-
UnusedSpringBean() {
222-
not this instanceof LiveSpringBean
223-
}
216+
UnusedSpringBean() { not this instanceof LiveSpringBean }
224217
}
225218

226219
from UnusedSpringBean unused

java/ql/src/Likely Bugs/Collections/ReadOnlyContainer.ql

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ where
2626
// Every access to `v` is either...
2727
forall(VarAccess va | va = v.getAnAccess() |
2828
// ...an assignment storing a fresh container into `v`,
29-
exists(AssignExpr assgn | va = assgn.getDest() | assgn.getSource() instanceof FreshContainer) or
29+
exists(AssignExpr assgn | va = assgn.getDest() | assgn.getSource() instanceof FreshContainer)
30+
or
3031
// ...a return (but only if `v` is a local variable)
31-
(v instanceof LocalVariableDecl and exists(ReturnStmt ret | ret.getResult() = va)) or
32+
(v instanceof LocalVariableDecl and exists(ReturnStmt ret | ret.getResult() = va))
33+
or
3234
// ...or a call to a query method on `v`.
33-
exists(MethodAccess ma | va = ma.getQualifier() | ma.getMethod() instanceof ContainerQueryMethod)
35+
exists(MethodAccess ma | va = ma.getQualifier() |
36+
ma.getMethod() instanceof ContainerQueryMethod
37+
)
3438
) and
3539
// There is at least one call to a query method.
3640
exists(MethodAccess ma | v.getAnAccess() = ma.getQualifier() |

0 commit comments

Comments
 (0)