77import java
88
99/**
10+ * DEPRECATED: Use `conditionCheckMethodArgument` instead.
1011 * Holds if `m` is a non-overridable method that checks that its first argument
1112 * is equal to `checkTrue` and throws otherwise.
1213 */
13- predicate conditionCheckMethod ( Method m , boolean checkTrue ) {
14- m .getDeclaringType ( ) .hasQualifiedName ( "com.google.common.base" , "Preconditions" ) and
15- checkTrue = true and
16- ( m .hasName ( "checkArgument" ) or m .hasName ( "checkState" ) )
14+ deprecated predicate conditionCheckMethod ( Method m , boolean checkTrue ) {
15+ conditionCheckMethodArgument ( m , 0 , checkTrue )
16+ }
17+
18+ /**
19+ * Holds if `m` is a non-overridable method that checks that its zero-indexed `argument`
20+ * is equal to `checkTrue` and throws otherwise.
21+ */
22+ predicate conditionCheckMethodArgument ( Method m , int argument , boolean checkTrue ) {
23+ condtionCheckMethodGooglePreconditions ( m , checkTrue ) and argument = 0
1724 or
18- m .getDeclaringType ( ) .hasQualifiedName ( "org.apache.commons.lang3" , "Validate" ) and
19- checkTrue = true and
20- ( m .hasName ( "isTrue" ) or m .hasName ( "validState" ) )
25+ conditionCheckMethodApacheCommonsLang3Validate ( m , checkTrue ) and argument = 0
26+ or
27+ condtionCheckMethodTestingFramework ( m , argument , checkTrue )
28+ or
29+ exists ( Parameter p , MethodAccess ma , int argIndex , boolean ct , Expr arg |
30+ p = m .getParameter ( argument ) and
31+ not m .isOverridable ( ) and
32+ m .getBody ( ) .getStmt ( 0 ) .( ExprStmt ) .getExpr ( ) = ma and
33+ conditionCheckArgument ( ma , argIndex , ct ) and
34+ ma .getArgument ( argIndex ) = arg and
35+ (
36+ arg .( LogNotExpr ) .getExpr ( ) .( VarAccess ) .getVariable ( ) = p and
37+ checkTrue = ct .booleanNot ( )
38+ or
39+ arg .( VarAccess ) .getVariable ( ) = p and checkTrue = ct
40+ )
41+ )
2142 or
2243 exists ( Parameter p , IfStmt ifstmt , Expr cond |
23- p = m .getParameter ( 0 ) and
44+ p = m .getParameter ( argument ) and
2445 not m .isOverridable ( ) and
2546 p .getType ( ) instanceof BooleanType and
2647 m .getBody ( ) .getStmt ( 0 ) = ifstmt and
@@ -35,26 +56,68 @@ predicate conditionCheckMethod(Method m, boolean checkTrue) {
3556 ifstmt .getThen ( ) .( SingletonBlock ) .getStmt ( ) instanceof ThrowStmt
3657 )
3758 )
38- or
39- exists ( Parameter p , MethodAccess ma , boolean ct , Expr arg |
40- p = m .getParameter ( 0 ) and
41- not m .isOverridable ( ) and
42- m .getBody ( ) .getStmt ( 0 ) .( ExprStmt ) .getExpr ( ) = ma and
43- conditionCheck ( ma , ct ) and
44- ma .getArgument ( 0 ) = arg and
59+ }
60+
61+ private predicate condtionCheckMethodGooglePreconditions ( Method m , boolean checkTrue ) {
62+ m .getDeclaringType ( ) .hasQualifiedName ( "com.google.common.base" , "Preconditions" ) and
63+ checkTrue = true and
64+ ( m .hasName ( "checkArgument" ) or m .hasName ( "checkState" ) )
65+ }
66+
67+ private predicate conditionCheckMethodApacheCommonsLang3Validate ( Method m , boolean checkTrue ) {
68+ m .getDeclaringType ( ) .hasQualifiedName ( "org.apache.commons.lang3" , "Validate" ) and
69+ checkTrue = true and
70+ ( m .hasName ( "isTrue" ) or m .hasName ( "validState" ) )
71+ }
72+
73+ /**
74+ * Holds if `m` is a non-overridable testing framework method that checks that its first argument
75+ * is equal to `checkTrue` and throws otherwise.
76+ */
77+ private predicate condtionCheckMethodTestingFramework ( Method m , int argument , boolean checkTrue ) {
78+ argument = 0 and
79+ (
80+ m .getDeclaringType ( ) .hasQualifiedName ( "org.junit" , "Assume" ) and
81+ checkTrue = true and
82+ m .hasName ( "assumeTrue" )
83+ or
84+ m .getDeclaringType ( ) .hasQualifiedName ( "org.junit.jupiter.api" , "Assertions" ) and
4585 (
46- arg .( LogNotExpr ) .getExpr ( ) .( VarAccess ) .getVariable ( ) = p and
47- checkTrue = ct .booleanNot ( )
86+ checkTrue = true and m .hasName ( "assertTrue" )
4887 or
49- arg .( VarAccess ) .getVariable ( ) = p and checkTrue = ct
88+ checkTrue = false and m .hasName ( "assertFalse" )
89+ )
90+ or
91+ m .getDeclaringType ( ) .hasQualifiedName ( "org.junit.jupiter.api" , "Assumptions" ) and
92+ (
93+ checkTrue = true and m .hasName ( "assumeTrue" )
94+ or
95+ checkTrue = false and m .hasName ( "assumeFalse" )
5096 )
5197 )
98+ or
99+ m .getDeclaringType ( ) .hasQualifiedName ( [ "org.junit" , "org.testng" ] , "Assert" ) and
100+ m .getParameter ( argument ) .getType ( ) instanceof BooleanType and
101+ (
102+ checkTrue = true and m .hasName ( "assertTrue" )
103+ or
104+ checkTrue = false and m .hasName ( "assertFalse" )
105+ )
52106}
53107
54108/**
109+ * DEPRECATED: Use `conditionCheckArgument` instead.
55110 * Holds if `ma` is an access to a non-overridable method that checks that its
56111 * first argument is equal to `checkTrue` and throws otherwise.
57112 */
58- predicate conditionCheck ( MethodAccess ma , boolean checkTrue ) {
59- conditionCheckMethod ( ma .getMethod ( ) .getSourceDeclaration ( ) , checkTrue )
113+ deprecated predicate conditionCheck ( MethodAccess ma , boolean checkTrue ) {
114+ conditionCheckArgument ( ma , 0 , checkTrue )
115+ }
116+
117+ /**
118+ * Holds if `ma` is an access to a non-overridable method that checks that its
119+ * zero-indexed `argument` is equal to `checkTrue` and throws otherwise.
120+ */
121+ predicate conditionCheckArgument ( MethodAccess ma , int argument , boolean checkTrue ) {
122+ conditionCheckMethodArgument ( ma .getMethod ( ) .getSourceDeclaration ( ) , argument , checkTrue )
60123}
0 commit comments