@@ -37,9 +37,7 @@ class InstanceFieldWrite extends FieldWrite {
3737 */
3838class 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 */
5856private 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 */
220215class UnusedSpringBean extends SpringBean {
221- UnusedSpringBean ( ) {
222- not this instanceof LiveSpringBean
223- }
216+ UnusedSpringBean ( ) { not this instanceof LiveSpringBean }
224217}
225218
226219from UnusedSpringBean unused
0 commit comments